@@ -27,6 +27,7 @@ import (
2727 "github.com/panjf2000/gnet/v2/pkg/buffer/elastic"
2828 errorx "github.com/panjf2000/gnet/v2/pkg/errors"
2929 bbPool "github.com/panjf2000/gnet/v2/pkg/pool/bytebuffer"
30+ bsPool "github.com/panjf2000/gnet/v2/pkg/pool/byteslice"
3031 goPool "github.com/panjf2000/gnet/v2/pkg/pool/goroutine"
3132)
3233
@@ -54,6 +55,7 @@ type conn struct {
5455 ctx any // user-defined context
5556 loop * eventloop // owner event-loop
5657 buffer * bbPool.ByteBuffer // reuse memory of inbound data as a temporary buffer
58+ cache []byte // temporary cache for the inbound data
5759 rawConn net.Conn // original connection
5860 localAddr net.Addr // local server addr
5961 remoteAddr net.Addr // remote addr
@@ -116,6 +118,7 @@ func newUDPConn(el *eventloop, pc net.PacketConn, localAddr, remoteAddr net.Addr
116118func (c * conn ) resetBuffer () {
117119 c .buffer .Reset ()
118120 c .inboundBuffer .Reset ()
121+ c .inboundBuffer .Done ()
119122}
120123
121124func (c * conn ) Read (p []byte ) (n int , err error ) {
@@ -149,22 +152,10 @@ func (c *conn) Next(n int) (buf []byte, err error) {
149152 c .buffer .B = c .buffer .B [n :]
150153 return
151154 }
152- head , tail := c .inboundBuffer .Peek (n )
153- defer c .inboundBuffer .Discard (n ) //nolint:errcheck
154- c .loop .cache .Reset ()
155- c .loop .cache .Write (head )
156- if len (head ) == n {
157- return c .loop .cache .Bytes (), err
158- }
159- c .loop .cache .Write (tail )
160- if inBufferLen >= n {
161- return c .loop .cache .Bytes (), err
162- }
163155
164- remaining := n - inBufferLen
165- c .loop .cache .Write (c .buffer .B [:remaining ])
166- c .buffer .B = c .buffer .B [remaining :]
167- return c .loop .cache .Bytes (), err
156+ buf = bsPool .Get (n )
157+ _ , err = c .Read (buf )
158+ return
168159}
169160
170161func (c * conn ) Peek (n int ) (buf []byte , err error ) {
@@ -181,25 +172,31 @@ func (c *conn) Peek(n int) (buf []byte, err error) {
181172 if len (head ) == n {
182173 return head , err
183174 }
184- c . loop . cache . Reset ()
185- c . loop . cache . Write ( head )
186- c . loop . cache . Write ( tail )
175+ buf = bsPool . Get ( n )[: 0 ]
176+ buf = append ( buf , head ... )
177+ buf = append ( buf , tail ... )
187178 if inBufferLen >= n {
188- return c . loop . cache . Bytes (), err
179+ return
189180 }
190181
191182 remaining := n - inBufferLen
192- c .loop .cache .Write (c .buffer .B [:remaining ])
193- return c .loop .cache .Bytes (), err
183+ buf = append (buf , c .buffer .B [:remaining ]... )
184+ c .cache = buf
185+ return
194186}
195187
196188func (c * conn ) Discard (n int ) (int , error ) {
189+ if len (c .cache ) > 0 {
190+ bsPool .Put (c .cache )
191+ c .cache = nil
192+ }
193+
197194 inBufferLen := c .inboundBuffer .Buffered ()
198- tempBufferLen := c .buffer .Len ()
199- if inBufferLen + tempBufferLen < n || n <= 0 {
195+ if totalLen := inBufferLen + c .buffer .Len (); n >= totalLen || n <= 0 {
200196 c .resetBuffer ()
201- return inBufferLen + tempBufferLen , nil
197+ return totalLen , nil
202198 }
199+
203200 if c .inboundBuffer .IsEmpty () {
204201 c .buffer .B = c .buffer .B [n :]
205202 return n , nil
0 commit comments