-
Notifications
You must be signed in to change notification settings - Fork 47
/
packet.go
139 lines (110 loc) · 3.28 KB
/
packet.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
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT License was not distributed with this
// file, you can obtain one at https://opensource.org/licenses/MIT.
//
// Copyright (c) DUSK NETWORK. All rights reserved.
package rcudp
import (
"errors"
)
const (
messageIDFieldSize = 8
numSourceSymbolsFieldSize = 2
paddingSizeFieldSize = 2
transferLengthFieldSize = 4
blockIDFieldSize = 4
bcastHeightSize = 1
// BcastHeightPos is position in packet binary of height byte.
BcastHeightPos = 20
packetMinSize = messageIDFieldSize +
numSourceSymbolsFieldSize +
paddingSizeFieldSize +
transferLengthFieldSize +
blockIDFieldSize +
bcastHeightSize
)
// Packet is the UDP packet that consists of encoding symbol data unit and raptor-specific data.
type Packet struct {
messageID msgID
NumSourceSymbols uint16
PaddingSize uint16
transferLength uint32
// payload fields
blockID uint32
block []byte
bcastHeight byte
}
func newPacket(msgID []byte, numSourceSymbols uint16,
paddingSize uint16, transferLength uint32,
blockID uint32, block []byte, bcastHeight byte) Packet {
p := Packet{
NumSourceSymbols: numSourceSymbols,
PaddingSize: paddingSize,
transferLength: transferLength,
blockID: blockID,
bcastHeight: bcastHeight,
block: block,
}
copy(p.messageID[:], msgID)
return p
}
// marshal serializes a packet struct to a byte blob.
// Any mem alloc here would impact perf so bytes.Buffer is not used.
func (p *Packet) marshal() ([]byte, error) {
blob := make([]byte, 0, packetMinSize+len(p.block))
// source object id
blob = append(blob, p.messageID[:]...)
// NumSourceSymbols
b := make([]byte, numSourceSymbolsFieldSize)
byteOrder.PutUint16(b, p.NumSourceSymbols)
blob = append(blob, b...)
b[0] = 0
b[1] = 0
// PaddingSize
byteOrder.PutUint16(b, p.PaddingSize)
blob = append(blob, b...)
// transferLength
b = make([]byte, transferLengthFieldSize)
byteOrder.PutUint32(b, p.transferLength)
blob = append(blob, b...)
// blockID
b[0] = 0
b[1] = 0
b[2] = 0
b[3] = 0
byteOrder.PutUint32(b, p.blockID)
blob = append(blob, b...)
// BCAST_HEIGHT field
blob = append(blob, p.bcastHeight)
// block data
blob = append(blob, p.block[:]...)
if len(blob) > maxPacketLen {
return nil, ErrTooLargeUDP
}
return blob, nil
}
// unmarshal constructs a packet struct from a byte blob.
// Any mem alloc here would impact perf so bytes.Buffer is not used.
func (p *Packet) unmarshal(buf []byte) error {
if len(buf) < packetMinSize {
return errors.New("invalid packet size")
}
if len(buf) > maxPacketLen {
return ErrTooLargeUDP
}
offset := 0
copy(p.messageID[:], buf[offset:len(p.messageID)])
offset += len(p.messageID)
p.NumSourceSymbols = byteOrder.Uint16(buf[offset : offset+numSourceSymbolsFieldSize])
offset += numSourceSymbolsFieldSize
p.PaddingSize = byteOrder.Uint16(buf[offset : offset+paddingSizeFieldSize])
offset += paddingSizeFieldSize
p.transferLength = byteOrder.Uint32(buf[offset : offset+transferLengthFieldSize])
offset += transferLengthFieldSize
p.blockID = byteOrder.Uint32(buf[offset : offset+blockIDFieldSize])
offset += blockIDFieldSize
p.bcastHeight = buf[offset]
offset += bcastHeightSize
p.block = buf[offset:]
return nil
}