-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
81 lines (67 loc) · 1.58 KB
/
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
package websocket
import (
"io"
"net"
"time"
"github.com/gorilla/websocket"
"v2ray.com/core/common/errors"
)
// connection is a wrapper for net.Conn over WebSocket connection.
type connection struct {
wsc *websocket.Conn
reader io.Reader
}
// Read implements net.Conn.Read()
func (c *connection) Read(b []byte) (int, error) {
for {
reader, err := c.getReader()
if err != nil {
return 0, err
}
nBytes, err := reader.Read(b)
if errors.Cause(err) == io.EOF {
c.reader = nil
continue
}
return nBytes, err
}
}
func (c *connection) getReader() (io.Reader, error) {
if c.reader != nil {
return c.reader, nil
}
_, reader, err := c.wsc.NextReader()
if err != nil {
return nil, err
}
c.reader = reader
return reader, nil
}
func (c *connection) Write(b []byte) (int, error) {
if err := c.wsc.WriteMessage(websocket.BinaryMessage, b); err != nil {
return 0, err
}
return len(b), nil
}
func (c *connection) Close() error {
c.wsc.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""), time.Now().Add(time.Second*5))
return c.wsc.Close()
}
func (c *connection) LocalAddr() net.Addr {
return c.wsc.LocalAddr()
}
func (c *connection) RemoteAddr() net.Addr {
return c.wsc.RemoteAddr()
}
func (c *connection) SetDeadline(t time.Time) error {
if err := c.SetReadDeadline(t); err != nil {
return err
}
return c.SetWriteDeadline(t)
}
func (c *connection) SetReadDeadline(t time.Time) error {
return c.wsc.SetReadDeadline(t)
}
func (c *connection) SetWriteDeadline(t time.Time) error {
return c.wsc.SetWriteDeadline(t)
}