/
message.go
60 lines (47 loc) · 1 KB
/
message.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
package discover
import (
"github.com/vmihailenco/msgpack"
"net"
)
const (
macSize = 256 / 8
sigSize = 520 / 8
headSize = macSize + sigSize // space of packet frame data
)
var (
headSpace = make([]byte, headSize)
// Neighbors replies are sent across multiple packets to
// stay below the 1280 byte limit. We compute the maximum number
// of entries by stuffing a packet until it grows too large.
maxNeighbors int
)
type Tx struct {
_msgpack struct{} `msgpack:",omitempty"`
Type byte
SignMsg []byte
ID []byte
netType NetType
fromAddr *net.UDPAddr
fromID NodeID
data []byte
}
func (tx *Tx) Encode() ([]byte, error) {
pk, err := msgpack.Marshal(tx)
if err != nil {
return nil, err
}
return pk, nil
//return append(pk, []byte(Delimiter)...), nil
}
func DecodeTx(data []byte) (*Tx, error) {
tx := &Tx{}
err := msgpack.Unmarshal(data, tx)
if err != nil {
return nil, err
}
tx.Event.Verify()
return tx, nil
}
func (tx *Tx) String() string {
return tx.Event.String()
}