From ec77cb0ece8f9c1d77d4d1f6f5eccffe4d965451 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Wed, 21 May 2014 17:42:06 +1000 Subject: [PATCH] Misc minor improvements --- client.go | 12 +++++------- connection.go | 2 +- torrent.go | 2 +- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/client.go b/client.go index 2ae3b832..4902a196 100644 --- a/client.go +++ b/client.go @@ -161,7 +161,8 @@ func (cl *Client) TorrentReadAt(ih InfoHash, off int64, p []byte) (n int, err er 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 @@ func (me *Client) connectionLoop(torrent *torrent, conn *connection) error { 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 @@ func (me *Client) connectionLoop(torrent *torrent, conn *connection) error { 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) dropConnection(torrent *torrent, conn *connection) { 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 fada33f4..5c153e19 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 08bce0b8..4f94b89b 100644 --- a/torrent.go +++ b/torrent.go @@ -75,7 +75,7 @@ func (t *torrent) piecesByPendingBytes() (indices []peer_protocol.Integer) { 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 } -- 2.48.1