-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathclient.go
449 lines (393 loc) · 9.91 KB
/
client.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
package acfundanmu
import (
"context"
"encoding/binary"
"errors"
"fmt"
"io"
"log"
"net"
"sync"
"time"
"github.com/orzogc/acfundanmu/acproto"
"github.com/orzogc/fastws"
)
var msgPool = sync.Pool{
New: func() any {
b := make([]byte, maxBytesLength)
return &b
},
}
// 从 msgPool 获取 byte slice
func getBytes() *[]byte {
msg := msgPool.Get().(*[]byte)
if msg == nil {
b := make([]byte, maxBytesLength)
msg = &b
}
return msg
}
// 放入 byte slice 到 msgPool
func putBytes(msg *[]byte) {
if msg != nil {
msgPool.Put(msg)
}
}
// DanmuClientType 弹幕客户端类型
type DanmuClientType uint8
const (
// 弹幕客户端使用 WebSocket 连接
WebSocketDanmuClientType DanmuClientType = iota
// 弹幕客户端使用 TCP 连接
TCPDanmuClientType
)
// DanmuClient 弹幕客户端
type DanmuClient interface {
// New 返回新的弹幕客户端
NewDanmuClient() DanmuClient
// Type 返回弹幕客户端类型
Type() DanmuClientType
// Dial 连接弹幕服务器,address 是地址,在调用 Close() 后可重复调用
Dial(address string) error
// 读接口
io.Reader
// 写接口
io.Writer
// Close 关闭连接
Close(message string) error
}
// WebSocketDanmuClient 使用 WebSocket 连接的弹幕客户端
type WebSocketDanmuClient struct {
conn *fastws.Conn
}
// NewDanmuClient 返回新的 WebSocketDanmuClient
func (client *WebSocketDanmuClient) NewDanmuClient() DanmuClient {
return &WebSocketDanmuClient{}
}
// Type 返回弹幕客户端类型 WebSocketDanmuClientType
func (client *WebSocketDanmuClient) Type() DanmuClientType {
return WebSocketDanmuClientType
}
// Dial 连接弹幕服务器,address 是地址
func (client *WebSocketDanmuClient) Dial(address string) error {
conn, err := fastws.Dial(address)
if err != nil {
client.conn = nil
return err
}
conn.SetReadTimeout(timeout)
conn.SetWriteTimeout(timeout)
client.conn = conn
return nil
}
// Read 读数据
func (client *WebSocketDanmuClient) Read(p []byte) (n int, err error) {
if client.conn != nil {
_, p, err = client.conn.ReadMessage(p[:0])
return len(p), err
} else {
return 0, fmt.Errorf("请先调用 Dail() 连接服务器")
}
}
// Write 写数据
func (client *WebSocketDanmuClient) Write(p []byte) (n int, err error) {
if client.conn != nil {
return client.conn.WriteMessage(fastws.ModeBinary, p)
} else {
return 0, fmt.Errorf("请先调用 Dail() 连接服务器")
}
}
// Close 关闭连接
func (client *WebSocketDanmuClient) Close(message string) error {
if client.conn != nil {
return client.conn.CloseString(message)
} else {
return nil
}
}
// TCPDanmuClient 使用 TCP 连接的弹幕客户端
type TCPDanmuClient struct {
conn net.Conn
}
// NewDanmuClient 返回新的 TCPDanmuClient
func (client *TCPDanmuClient) NewDanmuClient() DanmuClient {
return &TCPDanmuClient{}
}
// Type 返回弹幕客户端类型 TCPDanmuClientType
func (client *TCPDanmuClient) Type() DanmuClientType {
return TCPDanmuClientType
}
// Dial 连接弹幕服务器,address 是地址
func (client *TCPDanmuClient) Dial(address string) error {
conn, err := net.DialTimeout("tcp", address, timeout)
if err != nil {
client.conn = nil
return err
}
client.conn = conn
return nil
}
// Read 读数据
func (client *TCPDanmuClient) Read(p []byte) (n int, err error) {
if client.conn != nil {
client.conn.SetReadDeadline(time.Now().Add(timeout))
defer client.conn.SetReadDeadline(time.Time{})
return client.conn.Read(p)
} else {
return 0, fmt.Errorf("请先调用 Dail() 连接服务器")
}
}
// Write 写数据
func (client *TCPDanmuClient) Write(p []byte) (n int, err error) {
if client.conn != nil {
client.conn.SetWriteDeadline(time.Now().Add(timeout))
defer client.conn.SetWriteDeadline(time.Time{})
return client.conn.Write(p)
} else {
return 0, fmt.Errorf("请先调用 Dail() 连接服务器")
}
}
// Close 关闭连接
func (client *TCPDanmuClient) Close(message string) error {
if client.conn != nil {
return client.conn.Close()
} else {
return nil
}
}
// 弹幕消息
type message struct {
bytes *[]byte
len int
}
// 获取弹幕数据
func (m *message) data() []byte {
return (*m.bytes)[:m.len]
}
// 定时发送 heartbeat 和 keepalive 数据
func (ac *AcFunLive) clientHeartbeat(ctx context.Context, interval int64) {
defer func() {
if err := recover(); err != nil {
log.Printf("Recovering from panic in clientHeartbeat(), the error is: %v", err)
// 重新启动 clientHeartbeat()
time.Sleep(2 * time.Second)
ac.clientHeartbeat(ctx, interval)
}
}()
ticker := time.NewTicker(time.Duration(interval) * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
_, err := ac.danmuClient.Write(ac.t.heartbeat())
checkErr(err)
if ac.t.heartbeatSeqID%5 == 4 {
_, err = ac.danmuClient.Write(ac.t.keepAlive())
checkErr(err)
}
}
}
}
// 启动弹幕 client
func (ac *AcFunLive) clientStart(ctx context.Context, event bool, errCh chan<- error) {
defer func() {
if err := recover(); err != nil {
log.Printf("Recovering from panic in clientStart(), the error is: %v", err)
log.Println("停止获取弹幕")
errCh <- err.(error)
close(errCh)
if event {
ac.callEvent(stopDanmu, err.(error))
}
}
}()
if !event {
defer ac.q.Dispose()
}
// 用于关闭 danmuClient
clientCtx, clientCancel := context.WithCancel(ctx)
defer clientCancel()
// WebSocket 连接可以直接发送注册消息,TCP 连接需要先握手
if ac.danmuClient.Type() == WebSocketDanmuClientType {
err := ac.danmuClient.Dial(wsHost)
checkErr(err)
_, err = ac.danmuClient.Write(ac.t.register())
checkErr(err)
} else if ac.danmuClient.Type() == TCPDanmuClientType {
err := ac.danmuClient.Dial(tcpHost)
checkErr(err)
_, err = ac.danmuClient.Write(ac.t.handshake())
checkErr(err)
}
go func() {
<-clientCtx.Done()
ac.danmuClient.Close("")
}()
msgCh := make(chan message, queueLen)
payloadCh := make(chan *acproto.DownstreamPayload, queueLen)
hasError := false
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
defer close(msgCh)
var n int
var err error
for {
msg := getBytes()
n, err = ac.danmuClient.Read(*msg)
if err != nil || ac.t.err.Load() != nil {
acErr := ac.t.err.Load()
if acErr != nil {
err = acErr
}
if !(errors.Is(err, io.EOF) || errors.Is(err, net.ErrClosed)) {
log.Printf("接收弹幕数据出现错误:%v", err)
log.Printf("停止获取 uid 为 %d 的主播的直播弹幕", ac.t.liverUID)
hasError = true
errCh <- err
close(errCh)
if event {
ac.callEvent(stopDanmu, err)
}
}
putBytes(msg)
break
}
msgCh <- message{bytes: msg, len: n}
}
}()
wg.Add(1)
go func() {
defer wg.Done()
defer close(payloadCh)
remain := []byte{}
for msg := range msgCh {
if msg.bytes == nil {
continue
}
if ac.danmuClient.Type() == WebSocketDanmuClientType {
// WebSocket 连接的数据自动分帧
stream, err := ac.t.decode(msg.data())
if err != nil {
log.Printf("解码接收到的弹幕数据出现错误:%v", err)
putBytes(msg.bytes)
continue
}
putBytes(msg.bytes)
payloadCh <- stream
} else if ac.danmuClient.Type() == TCPDanmuClientType {
// TCP 连接的数据需要自行分帧
if remain != nil {
remain = append(remain, msg.data()...)
} else {
remain = append([]byte{}, msg.data()...)
}
if len(remain) > 12 {
var frames [][]byte
var err error
frames, remain, err = getFrames(remain)
if err != nil {
log.Printf("解码接收到的弹幕数据出现错误:%v", err)
putBytes(msg.bytes)
continue
}
for _, frame := range frames {
stream, err := ac.t.decode(frame)
if err != nil {
log.Printf("解码接收到的弹幕数据出现错误:%v", err)
continue
}
payloadCh <- stream
}
}
putBytes(msg.bytes)
}
}
}()
wg.Add(1)
go func() {
defer wg.Done()
for stream := range payloadCh {
go func(stream *acproto.DownstreamPayload) {
err := ac.handleCommand(clientCtx, stream, event)
if err != nil {
log.Printf("处理接收到的弹幕数据出现错误:%v", err)
}
}(stream)
}
}()
wg.Wait()
if !hasError {
errCh <- nil
close(errCh)
if event {
ac.callEvent(stopDanmu, nil)
}
}
}
// 停止弹幕 client
func (ac *AcFunLive) clientStop(message string) {
_, err := ac.danmuClient.Write(ac.t.userExit())
checkErr(err)
_, err = ac.danmuClient.Write(ac.t.unregister())
checkErr(err)
_ = ac.danmuClient.Close(message)
}
// 检查魔法数字
func checkMagicNumber(data []byte) bool {
return data[0] == 0xAB && data[1] == 0xCD && data[2] == 0x00 && data[3] == 0x01
}
// 获取弹幕数据长度
func getDataLength(data []byte) int {
headerLength := binary.BigEndian.Uint32(data[4:8])
payloadLength := binary.BigEndian.Uint32(data[8:12])
return int(headerLength) + int(payloadLength) + 12
}
// 获取弹幕数据
func getFrame(data []byte) ([]byte, []byte) {
if checkMagicNumber(data) {
length := getDataLength(data)
if len(data) < length {
return nil, data
} else if len(data) > length {
return data[:length], data[length:]
} else {
return data, nil
}
} else {
return nil, nil
}
}
// 获取弹幕数据
func getFrames(data []byte) ([][]byte, []byte, error) {
if len(data) > 12 {
frames := [][]byte{}
var frame []byte
remain := data
for {
frame, remain = getFrame(remain)
if frame != nil && remain != nil {
frames = append(frames, frame)
if len(remain) <= 12 {
return frames, remain, nil
}
} else if frame != nil && remain == nil {
frames = append(frames, frame)
return frames, nil, nil
} else if frame == nil && remain != nil {
if len(frames) > 0 {
return frames, remain, nil
} else {
return nil, remain, nil
}
} else {
return nil, nil, fmt.Errorf("错误的弹幕数据格式")
}
}
} else {
return nil, data, nil
}
}