forked from klintcheng/kim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
72 lines (59 loc) · 1.25 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
package websocket
import (
"bufio"
"net"
"github.com/gobwas/ws"
"github.com/klintcheng/kim"
)
type Frame struct {
raw ws.Frame
}
func (f *Frame) SetOpCode(code kim.OpCode) {
f.raw.Header.OpCode = ws.OpCode(code)
}
func (f *Frame) GetOpCode() kim.OpCode {
return kim.OpCode(f.raw.Header.OpCode)
}
func (f *Frame) SetPayload(payload []byte) {
f.raw.Payload = payload
}
func (f *Frame) GetPayload() []byte {
if f.raw.Header.Masked {
ws.Cipher(f.raw.Payload, f.raw.Header.Mask, 0)
}
f.raw.Header.Masked = false
return f.raw.Payload
}
type WsConn struct {
net.Conn
rd *bufio.Reader
wr *bufio.Writer
}
func NewConn(conn net.Conn) kim.Conn {
return &WsConn{
Conn: conn,
rd: bufio.NewReaderSize(conn, 4096),
wr: bufio.NewWriterSize(conn, 1024),
}
}
func NewConnWithRW(conn net.Conn, rd *bufio.Reader, wr *bufio.Writer) *WsConn {
return &WsConn{
Conn: conn,
rd: rd,
wr: wr,
}
}
func (c *WsConn) ReadFrame() (kim.Frame, error) {
f, err := ws.ReadFrame(c.rd)
if err != nil {
return nil, err
}
return &Frame{raw: f}, nil
}
func (c *WsConn) WriteFrame(code kim.OpCode, payload []byte) error {
f := ws.NewFrame(ws.OpCode(code), true, payload)
return ws.WriteFrame(c.wr, f)
}
func (c *WsConn) Flush() error {
return c.wr.Flush()
}