-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacket.go
More file actions
66 lines (53 loc) · 1.42 KB
/
packet.go
File metadata and controls
66 lines (53 loc) · 1.42 KB
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
// Copyright 2020 @thiinbit. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file
package gosocket
import "hash/adler32"
const (
PacketVersion byte = 0x2A // 101010 -> 42 // Packet ver: 42 -> 43 -> 44 -> ...
PacketHeartbeatVersion byte = 0xFF // 11111111 -> 255 // Heartbeat ver: 255 -> 254- > 253 -> ...
)
const (
// Default max packet body length 4MB
defaultMaxPacketBodyLength = 4 * 1024 * 1024
)
type Packet struct {
// ==== Header start ====
// Version //=
ver byte ///////////////=
// Packet length //=
len uint32 //////////////=
// ==== Header end ======
// ==== Body start ====
// Packet body //=
body []byte //////////=
// ==== Body end ======
// ==== Checksum start ====
// Checksum //=
checksum uint32 //////////=
// ==== Checksum end ======
}
func NewPacket(ver byte, len uint32, packet []byte, checksum uint32) *Packet {
return &Packet{
ver: ver,
len: len,
body: packet,
checksum: checksum,
}
}
// Ver return the packet version
func (p *Packet) Ver() byte {
return p.ver
}
// Len return the packet body length
func (p *Packet) Len() uint32 {
return p.len
}
// Body return the packet body
func (p *Packet) Body() []byte {
return p.body
}
// Checksum return checksum is success
func (p *Packet) Checksum() bool {
return p.checksum == adler32.Checksum(p.body)
}