-
Notifications
You must be signed in to change notification settings - Fork 0
/
peer.go
149 lines (134 loc) · 2.54 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
144
145
146
147
148
149
package peer
import (
"errors"
"net"
"strconv"
"sync"
"time"
"github.com/wqsa/bget/bencode"
"github.com/wqsa/bget/common/bitmap"
)
const (
//PeerIDLen is length of peer id
PeerIDLen = 20
compressPeerLen = 6
stateInit = 0 + iota
stateReady
stateRunning
stateStop
)
var (
errBaseLen = errors.New("insufficient data for base length type")
errPeerFormat = errors.New("peer format error")
)
type peer struct {
id [PeerIDLen]byte
addr string
conn net.Conn
fastExtension bool
isInterest bool
isChoke bool
fastSet []int
state int
stateLock sync.RWMutex
lastActive time.Time
Bitmap *bitmap.Bitmap
closing chan struct{}
Download int64
Upload int64
haveReq bool
}
func newPeer(id []byte, addr string) *peer {
if id != nil && len(id) != PeerIDLen {
return nil
}
peer := &peer{addr: addr, state: stateInit, closing: make(chan struct{})}
if id != nil {
copy(peer.id[:], id)
}
return peer
}
func (p *peer) getState() int {
p.stateLock.RLock()
defer p.stateLock.RUnlock()
return p.state
}
func (p *peer) setState(state int) {
p.stateLock.Lock()
defer p.stateLock.Unlock()
p.state = state
return
}
func (p *peer) UnmarshalBencode(data []byte) error {
var content map[string]interface{}
err := bencode.Unmarshal(data, &content)
if err != nil {
return err
}
id, ok := content["peer id"].(string)
if !ok || len(id) != PeerIDLen {
return errPeerFormat
}
copy(p.id[:], []byte(id))
ip, ok := content["ip"].(string)
if !ok {
return errPeerFormat
}
//check wheather port vaild is?
port, ok := content["port"].(int)
if !ok {
return errPeerFormat
}
p.addr = net.JoinHostPort(ip, strconv.Itoa(port))
return nil
}
type messageWithID struct {
PeerID string
ID msgID
Data interface{}
}
func (p *peer) recvMessage(msgc chan<- *messageWithID) {
for {
m, err := p.readMessage()
if err != nil {
p.close()
return
}
if m.Length == 0 {
p.lastActive = time.Now()
continue
}
msg, err := p.dispatch(m)
if err != nil {
p.close()
return
}
select {
case msgc <- &msg:
case <-p.closing:
p.close()
return
}
}
}
func (p *peer) close() error {
p.stateLock.Lock()
defer p.stateLock.Unlock()
p.state = stateStop
err := p.conn.Close()
p.conn = nil
return err
}
func (p *peer) Close() error {
p.stateLock.RLock()
defer p.stateLock.RUnlock()
if p.state != stateRunning {
return nil
}
close(p.closing)
//p.closing <- struct{}{}
return nil
}
func isChoke(err error) bool {
return err == errChoke
}