-
Notifications
You must be signed in to change notification settings - Fork 4
/
client.go
203 lines (170 loc) · 3.54 KB
/
client.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package main
import (
"net"
"fmt"
"log"
"os"
"bufio"
"strconv"
"encoding/json"
"encoding/binary"
)
var done chan bool
var out chan int
type Message struct {
Angle int `json:"angle"`
}
type FrameError string
func (e FrameError) Error() string {
return string(e)
}
func getLength(r *bufio.Reader) (int64, error) {
if mode, err := r.ReadByte(); err != nil {
return -1, err
} else if mode != 0x81 {
return -1, FrameError(fmt.Sprintf("Invalid mode: '%x'", mode))
}
mask, err := r.ReadByte()
if err != nil {
return -1, err
} else if (mask & 0x80) != 0x80 {
return -1, FrameError(fmt.Sprintf("Invalid mask: '%x'", mask))
}
// get rid of the mask bit to make later comparisons simpler
mask &= 0x7f
if mask <= 0x7d {
// the length fits in the mask, so we're done
return int64(mask), nil
}
// the next 2 or 8 bytes contains the length
// 0x7e for 2 bytes
// 0x7f for 8 bytes,
if mask == 0x7e {
a, err := r.ReadByte()
if err != nil {
return -1, err
}
b, err := r.ReadByte()
if err != nil {
return -1, err
}
return int64((a << 8) | b), nil
} else if mask == 0x7f {
var length int64
var b byte
for i := 8; i >= 0; i-- {
b, err = r.ReadByte()
if err != nil {
return -1, err
}
length |= int64(b << byte(8 * i))
}
return length, nil
}
// can't get here, all cases handled
return -1, nil
}
func handleRead(c net.Conn) {
r := bufio.NewReader(c)
for {
length, err := getLength(r)
if err != nil {
log.Println(err)
if _, isFrameErr := err.(FrameError); isFrameErr {
// give up and continue loop
log.Println(err)
continue
} else {
return
}
}
content := make([]byte, length)
n, err := r.Read(content)
l := 0
if err != nil {
log.Println(err)
}
for ; err == nil && n + l < len(content); l, err = c.Read(content[n:]) {
n += l
}
fmt.Printf(" Reply: %s\n", content)
fmt.Print("Enter angle: ")
}
}
func handleWrite(c net.Conn) {
for angle := range out {
m := Message{angle}
if buf, err := json.Marshal(m); err != nil {
log.Printf("Could not json encode angle: %+v\n", m)
} else {
var frame []byte
mode := byte(0x81)
mask := byte(0x80)
if len(buf) <= 125 {
// can fit in the mask
frame = []byte{mode, mask | byte(len(buf))}
} else if len(buf) > 0xffff {
// needs 8 extra bytes
frame = make([]byte, 10)
frame[0] = mode
frame[1] = mask | 127
// for now, len is 32-bit
// TODO: update when 64-bit is supported
// this should never happen anyway
binary.PutVarint(frame[2:], int64(len(buf)))
} else {
// needs 2 extra bytes
frame = []byte{
mode,
mask | 128,
byte(len(buf) & 0xff00) >> 8,
byte(len(buf) & 0xff)}
}
c.Write(append(frame, buf...))
}
}
}
func handleStdio() {
r := bufio.NewReader(os.Stdin)
fmt.Print("Enter angle: ")
for {
line, isPrefix, err := r.ReadLine()
if err != nil {
panic(err)
}
var l []byte
for isPrefix {
l, isPrefix, err = r.ReadLine()
if err != nil {
panic(err)
}
line = append(line, l...)
}
angle, err := strconv.Atoi(string(line))
if err != nil {
fmt.Printf("\nInvalid angle: %s\n", line)
fmt.Print("Enter angle: ")
} else {
// valid angle, so send it
out<-angle
}
}
}
func main() {
conn, err := net.Dial("tcp", "192.168.254.177:5000")
if err != nil {
panic(err)
}
defer conn.Close()
done = make(chan bool)
out = make(chan int)
defer func() {
close(done)
close(out)
}()
go handleRead(conn)
go handleWrite(conn)
go handleStdio()
<-done
fmt.Println("All done, ending program")
}