-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(sampledconn): Add Windows OSPeekConn impl
- Loading branch information
Showing
2 changed files
with
45 additions
and
1 deletion.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
p2p/transport/tcpreuse/internal/sampledconn/sampledconn_other.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
//go:build !unix | ||
//go:build !unix && !windows | ||
|
||
package sampledconn | ||
|
||
|
44 changes: 44 additions & 0 deletions
44
p2p/transport/tcpreuse/internal/sampledconn/sampledconn_windows.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |