-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter.go
61 lines (48 loc) · 1.82 KB
/
writer.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
package packet
import "encoding/binary"
// Write will try to write the data passed
func (p *Packet) Write(data ...interface{}) (err error) {
// Loop through each of the parameters
for _, v := range data {
err = binary.Write(&p.buf, binary.LittleEndian, v)
if err != nil { // Error has occured during writing; return with error
return
}
}
return
}
// WriteByte appends a byte to the packet
func (p *Packet) WriteByte(d byte) (err error) {
return binary.Write(&p.buf, binary.LittleEndian, d)
}
// WriteBytes appends an array of bytes to the packet
func (p *Packet) WriteBytes(d []byte) (err error) {
return binary.Write(&p.buf, binary.LittleEndian, d)
}
// WriteShort appends a short to the packet
func (p *Packet) WriteShort(w uint16) (err error) {
return binary.Write(&p.buf, binary.LittleEndian, w)
}
// Writeint appends an int to the packet
func (p *Packet) WriteInt(dw uint32) (err error) {
return binary.Write(&p.buf, binary.LittleEndian, dw)
}
// WriteLong appends a long to the packet
func (p *Packet) WriteLong(qw uint64) (err error) {
return binary.Write(&p.buf, binary.LittleEndian, qw)
}
// WriteString appends a string to the packet
//
// Strings are encoded on the packet with a short containing the length of the string and then the string in bytes.
func (p *Packet) WriteString(str string) (err error) {
err = p.WriteShort(uint16(len(str))) // Length of the string
if err != nil {
return
}
err = p.WriteBytes([]byte(str)) // The string in bytes
return
}
func (p *Packet) WriteSignedByte(b int8) (err error) { return p.WriteByte(byte(b)) }
func (p *Packet) WriteSignedShort(b int16) (err error) { return p.WriteShort(uint16(b)) }
func (p *Packet) WriteSignedInt(b int32) (err error) { return p.WriteInt(uint32(b)) }
func (p *Packet) WriteSignedLong(b int64) (err error) { return p.WriteLong(uint64(b)) }