forked from sevennt/rocketmq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmessage.go
188 lines (169 loc) · 5.14 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
package rocketmq
import (
"bytes"
"compress/zlib"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"strconv"
)
const (
CompressedFlag = (0x1 << 0)
MultiTagsFlag = (0x1 << 1)
TransactionNotType = (0x0 << 2)
TransactionPreparedType = (0x1 << 2)
TransactionCommitType = (0x2 << 2)
TransactionRollbackType = (0x3 << 2)
)
const (
NameValueSeparator = 1 + iota
PropertySeparator
)
const (
CharacterMaxLength = 255
)
type Message struct {
Topic string
Flag int32
Properties map[string]string
Body []byte
}
func NewMessage(topic string, body []byte) *Message {
return &Message{
Topic: topic,
Body: body,
Properties: make(map[string]string),
}
}
type MessageExt struct {
Message
QueueId int32
StoreSize int32
QueueOffset int64
SysFlag int32
BornTimestamp int64
// bornHost
StoreTimestamp int64
// storeHost
MsgId string
CommitLogOffset int64
BodyCRC int32
ReconsumeTimes int32
PreparedTransactionOffset int64
}
func decodeMessage(data []byte) []*MessageExt {
buf := bytes.NewBuffer(data)
var storeSize, magicCode, bodyCRC, queueId, flag, sysFlag, reconsumeTimes, bodyLength, bornPort, storePort int32
var queueOffset, physicOffset, preparedTransactionOffset, bornTimeStamp, storeTimestamp int64
var topicLen byte
var topic, body, properties, bornHost, storeHost []byte
var propertiesLength int16
var propertiesMap map[string]string
msgs := make([]*MessageExt, 0, 32)
for buf.Len() > 0 {
msg := new(MessageExt)
binary.Read(buf, binary.BigEndian, &storeSize)
binary.Read(buf, binary.BigEndian, &magicCode)
binary.Read(buf, binary.BigEndian, &bodyCRC)
binary.Read(buf, binary.BigEndian, &queueId)
binary.Read(buf, binary.BigEndian, &flag)
binary.Read(buf, binary.BigEndian, &queueOffset)
binary.Read(buf, binary.BigEndian, &physicOffset)
binary.Read(buf, binary.BigEndian, &sysFlag)
binary.Read(buf, binary.BigEndian, &bornTimeStamp)
bornHost = make([]byte, 4)
binary.Read(buf, binary.BigEndian, &bornHost)
binary.Read(buf, binary.BigEndian, &bornPort)
binary.Read(buf, binary.BigEndian, &storeTimestamp)
storeHost = make([]byte, 4)
binary.Read(buf, binary.BigEndian, &storeHost)
binary.Read(buf, binary.BigEndian, &storePort)
binary.Read(buf, binary.BigEndian, &reconsumeTimes)
binary.Read(buf, binary.BigEndian, &preparedTransactionOffset)
binary.Read(buf, binary.BigEndian, &bodyLength)
if bodyLength > 0 {
body = make([]byte, bodyLength)
binary.Read(buf, binary.BigEndian, body)
if (sysFlag & CompressedFlag) == CompressedFlag {
b := bytes.NewReader(body)
z, err := zlib.NewReader(b)
if err != nil {
fmt.Println(err)
return nil
}
defer z.Close()
body, err = ioutil.ReadAll(z)
if err != nil {
fmt.Println(err)
return nil
}
}
}
binary.Read(buf, binary.BigEndian, &topicLen)
topic = make([]byte, 0)
binary.Read(buf, binary.BigEndian, &topic)
binary.Read(buf, binary.BigEndian, &propertiesLength)
if propertiesLength > 0 {
properties = make([]byte, propertiesLength)
binary.Read(buf, binary.BigEndian, &properties)
propertiesMap = make(map[string]string)
json.Unmarshal(properties, &propertiesMap)
}
if magicCode != -626843481 {
fmt.Printf("magic code is error %d", magicCode)
return nil
}
msg.Topic = string(topic)
msg.QueueId = queueId
msg.SysFlag = sysFlag
msg.QueueOffset = queueOffset
msg.BodyCRC = bodyCRC
msg.StoreSize = storeSize
msg.BornTimestamp = bornTimeStamp
msg.ReconsumeTimes = reconsumeTimes
msg.Flag = flag
//msg.commitLogOffset=physicOffset
msg.StoreTimestamp = storeTimestamp
msg.PreparedTransactionOffset = preparedTransactionOffset
msg.Body = body
msg.Properties = propertiesMap
msgs = append(msgs, msg)
}
return msgs
}
func messageProperties2String(properties map[string]string) string {
StringBuilder := bytes.NewBuffer([]byte{})
if properties != nil && len(properties) != 0 {
for k, v := range properties {
binary.Write(StringBuilder, binary.BigEndian, k) // 4
binary.Write(StringBuilder, binary.BigEndian, NameValueSeparator) // 4
binary.Write(StringBuilder, binary.BigEndian, v) // 4
binary.Write(StringBuilder, binary.BigEndian, PropertySeparator) // 4
}
}
return StringBuilder.String()
}
func (msg Message) checkMessage(producer *DefaultProducer) (err error) {
if err = checkTopic(msg.Topic); err != nil {
if len(msg.Body) == 0 {
err = errors.New("ResponseCode:" + strconv.Itoa(MsgIllegal) + ", the message body is null")
} else if len(msg.Body) > producer.maxMessageSize {
err = errors.New("ResponseCode:" + strconv.Itoa(MsgIllegal) + ", the message body size over max value, MAX:" + strconv.Itoa(producer.maxMessageSize))
}
}
return
}
func checkTopic(topic string) (err error) {
if topic == "" {
err = errors.New("the specified topic is blank")
}
if len(topic) > CharacterMaxLength {
err = errors.New("the specified topic is longer than topic max length 255")
}
if topic == DefaultTopic {
err = errors.New("the topic[" + topic + "] is conflict with default topic")
}
return
}