-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpeer.go
143 lines (114 loc) · 3.44 KB
/
peer.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
package gotorrent
import (
log "code.google.com/p/tcgl/applog"
"github.com/moretti/gotorrent/bitarray"
"github.com/moretti/gotorrent/messages"
"net"
)
const (
PeerMaxRequests = 16
)
type Peer struct {
connection *PeerConnection
torrent *Torrent
bitField *bitarray.BitArray
amInterested bool
IsChoked bool
RequestsCount int
}
func NewPeer(
addr net.TCPAddr,
torrent *Torrent,
peerErrors chan<- PeerError,
outMessages chan<- PeerMessage,
) *Peer {
p := new(Peer)
p.torrent = torrent
p.connection = NewPeerConnection(addr, peerErrors, outMessages)
p.bitField = bitarray.New(p.torrent.PieceCount)
p.IsChoked = true
p.amInterested = false
return p
}
func (p *Peer) String() string {
return p.connection.addr.String()
}
func (p *Peer) Connect() {
go func() {
p.connection.Connect()
p.SendHandshake(p.torrent.InfoHash, string(p.torrent.ClientId))
p.SendUnchoke()
//p.SendInterested()
}()
}
func (p *Peer) SendUnchoke() {
log.Debugf("Peer %v - Sending unchoke", p.String())
p.connection.SendMessage(messages.NewUnchoke())
}
func (p *Peer) SendInterested(interested bool) {
if interested {
log.Debugf("Peer %v - Sending interested", p.String())
p.connection.SendMessage(messages.NewInterested())
} else {
log.Debugf("Peer %v - Sending not interested", p.String())
p.connection.SendMessage(messages.NewNotInterested())
}
}
func (p *Peer) SendHandshake(infoHash, peerId string) {
p.connection.SendMessage(messages.NewHandshake(infoHash, peerId))
}
func (p *Peer) SendRequest(pieceIndex, blockOffset, blockLength int) {
p.connection.SendMessage(messages.NewRequest(uint32(pieceIndex), uint32(blockOffset), uint32(blockLength)))
}
func (p *Peer) SetKeepAlive() {
}
func (p *Peer) SetHave(message messages.Message) {
haveMsg, err := message.ToHave()
if err != nil {
log.Errorf("Peer %v - Unable to parse the have message: %v", p.String(), err)
return
}
if int(haveMsg.PieceIndex) >= p.bitField.Len() {
log.Errorf("Peer %v - Have message, invalid piece index: %v, piece count: %v", p.String(), haveMsg.PieceIndex, p.bitField.Len())
return
}
log.Debugf("Peer %v - Have piece #%v", p.String(), haveMsg.PieceIndex)
p.bitField.Set(int(haveMsg.PieceIndex))
}
func (p *Peer) SetBitField(message messages.Message) {
bitMsg, err := message.ToBitArray()
if err != nil {
log.Errorf("Peer %v - Unable to parse the bitarray message: %v", p.String(), err)
return
}
pieceCount := p.torrent.PieceCount
if bitCount := (bitMsg.Header.Length - 1) * 8; pieceCount > int(bitCount) {
log.Errorf("Peer %v - Invalid bitfield, bit count: %v, piece count: %v", p.String(), bitCount, pieceCount)
return
}
p.bitField = bitarray.NewFromBytes(bitMsg.BitField, pieceCount)
log.Debugf("Peer %v - New BitField:", p.String())
}
func (p *Peer) BitField() *bitarray.BitArray {
return p.bitField
}
func (p *Peer) RequestPiece(piece *Piece) {
for {
block := piece.NextBlock()
if block == nil {
break
}
p.SendRequest(piece.Index(), block.Begin, block.Length)
}
}
func (p *Peer) AmInterested(activePieces, completedPieces *bitarray.BitArray) (interested bool, pieces []int) {
// pieces = peerHas ^ (peerHas & (active | completed))
pieces = p.BitField().Xor(p.bitField.And(activePieces.Or(completedPieces))).SetIndices()
interested = len(pieces) > 0
if interested != p.amInterested {
p.amInterested = interested
p.SendInterested(interested)
}
//log.Debugf("Peer %v - Piece indices that I don't have: %v", p.String(), pieces)
return
}