-
Notifications
You must be signed in to change notification settings - Fork 14
/
connection.go
162 lines (134 loc) · 2.91 KB
/
connection.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
package lrserver
import (
"encoding/json"
"strconv"
"sync"
"time"
"github.com/gorilla/websocket"
)
type conn struct {
conn *websocket.Conn
server *Server
handshake bool
reloadChan chan string
alertChan chan string
closeChan chan closeSignal
}
func (c *conn) start() {
go c.receive()
go c.transmit()
// Say hello
err := c.conn.WriteJSON(makeServerHello(c.server.Name()))
if err != nil {
c.close(websocket.CloseInternalServerErr, err)
}
// Block until close signal is sent
<-c.closeChan
}
func (c *conn) receive() {
for {
// Get next message
msgType, reader, err := c.conn.NextReader()
if err != nil {
c.close(0, err)
return
}
// Close if binary instead of text
if msgType == websocket.BinaryMessage {
c.close(websocket.CloseUnsupportedData, nil)
return
}
// Close if it's not JSON
hello := new(clientHello)
err = json.NewDecoder(reader).Decode(hello)
if err != nil {
c.close(websocket.ClosePolicyViolation, err)
return
}
// Close if missing a command field
if hello.Command == "" {
c.close(websocket.ClosePolicyViolation, nil)
}
// Validate handshake
if !c.handshake {
if !validateHello(hello) {
c.badHandshake()
return
}
c.handshake = true
c.server.logStatus("connected")
}
}
}
func (c *conn) transmit() {
for {
var resp interface{}
select {
// Reload
case file := <-c.reloadChan:
if !c.handshake {
c.badHandshake()
return
}
resp = makeServerReload(file, c.server.LiveCSS())
// Alert
case msg := <-c.alertChan:
if !c.handshake {
c.badHandshake()
return
}
resp = makeServerAlert(msg)
}
err := c.conn.WriteJSON(resp)
if err != nil {
c.close(websocket.CloseInternalServerErr, err)
return
}
}
}
func (c *conn) badHandshake() {
c.close(websocket.ClosePolicyViolation, websocket.ErrBadHandshake)
}
func (c *conn) close(closeCode int, closeErr error) error {
var err error
var errMsg string
if closeErr != nil {
errMsg = closeErr.Error()
c.server.logError(closeErr)
// Attempt to set close code from error message
errMsgLen := len(errMsg)
if errMsgLen >= 21 && errMsg[:17] == "websocket: close " {
closeCode, _ = strconv.Atoi(errMsg[17:21])
if errMsgLen > 21 {
errMsg = errMsg[22:]
}
}
}
// Default close code
if closeCode == 0 {
closeCode = websocket.CloseNoStatusReceived
}
// Send close message
closeMessage := websocket.FormatCloseMessage(closeCode, errMsg)
deadline := time.Now().Add(time.Second)
err = c.conn.WriteControl(websocket.CloseMessage, closeMessage, deadline)
// Kill and remove connection
c.closeChan <- closeSignal{}
c.server.connSet.remove(c)
return err
}
type connSet struct {
conns map[*conn]struct{}
m sync.Mutex
}
func (cs *connSet) add(c *conn) {
cs.m.Lock()
cs.conns[c] = struct{}{}
cs.m.Unlock()
}
func (cs *connSet) remove(c *conn) {
cs.m.Lock()
delete(cs.conns, c)
cs.m.Unlock()
}
type closeSignal struct{}