-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.go
62 lines (49 loc) · 1.31 KB
/
index.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
package parser
import (
"sync"
"github.com/zishang520/engine.io-go-parser/types"
)
// Protocol version.
const Protocol = 5
// A manager of a binary event's 'buffer sequence'. Should
// be constructed whenever a packet of type BINARY_EVENT is
// decoded.
type binaryreconstructor struct {
buffers []types.BufferInterface
reconPack *Packet
mu sync.Mutex
}
func NewBinaryReconstructor(packet *Packet) *binaryreconstructor {
return &binaryreconstructor{
buffers: []types.BufferInterface{},
reconPack: packet,
}
}
// Method to be called when binary data received from connection
// after a BINARY_EVENT packet.
func (b *binaryreconstructor) takeBinaryData(binData types.BufferInterface) (*Packet, error) {
b.mu.Lock()
defer b.mu.Unlock()
if b.reconPack == nil {
return nil, nil
}
b.buffers = append(b.buffers, binData)
if attachments := b.reconPack.Attachments; attachments != nil && uint64(len(b.buffers)) == *attachments {
// done with buffer list
packet, err := ReconstructPacket(b.reconPack, b.buffers)
if err != nil {
return nil, err
}
b.reconPack = nil
b.buffers = nil
return packet, nil
}
return nil, nil
}
// Cleans up binary packet reconstruction variables.
func (b *binaryreconstructor) finishedReconstruction() {
b.mu.Lock()
defer b.mu.Unlock()
b.reconPack = nil
b.buffers = nil
}