-
Notifications
You must be signed in to change notification settings - Fork 3
/
message.go
145 lines (126 loc) · 2.46 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
package rpckit
import (
"bytes"
"encoding/binary"
"errors"
"io"
)
type Message struct {
buf []byte
len int
pos int // for reading
LastError error
}
func NewMessage(capacity int) *Message {
m := &Message{
buf: make([]byte, 4+capacity),
len: 4, // make room for length prefix
pos: 4, // make room for length prefix
}
return m
}
func MessageFromBytes(msg []byte) (*Message, int) {
if len(msg) > 4 {
length := int(binary.BigEndian.Uint32(msg))
if len(msg) >= length {
m := &Message{
buf: msg,
len: len(msg),
pos: 4,
}
return m, length
}
}
return nil, 0
}
func (m *Message) Bytes() []byte {
b := m.buf[:m.len]
binary.BigEndian.PutUint32(b, uint32(m.len))
return b
}
func (m *Message) WriteInt(v uint64) {
m.grow(10)
//i := 0
for v >= 0x80 {
m.buf[m.len] = byte(v) | 0x80
m.len++
v >>= 7
//i++
}
m.buf[m.len] = byte(v)
m.len++
}
func (m *Message) WriteString(v string) {
stringLength := len(v)
m.WriteInt(uint64(stringLength))
m.grow(stringLength)
copy(m.buf[m.len:], v)
m.len += stringLength
}
func (m *Message) WriteBytes(v []byte) {
byteLength := len(v)
m.WriteInt(uint64(byteLength))
m.grow(byteLength)
copy(m.buf[m.len:], v)
m.len += byteLength
}
func (m *Message) grow(needed int) {
if m.len+needed > len(m.buf) {
// Not enough space anywhere, we need to allocate.
buf := makeSlice(2*cap(m.buf) + needed)
copy(buf, m.buf[0:m.len])
m.buf = buf
}
}
func (m *Message) ReadString() (string, error) {
v, err := m.ReadInt()
if err != nil {
m.LastError = err
return "", err
}
length := int(v)
if m.pos+length > m.len {
m.LastError = io.EOF
return "", io.EOF
}
str := m.buf[m.pos : m.pos+length]
m.pos += length
return string(str), nil
}
var ErrOverflow = errors.New("Overflow in varint")
func (m *Message) ReadInt() (uint64, error) {
var x uint64
var s uint
for i := 0; ; i++ {
if m.pos+1 > m.len {
m.LastError = io.EOF
return 0, io.EOF
}
b := m.buf[m.pos]
m.pos++
if b < 0x80 {
if i > 9 || (i == 9 && b > 1) {
m.LastError = ErrOverflow
return 0, ErrOverflow
}
return x | uint64(b)<<s, nil
}
x |= uint64(b&0x7f) << s
s += 7
}
}
func (m *Message) ReadFloat64() (float64, error) {
if m.pos+8 > m.len {
m.LastError = io.EOF
return 0, io.EOF
}
b := m.buf[m.pos : m.pos+8]
m.pos += 8
var value float64
buf := bytes.NewReader(b)
err := binary.Read(buf, binary.BigEndian, &value)
if err != nil {
return 0, err
}
return value, err
}