-
Notifications
You must be signed in to change notification settings - Fork 75
/
messages.go
146 lines (110 loc) · 4.19 KB
/
messages.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
140
141
142
143
144
145
146
package peerprotocol
import (
"encoding/binary"
"io"
)
// Message is a Peer message of BitTorrent protocol.
type Message interface {
io.Reader
ID() MessageID
}
// HaveMessage indicates a peer has the piece with index.
type HaveMessage struct {
Index uint32
}
// ID returns the peer protocol message type.
func (m HaveMessage) ID() MessageID { return Have }
func (m HaveMessage) Read(b []byte) (int, error) {
binary.BigEndian.PutUint32(b[0:4], m.Index)
return 4, io.EOF
}
// RequestMessage is sent when a peer needs a certain piece.
type RequestMessage struct {
Index, Begin, Length uint32
}
// ID returns the peer protocol message type.
func (m RequestMessage) ID() MessageID { return Request }
// Read message data into buffer b.
func (m RequestMessage) Read(b []byte) (int, error) {
binary.BigEndian.PutUint32(b[0:4], m.Index)
binary.BigEndian.PutUint32(b[4:8], m.Begin)
binary.BigEndian.PutUint32(b[8:12], m.Length)
return 12, io.EOF
}
// PieceMessage is sent when a peer wants to upload piece data.
type PieceMessage struct {
Index, Begin uint32
}
// ID returns the peer protocol message type.
func (m PieceMessage) ID() MessageID { return Piece }
// Read message data into buffer b.
func (m PieceMessage) Read(b []byte) (int, error) {
binary.BigEndian.PutUint32(b[0:4], m.Index)
binary.BigEndian.PutUint32(b[4:8], m.Begin)
return 8, io.EOF
}
// BitfieldMessage sent after the peer handshake to exchange piece availability information between peers.
type BitfieldMessage struct {
Data []byte
pos int
}
// ID returns the peer protocol message type.
func (m BitfieldMessage) ID() MessageID { return Bitfield }
// Read message data into buffer b.
func (m *BitfieldMessage) Read(b []byte) (n int, err error) {
n = copy(b, m.Data[m.pos:])
m.pos += n
if m.pos == len(m.Data) {
err = io.EOF
}
return
}
// PortMessage is sent to announce the UDP port number of DHT node run by the peer.
type PortMessage struct {
Port uint16
}
// ID returns the peer protocol message type.
func (m PortMessage) ID() MessageID { return Port }
// Read message data into buffer b.
func (m PortMessage) Read(b []byte) (n int, err error) {
binary.BigEndian.PutUint16(b[0:2], m.Port)
return 2, io.EOF
}
type emptyMessage struct{}
func (m emptyMessage) Read(b []byte) (int, error) {
return 0, io.EOF
}
// AllowedFastMessage is sent to tell a peer that it can download pieces regardless of choking status.
type AllowedFastMessage struct{ HaveMessage }
// ChokeMessage is sent to peer that it should not request pieces.
type ChokeMessage struct{ emptyMessage }
// UnchokeMessage is sent to peer that it can request pieces.
type UnchokeMessage struct{ emptyMessage }
// InterestedMessage is sent to peer that we want to request pieces if you unchoke us.
type InterestedMessage struct{ emptyMessage }
// NotInterestedMessage is sent to peer that we don't want any peer from you.
type NotInterestedMessage struct{ emptyMessage }
// HaveAllMessage can be sent to peer to indicate that we are a seed for this torrent.
type HaveAllMessage struct{ emptyMessage }
// HaveNoneMessage is sent to peer to tell that we don't have any pieces.
type HaveNoneMessage struct{ emptyMessage }
// RejectMessage is sent to peer to tell that we are rejecting a request from you.
type RejectMessage struct{ RequestMessage }
// CancelMessage is sent to peer to cancel previosly sent request.
type CancelMessage struct{ RequestMessage }
// ID returns the peer protocol message type.
func (m ChokeMessage) ID() MessageID { return Choke }
// ID returns the peer protocol message type.
func (m UnchokeMessage) ID() MessageID { return Unchoke }
// ID returns the peer protocol message type.
func (m InterestedMessage) ID() MessageID { return Interested }
// ID returns the peer protocol message type.
func (m NotInterestedMessage) ID() MessageID { return NotInterested }
// ID returns the peer protocol message type.
func (m HaveAllMessage) ID() MessageID { return HaveAll }
// ID returns the peer protocol message type.
func (m HaveNoneMessage) ID() MessageID { return HaveNone }
// ID returns the peer protocol message type.
func (m RejectMessage) ID() MessageID { return Reject }
// ID returns the peer protocol message type.
func (m CancelMessage) ID() MessageID { return Cancel }