Skip to content

Commit

Permalink
refactor(sampledconn): Add Windows OSPeekConn impl
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPolo committed Dec 4, 2024
1 parent 2174a1e commit 0e1d9c2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !unix
//go:build !unix && !windows

package sampledconn

Expand Down
44 changes: 44 additions & 0 deletions p2p/transport/tcpreuse/internal/sampledconn/sampledconn_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//go:build windows

package sampledconn

import (
"errors"
"io"
"syscall"

"golang.org/x/sys/windows"
)

func OSPeekConn(conn syscall.Conn) (PeekedBytes, error) {
s := PeekedBytes{}

rawConn, err := conn.SyscallConn()
if err != nil {
return s, err
}

var readErr error
var n uint32
err = rawConn.Read(func(fd uintptr) bool {
flags := uint32(windows.MSG_PEEK | windows.MSG_WAITALL)
wsabuf := windows.WSABuf{
Len: uint32(len(s)),
Buf: &s[0],
}

readErr = windows.WSARecv(windows.Handle(fd), &wsabuf, 1, &n, &flags, nil, nil)
return !errors.Is(readErr, windows.WSAEWOULDBLOCK)
})
if err != nil {
return s, err
}
if readErr != nil {
return s, readErr
}
if n < peekSize {
return s, io.EOF
}

return s, nil
}

0 comments on commit 0e1d9c2

Please sign in to comment.