client.go | 12 +++++------- connection.go | 2 +- torrent.go | 2 +- diff --git a/client.go b/client.go index 2ae3b8321858160b9e7a460d119d5d5f4b698bb7..4902a1962457177fefef38bbd650e3dbf13344fd 100644 --- a/client.go +++ b/client.go @@ -161,7 +161,8 @@ } return t.Data.ReadAt(p, off) } -// Starts the client. Defaults are applied. The client will begin accepting connections and tracking. +// Starts the client. Defaults are applied. The client will begin accepting +// connections and tracking. func (c *Client) Start() { c.event.L = &c.mu c.torrents = make(map[InfoHash]*torrent) @@ -406,10 +407,7 @@ case peer_protocol.Request: if conn.PeerRequests == nil { conn.PeerRequests = make(map[request]struct{}, maxRequests) } - request := request{ - Index: msg.Index, - chunkSpec: chunkSpec{msg.Begin, msg.Length}, - } + request := newRequest(msg.Index, msg.Begin, msg.Length) conn.PeerRequests[request] = struct{}{} // TODO: Requests should be satisfied from a dedicated upload routine. p := make([]byte, msg.Length) @@ -455,7 +453,7 @@ } delete(conn.Requests, request_) err = me.downloadedChunk(torrent, msg) default: - log.Printf("received unknown message type: %#v", msg.Type) + err = fmt.Errorf("received unknown message type: %#v", msg.Type) } if err != nil { return err @@ -483,7 +481,7 @@ func (me *Client) addConnection(t *torrent, c *connection) bool { for _, c0 := range t.Conns { if c.PeerId == c0.PeerId { - log.Printf("%s and %s have the same ID: %s", c.Socket.RemoteAddr(), c0.Socket.RemoteAddr(), c.PeerId) + // Already connected to a client with that ID. return false } } diff --git a/connection.go b/connection.go index fada33f49448e6f98c01c76fd375df007d93f04a..5c153e19b810ff0ea9e679401480c827a151f1d9 100644 --- a/connection.go +++ b/connection.go @@ -66,7 +66,7 @@ func (c *connection) Request(chunk request) bool { if len(c.Requests) >= maxRequests { return false } - if !c.PeerPieces[chunk.Index] { + if !c.PeerHasPiece(chunk.Index) { return true } c.SetInterested(true) diff --git a/torrent.go b/torrent.go index 08bce0b8bda99c68e2dc28bb161db806b6e59831..4f94b89bc0bb842e78c2ba907f0eeba09cab2cf8 100644 --- a/torrent.go +++ b/torrent.go @@ -75,7 +75,7 @@ for i := range t.Pieces { slice.Pending = append(slice.Pending, t.PieceNumPendingBytes(peer_protocol.Integer(i))) slice.Indices = append(slice.Indices, peer_protocol.Integer(i)) } - sort.Sort(sort.Reverse(slice)) + sort.Sort(slice) return slice.Indices }