/
protocol.go
215 lines (187 loc) · 5.51 KB
/
protocol.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
package emqd
import (
"bytes"
"fmt"
"net"
"sync/atomic"
"time"
log "github.com/ericluj/elog"
"github.com/ericluj/emq/internal/command"
"github.com/ericluj/emq/internal/common"
"github.com/ericluj/emq/internal/protocol"
)
// 协议结构体 用来组织emqd和client的关联处理
type Protocol struct {
emqd *EMQD
}
func (p *Protocol) NewClient(conn net.Conn) protocol.Client {
id := atomic.AddInt64(&p.emqd.idGenerator, 1)
c := NewClient(id, conn)
return c
}
func (p *Protocol) IOLoop(c protocol.Client) error {
var (
err error
line []byte
)
client := c.(*Client)
// client的一个goruntine,用来执行消息处理分发等相关操作
messagePumpStartedChan := make(chan bool)
go p.MessagePump(client, messagePumpStartedChan)
// 保证在messagePump的初始化完成后才往下执行
<-messagePumpStartedChan
// 接受cmd并执行操作
for {
// 设置deadline
err = client.conn.SetReadDeadline(time.Now().Add(common.ReadTimeout))
if err != nil {
log.Errorf("SetReadDeadline: %v", err)
return err
}
line, err = client.reader.ReadSlice('\n')
if err != nil {
log.Errorf("ReadSlice: %v", err)
break
}
// 去掉'\n'(返回的line包含'\n')
line = line[:len(line)-1]
// 去掉'\r'
if len(line) > 0 && line[len(line)-1] == '\r' {
line = line[:len(line)-1]
}
// 空格拆分
params := bytes.Split(line, common.SeparatorBytes)
log.Debugf("PROTOCOL: %s, %s", client.conn.RemoteAddr(), params)
var response []byte
response, err = p.Exec(client, params)
if err != nil {
log.Errorf("Exec: %v", err)
sendErr := p.Send(client, common.FrameTypeError, []byte(err.Error()))
if sendErr != nil {
err = fmt.Errorf("send FrameTypeError error: %v", err)
break
}
// 如果是严重错误,那么断开连接,普通错误则继续执行循环
if _, ok := err.(*protocol.FatalClientErr); ok {
break
}
continue
}
if response != nil {
err = p.Send(client, common.FrameTypeResponse, response)
if err != nil {
err = fmt.Errorf("send FrameTypeResponse error: %v", err)
break
}
}
}
log.Infof("PROTOCOL: %s exiting IOLoop", client.conn.RemoteAddr())
// client关闭
close(client.exitChan)
// 如果client有订阅channel,那么把关联关系删除
if client.channel != nil {
client.channel.RemoveClient(client.ID)
}
return err
}
func (p *Protocol) Exec(client *Client, params [][]byte) ([]byte, error) {
if bytes.Equal(params[0], []byte(command.IDENTIFY)) {
return p.IDENTITY(client, params)
}
switch {
case bytes.Equal(params[0], []byte(command.PUB)):
return p.PUB(client, params)
case bytes.Equal(params[0], []byte(command.SUB)):
return p.SUB(client, params)
case bytes.Equal(params[0], []byte(command.NOP)):
return p.NOP(client, params)
case bytes.Equal(params[0], []byte(command.REQ)):
return p.REQ(client, params)
}
return nil, protocol.NewFatalClientErr(nil, protocol.ErrTypeInvalid, fmt.Sprintf("invalid command %s", params[0]))
}
func (p *Protocol) MessagePump(client *Client, startedChan chan bool) {
var (
memoryMsgChan chan *Message // 消息队列(初始为nil,不会被执行)
backendMsgChan chan []byte
subChannel *Channel // 订阅的channel
)
heartbeatTicker := time.NewTicker(common.HeartbeatTimeout)
close(startedChan)
for {
select {
// 订阅
case subChannel = <-client.subEventChan:
client.subEventChan = nil // 订阅事件发生,置为nil让它不能再次订阅
memoryMsgChan = subChannel.memoryMsgChan // 用memoryMsgChan是为了防止空指针
backendMsgChan = subChannel.backend.ReadChan()
// 获取消息
case msg := <-memoryMsgChan:
msg.Attempts++
subChannel.StartInFlight(msg, client.ID)
atomic.AddInt64(&subChannel.messageCount, -1)
err := p.SendMessage(client, msg)
if err != nil {
log.Errorf("SendMessage: %v, RemoteAddr: %s", err, client.conn.RemoteAddr())
goto exit
}
// 磁盘消息
case bs := <-backendMsgChan:
m, err := protocol.DecodeMessage(bs)
if err != nil {
log.Errorf("DecodeMessage: %v, RemoteAddr: %s", err, client.conn.RemoteAddr())
goto exit
}
msg := &Message{
Message: m,
}
msg.Attempts++
subChannel.StartInFlight(msg, client.ID)
err = p.SendMessage(client, msg)
if err != nil {
log.Errorf("SendMessage: %v RemoteAddr: %s", client.conn.RemoteAddr(), err)
goto exit
}
// 心跳
case <-heartbeatTicker.C:
log.Infof("send heartbeat to %s", client.conn.RemoteAddr())
err := p.Send(client, common.FrameTypeResponse, common.HeartbeatBytes)
if err != nil {
log.Errorf("Send heartbeat: %v, RemoteAddr: %s", err, client.conn.RemoteAddr())
goto exit
}
// 退出
case <-client.exitChan:
goto exit
}
}
exit:
log.Infof("exiting messagePump RemoteAddr: %s", client.conn.RemoteAddr())
heartbeatTicker.Stop()
}
func (p *Protocol) SendMessage(client *Client, msg *Message) error {
log.Infof("PROTOCOL: SendMessage to client %s, id %s, timestamp %d, attempts %d, body %s", client.conn.RemoteAddr(), msg.ID, msg.Timestamp, msg.Attempts, msg.Body)
// 发送数据
buf, err := msg.Bytes()
if err != nil {
return err
}
err = p.Send(client, common.FrameTypeMessage, buf)
if err != nil {
return err
}
return nil
}
func (p *Protocol) Send(client *Client, frameType int32, data []byte) error {
client.mtx.Lock()
defer client.mtx.Unlock()
err := client.conn.SetWriteDeadline(time.Now().Add(common.WriteTimeout))
if err != nil {
return err
}
err = protocol.SendFrameData(client.conn, frameType, data)
if err != nil {
return err
}
return nil
}