forked from jeffallen/mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathincoming_conn.go
321 lines (276 loc) · 7.21 KB
/
incoming_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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package mqtt
import (
"fmt"
"io"
"log"
"net"
"strings"
"sync"
proto "github.com/huin/mqtt"
)
// An IncomingConn represents a connection into a Server.
type incomingConn struct {
svr *Server
conn net.Conn
jobs chan job
clientid string
Done chan struct{}
}
var clients = make(map[string]*incomingConn)
var clientsMu sync.Mutex
const sendingQueueLength = 10000
// newIncomingConn creates a new incomingConn associated with this
// server. The connection becomes the property of the incomingConn
// and should not be touched again by the caller until the Done
// channel becomes readable.
func (s *Server) newIncomingConn(conn net.Conn) *incomingConn {
return &incomingConn{
svr: s,
conn: conn,
jobs: make(chan job, sendingQueueLength),
Done: make(chan struct{}),
}
}
type receipt chan struct{}
// Wait for the receipt to indicate that the job is done.
func (r receipt) wait() {
// TODO: timeout
<-r
}
type job struct {
m proto.Message
r receipt
}
// Start reading and writing on this connection.
func (c *incomingConn) start() {
go c.reader()
go c.writer()
}
// Add this connection to the map, or find out that an existing connection
// already exists for the same client-id.
func (c *incomingConn) add() *incomingConn {
clientsMu.Lock()
defer clientsMu.Unlock()
existing, ok := clients[c.clientid]
if !ok {
// this client id already exists, return it
return existing
}
clients[c.clientid] = c
return nil
}
// Delete a connection; the conection must be closed by the caller first.
func (c *incomingConn) del() {
clientsMu.Lock()
defer clientsMu.Unlock()
delete(clients, c.clientid)
return
}
// Replace any existing connection with this one. The one to be replaced,
// if any, must be closed first by the caller.
func (c *incomingConn) replace() {
clientsMu.Lock()
defer clientsMu.Unlock()
// Check that any existing connection is already closed.
existing, ok := clients[c.clientid]
if ok {
die := false
select {
case _, ok := <-existing.jobs:
// what? we are expecting that this channel is closed!
if ok {
die = true
}
default:
die = true
}
if die {
panic("attempting to replace a connection that is not closed")
}
delete(clients, c.clientid)
}
clients[c.clientid] = c
return
}
// Queue a message; no notification of sending is done.
func (c *incomingConn) submit(m proto.Message) {
j := job{m: m}
select {
case c.jobs <- j:
default:
log.Print(c, ": failed to submit message")
}
return
}
func (c *incomingConn) String() string {
return fmt.Sprintf("{IncomingConn: %v}", c.clientid)
}
// Queue a message, returns a channel that will be readable
// when the message is sent.
func (c *incomingConn) submitSync(m proto.Message) receipt {
j := job{m: m, r: make(receipt)}
c.jobs <- j
return j.r
}
func (c *incomingConn) reader() {
// On exit, close the connection and arrange for the writer to exit
// by closing the output channel.
defer func() {
c.conn.Close()
c.svr.stats.clientDisconnect()
close(c.jobs)
}()
for {
// TODO: timeout (first message and/or keepalives)
m, err := proto.DecodeOneMessage(c.conn, nil)
if err != nil {
if err == io.EOF {
return
}
if strings.HasSuffix(err.Error(), "use of closed network connection") {
return
}
log.Print("reader: ", err)
return
}
c.svr.stats.messageRecv()
if c.svr.Dump {
log.Printf("dump in: %T", m)
}
switch m := m.(type) {
case *proto.Connect:
rc := proto.RetCodeAccepted
if (m.ProtocolName != "MQIsdp" && m.ProtocolName != "MQTT") ||
(m.ProtocolVersion != 3 && m.ProtocolVersion != 4) {
log.Print("reader: reject connection from ", m.ProtocolName, " version ", m.ProtocolVersion)
rc = proto.RetCodeUnacceptableProtocolVersion
}
// Check client id.
if len(m.ClientId) < 1 || len(m.ClientId) > 23 {
rc = proto.RetCodeIdentifierRejected
}
c.clientid = m.ClientId
// Disconnect existing connections.
if existing := c.add(); existing != nil {
disconnect := &proto.Disconnect{}
r := existing.submitSync(disconnect)
r.wait()
existing.del()
}
c.add()
// TODO: Last will
connack := &proto.ConnAck{
ReturnCode: rc,
}
c.submit(connack)
// close connection if it was a bad connect
if rc != proto.RetCodeAccepted {
log.Printf("Connection refused for %v: %v", c.conn.RemoteAddr(), ConnectionErrors[rc])
return
}
// Log in mosquitto format.
clean := 0
if m.CleanSession {
clean = 1
}
log.Printf("New client connected from %v as %v (c%v, k%v).", c.conn.RemoteAddr(), c.clientid, clean, m.KeepAliveTimer)
case *proto.Publish:
// TODO: Proper QoS support
if m.Header.QosLevel != proto.QosAtMostOnce {
log.Printf("reader: no support for QoS %v yet", m.Header.QosLevel)
return
}
if m.Header.QosLevel != proto.QosAtMostOnce && m.MessageId == 0 {
// Invalid message ID. See MQTT-2.3.1-1.
log.Printf("reader: invalid MessageId in PUBLISH.")
return
}
if isWildcard(m.TopicName) {
log.Print("reader: ignoring PUBLISH with wildcard topic ", m.TopicName)
} else {
c.svr.subs.submit(c, m)
}
c.submit(&proto.PubAck{MessageId: m.MessageId})
case *proto.PingReq:
c.submit(&proto.PingResp{})
case *proto.Subscribe:
if m.Header.QosLevel != proto.QosAtLeastOnce {
// protocol error, disconnect
return
}
if m.MessageId == 0 {
// Invalid message ID. See MQTT-2.3.1-1.
log.Printf("reader: invalid MessageId in SUBSCRIBE.")
return
}
suback := &proto.SubAck{
MessageId: m.MessageId,
TopicsQos: make([]proto.QosLevel, len(m.Topics)),
}
for i, tq := range m.Topics {
// TODO: Handle varying QoS correctly
c.svr.subs.add(tq.Topic, c)
suback.TopicsQos[i] = proto.QosAtMostOnce
}
c.submit(suback)
// Process retained messages.
for _, tq := range m.Topics {
c.svr.subs.sendRetain(tq.Topic, c)
}
case *proto.Unsubscribe:
if m.Header.QosLevel != proto.QosAtMostOnce && m.MessageId == 0 {
// Invalid message ID. See MQTT-2.3.1-1.
log.Printf("reader: invalid MessageId in UNSUBSCRIBE.")
return
}
for _, t := range m.Topics {
c.svr.subs.unsub(t, c)
}
ack := &proto.UnsubAck{MessageId: m.MessageId}
c.submit(ack)
case *proto.Disconnect:
return
default:
log.Printf("reader: unknown msg type %T", m)
return
}
}
}
func (c *incomingConn) writer() {
// Close connection on exit in order to cause reader to exit.
defer func() {
c.conn.Close()
c.del()
c.svr.subs.unsubAll(c)
}()
for job := range c.jobs {
if c.svr.Dump {
log.Printf("dump out: %T", job.m)
}
// TODO: write timeout
err := job.m.Encode(c.conn)
if job.r != nil {
// notifiy the sender that this message is sent
close(job.r)
}
if err != nil {
// This one is not interesting; it happens when clients
// disappear before we send their acks.
oe, isoe := err.(*net.OpError)
if isoe && oe.Err.Error() == "use of closed network connection" {
return
}
// In Go < 1.5, the error is not an OpError.
if err.Error() == "use of closed network connection" {
return
}
log.Print("writer: ", err)
return
}
c.svr.stats.messageSend()
if _, ok := job.m.(*proto.Disconnect); ok {
log.Print("writer: sent disconnect message")
return
}
}
}