Skip to content

Commit 44df03b

Browse files
committed
Try setting the socket to be blocking
1 parent 0e1d9c2 commit 44df03b

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

p2p/transport/tcpreuse/internal/sampledconn/sampledconn_windows.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,45 @@ import (
66
"errors"
77
"io"
88
"syscall"
9+
"unsafe"
910

1011
"golang.org/x/sys/windows"
1112
)
1213

14+
const fionbio = 0x8004667e
15+
16+
// updateBlocking updates the blocking mode of the file descriptor.
17+
// It returns true if the blocking mode was changed, and false if it was already in the desired mode.
18+
// If an error occurs, it returns the error.
19+
func updateBlocking(fd windows.Handle, blocking bool) (bool, error) {
20+
// Determine the desired mode
21+
var desiredMode uint32
22+
if !blocking {
23+
desiredMode = 1
24+
} else {
25+
desiredMode = 0
26+
}
27+
28+
// Query the current mode
29+
var currentMode uint32
30+
err := windows.WSAIoctl(fd, fionbio, (*byte)(unsafe.Pointer(&currentMode)), 4, nil, 0, nil, nil, 0)
31+
if err != nil {
32+
return false, err
33+
}
34+
35+
if currentMode == desiredMode {
36+
return false, nil
37+
}
38+
39+
// Update to the desired mode
40+
err = windows.WSAIoctl(fd, fionbio, (*byte)(unsafe.Pointer(&desiredMode)), 4, nil, 0, nil, nil, 0)
41+
if err != nil {
42+
return false, err
43+
}
44+
45+
return true, nil
46+
}
47+
1348
func OSPeekConn(conn syscall.Conn) (PeekedBytes, error) {
1449
s := PeekedBytes{}
1550

@@ -18,6 +53,24 @@ func OSPeekConn(conn syscall.Conn) (PeekedBytes, error) {
1853
return s, err
1954
}
2055

56+
var updatedBlocking bool
57+
ctlErr := rawConn.Control(func(fd uintptr) {
58+
updatedBlocking, err = updateBlocking(windows.Handle(fd), true)
59+
})
60+
if ctlErr != nil {
61+
return s, ctlErr
62+
}
63+
if err != nil {
64+
return s, err
65+
}
66+
if updatedBlocking {
67+
defer func() {
68+
_ = rawConn.Control(func(fd uintptr) {
69+
_, _ = updateBlocking(windows.Handle(fd), false)
70+
})
71+
}()
72+
}
73+
2174
var readErr error
2275
var n uint32
2376
err = rawConn.Read(func(fd uintptr) bool {

0 commit comments

Comments
 (0)