-
Notifications
You must be signed in to change notification settings - Fork 0
/
wsocket.go
386 lines (355 loc) · 9.04 KB
/
wsocket.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
package wsocket
import (
"context"
"errors"
"fmt"
"io"
"log"
"strings"
mqtt "github.com/soypat/natiu-mqtt"
"nhooyr.io/websocket"
)
var (
ErrNotConnected = errors.New("client not connected")
)
// Client shall be safe to use concurrently between two goroutines, one reader (Rx) and
// one that writes (Tx).
type Client struct {
mqc *mqtt.Client
cfg ClientConfig
msg *messenger
lastRxCtx context.Context
currentPI uint16
}
type ClientConfig struct {
// TODO(soypat): Add Will flags/fields?
URL string
// MQTT keepalive is amount of seconds between messages before server disconnects client automatically.
MQTTKeepAlive uint16
Username, Password string
WSOptions *websocket.DialOptions
Subs mqtt.Subscriptions
}
func NewClient(clientID string, config ClientConfig) (*Client, error) {
if config.Subs == nil {
return nil, errors.New("nil Sub field in config")
}
// Unsubscribe from all.
config.Subs.Unsubscribe("#", nil)
const bufsize = 8 * 1024
mqc := mqtt.NewClient(mqtt.DecoderNoAlloc{UserBuffer: make([]byte, bufsize)})
mqc.ID = clientID
c := Client{
mqc: mqc,
cfg: config,
currentPI: 1,
}
return &c, nil
}
func (c *Client) LastPacketIDSent() uint16 { return c.currentPI - 1 }
func (c *Client) Connect(ctx context.Context) error {
// A large part of this function is websocket setup and callback setup.
// The actual packet sending happens after the configuration.
if c.IsConnected() {
return errors.New("already connected")
}
c.lastRxCtx = ctx
conn, resp, err := websocket.Dial(ctx, c.cfg.URL, c.cfg.WSOptions)
if err != nil {
var answer []byte
if resp != nil && resp.Body != nil {
answer, _ = io.ReadAll(resp.Body)
}
return fmt.Errorf("ws err:%w. Response if present: %q", err, answer)
}
c.msg = &messenger{
ws: conn,
}
rxtx := c.rxtx()
rxtx.RxCallbacks, rxtx.TxCallbacks = c.callbacks()
// Setup Rx.
rxtx.SetRxTransport(&clientReader{Client: c})
err = c.UnsafePrepareRx(ctx)
if err != nil {
return err
}
err = c.UnsafePrepareTx(ctx)
if err != nil {
return err
}
// Ready to start sending packet now.
varconn := c.varconnect()
// TODO: Add Connack SP logic.
_, err = c.mqc.Connect(&varconn)
if err != nil {
return err
}
return nil
}
func (c *Client) callbacks() (mqtt.RxCallbacks, mqtt.TxCallbacks) {
return mqtt.RxCallbacks{
OnRxError: func(r *mqtt.Rx, err error) {
c.abnormalDisconnect(err)
},
OnConnack: func(r *mqtt.Rx, vc mqtt.VariablesConnack) error {
if vc.ReturnCode != 0 {
return errors.New(vc.ReturnCode.String())
}
return nil
},
}, mqtt.TxCallbacks{
OnTxError: func(tx *mqtt.Tx, err error) {
c.abnormalDisconnect(err)
},
OnSuccessfulTx: func(tx *mqtt.Tx) {
// Websocket Writers accumulate writes until Close is called.
// After Close called the writer flushes contents onto the network.
// This means we have to set the transport before each message.
w := c.msg.w
if w != nil {
err := w.Close()
if err != nil {
log.Println("error during OnSuccessfulTx closing writer")
}
}
c.msg.w = nil
},
}
}
func (c *Client) Ping(ctx context.Context) error {
if !c.IsConnected() {
return ErrNotConnected
}
err := c.UnsafePrepareTx(ctx)
if err != nil {
return err
}
err = c.UnsafePrepareRx(ctx)
if err != nil {
return err
}
return c.mqc.Ping()
}
func (c *Client) Subscribe(ctx context.Context, subReq []mqtt.SubscribeRequest) error {
if !c.IsConnected() {
return ErrNotConnected
}
err := c.UnsafePrepareTx(ctx)
if err != nil {
return err
}
err = c.UnsafePrepareRx(ctx)
if err != nil {
return err
}
suback, err := c.mqc.Subscribe(mqtt.VariablesSubscribe{
PacketIdentifier: c.newPI(),
TopicFilters: subReq,
})
if err != nil {
return err
}
if len(subReq) != len(suback.ReturnCodes) {
return errors.New("length of SUBACK return codes does not match SUBSCRIBE requests")
}
if err := suback.Validate(); err != nil {
return err
}
for i, rc := range suback.ReturnCodes {
if rc.IsValid() {
err := c.cfg.Subs.Subscribe(subReq[i].TopicFilter)
if err != nil {
return err
}
}
}
return nil
}
func (c *Client) PublishPayload(ctx context.Context, topic string, qos mqtt.QoSLevel, payload []byte) error {
if !c.IsConnected() {
return ErrNotConnected
}
if qos != mqtt.QoS0 {
return errors.New("only QoS0 supported")
}
pflags, err := mqtt.NewPublishFlags(qos, false, false)
if err != nil {
return err
}
vp := mqtt.VariablesPublish{
TopicName: []byte(topic),
PacketIdentifier: c.newPI(),
}
hdr, err := mqtt.NewHeader(mqtt.PacketPublish, pflags, uint32(vp.Size(qos)+len(payload)))
if err != nil {
return err
}
err = c.UnsafePrepareTx(ctx)
if err != nil {
return err
}
// c.lastRxCtx = ctx
err = c.mqc.PublishPayload(hdr, vp, payload)
if err != nil {
return err
}
return nil
}
// Disconnect performs a clean disconnect. If the clean disconnect fails it returns an error.
// Even if the clean disconnect fails and Disconnect returns error the result of IsConnected
// will always be false after a call to Disonnect.
func (c *Client) Disconnect(ctx context.Context) error {
if !c.IsConnected() {
return nil
}
// c.lastRxCtx = ctx
err := c.UnsafePrepareTx(ctx)
if err != nil {
return err
}
defer func() { c.msg = nil }()
err = c.mqc.Disconnect()
if err != nil {
c.abnormalDisconnect(fmt.Errorf("during Disconnect: %w", err))
}
c.msg = nil
return err
}
func (c *Client) ReadNextPacket(ctx context.Context) (int, error) {
if !c.IsConnected() {
return 0, ErrNotConnected
}
rxtx := c.rxtx()
err := c.UnsafePrepareRx(ctx)
if err != nil {
return 0, err
}
return rxtx.ReadNextPacket()
}
func (c *Client) SetOnPublishCallback(f func(mqtt.Header, mqtt.VariablesPublish, io.Reader) error) {
if f == nil {
c.rxtx().RxCallbacks.OnPub = nil
return
}
c.rxtx().RxCallbacks.OnPub = func(rx *mqtt.Rx, varPub mqtt.VariablesPublish, r io.Reader) error {
h := rx.LastReceivedHeader
return f(h, varPub, r)
}
}
// Prepare Tx must be called before sending a message over the RxTx
// returned by UnsafeRxTx.
func (c *Client) UnsafePrepareTx(ctx context.Context) error {
msg := c.msg // If msg is edited between here and NewTx no harm is done.
if !c.IsConnected() || msg == nil {
return ErrNotConnected
}
transport, err := msg.NewTx(ctx)
if err != nil {
return err
}
rxtx := c.rxtx()
rxtx.SetTxTransport(transport)
return nil
}
// Prepare Tx must be called before sending a message over the RxTx
// returned by UnsafeRxTx.
func (c *Client) UnsafePrepareRx(ctx context.Context) error {
msg := c.msg // If msg is edited between here and NewTx no harm is done.
if !c.IsConnected() || msg == nil {
return ErrNotConnected
}
c.lastRxCtx = ctx
return nil
}
// UnsafeRxTx returns the underyling RxTx with callback handlers and all.
// Not safe for concurrent use.
func (c *Client) UnsafeRxTx() *mqtt.RxTx { return c.rxtx() }
func (c *Client) abnormalDisconnect(err error) {
if c.IsConnected() {
// log.Println("abnormal disconnect call:", err)
// TODO, should this error be handled? I think not.
_ = c.msg.ws.Close(websocket.StatusInternalError, "graceful disconnect")
} else {
log.Println("abnormal disconnect call while connected:", err)
}
c.msg = nil
}
func (c *Client) IsConnected() bool { return c.msg != nil }
func (c *Client) varconnect() (varConn mqtt.VariablesConnect) {
varConn.SetDefaultMQTT([]byte(c.mqc.ID))
varConn.KeepAlive = c.cfg.MQTTKeepAlive
varConn.Username = []byte(c.cfg.Username)
varConn.Password = []byte(c.cfg.Password)
return varConn
}
func (c *Client) rxtx() *mqtt.RxTx { return c.mqc.UnsafeRxTxPointer() }
func (c *Client) newPI() uint16 {
pi := c.currentPI
c.currentPI++
return pi
}
type messenger struct {
ws *websocket.Conn
w io.WriteCloser
}
func (msr *messenger) NewTx(ctx context.Context) (io.WriteCloser, error) {
if msr.HasTx() {
return nil, errors.New("last message not yet sent")
}
w, err := msr.ws.Writer(ctx, websocket.MessageBinary)
msr.w = w
return w, err
}
func (msr *messenger) HasTx() bool { return msr.w != nil }
type clientReader struct {
*Client
buf []byte
}
func (cr *clientReader) Close() error {
cr.buf = nil
return nil
}
func (cr *clientReader) Read(p []byte) (n int, _ error) {
if !cr.IsConnected() {
return 0, ErrNotConnected
}
if len(cr.buf) > 0 {
// Pre read of contents.
n = copy(p, cr.buf)
cr.buf = cr.buf[n:]
if len(cr.buf) == 0 {
cr.buf = nil // Prevent memory leaks.
}
if n == len(p) {
return n, nil
}
}
if err := cr.lastRxCtx.Err(); err != nil {
return n, err
}
mt, b, err := cr.msg.ws.Read(cr.lastRxCtx)
if err != nil {
if strings.Contains(err.Error(), "WebSocket closed") {
cr.abnormalDisconnect(err)
}
// This code is not working as expected:
// if errors.As(err, &websocket.CloseError{}) {
// cr.abnormalDisconnect(err)
// }
return n, err
}
if mt != websocket.MessageBinary {
return 0, errors.New("expected binary message")
}
if len(cr.buf) == 0 {
cr.buf = b
} else {
cr.buf = append(cr.buf, b...)
}
n += copy(p, cr.buf)
cr.buf = cr.buf[n:]
if len(cr.buf) == 0 {
cr.buf = nil
}
return n, nil
}