-
Notifications
You must be signed in to change notification settings - Fork 0
/
lengthCodec.go
175 lines (164 loc) · 4.28 KB
/
lengthCodec.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
/*
* Copyright (c) 2021 BlueStorm
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package codec
import (
"bytes"
"encoding/binary"
)
type Packer struct {
buffer bytes.Buffer
headLen int
bodyLen int
Datagram []byte
Receiver func(body []byte)
temp bytes.Buffer
tempLen int
tempHead bytes.Buffer
tempHeadLen int
offset int
addHead bool
}
func NewPacketFieldLength(lengthFieldLength int) *Packer {
return &Packer{headLen: lengthFieldLength}
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// size (2/4/8 bytes) | body (size bytes)
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
func (pk *Packer) PacketFieldFrameLength(body []byte) []byte {
pkgLen := len(body)
var headData = make([]byte, pk.headLen)
switch pk.headLen {
case 2:
binary.BigEndian.PutUint16(headData[:pk.headLen], uint16(pkgLen))
case 4:
binary.BigEndian.PutUint32(headData[:pk.headLen], uint32(pkgLen))
case 8:
binary.BigEndian.PutUint64(headData[:pk.headLen], uint64(pkgLen))
}
return headData
}
func (pk *Packer) PacketFieldLengthEncode(body []byte) (data []byte) {
pk.buffer.Write(pk.PacketFieldFrameLength(body))
pk.buffer.Write(body)
data = pk.buffer.Bytes()
pk.buffer.Reset()
return
}
func (pk *Packer) PacketFieldLengthDecode(body []byte) {
if pk.Receiver == nil {
return
}
pk.Datagram = body
if pk.tempLen > 0 {
count := len(pk.Datagram)
if pk.tempLen <= count {
pk.temp.Write(pk.Datagram[:pk.tempLen])
pk.copy(pk.temp.Bytes())
if pk.tempLen == count {
pk.reset(0)
return
}
pk.reset(pk.tempLen)
} else {
pk.temp.Write(pk.Datagram)
pk.tempLen = pk.tempLen - len(pk.Datagram)
return
}
}
if pk.bodyLen == 0 {
if pk.fieldHeadRead() {
return
}
}
if pk.bodyLen > 0 {
for {
size := len(pk.Datagram[pk.offset:])
if size < pk.bodyLen {
if size > 0 {
data := pk.Datagram[pk.offset : pk.offset+size]
pk.temp.Write(data)
}
pk.offset = 0
pk.tempLen = pk.bodyLen - size
pk.Datagram = pk.Datagram[:0]
return
} else {
data := pk.Datagram[pk.offset : pk.offset+pk.bodyLen]
pk.copy(data)
if pk.fieldHeadRead() {
pk.reset(0)
return
}
}
}
}
}
func (pk *Packer) fieldHeadRead() bool {
var data []byte
var next int
if pk.tempHeadLen > 0 {
pk.tempHead.Write(pk.Datagram[:pk.tempHeadLen])
data = pk.tempHead.Bytes()
pk.tempHead.Reset()
next = pk.tempHeadLen
pk.tempHeadLen = 0
} else {
offset := pk.offset + pk.bodyLen
count := len(pk.Datagram)
if offset >= count {
return true
}
next = offset + pk.headLen
if next > count {
pk.tempHead.Write(pk.Datagram[offset:])
pk.tempHeadLen = pk.headLen - pk.tempHead.Len()
return true
}
data = pk.Datagram[offset:next]
}
switch pk.headLen {
case 2:
pk.bodyLen = int(binary.BigEndian.Uint16(data))
case 4:
pk.bodyLen = int(binary.BigEndian.Uint32(data))
case 8:
pk.bodyLen = int(binary.BigEndian.Uint64(data))
default:
return true
}
pk.offset = next
return false
}
func (pk *Packer) copy(data []byte) {
body := make([]byte, pk.bodyLen)
copy(body, data)
go pk.Receiver(body)
}
func (pk *Packer) reset(offset int) {
pk.offset = offset
pk.bodyLen = 0
pk.tempLen = 0
pk.temp.Reset()
if pk.offset == 0 {
pk.Datagram = pk.Datagram[:0]
}
}