-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol.go
73 lines (62 loc) · 1.62 KB
/
protocol.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
package protocol
import (
"fmt"
)
// Opcode identifies the type of message
type Opcode uint8
// From the RFC:
// +----------+------------------+
// | Msg Type | Description |
// +----------+------------------+
// | 0 | HANDSHAKE |
// | 1 | DATA |
// | 2 | ACK |
// | 3 | HAVE |
// | 4 | INTEGRITY |
// | 5 | PEX_RESv4 |
// | 6 | PEX_REQ |
// | 7 | SIGNED_INTEGRITY |
// | 8 | REQUEST |
// | 9 | CANCEL |
// | 10 | CHOKE |
// | 11 | UNCHOKE |
// | 12 | PEX_RESv6 |
// | 13 | PEX_REScert |
// | 14-254 | Unassigned |
// | 255 | Reserved |
// +----------+------------------+
const (
Handshake Opcode = 0
Data Opcode = 1
Have Opcode = 3
Request Opcode = 8
Error Opcode = 255
)
const (
FileTransferProtocol = "/p2p/files/0.0.0"
HandshakeProtocol = "/p2p/handshake/0.0.0"
)
// PeerID identifies a peer
// To avoid cycle Import
type PeerID interface {
String() string
}
type Protocol interface {
HandleDatagram(d *Datagram, id PeerID)
SetDatagramSender(f func(Datagram, PeerID) error)
}
// Datagram holds a protocol datagram
type Datagram struct {
Msgs []Msg
}
func NewDataGram(m ...Msg) *Datagram {
return &Datagram{Msgs: m}
}
// MsgError is an error that happens while handling an incoming message
type MsgError struct {
m Msg
info string
}
func (e MsgError) Error() string {
return fmt.Sprintf("message error : %v\n%v", e.info, e.m)
}