forked from meshifyiot/paho.mqtt.golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.go
433 lines (404 loc) · 11.7 KB
/
net.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
* Copyright (c) 2013 IBM Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Seth Hoenig
* Allan Stockdill-Mander
* Mike Robertson
*/
package mqtt
import (
"crypto/tls"
"errors"
"fmt"
"net"
"net/url"
"os"
"reflect"
"sync/atomic"
"time"
"github.com/eclipse/paho.mqtt.golang/packets"
"golang.org/x/net/proxy"
"golang.org/x/net/websocket"
)
// ErrAckNotSent indicates that the acknowledgement has not been sent for the
// corresponding message.
var ErrAckNotSent = errors.New("message ack not sent")
// ErrAckUnknown indicates that it is not known if the message acknowledgement
// has been sent.
//
// This happens if the MQTT client is stopped after the packets is enqueued to
// be sent, but the confirmation that the message has been sent has not been
// received.
var ErrAckUnknown = errors.New("message ack not confirmed")
func signalError(c chan<- error, err error) {
select {
case c <- err:
default:
}
}
func openConnection(uri *url.URL, tlsc *tls.Config, timeout time.Duration) (net.Conn, error) {
switch uri.Scheme {
case "ws":
conn, err := websocket.Dial(uri.String(), "mqtt", fmt.Sprintf("http://%s", uri.Host))
if err != nil {
return nil, err
}
conn.PayloadType = websocket.BinaryFrame
return conn, err
case "wss":
config, _ := websocket.NewConfig(uri.String(), fmt.Sprintf("https://%s", uri.Host))
config.Protocol = []string{"mqtt"}
config.TlsConfig = tlsc
conn, err := websocket.DialConfig(config)
if err != nil {
return nil, err
}
conn.PayloadType = websocket.BinaryFrame
return conn, err
case "tcp":
allProxy := os.Getenv("all_proxy")
if len(allProxy) == 0 {
conn, err := net.DialTimeout("tcp", uri.Host, timeout)
if err != nil {
return nil, err
}
return conn, nil
}
proxyDialer := proxy.FromEnvironment()
conn, err := proxyDialer.Dial("tcp", uri.Host)
if err != nil {
return nil, err
}
return conn, nil
case "unix":
conn, err := net.DialTimeout("unix", uri.Host, timeout)
if err != nil {
return nil, err
}
return conn, nil
case "ssl":
fallthrough
case "tls":
fallthrough
case "tcps":
allProxy := os.Getenv("all_proxy")
if len(allProxy) == 0 {
conn, err := tls.DialWithDialer(&net.Dialer{Timeout: timeout}, "tcp", uri.Host, tlsc)
if err != nil {
return nil, err
}
return conn, nil
}
proxyDialer := proxy.FromEnvironment()
conn, err := proxyDialer.Dial("tcp", uri.Host)
if err != nil {
return nil, err
}
tlsConn := tls.Client(conn, tlsc)
err = tlsConn.Handshake()
if err != nil {
conn.Close()
return nil, err
}
return tlsConn, nil
}
return nil, errors.New("Unknown protocol")
}
// actually read incoming messages off the wire
// send Message object into ibound channel
func incoming(c *client) {
var err error
var cp packets.ControlPacket
defer c.workers.Done()
DEBUG.Println(NET, "incoming started")
for {
if cp, err = packets.ReadPacket(c.conn); err != nil {
break
}
DEBUG.Println(NET, "Received Message")
select {
case c.ibound <- cp:
// Notify keepalive logic that we recently received a packet
if c.options.KeepAlive != 0 {
atomic.StoreInt64(&c.lastReceived, time.Now().Unix())
}
case <-c.stop:
// This avoids a deadlock should a message arrive while shutting down.
// In that case the "reader" of c.ibound might already be gone
WARN.Println(NET, "incoming dropped a received message during shutdown")
break
}
}
// We received an error on read.
// If disconnect is in progress, swallow error and return
select {
case <-c.stop:
DEBUG.Println(NET, "incoming stopped")
return
// Not trying to disconnect, send the error to the errors channel
default:
ERROR.Println(NET, "incoming stopped with error", err)
signalError(c.errors, err)
return
}
}
// receive a Message object on obound, and then
// actually send outgoing message to the wire
func outgoing(c *client) {
defer c.workers.Done()
DEBUG.Println(NET, "outgoing started")
for {
DEBUG.Println(NET, "outgoing waiting for an outbound message")
select {
case <-c.stop:
DEBUG.Println(NET, "outgoing stopped")
return
case pub := <-c.obound:
msg := pub.p.(*packets.PublishPacket)
if c.options.WriteTimeout > 0 {
c.conn.SetWriteDeadline(time.Now().Add(c.options.WriteTimeout))
}
if err := msg.Write(c.conn); err != nil {
ERROR.Println(NET, "outgoing stopped with error", err)
pub.t.setError(err)
signalError(c.errors, err)
return
}
if c.options.WriteTimeout > 0 {
// If we successfully wrote, we don't want the timeout to happen during an idle period
// so we reset it to infinite.
c.conn.SetWriteDeadline(time.Time{})
}
if msg.Qos == 0 {
pub.t.flowComplete()
}
DEBUG.Println(NET, "obound wrote msg, id:", msg.MessageID)
case msg := <-c.oboundP:
switch msg.p.(type) {
case *packets.SubscribePacket:
msg.p.(*packets.SubscribePacket).MessageID = c.getID(msg.t)
case *packets.UnsubscribePacket:
msg.p.(*packets.UnsubscribePacket).MessageID = c.getID(msg.t)
}
DEBUG.Println(NET, "obound priority msg to write, type", reflect.TypeOf(msg.p))
if err := msg.p.Write(c.conn); err != nil {
if msg.sentCB != nil {
msg.sentCB(err)
}
ERROR.Println(NET, "outgoing stopped with error", err)
if msg.t != nil {
msg.t.setError(err)
} else {
ERROR.Println(NET, "unexpected nil message token")
}
signalError(c.errors, err)
return
} else if msg.sentCB != nil {
msg.sentCB(nil)
}
switch msg.p.(type) {
case *packets.DisconnectPacket:
msg.t.(*DisconnectToken).flowComplete()
DEBUG.Println(NET, "outbound wrote disconnect, stopping")
return
}
}
// Reset ping timer after sending control packet.
if c.options.KeepAlive != 0 {
atomic.StoreInt64(&c.lastSent, time.Now().Unix())
}
}
}
// sendPacketDeferred returns a closure that sends the given ControlPacket when
// called.
func sendPacketDeferred(c *client, p packets.ControlPacket) func() error {
return func() (e error) {
defer func() {
if r := recover(); r != nil {
e = fmt.Errorf("recovery: %+v", r)
}
}()
sent := make(chan error)
pt := &PacketAndToken{p: p,
sentCB: func(e error) {
sent <- e
},
}
// Due to Go randomly selecting a case in select blocks in the event
// of multiple channel being ready simultaneously, this is
// necessary.
// Because the stop channel may be closed before this function is
// called, it is effectively random if the packet send case or the
// stop receive case is taken. This prevents the message from being
// sent when the stop channel is closed.
select {
case <-c.stop:
return ErrAckNotSent
default:
}
defer func() {
// This timer is to fix a case where the sent channel is about
// to be ready, but not quite ready. In the common, happy path,
// case, this timer will wait for almost no time.
//
// In tests, this brings the sent channel being preferred over
// the stop channel from 95+% of the time to 100% of the time.
t := time.NewTimer(25 * time.Millisecond)
defer t.Stop()
// time.Sleep(100 * time.Millisecond)
// This is select is used for the same reason as above, but
// after the receive. Since send can only return a value if it
// was written out, its error should be used.
select {
case err := <-sent:
e = err
case <-t.C:
}
}()
// Enqueue the packet to be sent and block until it is sent.
select {
case c.oboundP <- pt:
select {
case err := <-sent:
return err
case <-c.stop:
return ErrAckUnknown
}
case <-c.stop:
return ErrAckNotSent
}
}
}
// receive Message objects on ibound
// store messages if necessary
// send replies on obound
// delete messages from store if necessary
func alllogic(c *client) {
defer c.workers.Done()
DEBUG.Println(NET, "logic started")
for {
DEBUG.Println(NET, "logic waiting for msg on ibound")
select {
case msg := <-c.ibound:
DEBUG.Println(NET, "logic got msg on ibound")
persistInbound(c.persist, msg)
switch m := msg.(type) {
case *packets.PingrespPacket:
DEBUG.Println(NET, "received pingresp")
atomic.StoreInt32(&c.pingOutstanding, 0)
case *packets.SubackPacket:
DEBUG.Println(NET, "received suback, id:", m.MessageID)
token := c.getToken(m.MessageID)
switch t := token.(type) {
case *SubscribeToken:
DEBUG.Println(NET, "granted qoss", m.ReturnCodes)
for i, qos := range m.ReturnCodes {
t.subResult[t.subs[i]] = qos
}
}
token.flowComplete()
c.freeID(m.MessageID)
case *packets.UnsubackPacket:
DEBUG.Println(NET, "received unsuback, id:", m.MessageID)
c.getToken(m.MessageID).flowComplete()
c.freeID(m.MessageID)
case *packets.PublishPacket:
DEBUG.Println(NET, "received publish, msgId:", m.MessageID)
DEBUG.Println(NET, "putting msg on onPubChan")
switch m.Qos {
case 2:
pr := packets.NewControlPacket(packets.Pubrec).(*packets.PubrecPacket)
pr.MessageID = m.MessageID
if c.options.DeferredAck {
pr.Qos = m.Qos
m.AckCB = sendPacketDeferred(c, pr)
c.incomingPubChan <- m
DEBUG.Println(NET, "done putting msg on incomingPubChan")
} else {
c.incomingPubChan <- m
DEBUG.Println(NET, "done putting msg on incomingPubChan")
DEBUG.Println(NET, "putting pubrec msg on obound")
select {
case c.oboundP <- &PacketAndToken{p: pr, t: nil}:
case <-c.stop:
}
DEBUG.Println(NET, "done putting pubrec msg on obound")
}
case 1:
pa := packets.NewControlPacket(packets.Puback).(*packets.PubackPacket)
pa.MessageID = m.MessageID
if c.options.DeferredAck {
pa.Qos = m.Qos
m.AckCB = sendPacketDeferred(c, pa)
c.incomingPubChan <- m
DEBUG.Println(NET, "done putting msg on incomingPubChan")
} else {
c.incomingPubChan <- m
DEBUG.Println(NET, "done putting msg on incomingPubChan")
DEBUG.Println(NET, "putting puback msg on obound")
persistOutbound(c.persist, pa)
select {
case c.oboundP <- &PacketAndToken{p: pa, t: nil}:
case <-c.stop:
}
DEBUG.Println(NET, "done putting puback msg on obound")
}
case 0:
select {
case c.incomingPubChan <- m:
case <-c.stop:
}
DEBUG.Println(NET, "done putting msg on incomingPubChan")
}
case *packets.PubackPacket:
DEBUG.Println(NET, "received puback, id:", m.MessageID)
// c.receipts.get(msg.MsgId()) <- Receipt{}
// c.receipts.end(msg.MsgId())
c.getToken(m.MessageID).flowComplete()
c.freeID(m.MessageID)
case *packets.PubrecPacket:
DEBUG.Println(NET, "received pubrec, id:", m.MessageID)
prel := packets.NewControlPacket(packets.Pubrel).(*packets.PubrelPacket)
prel.MessageID = m.MessageID
select {
case c.oboundP <- &PacketAndToken{p: prel, t: nil}:
case <-c.stop:
}
case *packets.PubrelPacket:
DEBUG.Println(NET, "received pubrel, id:", m.MessageID)
pc := packets.NewControlPacket(packets.Pubcomp).(*packets.PubcompPacket)
pc.MessageID = m.MessageID
persistOutbound(c.persist, pc)
select {
case c.oboundP <- &PacketAndToken{p: pc, t: nil}:
case <-c.stop:
}
case *packets.PubcompPacket:
DEBUG.Println(NET, "received pubcomp, id:", m.MessageID)
c.getToken(m.MessageID).flowComplete()
c.freeID(m.MessageID)
}
case <-c.stop:
WARN.Println(NET, "logic stopped")
return
}
}
}
func errorWatch(c *client) {
defer c.workers.Done()
select {
case <-c.stop:
WARN.Println(NET, "errorWatch stopped")
return
case err := <-c.errors:
ERROR.Println(NET, "error triggered, stopping")
go c.internalConnLost(err)
return
}
}