Skip to content

Commit

Permalink
Fix a potential problem in Pipe and add comment.
Browse files Browse the repository at this point in the history
  • Loading branch information
cyfdecyf committed Dec 12, 2012
1 parent 5715ecb commit 511acfa
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions shadowsocks/pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,30 @@ import (
"net"
)

func Pipe(src net.Conn, dst net.Conn, end chan int) {
func Pipe(src, dst net.Conn, end chan int) {
// Should not use io.Copy here.
// io.Copy will try to use the ReadFrom interface of TCPConn, but the src
// here is not a regular file, so sendfile is not applicable.
// io.Copy will fallback to the normal copy after discovering this,
// introducing unnecessary overhead.
buf := make([]byte, 4096)
for {
num, err := src.Read(buf)
if err == nil {
_, err := dst.Write(buf[0:num])
if err != nil {
// read may return EOF with num > 0
// should always process num > 0 bytes before handling error
if num > 0 {
if _, err = dst.Write(buf[0:num]); err != nil {
log.Println("write:", err)
end <- 1
return
break
}
} else {
log.Println("read:", err)
end <- 1
return
}
if num == 0 {
end <- 1
return
if num == 0 { // num == 0 should associate with EOF
break
}
if err != nil {
log.Println("read:", err)
break
}
}
end <- 1
}

0 comments on commit 511acfa

Please sign in to comment.