/
websocket.go
203 lines (163 loc) · 3.46 KB
/
websocket.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
191
192
193
194
195
196
197
198
199
200
201
202
203
package transport
import (
"context"
"net"
"net/http"
"time"
"github.com/gobwas/ws/wsutil"
"github.com/gorilla/websocket"
"github.com/VolantMQ/volantmq/configuration"
"github.com/VolantMQ/volantmq/systree"
)
type httpServer struct {
mux *http.ServeMux
http *http.Server
}
type wsConn struct {
*conn
rem []byte
}
// Read ...
func (c *wsConn) Read(b []byte) (int, error) {
var n int
if len(c.rem) > 0 {
n = copy(b, c.rem)
if n < len(c.rem) {
c.rem = c.rem[n:]
}
}
var err error
if n < len(b) {
var data []byte
data, err = wsutil.ReadClientBinary(c.Conn)
n1 := copy(b[n:], data)
n += n1
if n1 < len(data) {
c.rem = data[n1:]
}
}
c.stat.Received(uint64(n))
return n, err
}
// Write ...
func (c *wsConn) Write(b []byte) (int, error) {
err := wsutil.WriteServerBinary(c.conn, b)
n := 0
if err == nil {
n = len(b)
c.stat.Sent(uint64(n))
}
return n, err
}
// ConfigWS listener object for websocket server
type ConfigWS struct {
// CertFile
CertFile string
// KeyFile
KeyFile string
// Path
Path string
// SubProtocols
SubProtocols []string
transport *Config
}
type ws struct {
baseConfig
httpServer
up websocket.Upgrader
certFile string
keyFile string
}
// NewConfigWS allocate new transport config for websocket transport
// Use of this function is preferable instead of direct allocation of ConfigWS
func NewConfigWS(transport *Config) *ConfigWS {
return &ConfigWS{
Path: "/",
transport: transport,
}
}
// NewWS create new websocket transport
func NewWS(config *ConfigWS, internal *InternalConfig) (Provider, error) {
l := &ws{
certFile: config.CertFile,
keyFile: config.KeyFile,
}
l.quit = make(chan struct{})
l.InternalConfig = *internal
l.config = *config.transport
if len(l.certFile) != 0 {
l.protocol = "wss"
} else {
l.protocol = "ws"
}
l.log = configuration.GetLogger().Named("listener: " + l.protocol + "://:" + config.transport.Port)
if len(config.Path) == 0 {
config.Path = "/"
}
l.mux = http.NewServeMux()
l.mux.HandleFunc(config.Path, l.serve)
l.http = &http.Server{
Addr: ":" + config.transport.Port,
Handler: l,
}
return l, nil
}
func (l *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
l.mux.ServeHTTP(w, r)
}
func (l *ws) newConn(cn net.Conn, stat systree.BytesMetric) (Conn, error) {
wr, err := newConn(l.EPoll, cn, stat)
if err != nil {
return nil, err
}
c := &wsConn{
conn: wr,
}
return c, nil
}
func (l *ws) serve(w http.ResponseWriter, r *http.Request) {
// cn, err := l.up.Upgrade(w, r, nil)
// if err != nil {
// l.log.Errorf("upgrade error: %s", err)
// return
// }
//
// l.onConnection.Add(1)
// go func() {
// defer l.onConnection.Done()
// if inConn, e := l.newConn(cn, l.Metric.Bytes()); e != nil {
// l.log.Error("Couldn't create connection interface", zap.Error(e))
// } else {
// l.handleConnection(inConn)
// }
// }()
}
// Ready ...
func (l *ws) Ready() error {
return nil
}
// Alive ...
func (l *ws) Alive() error {
return nil
}
// Serve ...
func (l *ws) Serve() error {
var e error
if len(l.certFile) != 0 && len(l.keyFile) != 0 {
e = l.http.ListenAndServeTLS(l.certFile, l.keyFile)
} else {
e = l.http.ListenAndServe()
}
return e
}
// Close websocket listener
func (l *ws) Close() error {
var err error
l.onceStop.Do(func() {
ctx, ctxCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer ctxCancel()
err = l.http.Shutdown(ctx)
l.onConnection.Wait()
})
return err
}