-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhub.go
187 lines (166 loc) · 3.83 KB
/
hub.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
package main
import (
"fmt"
"github.com/gorilla/websocket"
"strconv"
"strings"
"sync"
)
var gameMapMutex = &sync.Mutex{}
type Vertex struct {
X, Y int
}
type Zombie struct {
sync.Mutex
pos *Vertex
}
type State struct {
zombieChan chan *Zombie
gameInfo chan string
score *Score
}
type Score struct {
sync.Mutex
client int
server int
}
type Client struct {
hub *Hub
conn *websocket.Conn
shotChan chan *Vertex
}
type Hub struct {
games map[*Client]*State
register chan *Client
unregister chan *Client
}
func newHub() *Hub {
return &Hub{
games: make(map[*Client]*State),
register: make(chan *Client),
unregister: make(chan *Client),
}
}
func (h *Hub) run() {
for {
select {
case client := <-h.register:
gameMapMutex.Lock()
h.games[client] = &State{
zombieChan: make(chan *Zombie, 1024),
gameInfo: make(chan string),
score: &Score{client: 0, server: 0},
}
gameMapMutex.Unlock()
go client.writePump() // route zombies to client
go client.readPump() // route shots to game
case client := <-h.unregister:
fmt.Println("client unregister ", client)
gameMapMutex.Lock()
delete(h.games, client)
gameMapMutex.Unlock()
}
}
}
func handleClientMessage(c *Client, message []byte) {
if string(message) == "start" {
fmt.Println("starting game for ", c)
gameMapMutex.Lock()
go startZombie(c.hub.games[c], c)
gameMapMutex.Unlock()
} else {
shot := strToVertex(message)
c.shotChan <- shot
}
fmt.Println("got a message: " + string(message))
}
// run readPump per connection
func (c *Client) readPump() {
defer func() {
fmt.Println("closing connection and sending unregister Read Pump")
c.hub.unregister <- c
c.conn.Close()
}()
ListenForMessages:
for {
_, message, err := c.conn.ReadMessage()
if err != nil {
fmt.Println("connection lost. exiting read loop")
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
fmt.Printf("error: %v", err)
}
break ListenForMessages
}
handleClientMessage(c, message)
}
}
func (c *Client) writePump() {
defer func() {
fmt.Println("closing connection and sending unregister. Write Pump")
err := c.conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
if err != nil {
fmt.Println("write close(closed by other end):", err)
return
}
c.hub.unregister <- c
c.conn.Close()
}()
gameMapMutex.Lock()
game := c.hub.games[c]
gameMapMutex.Unlock()
WriteLoop:
for {
select {
case zombieMove, ok := <-game.zombieChan:
if !ok {
fmt.Println("zombie chan closed, stop listening")
} else {
w, err := c.conn.NextWriter(websocket.TextMessage)
if err != nil {
fmt.Println(err)
return // stay in loop and keep writing from game.info chan
}
zombieMove.Lock()
msg := XYToStr(zombieMove.pos)
zombieMove.Unlock()
w.Write([]byte(msg))
if err := w.Close(); err != nil { // not sure if I should close writer each time here
fmt.Println(err)
}
}
case info, ok := <-game.gameInfo:
if !ok {
fmt.Println("info chan closed")
break WriteLoop
}
w, err := c.conn.NextWriter(websocket.TextMessage)
if err != nil {
fmt.Println("websocket write failed")
fmt.Println(err)
break WriteLoop
}
w.Write([]byte(info))
if err := w.Close(); err != nil {
fmt.Println("websocket close failed")
fmt.Println(err)
}
}
}
}
// conversion helpers
func strToVertex(s []byte) *Vertex {
strPos := strings.Split(string(s), " ")
x, err := strconv.Atoi(strPos[0])
if err != nil {
fmt.Errorf("failed to convert coordinates")
}
y, erry := strconv.Atoi(strPos[1])
if erry != nil {
fmt.Errorf("failed to convert coordinates")
}
return &Vertex{X: x, Y: y}
}
func XYToStr(pos *Vertex) (s string) {
s = string(strconv.Itoa(pos.X) + " " + strconv.Itoa(pos.Y))
return
}