-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathpacket.go
211 lines (175 loc) · 4.72 KB
/
packet.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package sonic
import (
"fmt"
"io"
"net"
"sync/atomic"
"syscall"
"github.com/talostrading/sonic/internal"
"github.com/talostrading/sonic/sonicerrors"
"github.com/talostrading/sonic/sonicopts"
)
var _ PacketConn = &packetConn{}
type packetConn struct {
ioc *IO
slot internal.Slot
localAddr net.Addr
remoteAddr net.Addr
closed uint32
}
// NewPacketConn establishes a packet based stream-less connection which is optionally bound to the specified addr.
//
// If addr is empty, the connection is bound to a random address which can be obtained by calling LocalAddr().
func NewPacketConn(ioc *IO, network, addr string, opts ...sonicopts.Option) (PacketConn, error) {
if network[:3] != "udp" {
return nil, fmt.Errorf("network must start with udp for DialPacket")
}
fd, localAddr, err := internal.CreateSocketUDP(network, addr)
if err != nil {
return nil, err
}
if err := syscall.Bind(fd, internal.ToSockaddr(localAddr)); err != nil {
return nil, err
}
return &packetConn{
ioc: ioc,
slot: internal.Slot{Fd: fd},
localAddr: localAddr,
closed: 0,
}, nil
}
func (c *packetConn) ReadFrom(b []byte) (n int, from net.Addr, err error) {
var addr syscall.Sockaddr
n, addr, err = syscall.Recvfrom(c.slot.Fd, b, 0)
from = internal.FromSockaddr(addr)
if err != nil {
if err == syscall.EWOULDBLOCK || err == syscall.EAGAIN {
return 0, nil, sonicerrors.ErrWouldBlock
}
return 0, nil, err
}
if n == 0 {
return 0, from, io.EOF
}
if n < 0 {
n = 0 // errors embeds the information
}
return n, from, err
}
func (c *packetConn) AsyncReadFrom(b []byte, cb AsyncReadCallbackPacket) {
c.asyncReadFrom(b, false, cb)
}
func (c *packetConn) AsyncReadAllFrom(b []byte, cb AsyncReadCallbackPacket) {
c.asyncReadFrom(b, true, cb)
}
func (c *packetConn) asyncReadFrom(b []byte, readAll bool, cb AsyncReadCallbackPacket) {
if c.ioc.Dispatched < MaxCallbackDispatch {
c.asyncReadNow(b, 0, readAll, func(err error, n int, addr net.Addr) {
c.ioc.Dispatched++
cb(err, n, addr)
c.ioc.Dispatched--
})
} else {
c.scheduleRead(b, 0, readAll, cb)
}
}
func (c *packetConn) asyncReadNow(b []byte, readBytes int, readAll bool, cb AsyncReadCallbackPacket) {
n, addr, err := c.ReadFrom(b)
readBytes += n
if err == nil && !(readAll && readBytes != len(b)) {
cb(err, readBytes, addr)
return
}
if err == sonicerrors.ErrWouldBlock {
c.scheduleRead(b, readBytes, readAll, cb)
} else {
cb(err, readBytes, addr)
}
}
func (c *packetConn) scheduleRead(b []byte, readBytes int, readAll bool, cb AsyncReadCallbackPacket) {
if c.Closed() {
cb(io.EOF, readBytes, nil)
return
}
handler := c.getReadHandler(b, readBytes, readAll, cb)
c.slot.Set(internal.ReadEvent, handler)
if err := c.ioc.SetRead(&c.slot); err != nil {
cb(err, readBytes, nil)
} else {
c.ioc.Register(&c.slot)
}
}
func (c *packetConn) getReadHandler(b []byte, readBytes int, readAll bool, cb AsyncReadCallbackPacket) internal.Handler {
return func(err error) {
c.ioc.Deregister(&c.slot)
if err != nil {
cb(err, readBytes, nil)
} else {
c.asyncReadNow(b, readBytes, readAll, cb)
}
}
}
func (c *packetConn) WriteTo(b []byte, to net.Addr) error {
err := syscall.Sendto(c.slot.Fd, b, 0, internal.ToSockaddr(to))
if err == syscall.EWOULDBLOCK || err == syscall.EAGAIN {
return sonicerrors.ErrWouldBlock
}
return err
}
func (c *packetConn) AsyncWriteTo(b []byte, to net.Addr, cb AsyncWriteCallbackPacket) {
if c.ioc.Dispatched < MaxCallbackDispatch {
c.asyncWriteToNow(b, to, func(err error) {
c.ioc.Dispatched++
cb(err)
c.ioc.Dispatched--
})
} else {
c.scheduleWrite(b, to, cb)
}
}
func (c *packetConn) asyncWriteToNow(b []byte, to net.Addr, cb AsyncWriteCallbackPacket) {
err := c.WriteTo(b, to)
if err == sonicerrors.ErrWouldBlock {
c.scheduleWrite(b, to, cb)
} else {
cb(err)
}
}
func (c *packetConn) scheduleWrite(b []byte, to net.Addr, cb AsyncWriteCallbackPacket) {
if c.Closed() {
cb(io.EOF)
return
}
handler := c.getWriteHandler(b, to, cb)
c.slot.Set(internal.WriteEvent, handler)
if err := c.ioc.SetWrite(&c.slot); err != nil {
cb(err)
} else {
c.ioc.Register(&c.slot)
}
}
func (c *packetConn) getWriteHandler(b []byte, to net.Addr, cb AsyncWriteCallbackPacket) internal.Handler {
return func(err error) {
c.ioc.Deregister(&c.slot)
if err != nil {
cb(err)
} else {
c.asyncWriteToNow(b, to, cb)
}
}
}
func (c *packetConn) Close() error {
atomic.StoreUint32(&c.closed, 1)
_ = c.ioc.UnsetReadWrite(&c.slot)
c.ioc.Deregister(&c.slot)
return syscall.Close(c.slot.Fd)
}
func (c *packetConn) Closed() bool {
return atomic.LoadUint32(&c.closed) == 1
}
func (c *packetConn) LocalAddr() net.Addr {
return c.localAddr
}
func (c *packetConn) RawFd() int {
return c.slot.Fd
}