-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathconn.go
289 lines (242 loc) · 7.55 KB
/
conn.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright 2014-2015 GopherJS Team. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file.
package websocket
import (
"bytes"
"fmt"
"io"
"net"
"net/url"
"time"
"github.com/gopherjs/gopherjs/js"
"github.com/gopherjs/websocket/websocketjs"
)
func beginHandlerOpen(ch chan error, removeHandlers func()) func(ev *js.Object) {
return func(ev *js.Object) {
removeHandlers()
close(ch)
}
}
// closeError allows a CloseEvent to be used as an error.
type closeError struct {
*js.Object
Code int `js:"code"`
Reason string `js:"reason"`
WasClean bool `js:"wasClean"`
}
func (e *closeError) Error() string {
var cleanStmt string
if e.WasClean {
cleanStmt = "clean"
} else {
cleanStmt = "unclean"
}
return fmt.Sprintf("CloseEvent: (%s) (%d) %s", cleanStmt, e.Code, e.Reason)
}
func beginHandlerClose(ch chan error, removeHandlers func()) func(ev *js.Object) {
return func(ev *js.Object) {
removeHandlers()
go func() {
ch <- &closeError{Object: ev}
close(ch)
}()
}
}
type deadlineErr struct{}
func (e *deadlineErr) Error() string { return "i/o timeout: deadline reached" }
func (e *deadlineErr) Timeout() bool { return true }
func (e *deadlineErr) Temporary() bool { return true }
var errDeadlineReached = &deadlineErr{}
// TODO(nightexcessive): Add a Dial function that allows a deadline to be
// specified.
// Dial opens a new WebSocket connection. It will block until the connection is
// established or fails to connect.
func Dial(url string) (net.Conn, error) {
ws, err := websocketjs.New(url)
if err != nil {
return nil, err
}
conn := &conn{
WebSocket: ws,
ch: make(chan *messageEvent, 1),
}
conn.initialize()
openCh := make(chan error, 1)
var (
openHandler func(ev *js.Object)
closeHandler func(ev *js.Object)
)
// Handlers need to be removed to prevent a panic when the WebSocket closes
// immediately and fires both open and close before they can be removed.
// This way, handlers are removed before the channel is closed.
removeHandlers := func() {
ws.RemoveEventListener("open", false, openHandler)
ws.RemoveEventListener("close", false, closeHandler)
}
// We have to use variables for the functions so that we can remove the
// event handlers afterwards.
openHandler = beginHandlerOpen(openCh, removeHandlers)
closeHandler = beginHandlerClose(openCh, removeHandlers)
ws.AddEventListener("open", false, openHandler)
ws.AddEventListener("close", false, closeHandler)
err, ok := <-openCh
if ok && err != nil {
return nil, err
}
return conn, nil
}
// conn is a high-level wrapper around WebSocket. It implements net.Conn interface.
type conn struct {
*websocketjs.WebSocket
ch chan *messageEvent
readBuf *bytes.Reader
readDeadline time.Time
}
type messageEvent struct {
*js.Object
Data *js.Object `js:"data"`
}
func (c *conn) onMessage(event *js.Object) {
go func() {
c.ch <- &messageEvent{Object: event}
}()
}
func (c *conn) onClose(event *js.Object) {
go func() {
// We queue nil to the end so that any messages received prior to
// closing get handled first.
c.ch <- nil
}()
}
// initialize adds all of the event handlers necessary for a conn to function.
// It should never be called more than once and is already called if Dial was
// used to create the conn.
func (c *conn) initialize() {
// We need this so that received binary data is in ArrayBufferView format so
// that it can easily be read.
c.BinaryType = "arraybuffer"
c.AddEventListener("message", false, c.onMessage)
c.AddEventListener("close", false, c.onClose)
}
// handleFrame handles a single frame received from the channel. This is a
// convenience funciton to dedupe code for the multiple deadline cases.
func (c *conn) handleFrame(message *messageEvent, ok bool) (*messageEvent, error) {
if !ok { // The channel has been closed
return nil, io.EOF
} else if message == nil {
// See onClose for the explanation about sending a nil item.
close(c.ch)
return nil, io.EOF
}
return message, nil
}
// receiveFrame receives one full frame from the WebSocket. It blocks until the
// frame is received.
func (c *conn) receiveFrame(observeDeadline bool) (*messageEvent, error) {
var deadlineChan <-chan time.Time // Receiving on a nil channel always blocks indefinitely
if observeDeadline && !c.readDeadline.IsZero() {
now := time.Now()
if now.After(c.readDeadline) {
select {
case item, ok := <-c.ch:
return c.handleFrame(item, ok)
default:
return nil, errDeadlineReached
}
}
timer := time.NewTimer(c.readDeadline.Sub(now))
defer timer.Stop()
deadlineChan = timer.C
}
select {
case item, ok := <-c.ch:
return c.handleFrame(item, ok)
case <-deadlineChan:
return nil, errDeadlineReached
}
}
func getFrameData(obj *js.Object) []byte {
// Check if it's an array buffer. If so, convert it to a Go byte slice.
if constructor := obj.Get("constructor"); constructor == js.Global.Get("ArrayBuffer") {
uint8Array := js.Global.Get("Uint8Array").New(obj)
return uint8Array.Interface().([]byte)
}
return []byte(obj.String())
}
func (c *conn) Read(b []byte) (n int, err error) {
if c.readBuf != nil {
n, err = c.readBuf.Read(b)
if err == io.EOF {
c.readBuf = nil
err = nil
}
// If we read nothing from the buffer, continue to trying to receive.
// This saves us when the last Read call emptied the buffer and this
// call triggers the EOF. There's probably a better way of doing this,
// but I'm really tired.
if n > 0 {
return
}
}
frame, err := c.receiveFrame(true)
if err != nil {
return 0, err
}
receivedBytes := getFrameData(frame.Data)
n = copy(b, receivedBytes)
// Fast path: The entire frame's contents have been copied into b.
if n >= len(receivedBytes) {
return
}
c.readBuf = bytes.NewReader(receivedBytes[n:])
return
}
// Write writes the contents of b to the WebSocket using a binary opcode.
func (c *conn) Write(b []byte) (n int, err error) {
// []byte is converted to an Uint8Array by GopherJS, which fullfils the
// ArrayBufferView definition.
err = c.Send(b)
if err != nil {
return 0, err
}
return len(b), nil
}
// LocalAddr would typically return the local network address, but due to
// limitations in the JavaScript API, it is unable to. Calling this method will
// cause a panic.
func (c *conn) LocalAddr() net.Addr {
// BUG(nightexcessive): conn.LocalAddr() panics because the underlying
// JavaScript API has no way of figuring out the local address.
// TODO(nightexcessive): Find a more graceful way to handle this
panic("we are unable to implement websocket.conn.LocalAddr() due to limitations in the underlying JavaScript API")
}
// RemoteAddr returns the remote network address, based on
// websocket.WebSocket.URL.
func (c *conn) RemoteAddr() net.Addr {
wsURL, err := url.Parse(c.URL)
if err != nil {
// TODO(nightexcessive): Should we be panicking for this?
panic(err)
}
return &addr{wsURL}
}
// SetDeadline sets the read and write deadlines associated with the connection.
// It is equivalent to calling both SetReadDeadline and SetWriteDeadline.
//
// A zero value for t means that I/O operations will not time out.
func (c *conn) SetDeadline(t time.Time) error {
c.readDeadline = t
return nil
}
// SetReadDeadline sets the deadline for future Read calls. A zero value for t
// means Read will not time out.
func (c *conn) SetReadDeadline(t time.Time) error {
c.readDeadline = t
return nil
}
// SetWriteDeadline sets the deadline for future Write calls. Because our writes
// do not block, this function is a no-op.
func (c *conn) SetWriteDeadline(t time.Time) error {
return nil
}