@@ -57,19 +57,21 @@ func Copy(destination io.Writer, source io.Reader) (n int64, err error) {
57
57
}
58
58
59
59
func CopyExtended (originSource io.Reader , destination N.ExtendedWriter , source N.ExtendedReader , readCounters []N.CountFunc , writeCounters []N.CountFunc ) (n int64 , err error ) {
60
- safeSrc := N .IsSafeReader (source )
61
- headroom := N .CalculateFrontHeadroom (destination ) + N .CalculateRearHeadroom (destination )
62
- if safeSrc != nil {
63
- if headroom == 0 {
64
- return CopyExtendedWithSrcBuffer (originSource , destination , safeSrc , readCounters , writeCounters )
65
- }
66
- }
60
+ frontHeadroom := N .CalculateFrontHeadroom (destination )
61
+ rearHeadroom := N .CalculateRearHeadroom (destination )
67
62
readWaiter , isReadWaiter := CreateReadWaiter (source )
68
63
if isReadWaiter {
69
- var handled bool
70
- handled , n , err = copyWaitWithPool (originSource , destination , readWaiter , readCounters , writeCounters )
71
- if handled {
72
- return
64
+ needCopy := readWaiter .InitializeReadWaiter (& N.ReadWaitOptions {
65
+ FrontHeadroom : frontHeadroom ,
66
+ RearHeadroom : rearHeadroom ,
67
+ MTU : N .CalculateMTU (source , destination ),
68
+ })
69
+ if ! needCopy || common .LowMemory {
70
+ var handled bool
71
+ handled , n , err = copyWaitWithPool (originSource , destination , readWaiter , readCounters , writeCounters )
72
+ if handled {
73
+ return
74
+ }
73
75
}
74
76
}
75
77
return CopyExtendedWithPool (originSource , destination , source , readCounters , writeCounters )
@@ -113,6 +115,7 @@ func CopyExtendedBuffer(originSource io.Writer, destination N.ExtendedWriter, so
113
115
}
114
116
}
115
117
118
+ // Deprecated: Use ReadWaiter interface instead.
116
119
func CopyExtendedWithSrcBuffer (originSource io.Reader , destination N.ExtendedWriter , source N.ThreadSafeReader , readCounters []N.CountFunc , writeCounters []N.CountFunc ) (n int64 , err error ) {
117
120
var notFirstTime bool
118
121
for {
@@ -128,7 +131,7 @@ func CopyExtendedWithSrcBuffer(originSource io.Reader, destination N.ExtendedWri
128
131
dataLen := buffer .Len ()
129
132
err = destination .WriteBuffer (buffer )
130
133
if err != nil {
131
- buffer .Release ()
134
+ buffer .Leak ()
132
135
if ! notFirstTime {
133
136
err = N .ReportHandshakeFailure (originSource , err )
134
137
}
@@ -173,7 +176,7 @@ func CopyExtendedWithPool(originSource io.Reader, destination N.ExtendedWriter,
173
176
buffer .Resize (readBuffer .Start (), dataLen )
174
177
err = destination .WriteBuffer (buffer )
175
178
if err != nil {
176
- buffer .Release ()
179
+ buffer .Leak ()
177
180
if ! notFirstTime {
178
181
err = N .ReportHandshakeFailure (originSource , err )
179
182
}
@@ -256,35 +259,33 @@ func CopyPacket(destinationConn N.PacketWriter, source N.PacketReader) (n int64,
256
259
return
257
260
}
258
261
}
259
- safeSrc := N .IsSafePacketReader (source )
260
262
frontHeadroom := N .CalculateFrontHeadroom (destinationConn )
261
263
rearHeadroom := N .CalculateRearHeadroom (destinationConn )
262
- headroom := frontHeadroom + rearHeadroom
263
- if safeSrc != nil {
264
- if headroom == 0 {
265
- var copyN int64
266
- copyN , err = CopyPacketWithSrcBuffer (originSource , destinationConn , safeSrc , readCounters , writeCounters , n > 0 )
267
- n += copyN
268
- return
269
- }
270
- }
271
264
var (
272
265
handled bool
273
266
copeN int64
274
267
)
275
268
readWaiter , isReadWaiter := CreatePacketReadWaiter (source )
276
269
if isReadWaiter {
277
- handled , copeN , err = copyPacketWaitWithPool (originSource , destinationConn , readWaiter , readCounters , writeCounters , n > 0 )
278
- if handled {
279
- n += copeN
280
- return
270
+ needCopy := readWaiter .InitializeReadWaiter (& N.ReadWaitOptions {
271
+ FrontHeadroom : frontHeadroom ,
272
+ RearHeadroom : rearHeadroom ,
273
+ MTU : N .CalculateMTU (source , destinationConn ),
274
+ })
275
+ if ! needCopy || common .LowMemory {
276
+ handled , copeN , err = copyPacketWaitWithPool (originSource , destinationConn , readWaiter , readCounters , writeCounters , n > 0 )
277
+ if handled {
278
+ n += copeN
279
+ return
280
+ }
281
281
}
282
282
}
283
283
copeN , err = CopyPacketWithPool (originSource , destinationConn , source , readCounters , writeCounters , n > 0 )
284
284
n += copeN
285
285
return
286
286
}
287
287
288
+ // Deprecated: Use PacketReadWaiter interface instead.
288
289
func CopyPacketWithSrcBuffer (originSource N.PacketReader , destinationConn N.PacketWriter , source N.ThreadSafePacketReader , readCounters []N.CountFunc , writeCounters []N.CountFunc , notFirstTime bool ) (n int64 , err error ) {
289
290
var buffer * buf.Buffer
290
291
var destination M.Socksaddr
@@ -302,7 +303,7 @@ func CopyPacketWithSrcBuffer(originSource N.PacketReader, destinationConn N.Pack
302
303
}
303
304
err = destinationConn .WritePacket (buffer , destination )
304
305
if err != nil {
305
- buffer .Release ()
306
+ buffer .Leak ()
306
307
if ! notFirstTime {
307
308
err = N .ReportHandshakeFailure (originSource , err )
308
309
}
@@ -343,7 +344,7 @@ func CopyPacketWithPool(originSource N.PacketReader, destinationConn N.PacketWri
343
344
buffer .Resize (readBuffer .Start (), dataLen )
344
345
err = destinationConn .WritePacket (buffer , destination )
345
346
if err != nil {
346
- buffer .Release ()
347
+ buffer .Leak ()
347
348
if ! notFirstTime {
348
349
err = N .ReportHandshakeFailure (originSource , err )
349
350
}
@@ -379,7 +380,7 @@ func WritePacketWithPool(originSource N.PacketReader, destinationConn N.PacketWr
379
380
buffer .Resize (readBuffer .Start (), dataLen )
380
381
err = destinationConn .WritePacket (buffer , packetBuffer .Destination )
381
382
if err != nil {
382
- buffer .Release ()
383
+ buffer .Leak ()
383
384
if ! notFirstTime {
384
385
err = N .ReportHandshakeFailure (originSource , err )
385
386
}
0 commit comments