-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient_conn.go
58 lines (49 loc) · 1.19 KB
/
client_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
package peregrine
import (
"github.com/gobwas/ws"
"github.com/gobwas/ws/wsutil"
"net"
"sync/atomic"
)
type ClientConn struct {
state *atomic.Bool
conn net.Conn
}
func (c *ClientConn) close(statusCode ws.StatusCode, reason string) error {
if !c.state.Load() {
return net.ErrClosed
}
c.state.Store(false)
defer c.conn.Close()
return ws.WriteFrame(c.conn, ws.NewCloseFrame(ws.NewCloseFrameBody(statusCode, reason)))
}
func (c *ClientConn) Close() error {
return c.close(ws.StatusNormalClosure, "")
}
func (c *ClientConn) CloseByReason(statusCode ws.StatusCode, reason string) error {
return c.close(statusCode, reason)
}
func (c *ClientConn) Hijack() (net.Conn, error) {
if !c.state.Load() {
return nil, net.ErrClosed
}
return c.conn, nil
}
func (c *ClientConn) WriteText(p []byte) error {
if !c.state.Load() {
return net.ErrClosed
}
return wsutil.WriteClientText(c.conn, p)
}
func (c *ClientConn) WriteBinary(p []byte) error {
if !c.state.Load() {
return net.ErrClosed
}
return wsutil.WriteClientBinary(c.conn, p)
}
func (c *ClientConn) ReadMessages() ([]wsutil.Message, error) {
if !c.state.Load() {
return nil, net.ErrClosed
}
return wsutil.ReadServerMessage(c.conn, nil)
}