-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathkcp_buffer.go
66 lines (54 loc) · 1.37 KB
/
kcp_buffer.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
package gokcp
import (
"encoding/binary"
)
type Buffer struct {
srcBuffer []byte
buffer []byte
segBuffer []byte
reserved int
}
func NewBuffer(size int, reserved int) *Buffer {
b := &Buffer{
buffer: make([]byte, size),
segBuffer: make([]byte, KCP_OVERHEAD),
}
b.reserved = reserved
b.srcBuffer = b.buffer
b.buffer = b.buffer[reserved:]
b.buffer = b.buffer[:0]
return b
}
func (b *Buffer) Reset() {
b.buffer = b.buffer[:0]
}
func (b *Buffer) Write(p []byte) {
b.buffer = append(b.buffer, p...)
}
func (b *Buffer) WriteOverHeader(seg *segment) {
binary.LittleEndian.PutUint32(b.segBuffer, seg.convID)
b.segBuffer[4] = byte(seg.cmd)
b.segBuffer[5] = byte(seg.frg)
binary.LittleEndian.PutUint16(b.segBuffer[6:], uint16(seg.wnd))
binary.LittleEndian.PutUint32(b.segBuffer[8:], seg.ts)
binary.LittleEndian.PutUint32(b.segBuffer[12:], seg.sn)
binary.LittleEndian.PutUint32(b.segBuffer[16:], seg.una)
binary.LittleEndian.PutUint32(b.segBuffer[20:], uint32(len(seg.dataBuffer)))
b.segBuffer = b.segBuffer[0:]
b.Write(b.segBuffer)
}
func (b *Buffer) CompareDiff(size int) bool {
return len(b.buffer) > size
}
func (b *Buffer) Data() []byte {
return b.buffer
}
func (b *Buffer) RawData() []byte {
return b.srcBuffer[:len(b.buffer)+b.reserved]
}
func (b *Buffer) Len() int {
return len(b.buffer)
}
func (b *Buffer) RawLen() int {
return len(b.buffer) + b.reserved
}