-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin_tcp.go
More file actions
64 lines (55 loc) · 1.16 KB
/
admin_tcp.go
File metadata and controls
64 lines (55 loc) · 1.16 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
package main
import (
"bufio"
"bytes"
"io"
"log"
"net"
"strconv"
"strings"
)
func adminRun(hub *Hub, addr string) {
l, err := net.Listen("tcp", addr)
if err != nil {
log.Fatal(err)
}
defer l.Close()
for {
conn, err := l.Accept()
if err != nil {
log.Fatal(err)
}
go handleClient(conn, hub)
}
}
func handleClient(conn io.ReadWriter, hub *Hub) {
reader := bufio.NewReader(conn)
for {
msg, err := reader.ReadString(byte('\n'))
msg = strings.TrimRight(msg, "\r\n")
if err != nil {
break
}
if msg == "snapshots" {
snapshots := hub.getCoopSnapshots()
conn.Write([]byte(strconv.Itoa(len(snapshots)) + "\n"))
for _, snap := range snapshots {
conn.Write([]byte(
strconv.Itoa(snap.wordsGuessed) + " " + snap.word + " "))
playerMsgs := make([][]byte, len(snap.players))
for n, p := range snap.players {
pMsg := make([]byte, len(p.name), len(p.name)+1)
copy(pMsg, p.name)
if !p.present {
pMsg = append(pMsg, byte('_'))
}
playerMsgs[n] = pMsg
}
conn.Write(bytes.Join(playerMsgs, []byte(" ")))
conn.Write([]byte("\n"))
}
} else if msg == "phaseout" {
hub.phaseOutHub()
}
}
}