-
Notifications
You must be signed in to change notification settings - Fork 0
/
gobbus.go
284 lines (260 loc) · 6.28 KB
/
gobbus.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
package gobbus
import (
"context"
"fmt"
"io"
"log"
"strings"
"time"
"sync"
"github.com/jaracil/ei"
"gopkg.in/vmihailenco/msgpack.v2"
)
// Protocol commands
const (
cmdPing = 0x00
cmdSubscribe = 0x01
cmdUnsubscribe = 0x02
cmdUnsubscribeAll = 0x03
cmdPublish = 0x04
cmdPublishAck = 0x05
cmdTimeout = 0x06
cmdClose = 0x07
cmdTopicTableCreate = 0x08
cmdTopicTableSet = 0x09
cmdMessage = 0x10
cmdResponse = 0x11
cmdError = 0x12
)
var _commandStr = map[int]string{
cmdPing: "ping",
cmdSubscribe: "subscribe",
cmdUnsubscribe: "unsubscribe",
cmdUnsubscribeAll: "unsubscribeAll",
cmdPublish: "publish",
cmdPublishAck: "publishAck",
cmdTimeout: "timeout",
cmdClose: "close",
cmdTopicTableCreate: "topicTableCreate",
cmdTopicTableSet: "topicTableSet",
cmdMessage: "message",
cmdResponse: "response",
cmdError: "error",
}
func commandToStr(p int) string {
return _commandStr[p]
}
// Protocol errors
const (
errInvalidCommand = 0x01
errInvalidParamNum = 0x02
errInvalidParamType = 0x03
errInvalidFrame = 0x04
errUnauthorized = 0x05
errInvalidTopicIndex = 0x06
)
var _errStr = map[int]string{
errInvalidCommand: "invalid command",
errInvalidParamNum: "invalid number of parameters",
errInvalidParamType: "invalid type of parameter",
errInvalidFrame: "invalid frame",
errUnauthorized: "unauthorized",
errInvalidTopicIndex: "invalid topic index",
}
func errToStr(e int) string {
return _errStr[e]
}
// Obbus
type Obbus struct {
conn io.ReadWriteCloser
ctx context.Context
cancel context.CancelFunc
decoder *msgpack.Decoder
closed bool
err error
syncSlots map[int]*syncCmdSlot
syncLock *sync.Mutex
subscriptions map[string][]chan *Message
subscriptionsLock *sync.Mutex
topicToIndex map[string]int
indexToTopic map[int]string
topicTableLock *sync.Mutex
rpcPrefix string
rpcN uint64
rpcLock *sync.Mutex
rpcs map[string]chan *Message
writeLock *sync.Mutex
timeout time.Duration
keepAliveTimer *time.Timer
SyncCmdTimeout time.Duration
Debug bool
DebugLabel string
}
type ObbusOpts struct {
Timeout time.Duration
SyncCmdTimeout time.Duration
Debug bool
DebugLabel string
}
func NewObbus(conn io.ReadWriteCloser, opts ...*ObbusOpts) *Obbus {
tout := time.Second * 300
cmdTout := time.Second * 6
debug := false
label := ""
if len(opts) != 0 {
if opts[0].Timeout != 0 {
tout = opts[0].Timeout
}
if opts[0].SyncCmdTimeout != 0 {
cmdTout = opts[0].SyncCmdTimeout
}
if opts[0].Debug {
debug = true
}
if opts[0].DebugLabel != "" {
label = opts[0].DebugLabel + " "
}
}
obCtx, obCancel := context.WithCancel(context.Background())
obbus := &Obbus{
conn: conn,
ctx: obCtx,
cancel: obCancel,
decoder: msgpack.NewDecoder(conn),
err: nil,
closed: false,
syncSlots: map[int]*syncCmdSlot{},
syncLock: &sync.Mutex{},
subscriptions: map[string][]chan *Message{},
subscriptionsLock: &sync.Mutex{},
topicToIndex: map[string]int{},
indexToTopic: map[int]string{},
topicTableLock: &sync.Mutex{},
rpcPrefix: fmt.Sprintf("rpc%s.", genRandString(6)),
rpcN: 0,
rpcLock: &sync.Mutex{},
rpcs: map[string]chan *Message{},
writeLock: &sync.Mutex{},
timeout: tout,
keepAliveTimer: time.NewTimer(tout),
SyncCmdTimeout: cmdTout,
Debug: debug,
DebugLabel: label,
}
go obbus.recvWorker()
go obbus.keepAlive()
obbus.subscribeRpc()
return obbus
}
func (o *Obbus) subscribeRpc() {
_, err := o.execSyncCmd(cmdSubscribe, fmt.Sprintf("%s*", o.rpcPrefix))
if err != nil {
o.Close(err)
}
}
func (o *Obbus) Close(err error) {
if err != nil && o.err == nil {
o.err = err
if o.Debug {
log.Printf("%s!! %s\n", o.DebugLabel, err.Error())
}
}
o.syncLock.Lock()
if o.conn != nil {
o.cancel()
o.closed = true
o.syncCmd(cmdClose, cmdClose)
o.conn.Close()
o.conn = nil
}
o.syncSlots = map[int]*syncCmdSlot{}
o.syncLock.Unlock()
o.topicTableLock.Lock()
o.topicToIndex = map[string]int{}
o.indexToTopic = map[int]string{}
o.topicTableLock.Unlock()
o.subscriptionsLock.Lock()
closed := map[chan *Message]bool{}
for _, subs := range o.subscriptions {
for _, ch := range subs {
if _, ok := closed[ch]; !ok {
closed[ch] = true
close(ch)
}
}
}
o.subscriptions = map[string][]chan *Message{}
o.subscriptionsLock.Unlock()
}
func (o *Obbus) GetContext() context.Context {
return o.ctx
}
func (o *Obbus) keepAlive() {
ctx, _ := context.WithCancel(o.ctx)
for {
select {
case <-o.keepAliveTimer.C:
o.Ping(0)
case <-ctx.Done():
return
}
}
}
func (o *Obbus) recvWorker() {
for {
if o.closed {
break
}
recv, err := o.decoder.DecodeSlice()
if err != nil {
if err == io.EOF {
o.Close(fmt.Errorf("Connection was closed: EOF"))
} else {
o.Close(fmt.Errorf("Error decoding array: %s", err.Error()))
}
break
}
if o.closed {
break
}
if o.Debug {
log.Printf("%s<< %v\n", o.DebugLabel, recv)
}
cmd := ei.N(recv[0]).IntZ()
if cmd == cmdMessage {
o.handleMessage(recv)
} else {
o.handleResErr(cmd, recv)
}
}
}
func (o *Obbus) handleResErr(cmd int, recv []interface{}) {
o.writeResponseAndFreeSlot(&syncMsg{cmd: cmd, fromCmd: ei.N(recv[1]).IntZ(), val: ei.N(recv[2]).IntZ()})
}
func (o *Obbus) handleMessage(recv []interface{}) {
var flags uint
var topic, rtopic string
var ok bool
flags = ei.N(recv[1]).UintZ()
topic, ok = recv[2].(string)
if !ok {
topic, _ = o.getIndexTopic(ei.N(recv[2]).IntZ())
}
if len(recv) > 4 {
rtopic = ei.N(recv[4]).StringZ()
}
v := recv[3]
switch val := v.(type) {
case float32:
v = float64(val)
case int64:
v = int(val)
case uint64:
v = int(val)
}
if strings.HasPrefix(topic, o.rpcPrefix) {
o.rpcMsg(&Message{topic, flagsToStruct(flags), v, rtopic})
} else {
o.asyncMsg(&Message{topic, flagsToStruct(flags), v, rtopic})
}
}