client.go | 1 + connection.go | 28 ++++++++++++++++++++++++++++ diff --git a/client.go b/client.go index 3ca57e10a8f3ae12446a871b723d0c13dd6f38cb..e697884e81a192c5c2f5ec0fc358948743abb7e4 100644 --- a/client.go +++ b/client.go @@ -401,6 +401,7 @@ // TODO: This should be done from a dedicated unchoking routine. conn.Unchoke() case peer_protocol.NotInterested: conn.PeerInterested = false + conn.Choke() case peer_protocol.Have: me.peerGotPiece(torrent, conn, int(msg.Index)) case peer_protocol.Request: diff --git a/connection.go b/connection.go index 136520874c83e11648b2895572aa1db712e841d8..96a1d2154d9676370fe45af843a9f33d82ccaf9b 100644 --- a/connection.go +++ b/connection.go @@ -90,6 +90,24 @@ return true } // Returns true if an unsatisfied request was canceled. +func (c *connection) Cancel(r request) bool { + if c.Requests == nil { + return false + } + if _, ok := c.Requests[r]; !ok { + return false + } + delete(c.Requests, r) + c.Post(peer_protocol.Message{ + Type: peer_protocol.Cancel, + Index: r.Index, + Begin: r.Begin, + Length: r.Length, + }) + return true +} + +// Returns true if an unsatisfied request was canceled. func (c *connection) PeerCancel(r request) bool { if c.PeerRequests == nil { return false @@ -99,6 +117,16 @@ return false } delete(c.PeerRequests, r) return true +} + +func (c *connection) Choke() { + if c.Choked { + return + } + c.Post(peer_protocol.Message{ + Type: peer_protocol.Choke, + }) + c.Choked = true } func (c *connection) Unchoke() {