-
Notifications
You must be signed in to change notification settings - Fork 11
/
peer.go
117 lines (102 loc) · 2.21 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
package libtorrent
import (
"github.com/torrance/libtorrent/bitfield"
"io"
"sync"
//"testing/iotest"
)
type peer struct {
name string
conn io.ReadWriter
write chan binaryDumper
read chan peerDouble
amChoking bool
amInterested bool
peerChoking bool
peerInterested bool
mutex sync.RWMutex
bitf *bitfield.Bitfield
}
type peerDouble struct {
msg interface{}
peer *peer
}
func newPeer(name string, conn io.ReadWriter, readChan chan peerDouble) (p *peer) {
p = &peer{
name: name,
conn: conn,
write: make(chan binaryDumper, 10),
read: readChan,
amChoking: true,
amInterested: false,
peerChoking: true,
peerInterested: false,
}
// Write loop
go func() {
for {
//conn := iotest.NewWriteLogger("Writing", conn)
// TODO: send regular keep alive requests
msg := <-p.write
if err := msg.BinaryDump(conn); err != nil {
// TODO: Close peer
logger.Error("%s Received error writing to connection: %s", p.name, err)
return
}
}
}()
// Read loop
go func() {
for {
//conn := iotest.NewReadLogger("Reading", conn)
msg, err := parsePeerMessage(conn)
if _, ok := err.(unknownMessage); ok {
// Log unknown messages and then ignore
logger.Info(err.Error())
} else if err != nil {
// TODO: Close peer
logger.Debug("%s Received error reading connection: %s", p.name, err)
break
}
readChan <- peerDouble{msg: msg, peer: p}
}
}()
return
}
func (p *peer) GetAmChoking() (b bool) {
p.mutex.RLock()
b = p.amChoking
p.mutex.RUnlock()
return
}
func (p *peer) SetAmChoking(b bool) {
p.mutex.Lock()
p.amChoking = b
p.mutex.Unlock()
}
func (p *peer) SetPeerChoking(b bool) {
p.mutex.Lock()
p.peerChoking = b
p.mutex.Unlock()
}
func (p *peer) GetPeerInterested() (b bool) {
p.mutex.RLock()
b = p.peerInterested
p.mutex.RUnlock()
return
}
func (p *peer) SetPeerInterested(b bool) {
p.mutex.Lock()
p.peerInterested = b
p.mutex.Unlock()
}
func (p *peer) SetBitfield(bitf *bitfield.Bitfield) {
p.mutex.Lock()
p.bitf = bitf
p.mutex.Unlock()
}
func (p *peer) HasPiece(index int) {
p.mutex.Lock()
p.bitf.SetTrue(index)
p.mutex.Unlock()
}