-
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.
feat(tcpreuse): add Windows sampledconn
- Loading branch information
1 parent
8aafe86
commit 589ff76
Showing
2 changed files
with
50 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 | ||
|
||
|
49 changes: 49 additions & 0 deletions
49
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,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 | ||
} |