-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclient.go
144 lines (128 loc) · 3.33 KB
/
client.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
// Copyright 2019 The mqtt-go authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mqtt
import (
"io"
"sync"
)
// BaseClient is a low layer MQTT client.
// Zero values with valid underlying Transport is a valid BaseClient.
type BaseClient struct {
// Transport is an underlying connection. Typically net.Conn.
Transport io.ReadWriteCloser
// ConnState is called if the connection state is changed.
ConnState func(ConnState, error)
// MaxPayloadLen is a maximum allowed length of message payload.
// 0 means unlimited. (It will panic if exceeds protocol maximum message length (256MB).)
MaxPayloadLen int
handler Handler
sig *signaller
mu sync.RWMutex
connState ConnState
err error
muErr sync.RWMutex
connClosed chan struct{}
muConnecting sync.RWMutex
muWrite sync.Mutex
idLast uint32
}
// Handle registers the message handler.
func (c *BaseClient) Handle(handler Handler) {
c.mu.Lock()
defer c.mu.Unlock()
c.handler = handler
}
func (c *BaseClient) write(b []byte) error {
l := len(b)
c.muWrite.Lock()
defer c.muWrite.Unlock()
for i := 0; i < l; {
n, err := c.Transport.Write(b[i : l-i])
if err != nil {
return err
}
i += n
}
return nil
}
type signaller struct {
chConnAck chan *pktConnAck
chPingResp chan *pktPingResp
chPubAck map[uint16]chan *pktPubAck
chPubRec map[uint16]chan *pktPubRec
chPubComp map[uint16]chan *pktPubComp
chSubAck map[uint16]chan *pktSubAck
chUnsubAck map[uint16]chan *pktUnsubAck
mu sync.RWMutex
}
func (s *signaller) ConnAck() chan *pktConnAck {
s.mu.RLock()
defer s.mu.RUnlock()
return s.chConnAck
}
func (s *signaller) PingResp() chan *pktPingResp {
s.mu.RLock()
defer s.mu.RUnlock()
return s.chPingResp
}
func (s *signaller) PubAck(id uint16) (chan *pktPubAck, bool) {
s.mu.RLock()
defer s.mu.RUnlock()
if s.chPubAck == nil {
return nil, false
}
defer delete(s.chPubAck, id)
ch, ok := s.chPubAck[id]
return ch, ok
}
func (s *signaller) PubRec(id uint16) (chan *pktPubRec, bool) {
s.mu.RLock()
defer s.mu.RUnlock()
if s.chPubRec == nil {
return nil, false
}
defer delete(s.chPubRec, id)
ch, ok := s.chPubRec[id]
return ch, ok
}
func (s *signaller) PubComp(id uint16) (chan *pktPubComp, bool) {
s.mu.RLock()
defer s.mu.RUnlock()
if s.chPubComp == nil {
return nil, false
}
defer delete(s.chPubComp, id)
ch, ok := s.chPubComp[id]
return ch, ok
}
func (s *signaller) SubAck(id uint16) (chan *pktSubAck, bool) {
s.mu.RLock()
defer s.mu.RUnlock()
if s.chSubAck == nil {
return nil, false
}
defer delete(s.chSubAck, id)
ch, ok := s.chSubAck[id]
return ch, ok
}
func (s *signaller) UnsubAck(id uint16) (chan *pktUnsubAck, bool) {
s.mu.RLock()
defer s.mu.RUnlock()
if s.chUnsubAck == nil {
return nil, false
}
defer delete(s.chUnsubAck, id)
ch, ok := s.chUnsubAck[id]
return ch, ok
}