-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
174 lines (145 loc) · 3.6 KB
/
main.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
package main
import (
"errors"
"fmt"
"log"
"math/rand"
"net/http"
"os"
"strconv"
"time"
"game-server/games"
"game-server/messages"
"github.com/gorilla/websocket"
)
var serverId string
func main() {
s1 := rand.NewSource(time.Now().UnixNano())
r1 := rand.New(s1)
serverId = strconv.Itoa(r1.Intn(999999)) // change to random number later
log.Println("Game Server Id = " + serverId)
adminId := strconv.Itoa(r1.Intn(999999))
games.BuzzerGame.Init(adminId)
log.Println("Admin Server Id = " + adminId)
http.HandleFunc("/", httpHello)
http.HandleFunc("/ws", wsHandler)
port := os.Getenv("PORT")
if port == "" {
port = "80"
}
log.Println("Starting server at port " + port)
if err := http.ListenAndServe(":"+port, nil); err != nil {
log.Fatal(err)
}
}
func httpHello(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "game-server running")
}
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool { return true },
}
func wsHandler(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
uid, gameStatus, err := readInit(conn)
if err != nil {
errorMessage := messages.CreateErrorMessage(err.Error())
sendJSON(conn, &errorMessage)
log.Println(err)
conn.Close()
return
}
connectedMessage := messages.CreateConnectedMessage(uid)
sendJSON(conn, &connectedMessage)
userManager(conn, uid, gameStatus)
}
func sendJSON(conn *websocket.Conn, v interface{}) error {
if err := conn.WriteJSON(v); err != nil {
log.Println(err)
return err
}
return nil
}
func userManager(conn *websocket.Conn, uid int32, gameStatus games.Game) {
connectionClosed := false
conn.SetCloseHandler(func(code int, text string) error {
log.Println("Closed Connection for " + strconv.Itoa(int(uid)))
err := gameStatus.DisableUser(uid)
connectionClosed = true
return err
})
readChan := make(chan []byte, 1)
go func() {
readMessage(conn, readChan)
}()
for connectionClosed == false {
select {
case message := <-readChan:
gameStatus.ProcessMessage(message)
case status := <-gameStatus.GetSendChannel(uid):
if connectionClosed {
return
}
err := sendJSON(conn, &status)
if err != nil {
conn.Close()
connectionClosed = true
return
}
case <-time.After(time.Second * 30):
pingMessage := messages.CreatePingMessage()
if connectionClosed {
return
}
err := sendJSON(conn, &pingMessage)
if err != nil {
conn.Close()
connectionClosed = true
return
}
}
}
}
func readInit(conn *websocket.Conn) (int32, games.Game, error) {
// Wait 2 seconds for the init message otherwise close the connection
conn.SetReadDeadline(time.Now().Add(2 * time.Second))
initMessage := messages.InitMessage{}
err := conn.ReadJSON(&initMessage)
conn.SetReadDeadline(time.Time{}) // no more timeout
if err != nil {
return -1, nil, err
}
if initMessage.MessageType != messages.InitMessageType {
return -1, nil, errors.New("Incorrect MessageType")
}
if initMessage.ServerId != serverId {
return -1, nil, errors.New("Incorrect ServerId")
}
uid := int32(0)
var gameStatus games.Game
// add support for other games here
switch initMessage.GameType {
case games.BuzzGame:
gameStatus = &games.BuzzerGame
default:
return -1, nil, errors.New("Unknown Game")
}
uid = gameStatus.AddUser(initMessage, conn)
return uid, gameStatus, nil
}
func readMessage(conn *websocket.Conn, message chan []byte) {
for {
_, p, err := conn.ReadMessage()
if err != nil {
log.Println(err)
conn.Close()
return
}
message <- p
}
}