forked from packetflinger/q2admin-cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriting_data.go
53 lines (47 loc) · 1.2 KB
/
writing_data.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
package main
import (
//"bytes"
"encoding/binary"
)
/**
* Append to the end of our msg buffer
*/
func WriteData(data []byte, msg *MessageBuffer) {
msg.buffer = append(msg.buffer, data...)
msg.length += len(data)
}
/**
* Keep building a string until we hit a null
*/
func WriteString(data string, msg *MessageBuffer) {
b := []byte(data)
b = append(b, []byte{0}...) // null terminated
msg.buffer = append(msg.buffer, b...)
msg.length += len(b)
}
/**
* Write 4 byte int into to LE buffer
*/
func WriteLong(data int, msg *MessageBuffer) {
temp := make([]byte, 4)
binary.LittleEndian.PutUint32(temp, uint32(data) & 0xffffffff)
msg.buffer = append(msg.buffer, temp...)
msg.length += 4
}
/**
* Write 2 byte int into LE buffer
*/
func WriteShort(data int, msg *MessageBuffer) {
temp := make([]byte, 2)
binary.LittleEndian.PutUint16(temp, uint16(data) & 0xffff)
msg.buffer = append(msg.buffer, temp...)
msg.length += 2
}
// for consistency
func WriteByte(data byte, msg *MessageBuffer) {
temp := make([]byte, 1)
temp[0] = data & 0xff
//msg.buffer = append(msg.buffer, data & 0xff)
msg.buffer = append(msg.buffer, temp...)
msg.length++
}