Skip to content

Commit

Permalink
windows
Browse files Browse the repository at this point in the history
Signed-off-by: Adrian Cole <[email protected]>
  • Loading branch information
Adrian Cole committed Aug 7, 2023
1 parent 760e823 commit f82e74c
Showing 1 changed file with 37 additions and 33 deletions.
70 changes: 37 additions & 33 deletions internal/sysfs/sock_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,15 @@ const (
MSG_PEEK = 0x2
// _FIONBIO is the flag to set the O_NONBLOCK flag on socket handles using ioctlsocket.
_FIONBIO = 0x8004667e
// _WASWOULDBLOCK corresponds to syscall.EWOULDBLOCK in WinSock.
_WASWOULDBLOCK = 10035
)

var (
// modws2_32 is WinSock.
modws2_32 = syscall.NewLazyDLL("ws2_32.dll")
// procrecvfrom exposes recvfrom from WinSock.
procrecvfrom = modws2_32.NewProc("recvfrom")
// procaccept exposes accept from WinSock.
procaccept = modws2_32.NewProc("accept")
// procioctlsocket exposes ioctlsocket from WinSock.
procioctlsocket = modws2_32.NewProc("ioctlsocket")
// procselect exposes select from WinSock.
procselect = modws2_32.NewProc("select")
)

// recvfrom exposes the underlying syscall in Windows.
Expand Down Expand Up @@ -142,12 +136,25 @@ func (f *winTcpListenerFile) Accept() (socketapi.TCPConn, sys.Errno) {
}
}

// IsNonblock implements File.IsNonblock
// Close implements the same method as documented on sys.File
func (f *winTcpListenerFile) Close() sys.Errno {
if !f.closed {
return sys.UnwrapOSError(f.tl.Close())
}
return 0
}

// Addr is exposed for testing.
func (f *winTcpListenerFile) Addr() *net.TCPAddr {
return f.tl.Addr().(*net.TCPAddr)
}

// IsNonblock implements the same method as documented on fsapi.File
func (f *winTcpListenerFile) IsNonblock() bool {
return f.nonblock
}

// SetNonblock implements the same method as documented on sys.File
// SetNonblock implements the same method as documented on fsapi.File
func (f *winTcpListenerFile) SetNonblock(enabled bool) sys.Errno {
f.nonblock = enabled
_, errno := syscallConnControl(f.tl, func(fd uintptr) (int, sys.Errno) {
Expand All @@ -156,17 +163,9 @@ func (f *winTcpListenerFile) SetNonblock(enabled bool) sys.Errno {
return errno
}

// Close implements the same method as documented on sys.File
func (f *winTcpListenerFile) Close() sys.Errno {
if !f.closed {
return sys.UnwrapOSError(f.tl.Close())
}
return 0
}

// Addr is exposed for testing.
func (f *winTcpListenerFile) Addr() *net.TCPAddr {
return f.tl.Addr().(*net.TCPAddr)
// Poll implements the same method as documented on fsapi.File
func (f *winTcpListenerFile) Poll(fsapi.Pflag, int32) (ready bool, errno sys.Errno) {
return false, sys.ENOSYS
}

var _ socketapi.TCPConn = (*winTcpConnFile)(nil)
Expand All @@ -190,20 +189,6 @@ func newTcpConn(tc *net.TCPConn) socketapi.TCPConn {
return &winTcpConnFile{tc: tc}
}

// SetNonblock implements the same method as documented on sys.File
func (f *winTcpConnFile) SetNonblock(enabled bool) (errno sys.Errno) {
f.nonblock = true
_, errno = syscallConnControl(f.tc, func(fd uintptr) (int, sys.Errno) {
return 0, sys.UnwrapOSError(setNonblockSocket(syscall.Handle(fd), enabled))
})
return
}

// IsNonblock implements File.IsNonblock
func (f *winTcpConnFile) IsNonblock() bool {
return f.nonblock
}

// Read implements the same method as documented on sys.File
func (f *winTcpConnFile) Read(buf []byte) (n int, errno sys.Errno) {
if len(buf) == 0 {
Expand Down Expand Up @@ -279,3 +264,22 @@ func (f *winTcpConnFile) close() sys.Errno {
f.closed = true
return f.Shutdown(syscall.SHUT_RDWR)
}

// IsNonblock implements the same method as documented on fsapi.File
func (f *winTcpConnFile) IsNonblock() bool {
return f.nonblock
}

// SetNonblock implements the same method as documented on fsapi.File
func (f *winTcpConnFile) SetNonblock(enabled bool) (errno sys.Errno) {
f.nonblock = true
_, errno = syscallConnControl(f.tc, func(fd uintptr) (int, sys.Errno) {
return 0, sys.UnwrapOSError(setNonblockSocket(syscall.Handle(fd), enabled))
})
return
}

// Poll implements the same method as documented on fsapi.File
func (f *winTcpConnFile) Poll(fsapi.Pflag, int32) (ready bool, errno sys.Errno) {
return false, sys.ENOSYS
}

0 comments on commit f82e74c

Please sign in to comment.