forked from cloudwego/netpoll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sys_define_windows.go
93 lines (80 loc) · 2.34 KB
/
sys_define_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2021 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build windows
package netpoll
import (
"os"
"syscall"
"unsafe"
)
type iovec = syscall.WSABuf
type fdtype = syscall.Handle
const SEND_RECV_AGAIN = WSAEWOULDBLOCK
var ws2_32_mod = syscall.NewLazyDLL("ws2_32.dll")
var recvProc = ws2_32_mod.NewProc("recv")
var sendProc = ws2_32_mod.NewProc("send")
var acceptProc = ws2_32_mod.NewProc("accept")
var ioctlsocketProc = ws2_32_mod.NewProc("ioctlsocket")
var getsockoptProc = ws2_32_mod.NewProc("getsockopt")
const (
SO_ERROR = 0x4
FIONBIO = 0x8004667e
WSAEWOULDBLOCK syscall.Errno = 10035
WSAEALREADY syscall.Errno = 10037
WSAEINPROGRESS syscall.Errno = 10036
)
func sysRead(fd fdtype, p []byte) (n int, err error) {
rnu, _, err := recvProc.Call(uintptr(fd), uintptr(unsafe.Pointer(&p[0])), uintptr(len(p)), 0)
rn := int32(rnu)
if rn <= 0 {
return int(rn), err
}
return int(rn), nil
}
func sysWrite(fd fdtype, p []byte) (n int, err error) {
wnu, _, err := sendProc.Call(uintptr(fd), uintptr(unsafe.Pointer(&p[0])), uintptr(len(p)), 0)
wn := int(wnu)
if wn <= 0 {
return wn, err
}
return wn, nil
}
func sysSetNonblock(fd fdtype, is bool) error {
imode := 0
if is {
imode = 1
}
r, _, err := ioctlsocketProc.Call(uintptr(fd), FIONBIO, uintptr(unsafe.Pointer(&imode)))
if r != 0 {
return err
}
return nil
}
func sysGetsockoptInt(fd fdtype, level int, optname int) (int, error) {
out := 1
len := unsafe.Sizeof(out)
nerr, _, err := getsockoptProc.Call(uintptr(fd), uintptr(level), uintptr(optname), uintptr(unsafe.Pointer(&out)), uintptr(unsafe.Pointer(&len)))
if nerr != 0 {
return out, os.NewSyscallError("getsockopt", err)
}
return out, nil
}
func isChanClose(ch chan int) bool {
select {
case _, received := <-ch:
return !received
default:
}
return false
}