-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
178 lines (139 loc) · 3.13 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
package main
import (
"gopkg.in/mgo.v2/bson"
"net"
"io"
"fmt"
//"bytes"
)
const Deliminator = "\nEND\n"
//
// A primative type that models an Orb.
//
type Orb struct {
X float32
Y float32
ID string
}
//
// A construct responsibile for reading and writing Orb changes between the
// TCP client and server.
//
type OrbClient struct {
//
// The client'd connection. This will be an *net.TCPConn. We're using
// an interface here for mocking purposes.
//
conn net.Conn
//
// Client to broadcast its own Orb changes to its Room
//
read chan Orb
//
// For room to broadcast Orb changes to its clients
//
write chan Orb
//
// For client to notify the room that it has been disconnected
//
disconnect chan bool
}
//
// Getters
//
func (self *OrbClient) Conn() net.Conn {
return self.conn
}
func (self *OrbClient) Read() <-chan Orb {
return self.read
}
func (self *OrbClient) Write() chan<- Orb {
return self.write
}
func (self *OrbClient) Disconnect() <-chan bool {
return self.disconnect
}
//
// Sends `true` to the disconnect channel, notifying any listeners that
// the client has disconnected from the server
//
func (self *OrbClient) broadcastDisconnect() {
self.disconnect <-true
}
//
// Runs a concurrent routine that deals with writing Orbs sent on the Write chan.
// Pushing Orb data to this channel will notify the client of the states of other
// Orbs in a Room
//
func (self *OrbClient) writeLoop() {
for {
changedOrb := <-self.write
fmt.Println("Change orb: ", changedOrb)
bsonBuf, err := bson.Marshal(changedOrb)
if err != nil {
fmt.Println("Error marshalling (todo handle properly): ", err)
}
//buff := bytes.NewBuffer(bson)
//buff.append(Deliminator)
bsonBuf = append(bsonBuf, []byte(Deliminator)...)
fmt.Printf("Writing %s\n", bsonBuf)
_, err = self.conn.Write(bsonBuf)
if err == io.EOF {
self.broadcastDisconnect()
return
} else if err != nil {
fmt.Println("Error writing (todo handle properly): ", err)
}
}
}
//
// Runs a concurrent routine that deals with reading updates sent down the
// pipeline. These updates should be solely for state of the client's Orb.
//
func (self *OrbClient) readLoop() {
for {
bsonBuf := make([]byte, 2048)
l, err := self.conn.Read(bsonBuf)
if err == io.EOF {
self.broadcastDisconnect()
return
} else if err == nil && l > 0 {
newOrb := &Orb{}
if err := bson.Unmarshal(bsonBuf, newOrb); err != nil {
fmt.Println("Error unmarshalling (todo handle properly): ", err)
} else {
self.read <-*newOrb
}
} else {
fmt.Println("Error reading (todo handle properly): ", err)
}
}
}
//
// Closes the connection. This is thread safe.
//
func (self *OrbClient) Close() error {
return self.conn.Close()
}
//
//
//
//func (self *OrbClient) Listen() {
//
// go self.readLoop()
// go self.writeLoop()
//}
//
// Ctor. Creates a new client and starts its read / write loops.
//
func NewOrbClient(conn net.Conn, readCh chan Orb) *OrbClient {
client := &OrbClient{
conn: conn,
write: make(chan Orb),
read: readCh,
disconnect: make(chan bool),
}
go client.readLoop()
go client.writeLoop()
return client
}