-
Notifications
You must be signed in to change notification settings - Fork 26
/
netcode_conn.go
188 lines (152 loc) · 3.39 KB
/
netcode_conn.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
package netcode
import (
"log"
"net"
)
type NetcodeData struct {
data []byte
from *net.UDPAddr
}
const (
SOCKET_RCVBUF_SIZE = 2048 * 2048
SOCKET_SNDBUF_SIZE = 2048 * 2048
)
type NetcodeRecvHandler func(data *NetcodeData)
type NetcodeConn struct {
conn *net.UDPConn
closeCh chan struct{}
isClosed bool
recvSize int
sendSize int
maxBytes int
recvHandlerFn NetcodeRecvHandler
}
func NewNetcodeConn() *NetcodeConn {
c := &NetcodeConn{}
c.closeCh = make(chan struct{})
c.isClosed = true
c.maxBytes = MAX_PACKET_BYTES
c.recvSize = SOCKET_RCVBUF_SIZE
c.sendSize = SOCKET_SNDBUF_SIZE
return c
}
func (c *NetcodeConn) SetRecvHandler(recvHandlerFn NetcodeRecvHandler) {
c.recvHandlerFn = recvHandlerFn
}
func (c *NetcodeConn) Write(b []byte) (int, error) {
if c.isClosed {
return -1, ErrWriteClosedSocket
}
return c.conn.Write(b)
}
func (c *NetcodeConn) WriteTo(b []byte, to *net.UDPAddr) (int, error) {
if c.isClosed {
return -1, ErrWriteClosedSocket
}
return c.conn.WriteTo(b, to)
}
func (c *NetcodeConn) Close() error {
if !c.isClosed {
close(c.closeCh)
}
c.isClosed = true
if c.conn != nil {
return c.conn.Close()
}
return nil
}
func (c *NetcodeConn) SetReadBuffer(bytes int) {
c.recvSize = bytes
}
func (c *NetcodeConn) SetWriteBuffer(bytes int) {
c.sendSize = bytes
}
// LocalAddr returns the local network address.
func (c *NetcodeConn) LocalAddr() net.Addr {
return c.conn.LocalAddr()
}
// RemoteAddr returns the remote network address.
func (c *NetcodeConn) RemoteAddr() net.Addr {
return c.conn.RemoteAddr()
}
func (c *NetcodeConn) Dial(address *net.UDPAddr) error {
var err error
if c.recvHandlerFn == nil {
return ErrPacketHandlerBeforeListen
}
c.closeCh = make(chan struct{})
c.conn, err = net.DialUDP(address.Network(), nil, address)
if err != nil {
return err
}
return c.create()
}
func (c *NetcodeConn) Listen(address *net.UDPAddr) error {
var err error
if c.recvHandlerFn == nil {
return ErrPacketHandlerBeforeListen
}
c.conn, err = net.ListenUDP(address.Network(), address)
if err != nil {
return err
}
c.create()
return err
}
func (c *NetcodeConn) create() error {
c.isClosed = false
c.conn.SetReadBuffer(c.recvSize)
c.conn.SetWriteBuffer(c.sendSize)
go c.readLoop()
return nil
}
func (c *NetcodeConn) receiver(ch chan *NetcodeData) {
for {
if err := c.read(); err == nil {
select {
case <-c.closeCh:
return
default:
continue
}
} else {
if c.isClosed {
return
}
log.Printf("error reading data from socket: %s\n", err)
}
}
}
// read does the actual connection read call, verifies we have a
// buffer > 0 and < maxBytes and is of a valid packet type before
// we bother to attempt to actually dispatch it to the recvHandlerFn.
func (c *NetcodeConn) read() error {
var n int
var from *net.UDPAddr
var err error
netData := &NetcodeData{}
netData.data = make([]byte, c.maxBytes)
n, from, err = c.conn.ReadFromUDP(netData.data)
if err != nil {
return err
}
if n == 0 {
return ErrSocketZeroRecv
}
if n > c.maxBytes {
return ErrPacketSizeMax
}
// check if it's a valid packet
if NewPacket(netData.data) == nil {
return ErrInvalidPacket
}
netData.data = netData.data[:n]
netData.from = from
c.recvHandlerFn(netData)
return nil
}
// dispatch the NetcodeData to the bound recvHandler function.
func (c *NetcodeConn) readLoop() {
dataCh := make(chan *NetcodeData)
go c.receiver(dataCh)
}