@@ -27,6 +27,7 @@ import (
27
27
"github.com/panjf2000/gnet/v2/pkg/buffer/elastic"
28
28
errorx "github.com/panjf2000/gnet/v2/pkg/errors"
29
29
bbPool "github.com/panjf2000/gnet/v2/pkg/pool/bytebuffer"
30
+ bsPool "github.com/panjf2000/gnet/v2/pkg/pool/byteslice"
30
31
goPool "github.com/panjf2000/gnet/v2/pkg/pool/goroutine"
31
32
)
32
33
@@ -54,6 +55,7 @@ type conn struct {
54
55
ctx any // user-defined context
55
56
loop * eventloop // owner event-loop
56
57
buffer * bbPool.ByteBuffer // reuse memory of inbound data as a temporary buffer
58
+ cache []byte // temporary cache for the inbound data
57
59
rawConn net.Conn // original connection
58
60
localAddr net.Addr // local server addr
59
61
remoteAddr net.Addr // remote addr
@@ -116,6 +118,7 @@ func newUDPConn(el *eventloop, pc net.PacketConn, localAddr, remoteAddr net.Addr
116
118
func (c * conn ) resetBuffer () {
117
119
c .buffer .Reset ()
118
120
c .inboundBuffer .Reset ()
121
+ c .inboundBuffer .Done ()
119
122
}
120
123
121
124
func (c * conn ) Read (p []byte ) (n int , err error ) {
@@ -149,22 +152,10 @@ func (c *conn) Next(n int) (buf []byte, err error) {
149
152
c .buffer .B = c .buffer .B [n :]
150
153
return
151
154
}
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
- }
163
155
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
168
159
}
169
160
170
161
func (c * conn ) Peek (n int ) (buf []byte , err error ) {
@@ -181,25 +172,31 @@ func (c *conn) Peek(n int) (buf []byte, err error) {
181
172
if len (head ) == n {
182
173
return head , err
183
174
}
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 ... )
187
178
if inBufferLen >= n {
188
- return c . loop . cache . Bytes (), err
179
+ return
189
180
}
190
181
191
182
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
194
186
}
195
187
196
188
func (c * conn ) Discard (n int ) (int , error ) {
189
+ if len (c .cache ) > 0 {
190
+ bsPool .Put (c .cache )
191
+ c .cache = nil
192
+ }
193
+
197
194
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 {
200
196
c .resetBuffer ()
201
- return inBufferLen + tempBufferLen , nil
197
+ return totalLen , nil
202
198
}
199
+
203
200
if c .inboundBuffer .IsEmpty () {
204
201
c .buffer .B = c .buffer .B [n :]
205
202
return n , nil
0 commit comments