forked from shadowsocks/shadowsocks-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipe.go
44 lines (41 loc) · 1002 Bytes
/
pipe.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package shadowsocks
import (
// "io"
"net"
"time"
)
func SetReadTimeout(c net.Conn) {
if readTimeout != 0 {
c.SetReadDeadline(time.Now().Add(readTimeout))
}
}
// PipeThenClose copies data from src to dst, closes dst when done.
func PipeThenClose(src, dst net.Conn) {
defer dst.Close()
buf := leakyBuf.Get()
defer leakyBuf.Put(buf)
for {
SetReadTimeout(src)
n, err := src.Read(buf)
// read may return EOF with n > 0
// should always process n > 0 bytes before handling error
if n > 0 {
// Note: avoid overwrite err returned by Read.
if _, err := dst.Write(buf[0:n]); err != nil {
Debug.Println("write:", err)
break
}
}
if err != nil {
// Always "use of closed network connection", but no easy way to
// identify this specific error. So just leave the error along for now.
// More info here: https://code.google.com/p/go/issues/detail?id=4373
/*
if bool(Debug) && err != io.EOF {
Debug.Println("read:", err)
}
*/
break
}
}
}