Make the discovery source for a connection more descriptive

This commit is contained in:
Matt Joiner 2014-07-16 17:06:18 +10:00
parent 9e8a72ff1d
commit 0d27ae7fd1
3 changed files with 31 additions and 20 deletions

View File

@ -239,7 +239,7 @@ func (cl *Client) acceptConnections() {
}
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)
}
}()
@ -290,8 +290,8 @@ func (me *Client) initiateConn(peer Peer, torrent *torrent) {
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)
}
@ -314,9 +314,9 @@ func (cl *Client) incomingPeerPort() int {
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,
@ -704,8 +704,9 @@ func (me *Client) connectionLoop(t *torrent, c *connection) error {
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)
@ -931,8 +932,9 @@ func (cl *Client) announceTorrentDHT(t *torrent) {
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[:],

View File

@ -13,14 +13,22 @@ import (
"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
@ -86,8 +94,8 @@ func (cn *connection) WriteStatus(w io.Writer) {
if !cn.Choked && !cn.PeerInterested {
c('?')
}
if cn.Incoming {
c('I')
if cn.Discovery != 0 {
c(byte(cn.Discovery))
}
fmt.Fprintln(w)
}

View File

@ -347,9 +347,10 @@ func (t *torrent) pendAllChunkSpecs(index pp.Integer) {
}
type Peer struct {
Id [20]byte
IP net.IP
Port int
Id [20]byte
IP net.IP
Port int
Source peerSource
}
func (t *torrent) PieceLength(piece pp.Integer) (len_ pp.Integer) {