Record if a connection was incoming

This commit is contained in:
Matt Joiner 2014-07-01 00:04:28 +10:00
parent 147ad31135
commit 23cfe49ea4
2 changed files with 12 additions and 5 deletions

View File

@ -224,6 +224,7 @@ func (cl *Client) acceptConnections() {
conn, err := cl.Listener.Accept() conn, err := cl.Listener.Accept()
select { select {
case <-cl.quit: case <-cl.quit:
conn.Close()
return return
default: default:
} }
@ -231,6 +232,7 @@ func (cl *Client) acceptConnections() {
log.Print(err) log.Print(err)
return return
} }
log.Printf("accepted connection from %s", conn.RemoteAddr())
go func() { go func() {
if err := cl.runConnection(conn, nil); err != nil { if err := cl.runConnection(conn, nil); err != nil {
log.Print(err) log.Print(err)
@ -309,6 +311,7 @@ func (cl *Client) incomingPeerPort() int {
func (me *Client) runConnection(sock net.Conn, torrent *torrent) (err error) { func (me *Client) runConnection(sock net.Conn, torrent *torrent) (err error) {
conn := &connection{ conn := &connection{
Incoming: torrent == nil,
Socket: sock, Socket: sock,
Choked: true, Choked: true,
PeerChoked: true, PeerChoked: true,

View File

@ -16,6 +16,7 @@ import (
// Maintains the state of a connection with a peer. // Maintains the state of a connection with a peer.
type connection struct { type connection struct {
Socket net.Conn Socket net.Conn
Incoming bool
closed bool closed bool
mu sync.Mutex // Only for closing. mu sync.Mutex // Only for closing.
post chan peer_protocol.Message post chan peer_protocol.Message
@ -76,6 +77,9 @@ func (cn *connection) WriteStatus(w io.Writer) {
if !cn.Choked && !cn.PeerInterested { if !cn.Choked && !cn.PeerInterested {
c('?') c('?')
} }
if cn.Incoming {
c('I')
}
fmt.Fprintln(w) fmt.Fprintln(w)
} }