-
-
Notifications
You must be signed in to change notification settings - Fork 169
/
connWS.go
116 lines (92 loc) · 1.81 KB
/
connWS.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
package transport
import (
"io"
"net"
"time"
"github.com/VolantMQ/volantmq/systree"
"github.com/gorilla/websocket"
)
type connWs struct {
conn *websocket.Conn
stat systree.BytesMetric
prev io.Reader
//pollDesc
}
var _ Conn = (*connWs)(nil)
// Read
// FIXME: looks ugly
func (c *connWs) Read(b []byte) (int, error) {
var mType int
var err error
var total int
// if previous reader exists try read remaining data
if c.prev != nil {
total, err = c.prev.Read(b)
if total > 0 {
c.stat.Received(uint64(total))
}
if err != nil {
c.prev = nil
}
if total > 0 {
return total, nil
}
}
if mType, c.prev, err = c.conn.NextReader(); err != nil {
return 0, io.EOF
}
switch mType {
case websocket.CloseMessage:
return 0, io.EOF
case websocket.TextMessage:
fallthrough
case websocket.PingMessage:
fallthrough
case websocket.PongMessage:
return 0, nil
}
total, err = c.prev.Read(b)
if total > 0 {
c.stat.Received(uint64(total))
}
if err != nil {
c.prev = nil
}
if total > 0 {
return total, nil
}
return 0, nil
}
func (c *connWs) Write(b []byte) (int, error) {
err := c.conn.WriteMessage(websocket.BinaryMessage, b)
n := 0
if err == nil {
n = len(b)
c.stat.Sent(uint64(n))
}
return n, err
}
func (c *connWs) Close() error {
return c.conn.Close()
}
func (c *connWs) LocalAddr() net.Addr {
return c.conn.LocalAddr()
}
func (c *connWs) RemoteAddr() net.Addr {
return c.conn.RemoteAddr()
}
func (c *connWs) SetDeadline(t time.Time) error {
if err := c.conn.SetReadDeadline(t); err != nil {
return err
}
if err := c.conn.SetWriteDeadline(t); err != nil {
return err
}
return nil
}
func (c *connWs) SetReadDeadline(t time.Time) error {
return c.conn.SetReadDeadline(t)
}
func (c *connWs) SetWriteDeadline(t time.Time) error {
return c.conn.SetWriteDeadline(t)
}