forked from gansidui/gotcp
/
streamUtils.go
executable file
·171 lines (136 loc) · 3.88 KB
/
streamUtils.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
//通讯协议处理,主要处理封包和解包的过程
package gotcp
import (
"bytes"
"encoding/binary"
//"fmt"
"hash/adler32"
//"runtime"
"fmt"
"strings"
)
const (
ConstHeader = "SeanCommu"
ConstHeaderLength int32 = 9
ConstSaveDataLength int32 = 4
ConstEndCheckECCLength int32 = 4
TempToken = "e675d1b7e23c8a9ca3a3479d6cf29232"
)
const (
ConstPackNoHeader = 0
ConstPackHasHeader = 1
)
var (
num = make(chan int, 100)
)
func init() {
go func() {
for i := 0; ; i++ {
num <- i
}
}()
}
type StreamPacket struct {
buff []byte
}
func (this *StreamPacket) Serialize() []byte {
return this.buff
}
func (this *StreamPacket) GetLength() uint32 {
return uint32(len(this.buff))
}
func (this *StreamPacket) GetBody() []byte {
return this.buff
}
func NewStreamPacket(buff []byte) *StreamPacket {
p := &StreamPacket{}
p.buff = buff
return p
}
func getUniqName() string {
return fmt.Sprintf("%s%d", "USER", GetUniq())
}
func GetUniq() int {
return <-num
}
func CheckECC(data []byte) uint32 {
if len(data) > 200 {
tmpdata := append(data[:100], data[len(data)-100:]...)
return adler32.Checksum(tmpdata)
} else {
return adler32.Checksum(data)
}
}
//封包
func DoPacket(message []byte) []byte {
data := append(append([]byte(ConstHeader), IntToBytes(int32(len(message)))...), message...)
return append(data, UInt32ToBytes(CheckECC(data))...)
}
//解包
func Unpack(buffer []byte, readerChannel chan Packet) (leftbuffer []byte) {
length := int32(len(buffer))
var i int32
for i = 0; i < length; i = i + 1 {
//如果当前缓冲区中的数据小于包头长度 ,不需要继续向下寻找包头了,寻找就越界了
if length < i+ConstHeaderLength+ConstSaveDataLength {
break
}
//if bytes.Contains(buffer[i:i+ConstHeaderLength], []byte(ConstHeader)) {
if strings.EqualFold(string(buffer[i:i+ConstHeaderLength]), ConstHeader) { //string(buffer[i:i+ConstHeaderLength]) == []byte(ConstHeader) {
//token = string(buffer[i+ConstHeaderLength+ConstTokenSizeLength : i+ConstHeaderLength+ConstTokenSizeLength+ConstTokenLength])
//fmt.Println(TokenStr)
//TokenStr
messageLength := BytesToInt(buffer[i+ConstHeaderLength : i+ConstHeaderLength+ConstSaveDataLength])
if length < i+ConstHeaderLength+ConstSaveDataLength+messageLength+ConstEndCheckECCLength {
break
}
data := buffer[i+ConstHeaderLength+ConstSaveDataLength : i+ConstHeaderLength+ConstSaveDataLength+messageLength]
ECCSourceData := buffer[i : i+ConstHeaderLength+ConstSaveDataLength+messageLength]
ECCSum := BytesTouInt32(buffer[i+ConstHeaderLength+ConstSaveDataLength+messageLength : i+ConstHeaderLength+ConstSaveDataLength+messageLength+ConstEndCheckECCLength])
ECCCalcSum := CheckECC(ECCSourceData)
if ECCSum != ECCCalcSum {
i += i + ConstHeaderLength - 1
continue
}
result := make([]byte, len(data))
copy(result, data)
pack := NewStreamPacket(data)
readerChannel <- pack
i += ConstHeaderLength + ConstSaveDataLength + messageLength + ConstEndCheckECCLength - 1
}
}
if i == length {
//return buffer, ConstPackNoHeader
return make([]byte, 0)
}
//runtime.GC()
return buffer[i:]
}
//整形转换成字节
func IntToBytes(n int32) []byte {
x := int32(n)
bytesBuffer := bytes.NewBuffer([]byte{})
binary.Write(bytesBuffer, binary.LittleEndian, x)
return bytesBuffer.Bytes()
}
//整形转换成字节
func UInt32ToBytes(n uint32) []byte {
x := uint32(n)
bytesBuffer := bytes.NewBuffer([]byte{})
binary.Write(bytesBuffer, binary.LittleEndian, x)
return bytesBuffer.Bytes()
}
//字节转换成整形
func BytesTouInt32(b []byte) uint32 {
bytesBuffer := bytes.NewBuffer(b)
var x uint32
binary.Read(bytesBuffer, binary.LittleEndian, &x)
return uint32(x)
}
//字节转换成整形
func BytesToInt(b []byte) int32 {
bytesBuffer := bytes.NewBuffer(b)
var x int32
binary.Read(bytesBuffer, binary.LittleEndian, &x)
return int32(x)
}