Skip to content

Commit

Permalink
feat(tcpreuse): add Windows sampledconn
Browse files Browse the repository at this point in the history
  • Loading branch information
aschmahmann committed Oct 30, 2024
1 parent 8aafe86 commit 589ff76
Show file tree
Hide file tree
Showing 2 changed files with 50 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
49 changes: 49 additions & 0 deletions p2p/transport/tcpreuse/internal/sampledconn/sampledconn_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//go:build windows

package sampledconn

import (
"errors"
"golang.org/x/sys/windows"
"syscall"
)

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

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

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

readErr = windows.WSARecv(windows.Handle(fd), &wsabuf, 1, &n, &flags, nil, nil)
if errors.Is(readErr, windows.WSAEWOULDBLOCK) {
return false
}
if readErr != nil {
return true
}
readBytes += int(n)
}
return true
})
if readErr != nil {
return s, readErr
}
if err != nil {
return s, err
}

return s, nil
}

0 comments on commit 589ff76

Please sign in to comment.