-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacket.go
More file actions
113 lines (92 loc) · 2.52 KB
/
packet.go
File metadata and controls
113 lines (92 loc) · 2.52 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
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
package marina
import (
"io"
"sync"
"github.com/lithdew/bytesutil"
"github.com/lithdew/kademlia"
)
type MessagePacket struct {
mu sync.Mutex
pubKadId *kademlia.ID // the publish-peer-node KadId
brkKadId *kademlia.ID // the broker-peer-node KadId
subKadId *kademlia.ID // the subscribe-peer-node KadId
mid uint32 // the number of the message-packet by the creator
qos byte
topic []byte
payLoad []byte
}
func NewMessagePacket(pubKadId *kademlia.ID, mid uint32, qos byte, topic []byte, payLoad []byte) *MessagePacket {
return theMessagePacketPool.acquire(pubKadId, mid, qos, topic, payLoad)
}
func (mp *MessagePacket) SetBrokerKadId(kadId *kademlia.ID) {
mp.mu.Lock()
defer mp.mu.Unlock()
mp.brkKadId = kadId
}
func (mp *MessagePacket) SetSubscriberKadId(kadId *kademlia.ID) {
mp.mu.Lock()
defer mp.mu.Unlock()
mp.subKadId = kadId
}
func (mp *MessagePacket) AppendTo(dst []byte) []byte {
mp.mu.Lock()
defer mp.mu.Unlock()
dst = bytesutil.AppendUint32BE(dst, mp.mid)
dst = append(dst, mp.qos)
dst = bytesutil.AppendUint16BE(dst, uint16(len(mp.topic)))
dst = append(dst, mp.topic...)
dst = bytesutil.AppendUint16BE(dst, uint16(len(mp.payLoad)))
dst = append(dst, mp.payLoad...)
dst = mp.pubKadId.AppendTo(dst)
dst = mp.brkKadId.AppendTo(dst)
dst = mp.subKadId.AppendTo(dst)
return dst
}
func UnmarshalMessagePacket(buf []byte) (*MessagePacket, error) {
var err error
var size uint16
var mid uint32
var qos byte
var topic, payLoad []byte
var pubKadId, brkKadId, subKadId kademlia.ID
if len(buf) < 5 {
return nil, io.ErrUnexpectedEOF
}
mid, buf = bytesutil.Uint32BE(buf[:4]), buf[4:]
qos, buf = buf[0], buf[1:]
if len(buf) < 2 {
return nil, io.ErrUnexpectedEOF
}
size, buf = bytesutil.Uint16BE(buf[:2]), buf[2:]
if uint16(len(buf)) < size {
return nil, io.ErrUnexpectedEOF
}
topic, buf = buf[:size], buf[size:]
if len(buf) < 2 {
return nil, io.ErrUnexpectedEOF
}
size, buf = bytesutil.Uint16BE(buf[:2]), buf[2:]
if uint16(len(buf)) < size {
return nil, io.ErrUnexpectedEOF
}
payLoad, buf = buf[:size], buf[size:]
pubKadId, buf, err = kademlia.UnmarshalID(buf)
if err != nil {
return nil, err
}
brkKadId, buf, err = kademlia.UnmarshalID(buf)
if err != nil {
return nil, err
}
subKadId, buf, err = kademlia.UnmarshalID(buf)
if err != nil {
return nil, err
}
pkt := NewMessagePacket(&pubKadId, mid, qos, topic, payLoad)
pkt.SetBrokerKadId(&brkKadId)
pkt.SetSubscriberKadId(&subKadId)
return pkt, nil
}
func (mp *MessagePacket) Release() {
theMessagePacketPool.release(mp)
}