-
Notifications
You must be signed in to change notification settings - Fork 1
/
pd.go
78 lines (65 loc) · 1.51 KB
/
pd.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
package main
import (
"unsafe"
)
const (
tagListenerAccept uint32 = iota
tagConnSend
tagConnRecv
tagCancelIo
)
const (
flagPolling uint32 = 1 << iota
flagCancel
)
type PollDesc struct {
tag uint32
flags uint32
data uint64
}
func (p *PollDesc) ptr() uint64 {
return uint64(uintptr(unsafe.Pointer(p)))
}
func (p *PollDesc) polling() bool {
return p.flags&flagPolling != 0
}
func (p *PollDesc) markPolling() bool {
if p.flags&(flagCancel|flagPolling) == 0 {
p.flags |= flagPolling
return true
} else {
return false
}
}
var completeFuncs = [...]func(*PollDesc, int32) error{
tagListenerAccept: completeAccept,
tagConnSend: completeSend,
tagConnRecv: completeRecv,
tagCancelIo: completeCancelIo,
}
func completeAccept(pd *PollDesc, res int32) error {
ln := *(**TcpListener)(unsafe.Pointer(&pd.data))
return ln.accept(int(res))
}
func completeSend(pd *PollDesc, res int32) error {
conn := *(**TcpConn)(unsafe.Pointer(&pd.data))
return conn.completeSend(int(res))
}
func completeRecv(pd *PollDesc, res int32) error {
conn := *(**TcpConn)(unsafe.Pointer(&pd.data))
return conn.completeRecv(int(res))
}
func completeCancelIo(pd *PollDesc, res int32) error {
pd2 := *(**PollDesc)(unsafe.Pointer(&pd.data))
delete(app.cancels, pd2)
delete(app.cancels, pd)
return nil
}
func completeIo(data uint64, res int32, _ uint32) error {
pd := *(**PollDesc)(unsafe.Pointer(&data))
pd.flags ^= flagPolling
if pd.flags&flagCancel != 0 {
return nil
}
return completeFuncs[pd.tag](pd, res)
}