-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtypes.go
More file actions
145 lines (125 loc) · 2.76 KB
/
types.go
File metadata and controls
145 lines (125 loc) · 2.76 KB
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 main
import (
"io"
"os"
"net"
"log"
"encoding/binary"
)
// This file handles various types utilized by the protocol.
// byte: 1
// short: 2
// int: 4
// long: 8
// float: 4
// double: 8
// modified utf-8 string: >= 2
// First two bytes determine the length
// bool: 1
// Gets a string from the connection
func ReadString(c net.Conn) (string, os.Error) {
buf1 := make([]byte, 2)
buf1[0], _ = ReadByte(c)
buf1[1], _ = ReadByte(c)
length := int(buf1[0]*255)
length += int(buf1[1])
log8.Println("String length should be", length)
buf := make([]byte, length)
for i := 0; i < len(buf); i++ {
char, err := ReadByte(c)
if err != nil {
return string(buf), err
}
buf[i] = char
}
return string(buf), nil
}
func makeString(orig string) (buf []byte) {
buf = make([]byte, len(orig)+2)
buf[1] = byte(len(orig))
buf[0] = byte((len(orig))/255)
for i, char := range orig {
// WARNING: Only works with ASCII. Should fix eventually.
buf[i+2] = byte(char)
}
return buf
}
func ReadByte(c io.Reader) (byte, os.Error) {
buf := make([]byte, 1)
_, err := c.Read(buf)
if err != nil {
log.Println(err)
os.Exit(1)
}
//log.Println("Received", n, "bytes from server. (byte:", buf, "char:", string(buf))
return buf[0], err
}
var bOrder = binary.BigEndian
func ReadShort(c net.Conn) (int16, os.Error) {
var b int16
binary.Read(c, bOrder, &b)
return b, nil
}
func ReadInt(c net.Conn) (int32, os.Error) {
var b int32
binary.Read(c, bOrder, &b)
return b, nil
}
func ReadLong(c net.Conn) (int64, os.Error) {
var b int64
binary.Read(c, bOrder, &b)
return b, nil
}
// Floating point ones don't seem to work :/
func ReadFloat(c net.Conn) (float32, os.Error) {
var b float32
binary.Read(c, bOrder, &b)
return b, nil
}
func ReadDouble(c net.Conn) (float64, os.Error) {
var b float64
binary.Read(c, bOrder, &b)
return b, nil
}
func ReadDataStream(c net.Conn) ([]byte, os.Error) {
buf := make([]byte, 1)
for {
b, _ := ReadByte(c)
buf = append(buf, b)
if b == 0x7F {
return buf[1:], nil
}
}
panic("Not reached!")
}
func WriteByte(c net.Conn, b byte) (os.Error) {
n, err := c.Write([]byte{b})
if err != nil {
log0.Println(err)
}
log9.Println("Wrote", n, "bytes to server. (byte:", b, "char:", string(b))
return err
}
func WriteShort(c net.Conn, b int16) {
binary.Write(c, bOrder, b)
}
func WriteInt(c net.Conn, b int32) {
binary.Write(c, bOrder, b)
}
func WriteLong(c net.Conn, b int64) {
binary.Write(c, bOrder, b)
}
func WriteFloat(c net.Conn, b float32) {
binary.Write(c, bOrder, b)
}
func WriteDouble(c net.Conn, b float64) {
binary.Write(c, bOrder, b)
}
func WriteString(c net.Conn, s string) {
buf := makeString(s)
n, err := c.Write(buf)
if err != nil {
log.Println(err)
}
log8.Println("Wrote", n, "bytes to server. (bytes:", buf, "string:", s)
}