-
Notifications
You must be signed in to change notification settings - Fork 2
/
message.go
198 lines (166 loc) · 3.94 KB
/
message.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
package edgex
import (
"bufio"
"bytes"
"encoding/binary"
"strings"
)
//
// Author: 陈哈哈 [email protected]
//
const (
FrameMagic byte = 0xED // Magic
FrameVersion = 0x01 // 版本
FrameEmpty = 0x00 // 分隔空帧
FrameVarData = 0xDA
)
const (
eventIdByteSize = 8
)
// Header 头部
type Header struct {
Magic byte // Magic字段,固定为 0xED
Version byte // 协议版本
ControlVar byte // 控制变量
EventId int64 // 消息事件ID,具有唯一性
}
// Message 消息接口。
type Message interface {
// Header 返回消息的Header
Header() Header
// NodeId 返回节点ID
NodeId() string
// BoardId 返回主板ID
BoardId() string
// MajorId 返回主ID
MajorId() string
// MinorId 返回次ID
MinorId() string
// UnionId 返回 NodeId:BoardId:MajorId:MinorId 的组合ID
UnionId() string
// EventId 返回消息Id。
// 消息ID使用SnowflakeID生成器具有唯一性。
EventId() int64
// Body 返回消息体字节
Body() []byte
// Bytes 返回消息对象全部字节
Bytes() []byte
}
////
type message struct {
Message
header *Header
unionId string
_unionId []string
body []byte
}
func (m *message) UnionId() string {
return m.unionId
}
func (m *message) NodeId() string {
return m._unionId[0]
}
func (m *message) BoardId() string {
return m._unionId[1]
}
func (m *message) MajorId() string {
return m._unionId[2]
}
func (m *message) MinorId() string {
return m._unionId[3]
}
func (m *message) Header() Header {
return *m.header
}
func (m *message) EventId() int64 {
return m.header.EventId
}
func (m *message) Body() []byte {
return m.body
}
func (m *message) Bytes() []byte {
buf := new(bytes.Buffer)
buf.WriteByte(m.header.Magic)
buf.WriteByte(m.header.Version)
buf.WriteByte(m.header.ControlVar)
buf.Write(encodeInt64(m.header.EventId))
buf.WriteString(m.unionId)
buf.WriteByte(FrameEmpty)
buf.Write(m.body)
return buf.Bytes()
}
func splitUnionId(unionId string) []string {
_unionId := strings.Split(unionId, ":")
remains := 4 - len(_unionId)
for i := 0; i < remains; i++ {
_unionId = append(_unionId, "")
}
return _unionId[:4]
}
// 创建消息对象
func NewMessageByUnionId(unionId string, bodyBytes []byte, eventId int64) Message {
return &message{
header: &Header{
Magic: FrameMagic,
Version: FrameVersion,
ControlVar: FrameVarData,
EventId: eventId,
},
unionId: unionId,
_unionId: splitUnionId(unionId),
body: bodyBytes,
}
}
// 创建消息对象
func NewMessage(nodeId, groupId, majorId, minorId string, bodyBytes []byte, eventId int64) Message {
return NewMessageByUnionId(
MakeUnionId(nodeId, groupId, majorId, minorId),
bodyBytes,
eventId)
}
// 解析消息对象
func ParseMessage(data []byte) Message {
reader := bufio.NewReader(bytes.NewReader(data))
magic, _ := reader.ReadByte()
version, _ := reader.ReadByte()
vars, _ := reader.ReadByte()
eventId := make([]byte, eventIdByteSize)
if _, err := reader.Read(eventId); nil != err {
panic(err)
}
uid, _ := reader.ReadBytes(FrameEmpty)
body := make([]byte, len(data)-(3 /*Magic+Ver+Var*/ +eventIdByteSize)-len(uid))
if _, err := reader.Read(body); nil != err {
panic(err)
}
unionId := string(uid[:len(uid)-1])
return &message{
header: &Header{
Magic: magic,
Version: version,
ControlVar: vars,
EventId: decodeInt64(eventId),
},
unionId: unionId,
_unionId: splitUnionId(unionId),
body: body,
}
}
func MakeUnionId(nodeId, groupId, majorId, minorId string) string {
checkRequiredId(nodeId, "nodeId")
checkRequiredId(groupId, "groupId")
checkRequiredId(majorId, "majorId")
return nodeId + ":" + groupId + ":" + majorId + ":" + minorId
}
func encodeInt64(num int64) []byte {
bs := make([]byte, eventIdByteSize)
binary.BigEndian.PutUint64(bs, uint64(num))
return bs
}
func decodeInt64(bs []byte) int64 {
if nil == bs || 0 == len(bs) {
return 0
} else {
return int64(binary.BigEndian.Uint64(bs))
}
}