Skip to content

Commit

Permalink
fallback on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
xtaci committed Dec 18, 2019
1 parent c8c084a commit 90ea4ba
Showing 1 changed file with 3 additions and 65 deletions.
68 changes: 3 additions & 65 deletions generic/rawcopy_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,72 +5,10 @@ package generic
import (
"io"
"net"
"syscall"
)

func rawCopy(dst io.Writer, src *net.TCPConn, ctrl *CopyControl) (written int64, err error) {
c, err := src.SyscallConn()
if err != nil {
return 0, err
}

buf := ctrl.Buffer
var locked bool
for {
var er error
var nr int
rr := c.Read(func(s uintptr) bool {
ctrl.Lock()
locked = true
var read uint32
var flags uint32
var wsabuf syscall.WSABuf
wsabuf.Buf = &buf[0]
wsabuf.Len = uint32(len(buf))
er = syscall.WSARecv(syscall.Handle(s), &wsabuf, 1, &read, &flags, nil, nil)
nr = int(read)
return true
})

// read EOF
if nr == 0 && er == nil {
break
}

if nr > 0 {
nw, ew := dst.Write(buf[0:nr])
ctrl.Unlock()
locked = false

if nw > 0 {
written += int64(nw)
}
if ew != nil {
err = ew
break
}
if nr != nw {
err = io.ErrShortWrite
break
}
}
if er != nil {
if er != io.EOF {
err = er
}
break
}
if rr != nil {
if rr != io.EOF {
err = rr
}
break
}
}

if locked {
ctrl.Unlock()
}

return written, err
// fallback to standard io.CopyBuffer
buf := make([]byte, bufSize)
return io.CopyBuffer(dst, src, buf)
}

0 comments on commit 90ea4ba

Please sign in to comment.