From: Matt Joiner <anacrolix@gmail.com>
Date: Wed, 21 May 2014 07:49:28 +0000 (+1000)
Subject: Implement connection.{Cancel,Choke}
X-Git-Tag: v1.0.0~1742
X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=309f70bcb54ca3790525a12d726705241ae6622d;p=btrtrc.git

Implement connection.{Cancel,Choke}
---

diff --git a/client.go b/client.go
index 3ca57e10..e697884e 100644
--- a/client.go
+++ b/client.go
@@ -401,6 +401,7 @@ func (me *Client) connectionLoop(torrent *torrent, conn *connection) error {
 			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 13652087..96a1d215 100644
--- a/connection.go
+++ b/connection.go
@@ -89,6 +89,24 @@ func (c *connection) Request(chunk request) bool {
 	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 {
@@ -101,6 +119,16 @@ func (c *connection) PeerCancel(r request) bool {
 	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() {
 	if !c.Choked {
 		return