-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
216 lines (194 loc) · 5.54 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package gearman
import (
"bufio"
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"io/ioutil"
"strings"
)
const separator = "\000"
// PacketType is the type of the packet, such as SUBMIT_JOB / GET_STATUS
type PacketType byte
const (
// PacketTypeMin is the min packet type value
PacketTypeMin = 1
// PacketTypeMax is the max packet type value
PacketTypeMax = 42
)
// MaxBodySize is the max body size
const MaxBodySize = 63
const headerSize = 12
var (
errInvalidMagic = errors.New("Invalid magic code")
errInvaldPacketType = errors.New("Invalid packet type")
errInvalidArgsSize = errors.New("Invalid arguments size")
errInvalidMsgRole = errors.New("The message type is unexpected for this role")
errInvalidArgsLen = errors.New("The length of arguments is incorrect")
errArgumentsTooLong = errors.New("Arguments too long")
)
var (
MsgPool = NewMessagePool()
MsgHeaderPool = NewMessageHeaderPool()
)
// byte order of the encoding
var byteOrder binary.ByteOrder = binary.BigEndian
// Message represents a REQ/RES packet
type Message struct {
MagicType MagicType
PacketType PacketType
Arguments []string
}
// Validate checks the validity of the message and return an error if has
// It validates -
// 1. If the packet type is expected for the current role (Eg. SUBMIT_JOB sent from a client to server is invalid)
// 2. If the length of the arguments is expected
func (m *Message) Validate(role RoleType) error {
if (role == RoleServer && m.MagicType == MagicRes) || (role != RoleServer && m.MagicType == MagicReq) {
return errInvalidMsgRole
}
allowedRoles, ok := msgAllowedRoles[calcMsgType(m.MagicType, m.PacketType)]
if !ok {
return errInvalidMsgRole
}
if role != RoleServer {
if !allowedRoles.hasType(role) {
return errInvalidMsgRole
}
}
argsLen, ok := msgArgsLens[m.PacketType]
if ok && len(m.Arguments) != argsLen {
return errInvalidArgsLen
}
return nil
}
// Encode encodes the message to bytes in the gearman official protocol format
func (m *Message) Encode() ([]byte, error) {
body := strings.Join(m.Arguments, separator)
for _, arg := range m.Arguments {
if len(arg) > MaxBodySize {
return nil, errArgumentsTooLong
}
}
if !m.MagicType.Valid() {
return nil, errInvalidMagic
}
if !m.PacketType.Valid() {
return nil, errInvaldPacketType
}
buff := bytes.NewBuffer(make([]byte, 0, len(body)+headerSize))
buff.WriteString(m.MagicType.String())
binary.Write(buff, byteOrder, uint32(m.PacketType))
// must convert to uint32 as encoding/binary dose not work with int
binary.Write(buff, byteOrder, uint32(len(body)))
buff.WriteString(body)
return buff.Bytes(), nil
}
//WriteTo writes the message data to a Writer
func (m *Message) WriteTo(writer io.Writer) (int64, error) {
payload, err := m.Encode()
if err != nil {
return 0, err
}
n, err := writer.Write(payload)
return int64(n), err
}
func firstByte(reader io.Reader) (byte, error) {
bufReader, ok := reader.(*bufio.Reader)
if ok {
return bufReader.ReadByte()
}
buff := make([]byte, 1)
_, err := io.ReadFull(reader, buff)
if err != nil {
return 0, err
}
return buff[0], err
}
// NextMessage reads next message from a bufio.Reader
// It returns one of binMsg and txtMsg leaving the other as zero value if no any error occured
// binMsg returned if the next message is binary, and txtMsg if it's text
// For binary message, it treats error for such cases -
// 1.read error from the reader
// 2. invalid magic code / packet type / body size
// (it will read the full message from the reader in this case, so the next message can be read as expected)
// It dose not care about the validity of the message, message.Validate() should be called for it
func NextMessage(reader *bufio.Reader) (binMsg *Message, txtMsg string, err error) {
beginByte, err := firstByte(reader)
if err != nil {
return nil, "", err
}
if beginByte != 0 {
content, err := reader.ReadString('\n')
contentLen := len(content)
if contentLen > 0 {
content = content[:contentLen-1]
}
return nil, string([]byte{beginByte}) + content, err
}
headers := MsgHeaderPool.Get()
defer MsgHeaderPool.Put(headers)
headers[0] = beginByte
_, err = io.ReadFull(reader, headers[1:])
if err != nil {
return nil, "", err
}
var magicType MagicType
var magicErr bool
switch string(headers[:4]) {
case MagicReqValue:
magicType = MagicReq
case MagicResValue:
magicType = MagicRes
default:
magicErr = true
}
var packetType PacketType
var packetTypeErr bool
packetType = PacketType(binary.BigEndian.Uint32(headers[4:8]))
if !magicErr && !packetType.Valid() {
packetTypeErr = true
}
bodySize := byteOrder.Uint32(headers[8:])
var arguments []string
var bodySizeErr bool
if !magicErr && !packetTypeErr {
if bodySize == 0 {
arguments = nil
} else {
body := make([]byte, bodySize)
_, err = io.ReadFull(reader, body)
if err != nil {
return nil, "", err
}
arguments = strings.Split(string(body), separator)
}
} else {
// we don't need the arguments but still need read it from the connection
// to make sure the next message can be read properly
_, err = io.CopyN(ioutil.Discard, reader, int64(bodySize))
if err != nil {
return nil, "", err
}
bodySizeErr = true
}
if magicErr {
return nil, "", errInvalidMagic
}
if packetTypeErr {
return nil, "", errInvaldPacketType
}
if bodySizeErr {
return nil, "", errInvalidArgsSize
}
msg := MsgPool.Get()
msg.MagicType = magicType
msg.PacketType = packetType
msg.Arguments = arguments
return msg, "", nil
}
func (m *Message) String() string {
return fmt.Sprintf("%s.%s", m.MagicType.String()[1:], m.PacketType)
}