-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisten_udp.go
111 lines (94 loc) · 2.36 KB
/
listen_udp.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package coap
import (
"errors"
"net"
"time"
)
type UdpListener struct {
name string
socket *net.UDPConn
handler *Server
shutdown bool
}
func (l *UdpListener) listen(name string, addr string, handler *Server) error {
uaddr, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
return err
}
listener, err := net.ListenUDP("udp", uaddr)
if err != nil {
return err
}
l.socket = listener
l.name = name
l.handler = handler
go l.reader()
return nil
}
func (l *UdpListener) reader() {
var rawReq = make([]byte, 8192)
for {
rawLen, from, err := l.socket.ReadFromUDP(rawReq)
if err != nil {
if l.shutdown {
logDebug(nil, nil, "coap: reader shutdown")
return
}
logWarn(nil, err, "coap: error reading COAP packet")
go l.reader()
return
}
newReq := append([]byte(nil), rawReq[:rawLen]...)
sniffActivity("udp", SniffRead, from.String(), l.socket.LocalAddr().String(), newReq)
go l.handle(newReq, from)
}
}
func (l *UdpListener) handle(rawReq []byte, from *net.UDPAddr) {
var req Message
if err := req.unmarshalBinary(rawReq); err != nil {
logError(nil, err, "coap: error parsing COAP header")
return
}
req.Meta.RemoteAddr = from.String()
req.Meta.ListenerName = l.name
req.Meta.ReceivedAt = time.Now().UTC()
req.Meta.Server = l.handler
rsp := l.handler.handleMessage(&req)
if rsp != nil {
rawRsp, err := rsp.marshalBinary()
if err != nil {
logError(nil, err, "coap: error marshaling COAP response")
return
}
if rawRsp != nil {
sniffActivity("udp", SniffWrite, l.socket.LocalAddr().String(), from.String(), rawRsp)
_, err = l.socket.WriteToUDP(rawRsp, from)
if err != nil {
logWarn(nil, err, "coap: error writing coap response")
}
}
}
return
}
func (l *UdpListener) Send(addr string, data []byte) error {
if l.shutdown {
return errors.New("coap: port is shutdown")
}
uaddr, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
return err
}
sniffActivity("udp", SniffWrite, l.socket.LocalAddr().String(), addr, data)
_, err = l.socket.WriteToUDP(data, uaddr)
if err != nil {
return err
}
return nil
}
func (l *UdpListener) Close() {
l.shutdown = true
_ = l.socket.Close()
}