-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgobwas_connection.go
162 lines (130 loc) · 3.26 KB
/
gobwas_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 wspubsub
import (
"io/ioutil"
"net"
"strings"
"time"
"github.com/gobwas/ws"
"github.com/gobwas/ws/wsutil"
"github.com/pkg/errors"
)
var _ WebsocketConnection = (*GobwasConnection)(nil)
// GobwasConnection is an implementation of WebsocketConnection.
type GobwasConnection struct {
conn net.Conn
logger Logger
readTimeout time.Duration
wrightTimout time.Duration
IsDebug bool
DebugFuncTimeLimit time.Duration
}
// Read reads a message from WebSocket connection.
func (c *GobwasConnection) Read() (Message, error) {
opCode, bytes, err := c.doRead()
if err != nil {
return Message{}, errors.WithStack(c.handleError(err))
}
message := Message{
Type: MessageType(opCode),
Payload: bytes,
}
return message, nil
}
// Write writes a message to WebSocket connection.
func (c *GobwasConnection) Write(message Message) error {
if c.IsDebug {
now := time.Now()
defer func() {
end := time.Since(now)
if end > c.DebugFuncTimeLimit {
c.logger.Warnf("gobwas.connection.write: took=%s", end)
}
}()
}
err := c.conn.SetWriteDeadline(time.Now().Add(c.wrightTimout))
if err != nil {
return errors.WithStack(c.handleError(err))
}
err = wsutil.WriteServerMessage(c.conn, ws.OpCode(message.Type), message.Payload)
if err != nil {
return errors.WithStack(c.handleError(err))
}
return nil
}
// Close closes a WebSocket connection.
func (c *GobwasConnection) Close() error {
if c.IsDebug {
now := time.Now()
defer func() {
end := time.Since(now)
if end > c.DebugFuncTimeLimit {
c.logger.Warnf("gobwas.connection.close: took=%s", end)
}
}()
}
err := c.conn.Close()
if err != nil {
return errors.WithStack(c.handleError(err))
}
return nil
}
func (c *GobwasConnection) doRead() (ws.OpCode, []byte, error) {
controlHandler := wsutil.ControlFrameHandler(c.conn, ws.StateServerSide)
reader := wsutil.Reader{
Source: c.conn,
State: ws.StateServerSide,
CheckUTF8: true,
SkipHeaderCheck: false,
OnIntermediate: controlHandler,
}
err := c.conn.SetReadDeadline(time.Now().Add(c.readTimeout))
if err != nil {
return 0, nil, err
}
for {
header, err := reader.NextFrame()
if err != nil {
return 0, nil, err
}
if header.OpCode.IsControl() {
if header.OpCode == ws.OpPong {
err := c.conn.SetReadDeadline(time.Now().Add(c.readTimeout))
if err != nil {
return 0, nil, err
}
}
err := controlHandler(header, &reader)
if err != nil {
return 0, nil, err
}
continue
}
if header.OpCode&(ws.OpText|ws.OpBinary) == 0 {
err := reader.Discard()
if err != nil {
return 0, nil, err
}
continue
}
bytes, err := ioutil.ReadAll(&reader)
return header.OpCode, bytes, err
}
}
func (c *GobwasConnection) handleError(err error) error {
if err == nil {
return nil
}
if _, ok := err.(wsutil.ClosedError); ok {
closeErr := NewConnectionClosedError(err)
return errors.WithStack(closeErr)
}
if strings.Contains(err.Error(), "use of closed network connection") {
closeErr := NewConnectionClosedError(err)
return errors.WithStack(closeErr)
}
if _, ok := err.(*net.OpError); ok {
closeErr := NewConnectionClosedError(err)
return errors.WithStack(closeErr)
}
return errors.WithStack(err)
}