-
-
Notifications
You must be signed in to change notification settings - Fork 576
/
transport.go
169 lines (147 loc) · 3.68 KB
/
transport.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
package uniws
import (
"sync"
"time"
"github.com/centrifugal/centrifuge"
"github.com/gorilla/websocket"
)
// websocketTransport is a wrapper struct over websocket connection to fit session
// interface so client will accept it.
type websocketTransport struct {
mu sync.RWMutex
writeMu sync.Mutex // sync general write with unidirectional ping write.
conn *websocket.Conn
closed bool
closeCh chan struct{}
graceCh chan struct{}
opts websocketTransportOptions
pingTimer *time.Timer
}
type websocketTransportOptions struct {
pingInterval time.Duration
writeTimeout time.Duration
compressionMinSize int
pingPongConfig centrifuge.PingPongConfig
}
func newWebsocketTransport(conn *websocket.Conn, opts websocketTransportOptions, graceCh chan struct{}) *websocketTransport {
transport := &websocketTransport{
conn: conn,
closeCh: make(chan struct{}),
graceCh: graceCh,
opts: opts,
}
if opts.pingInterval > 0 {
transport.addPing()
}
return transport
}
func (t *websocketTransport) ping() {
select {
case <-t.closeCh:
return
default:
deadline := time.Now().Add(t.opts.pingInterval / 2)
err := t.conn.WriteControl(websocket.PingMessage, nil, deadline)
if err != nil {
_ = t.Close(centrifuge.DisconnectWriteError)
return
}
t.addPing()
}
}
func (t *websocketTransport) addPing() {
t.mu.Lock()
if t.closed {
t.mu.Unlock()
return
}
t.pingTimer = time.AfterFunc(t.opts.pingInterval, t.ping)
t.mu.Unlock()
}
const transportName = "uni_websocket"
// Name returns name of transport.
func (t *websocketTransport) Name() string {
return transportName
}
// Protocol returns transport protocol.
func (t *websocketTransport) Protocol() centrifuge.ProtocolType {
return centrifuge.ProtocolTypeJSON
}
// ProtocolVersion returns transport protocol version.
func (t *websocketTransport) ProtocolVersion() centrifuge.ProtocolVersion {
return centrifuge.ProtocolVersion2
}
// Unidirectional returns whether transport is unidirectional.
func (t *websocketTransport) Unidirectional() bool {
return true
}
// DisabledPushFlags ...
func (t *websocketTransport) DisabledPushFlags() uint64 {
return 0
}
// PingPongConfig ...
func (t *websocketTransport) PingPongConfig() centrifuge.PingPongConfig {
return t.opts.pingPongConfig
}
// Emulation ...
func (t *websocketTransport) Emulation() bool {
return false
}
func (t *websocketTransport) writeData(data []byte) error {
if t.opts.compressionMinSize > 0 {
t.conn.EnableWriteCompression(len(data) > t.opts.compressionMinSize)
}
var messageType = websocket.TextMessage
if t.Protocol() == centrifuge.ProtocolTypeProtobuf {
messageType = websocket.BinaryMessage
}
t.writeMu.Lock()
if t.opts.writeTimeout > 0 {
_ = t.conn.SetWriteDeadline(time.Now().Add(t.opts.writeTimeout))
}
err := t.conn.WriteMessage(messageType, data)
if err != nil {
t.writeMu.Unlock()
return err
}
if t.opts.writeTimeout > 0 {
_ = t.conn.SetWriteDeadline(time.Time{})
}
t.writeMu.Unlock()
return nil
}
// Write data to transport.
func (t *websocketTransport) Write(message []byte) error {
return t.WriteMany(message)
}
// WriteMany data to transport.
func (t *websocketTransport) WriteMany(messages ...[]byte) error {
select {
case <-t.closeCh:
return nil
default:
for i := 0; i < len(messages); i++ {
err := t.writeData(messages[i])
if err != nil {
return err
}
}
return nil
}
}
const closeFrameWait = 5 * time.Second
// Close closes transport.
func (t *websocketTransport) Close(_ centrifuge.Disconnect) error {
t.mu.Lock()
if t.closed {
t.mu.Unlock()
return nil
}
t.closed = true
if t.pingTimer != nil {
t.pingTimer.Stop()
}
close(t.closeCh)
t.mu.Unlock()
return t.conn.Close()
}