/
ws_connection.go
executable file
·190 lines (146 loc) · 4.1 KB
/
ws_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
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
188
189
190
// Copyright 2016 Ilya Galimyanov All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.SE OR OTHER DEALINGS IN THE SOFTWARE.
package socketcrutch
import (
"errors"
"github.com/gorilla/websocket"
"io"
"net"
"time"
)
var (
ErrSend = errors.New("the message buffer full or the connection is closed")
)
type Conn interface {
// Send sends the given raw data to the websocket connection.
Send([]byte) error
// Close closes the underlying websocket connection.
Close() error
// NextMessage returns the next data message received from the connection.
NextMessage() ([]byte, bool)
// RemoteAddr returns the remote network address.
RemoteAddr() net.Addr
// Codec returns the established codec.
Codec() Codec
}
// connection represents a WebSocket connection.
type wsConnection struct {
ws *websocket.Conn
sc *SocketCrutch
inboxMessages chan []byte
outboxMessages chan []byte
done chan struct{}
}
func newConnection(ws *websocket.Conn, sc *SocketCrutch) *wsConnection {
conn := &wsConnection{
ws: ws,
sc: sc,
inboxMessages: make(chan []byte, sc.messageBufferSize),
outboxMessages: make(chan []byte, sc.messageBufferSize),
}
conn.configure()
return conn
}
func (c *wsConnection) configure() {
c.ws.SetReadLimit(c.sc.readLimit)
c.ws.SetPongHandler(c.pongHandler)
}
// run starts reading and writing the goroutines.
func (c *wsConnection) run() {
go c.writeMessagesLoop(c.Codec().WriteMode())
go c.readMessagesLoop(c.Codec().ReadMode())
}
func (c *wsConnection) wait() {
<-c.done
}
func (c *wsConnection) Codec() Codec {
return c.sc.codec
}
func (c *wsConnection) RemoteAddr() net.Addr {
return c.ws.RemoteAddr()
}
func (c *wsConnection) Send(packet []byte) error {
select {
case c.outboxMessages <- packet:
return nil
default:
return ErrSend
}
}
func (c *wsConnection) Close() error {
return c.ws.Close()
}
func (c *wsConnection) NextMessage() ([]byte, bool) {
data, ok := <-c.inboxMessages
return data, ok
}
func (c *wsConnection) pongHandler(string) error {
log.Debugf("received pong message")
c.ws.SetReadDeadline(time.Now().Add(c.sc.pingTimeout))
return nil
}
// write writes a message with the given message type and payload.
func (c *wsConnection) write(mode int, payload []byte) error {
c.ws.SetWriteDeadline(time.Now().Add(pingTimeout))
return c.ws.WriteMessage(mode, payload)
}
// readMessagesLoop reads messages from the websocket connection.
func (c *wsConnection) readMessagesLoop(mode int) {
// When disconnect
defer func() {
close(c.done)
close(c.outboxMessages)
close(c.inboxMessages)
}()
for {
// Reset the read deadline.
c.ws.SetReadDeadline(time.Now().Add(c.sc.pingTimeout))
messageType, message, err := c.ws.ReadMessage()
if err != nil {
isUnexpectedError := websocket.IsUnexpectedCloseError(err, websocket.CloseNormalClosure,
websocket.CloseGoingAway, websocket.CloseNoStatusReceived)
if err != io.EOF && isUnexpectedError {
log.Error("failed to read data from websocket: %v", err)
}
return
}
switch messageType {
case mode:
log.Debug("received message with a payload")
c.inboxMessages <- message
case websocket.CloseMessage:
log.Debug("got close message")
return
default:
log.Debug("got wrong message, message dropped")
}
}
}
// writeMessagesLoop writes messages from the session to the websocket connection.
func (c *wsConnection) writeMessagesLoop(mode int) {
var emptyMessage []byte
pingTicker := time.NewTicker(c.sc.pingInterval)
defer pingTicker.Stop()
for {
select {
case message, ok := <-c.outboxMessages:
if !ok {
log.Debug("write close message")
c.write(websocket.CloseMessage, emptyMessage)
return
}
log.Debugf("write message with a payload")
if err := c.write(mode, message); err != nil {
log.Errorf("an error has occurred in write binary message: %v", err)
return
}
case <-pingTicker.C:
log.Debug("write ping message")
if err := c.write(websocket.PingMessage, emptyMessage); err != nil {
log.Errorf("an error has occurred in write ping message: %v", err)
return
}
}
}
}