forked from zentures/surgemq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
203 lines (149 loc) · 4.25 KB
/
session.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
// Copyright (c) 2014 The SurgeMQ Authors. All rights reserved.
//
// 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 sessions
import (
"fmt"
"sync"
"github.com/liaoliaopro/surgemq/message"
)
const (
// Queue size for the ack queue
defaultQueueSize = 16
)
type Session struct {
// Ack queue for outgoing PUBLISH QoS 1 messages
Pub1ack *Ackqueue
// Ack queue for incoming PUBLISH QoS 2 messages
Pub2in *Ackqueue
// Ack queue for outgoing PUBLISH QoS 2 messages
Pub2out *Ackqueue
// Ack queue for outgoing SUBSCRIBE messages
Suback *Ackqueue
// Ack queue for outgoing UNSUBSCRIBE messages
Unsuback *Ackqueue
// Ack queue for outgoing PINGREQ messages
Pingack *Ackqueue
// cmsg is the CONNECT message
Cmsg *message.ConnectMessage
// Will message to publish if connect is closed unexpectedly
Will *message.PublishMessage
// Retained publish message
Retained *message.PublishMessage
// cbuf is the CONNECT message buffer, this is for storing all the will stuff
cbuf []byte
// rbuf is the retained PUBLISH message buffer
rbuf []byte
// topics stores all the topis for this session/client
topics map[string]byte
// Initialized?
initted bool
// Serialize access to this session
mu sync.Mutex
id string
}
func (this *Session) Init(msg *message.ConnectMessage) error {
this.mu.Lock()
defer this.mu.Unlock()
if this.initted {
return fmt.Errorf("Session already initialized")
}
this.cbuf = make([]byte, msg.Len())
this.Cmsg = message.NewConnectMessage()
if _, err := msg.Encode(this.cbuf); err != nil {
return err
}
if _, err := this.Cmsg.Decode(this.cbuf); err != nil {
return err
}
if this.Cmsg.WillFlag() {
this.Will = message.NewPublishMessage()
this.Will.SetQoS(this.Cmsg.WillQos())
this.Will.SetTopic(this.Cmsg.WillTopic())
this.Will.SetPayload(this.Cmsg.WillMessage())
this.Will.SetRetain(this.Cmsg.WillRetain())
}
this.topics = make(map[string]byte, 1)
this.id = string(msg.ClientId())
this.Pub1ack = newAckqueue(defaultQueueSize)
this.Pub2in = newAckqueue(defaultQueueSize)
this.Pub2out = newAckqueue(defaultQueueSize)
this.Suback = newAckqueue(defaultQueueSize)
this.Unsuback = newAckqueue(defaultQueueSize)
this.Pingack = newAckqueue(defaultQueueSize)
this.initted = true
return nil
}
func (this *Session) Update(msg *message.ConnectMessage) error {
this.mu.Lock()
defer this.mu.Unlock()
this.cbuf = make([]byte, msg.Len())
this.Cmsg = message.NewConnectMessage()
if _, err := msg.Encode(this.cbuf); err != nil {
return err
}
if _, err := this.Cmsg.Decode(this.cbuf); err != nil {
return err
}
return nil
}
func (this *Session) RetainMessage(msg *message.PublishMessage) error {
this.mu.Lock()
defer this.mu.Unlock()
this.rbuf = make([]byte, msg.Len())
this.Retained = message.NewPublishMessage()
if _, err := msg.Encode(this.rbuf); err != nil {
return err
}
if _, err := this.Retained.Decode(this.rbuf); err != nil {
return err
}
return nil
}
func (this *Session) AddTopic(topic string, qos byte) error {
this.mu.Lock()
defer this.mu.Unlock()
if !this.initted {
return fmt.Errorf("Session not yet initialized")
}
this.topics[topic] = qos
return nil
}
func (this *Session) RemoveTopic(topic string) error {
this.mu.Lock()
defer this.mu.Unlock()
if !this.initted {
return fmt.Errorf("Session not yet initialized")
}
delete(this.topics, topic)
return nil
}
func (this *Session) Topics() ([]string, []byte, error) {
this.mu.Lock()
defer this.mu.Unlock()
if !this.initted {
return nil, nil, fmt.Errorf("Session not yet initialized")
}
var (
topics []string
qoss []byte
)
for k, v := range this.topics {
topics = append(topics, k)
qoss = append(qoss, v)
}
return topics, qoss, nil
}
func (this *Session) ID() string {
return string(this.Cmsg.ClientId())
}