}
log.Printf("accepted connection from %s", conn.RemoteAddr())
go func() {
- if err := cl.runConnection(conn, nil); err != nil {
+ if err := cl.runConnection(conn, nil, peerSourceIncoming); err != nil {
log.Print(err)
}
}()
log.Printf("error connecting to peer: %s %#v", err, err)
return
}
- log.Printf("connected to %s", conn.RemoteAddr())
- err = me.runConnection(conn, torrent)
+ // log.Printf("connected to %s", conn.RemoteAddr())
+ err = me.runConnection(conn, torrent, peer.Source)
if err != nil {
log.Print(err)
}
return i
}
-func (me *Client) runConnection(sock net.Conn, torrent *torrent) (err error) {
+func (me *Client) runConnection(sock net.Conn, torrent *torrent, discovery peerSource) (err error) {
conn := &connection{
- Incoming: torrent == nil,
+ Discovery: discovery,
Socket: sock,
Choked: true,
PeerChoked: true,
err := me.AddPeers(t.InfoHash, func() (ret []Peer) {
for _, cp := range pexMsg.Added {
p := Peer{
- IP: make([]byte, 4),
- Port: int(cp.Port),
+ IP: make([]byte, 4),
+ Port: int(cp.Port),
+ Source: peerSourcePEX,
}
if n := copy(p.IP, cp.IP[:]); n != 4 {
panic(n)
err = cl.AddPeers(t.InfoHash, func() (ret []Peer) {
for _, cp := range cps {
ret = append(ret, Peer{
- IP: cp.IP[:],
- Port: int(cp.Port),
+ IP: cp.IP[:],
+ Port: int(cp.Port),
+ Source: peerSourceDHT,
})
log.Printf("peer from dht: %s", &net.UDPAddr{
IP: cp.IP[:],
"bitbucket.org/anacrolix/go.torrent/peer_protocol"
)
+type peerSource byte
+
+const (
+ peerSourceIncoming = 'I'
+ peerSourceDHT = 'H'
+ peerSourcePEX = 'X'
+)
+
// Maintains the state of a connection with a peer.
type connection struct {
- Socket net.Conn
- Incoming bool
- closed bool
- mu sync.Mutex // Only for closing.
- post chan peer_protocol.Message
- write chan []byte
+ Socket net.Conn
+ Discovery peerSource
+ closed bool
+ mu sync.Mutex // Only for closing.
+ post chan peer_protocol.Message
+ write chan []byte
// Stuff controlled by the local peer.
Interested bool
if !cn.Choked && !cn.PeerInterested {
c('?')
}
- if cn.Incoming {
- c('I')
+ if cn.Discovery != 0 {
+ c(byte(cn.Discovery))
}
fmt.Fprintln(w)
}