-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmessage.go
146 lines (124 loc) · 2.87 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
package main
import (
"encoding/hex"
"errors"
"io"
)
var noMessage error = errors.New("no message")
type MessageBuffer struct {
buffer []byte
}
type Message struct {
buffer []byte
}
// readInt read a 32-bit integer from byte buffer start
// from offset
func readInt(b []byte, offset int) (int, error) {
if offset < 0 || offset+4 > len(b) {
return 0, errors.New("out of index when reading integer")
}
n := int(b[offset]) & 0xff
n <<= 8
n |= int(b[offset+1]) & 0xff
n <<= 8
n |= int(b[offset+2]) & 0xff
n <<= 8
n |= int(b[offset+3]) & 0xff
return n, nil
}
func writeInt(b []byte, offset int, value int) error {
if offset < 0 || offset+4 > len(b) {
return errors.New("out of index when writting integer")
}
b[offset+0] = byte((value >> 24) & 0xff)
b[offset+1] = byte((value >> 16) & 0xff)
b[offset+2] = byte((value >> 8) & 0xff)
b[offset+3] = byte(value & 0xff)
return nil
}
// NewMessageBuffer creaate a MessageBuffer object
func NewMessageBuffer() *MessageBuffer {
return &MessageBuffer{buffer: make([]byte, 0)}
}
// Add add data to the buffer
func (p *MessageBuffer) Add(b []byte) {
p.buffer = append(p.buffer, b...)
}
// ExtractMessage extract a thrift message
func (p *MessageBuffer) ExtractMessage() (*Message, error) {
if len(p.buffer) > 4 {
n, err := readInt(p.buffer, 0)
if err == nil && len(p.buffer) >= 4+n {
msg := &Message{buffer: p.buffer[0 : 4+n]}
p.buffer = p.buffer[4+n:]
return msg, nil
}
}
return nil, noMessage
}
// NewMessage create a thrift Message object
func NewMessage(b []byte) *Message {
return &Message{buffer: b}
}
// Write write the message to a io.Writer object
func (m *Message) Write(writer io.Writer) error {
_, err := writer.Write(m.buffer)
return err
}
// Hex convert the message to hex format
func (m *Message) Hex() string {
return hex.Dump(m.buffer)
}
func (m *Message) GetSeqId() (int, error) {
offset, err := m.getSeqIdOffset()
if err == nil {
return readInt(m.buffer, offset)
}
return 0, err
}
func (m *Message) SetSeqId(seqId int) error {
offset, err := m.getSeqIdOffset()
if err == nil {
return writeInt(m.buffer, offset, seqId)
}
return err
}
// GetName get the name of call
func (m *Message) GetName() (string, error) {
offset := 4
if m.isFramed() {
offset += 4
}
n, err := readInt(m.buffer, offset)
if err == nil {
return string(m.buffer[offset+4 : offset+4+n]), nil
}
return "", err
}
// GetType get message type
// - 1, Call
// - 2, Reply
// - 3, Exception
// - 4, Oneway
func (m *Message) GetType() int {
offset := 0
if m.isFramed() {
offset += 4
}
return int(m.buffer[offset+3] & 0xff)
}
func (m *Message) isFramed() bool {
return m.buffer[0]&0x80 != 0x80
}
func (m *Message) getSeqIdOffset() (int, error) {
offset := 4
if m.isFramed() {
offset += 4
}
// read the name length
n, err := readInt(m.buffer, offset)
if err != nil {
return 0, err
}
return offset + 4 + n, nil
}