-
Notifications
You must be signed in to change notification settings - Fork 3
/
messages.go
179 lines (169 loc) · 3.78 KB
/
messages.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
// Copyright (c) 2020 Meng Huang ([email protected])
// This package is licensed under a MIT license that can be found in the LICENSE file.
package websocket
import (
"errors"
"github.com/hslam/buffer"
"github.com/hslam/writer"
"unsafe"
)
// SetBufferedOutput sets the buffered writer with the buffer size.
func (c *Conn) SetBufferedOutput(writeBufferSize int) {
c.writing.Lock()
if w, ok := c.writer.(*writer.Writer); ok {
w.Close()
}
if writeBufferSize > 0 {
c.writer = writer.NewWriter(c.conn, writeBufferSize)
} else {
c.writer = c.conn
writeBufferSize = bufferSize
}
c.writeBufferSize = writeBufferSize
if c.shared {
c.writePool = buffer.AssignPool(writeBufferSize)
} else {
c.writeBuffer = make([]byte, writeBufferSize)
}
c.writing.Unlock()
}
// SetBufferedInput sets the read buffer size.
func (c *Conn) SetBufferedInput(readBufferSize int) {
if readBufferSize < 1 {
readBufferSize = bufferSize
}
c.reading.Lock()
c.readBufferSize = readBufferSize
if c.shared {
c.readPool = buffer.AssignPool(readBufferSize)
} else {
c.readBuffer = make([]byte, readBufferSize)
}
c.reading.Unlock()
}
// ReceiveMessage receives single frame from ws, unmarshaled and stores in v.
func (c *Conn) ReceiveMessage(v interface{}) (err error) {
c.reading.Lock()
c.buffer = c.buffer[:0]
c.connBuffer = c.connBuffer[:0]
var f *frame
f, err = c.readFrame(nil)
if err == nil {
switch data := v.(type) {
case *string:
*data = *(*string)(unsafe.Pointer(&f.PayloadData))
c.putFrame(f)
case *[]byte:
*data = f.PayloadData
c.putFrame(f)
default:
err = errors.New("not supported")
}
}
c.reading.Unlock()
return
}
// SendMessage sends v marshaled as single frame to ws.
func (c *Conn) SendMessage(v interface{}) (err error) {
switch data := v.(type) {
case string:
if len(data) > 0 {
c.writing.Lock()
f := c.getFrame()
f.FIN = 1
f.Opcode = TextFrame
f.PayloadData = []byte(data)
err = c.writeFrame(f)
c.writing.Unlock()
}
return
case *string:
if len(*data) > 0 {
c.writing.Lock()
f := c.getFrame()
f.FIN = 1
f.Opcode = TextFrame
f.PayloadData = []byte(*data)
err = c.writeFrame(f)
c.writing.Unlock()
}
return
case []byte:
if len(data) > 0 {
c.writing.Lock()
f := c.getFrame()
f.FIN = 1
f.Opcode = BinaryFrame
f.PayloadData = data
err = c.writeFrame(f)
c.writing.Unlock()
}
return
case *[]byte:
if len(*data) > 0 {
c.writing.Lock()
f := c.getFrame()
f.FIN = 1
f.Opcode = BinaryFrame
f.PayloadData = *data
err = c.writeFrame(f)
c.writing.Unlock()
}
return
}
return errors.New("not supported")
}
// ReadMessage reads single message from ws.
func (c *Conn) ReadMessage(buf []byte) (p []byte, err error) {
c.reading.Lock()
c.buffer = c.buffer[:0]
c.connBuffer = c.connBuffer[:0]
var f *frame
f, err = c.readFrame(buf)
if err == nil {
p = f.PayloadData
c.putFrame(f)
}
c.reading.Unlock()
return
}
// WriteMessage writes single message to ws.
func (c *Conn) WriteMessage(b []byte) (err error) {
if len(b) > 0 {
c.writing.Lock()
f := c.getFrame()
f.FIN = 1
f.Opcode = BinaryFrame
f.PayloadData = b
err = c.writeFrame(f)
c.writing.Unlock()
}
return
}
// ReadTextMessage reads single text message from ws.
func (c *Conn) ReadTextMessage() (p string, err error) {
c.reading.Lock()
c.buffer = c.buffer[:0]
c.connBuffer = c.connBuffer[:0]
var f *frame
f, err = c.readFrame(nil)
if err == nil {
p = *(*string)(unsafe.Pointer(&f.PayloadData))
c.putFrame(f)
}
c.reading.Unlock()
return
}
// WriteTextMessage writes single text message to ws.
func (c *Conn) WriteTextMessage(b string) (err error) {
if len(b) > 0 {
c.writing.Lock()
f := c.getFrame()
f.FIN = 1
f.Opcode = TextFrame
f.PayloadData = []byte(b)
err = c.writeFrame(f)
c.writing.Unlock()
}
return
}