@@ -57,19 +57,21 @@ func Copy(destination io.Writer, source io.Reader) (n int64, err error) {
5757}
5858
5959func 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 )
6762 readWaiter , isReadWaiter := CreateReadWaiter (source )
6863 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+ }
7375 }
7476 }
7577 return CopyExtendedWithPool (originSource , destination , source , readCounters , writeCounters )
@@ -113,6 +115,7 @@ func CopyExtendedBuffer(originSource io.Writer, destination N.ExtendedWriter, so
113115 }
114116}
115117
118+ // Deprecated: Use ReadWaiter interface instead.
116119func CopyExtendedWithSrcBuffer (originSource io.Reader , destination N.ExtendedWriter , source N.ThreadSafeReader , readCounters []N.CountFunc , writeCounters []N.CountFunc ) (n int64 , err error ) {
117120 var notFirstTime bool
118121 for {
@@ -128,7 +131,7 @@ func CopyExtendedWithSrcBuffer(originSource io.Reader, destination N.ExtendedWri
128131 dataLen := buffer .Len ()
129132 err = destination .WriteBuffer (buffer )
130133 if err != nil {
131- buffer .Release ()
134+ buffer .Leak ()
132135 if ! notFirstTime {
133136 err = N .ReportHandshakeFailure (originSource , err )
134137 }
@@ -173,7 +176,7 @@ func CopyExtendedWithPool(originSource io.Reader, destination N.ExtendedWriter,
173176 buffer .Resize (readBuffer .Start (), dataLen )
174177 err = destination .WriteBuffer (buffer )
175178 if err != nil {
176- buffer .Release ()
179+ buffer .Leak ()
177180 if ! notFirstTime {
178181 err = N .ReportHandshakeFailure (originSource , err )
179182 }
@@ -256,35 +259,33 @@ func CopyPacket(destinationConn N.PacketWriter, source N.PacketReader) (n int64,
256259 return
257260 }
258261 }
259- safeSrc := N .IsSafePacketReader (source )
260262 frontHeadroom := N .CalculateFrontHeadroom (destinationConn )
261263 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- }
271264 var (
272265 handled bool
273266 copeN int64
274267 )
275268 readWaiter , isReadWaiter := CreatePacketReadWaiter (source )
276269 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+ }
281281 }
282282 }
283283 copeN , err = CopyPacketWithPool (originSource , destinationConn , source , readCounters , writeCounters , n > 0 )
284284 n += copeN
285285 return
286286}
287287
288+ // Deprecated: Use PacketReadWaiter interface instead.
288289func CopyPacketWithSrcBuffer (originSource N.PacketReader , destinationConn N.PacketWriter , source N.ThreadSafePacketReader , readCounters []N.CountFunc , writeCounters []N.CountFunc , notFirstTime bool ) (n int64 , err error ) {
289290 var buffer * buf.Buffer
290291 var destination M.Socksaddr
@@ -302,7 +303,7 @@ func CopyPacketWithSrcBuffer(originSource N.PacketReader, destinationConn N.Pack
302303 }
303304 err = destinationConn .WritePacket (buffer , destination )
304305 if err != nil {
305- buffer .Release ()
306+ buffer .Leak ()
306307 if ! notFirstTime {
307308 err = N .ReportHandshakeFailure (originSource , err )
308309 }
@@ -343,7 +344,7 @@ func CopyPacketWithPool(originSource N.PacketReader, destinationConn N.PacketWri
343344 buffer .Resize (readBuffer .Start (), dataLen )
344345 err = destinationConn .WritePacket (buffer , destination )
345346 if err != nil {
346- buffer .Release ()
347+ buffer .Leak ()
347348 if ! notFirstTime {
348349 err = N .ReportHandshakeFailure (originSource , err )
349350 }
@@ -379,7 +380,7 @@ func WritePacketWithPool(originSource N.PacketReader, destinationConn N.PacketWr
379380 buffer .Resize (readBuffer .Start (), dataLen )
380381 err = destinationConn .WritePacket (buffer , packetBuffer .Destination )
381382 if err != nil {
382- buffer .Release ()
383+ buffer .Leak ()
383384 if ! notFirstTime {
384385 err = N .ReportHandshakeFailure (originSource , err )
385386 }
0 commit comments