-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathudpconnection.go
296 lines (253 loc) · 6.26 KB
/
udpconnection.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Author Raido Pahtma
// License MIT
// Package moteconnection - A SerialForwarder connection library.
package moteconnection
import (
"bufio"
"errors"
"fmt"
"io"
"net"
"strconv"
"sync/atomic"
"time"
)
type uconnection struct {
Conn net.Conn
Reader *bufio.Reader
Incoming chan []byte // Channel to parent
Closed chan bool // Channel to parent
IsClosed atomic.Value // Info about the connection itself
}
// UDPConnection - A SerialForwarder connection
type UDPConnection struct {
BaseMoteConnection
Host string
Port uint16
connections []connection
started bool
server bool
}
func (sfc *UDPConnection) runErrorHandler(err error) error {
if err != io.EOF && err != io.ErrUnexpectedEOF {
sfc.Debug.Printf("%s\n", err)
}
return err
}
func (conn *uconnection) runErrorHandler(err error) error {
if err != io.EOF && err != io.ErrUnexpectedEOF {
//conn.Sfc.Debug.Printf("%s\n", err)
}
return err
}
func (sfc *UDPConnection) connectErrorHandler(conn net.Conn, err error) error {
if conn != nil {
conn.Close()
}
if sfc.autoconnect {
go sfc.dial(sfc.period)
}
return sfc.runErrorHandler(err)
}
// Connect - initiate a synchronous connect
func (sfc *UDPConnection) Connect() error {
sfc.connectlock.Lock()
sfc.server = false
sfc.shouldconnect.Store(true)
sfc.autoconnect = false
sfc.connectlock.Unlock()
return sfc.dial(0)
}
// Autoconnect - initiate an asynchronous and self-maintaining connection
func (sfc *UDPConnection) Autoconnect(period time.Duration) {
sfc.connectlock.Lock()
defer sfc.connectlock.Unlock()
sfc.server = false
sfc.shouldconnect.Store(true)
sfc.autoconnect = true
sfc.period = period
go sfc.dial(0)
}
// Listen - Start a server and listen for incoming SF connections
func (sfc *UDPConnection) Listen() error {
sfc.connectlock.Lock()
defer sfc.connectlock.Unlock()
sfc.server = true
sfc.shouldconnect.Store(true)
go sfc.listenUDP()
return nil
}
func (sfc *UDPConnection) connectWatchdog(timeout time.Duration, conn net.Conn) {
select {
case v := <-sfc.watchdog:
if v {
sfc.Debug.Printf("Watchdog stop.\n")
} else {
sfc.Debug.Printf("Watchdog interrupt.\n")
conn.Close()
}
case <-time.After(timeout):
sfc.Debug.Printf("Timeout.\n")
conn.Close()
}
}
func (sfc *UDPConnection) listenUDP() {
uConn, err := net.ListenPacket("udp", ":"+strconv.Itoa(int(sfc.Port)))
if err != nil {
return
}
if sfc.started == false {
sfc.started = true
go sfc.run()
}
for {
buffer := make([]byte, 256)
n, addr, err := uConn.ReadFrom(buffer)
fmt.Printf("uConn: %s %d\n", addr, n)
if err != nil {
sfc.Error.Println(err)
// TODO signal that listener has closed?
return
}
if n > 12 {
sfc.incoming <- buffer[12:n]
} else {
sfc.Warning.Printf("RCV short %x", buffer[0:n])
}
}
}
func (sfc *UDPConnection) dial(delay time.Duration) error {
time.Sleep(delay)
sfc.connectlock.Lock()
defer sfc.connectlock.Unlock()
if sfc.connected.Load().(bool) {
return errors.New("Already connected")
}
if sfc.shouldconnect.Load().(bool) == false {
return errors.New("Connect interrupted")
}
constring := sfc.Host + ":" + strconv.Itoa(int(sfc.Port))
sfc.Info.Printf("Connecting to %s\n", constring)
conn, err := net.Dial("udp", constring)
if err != nil {
return sfc.connectErrorHandler(conn, err)
}
return sfc.connect(conn)
}
func (sfc *UDPConnection) connect(conn net.Conn) error {
connbuf := bufio.NewReader(conn)
sfc.notifyWatchdog(true)
sfc.connected.Store(true)
var c connection
c.Conn = conn
c.Reader = connbuf
c.Incoming = sfc.incoming
c.Closed = sfc.closed
sfc.connections = append(sfc.connections, c)
sfc.Info.Printf("Connection established.\n")
go c.read()
if sfc.started == false {
sfc.started = true
go sfc.run()
}
return nil
}
func (conn *uconnection) read() {
for {
len, err := conn.Reader.ReadByte()
if err == nil {
msg := make([]byte, len)
_, err := io.ReadFull(conn.Reader, msg)
if err == nil {
conn.Incoming <- msg
} else {
conn.runErrorHandler(err)
break
}
} else {
conn.runErrorHandler(err)
break
}
}
conn.IsClosed.Store(true)
conn.Closed <- true
}
func (sfc *UDPConnection) run() {
closing := false
for {
select {
case msg := <-sfc.incoming:
sfc.Debug.Printf("RCV(%d): %X\n", len(msg), msg)
if len(msg) > 0 {
if dispatcher, ok := sfc.dispatchers[msg[0]]; ok {
err := dispatcher.Receive(msg)
if err != nil {
sfc.Debug.Printf("Dispatcher error: %s", err)
}
} else {
sfc.Debug.Printf("No dispatcher for %02X!\n", msg[0])
}
}
case msg := <-sfc.outgoing:
length := make([]byte, 1)
length[0] = byte(len(msg))
sfc.Debug.Printf("SND(%d): %X\n", length[0], msg)
for _, conn := range sfc.connections {
conn.Conn.Write(msg)
}
case <-sfc.close:
for _, conn := range sfc.connections {
conn.Conn.Close()
}
closing = true
case <-sfc.closed:
sfc.Debug.Printf("Connection closed.\n")
sfc.connectlock.Lock()
// Connection cleanup
var conns []connection
for _, conn := range sfc.connections {
if conn.IsClosed.Load() == false {
conns = append(conns, conn)
}
}
sfc.connections = conns
sfc.connected.Store(false)
sfc.connectlock.Unlock()
if len(sfc.connections) == 0 {
if sfc.server {
sfc.Debug.Printf("No connections left.\n")
}
if closing {
return
} else if sfc.autoconnect {
go sfc.dial(sfc.period)
}
}
case dispatcher := <-sfc.removeDispatcher:
if sfc.dispatchers[dispatcher.Dispatch()] != dispatcher {
panic("Asked to remove a dispatcher that is not registered!")
}
delete(sfc.dispatchers, dispatcher.Dispatch())
case dispatcher := <-sfc.addDispatcher:
sfc.dispatchers[dispatcher.Dispatch()] = dispatcher
}
}
}
// NewUDPConnection - create a new connection
func NewUDPConnection(host string, port uint16) *UDPConnection {
sfc := new(UDPConnection)
sfc.InitLoggers()
sfc.connections = make([]connection, 0)
sfc.Host = host
sfc.Port = port
sfc.dispatchers = make(map[byte]Dispatcher)
sfc.removeDispatcher = make(chan Dispatcher)
sfc.addDispatcher = make(chan Dispatcher)
sfc.outgoing = make(chan []byte)
sfc.incoming = make(chan []byte)
sfc.watchdog = make(chan bool)
sfc.closed = make(chan bool)
sfc.close = make(chan bool)
sfc.connected.Store(false)
return sfc
}