2
0
mirror of synced 2025-02-23 14:18:13 +00:00

Prepare to support sending reject messages

This commit is contained in:
Matt Joiner 2018-02-03 13:35:09 +11:00
parent 9f2a7ec3e4
commit 738a75bc1c
2 changed files with 32 additions and 1 deletions

View File

@ -824,13 +824,34 @@ func (c *connection) lastHelpful() (ret time.Time) {
return
}
func (c *connection) fastEnabled() bool {
return c.PeerExtensionBytes.SupportsFast() && c.t.cl.extensionBytes.SupportsFast()
}
// Returns true if we were able to reject the request.
func (c *connection) reject(r request) bool {
if !c.fastEnabled() {
return false
}
c.deleteRequest(r)
c.Post(r.ToMsg(pp.Reject))
return true
}
func (c *connection) onReadRequest(r request) error {
requestedChunkLengths.Add(strconv.FormatUint(r.Length.Uint64(), 10), 1)
if r.Begin+r.Length > c.t.pieceLength(int(r.Index)) {
return errors.New("bad request")
}
if _, ok := c.PeerRequests[r]; ok {
return nil
}
if c.Choked {
c.reject(r)
return nil
}
if len(c.PeerRequests) >= maxRequests {
// TODO: Should we drop them or Choke them instead?
c.reject(r)
return nil
}
if !c.t.havePiece(r.Index.Int()) {
@ -1254,6 +1275,7 @@ func (c *connection) deleteRequest(r request) bool {
}
delete(c.requests, r)
c.t.pendingRequests[r]--
c.updateRequests()
return true
}

View File

@ -17,6 +17,15 @@ type request struct {
chunkSpec
}
func (r request) ToMsg(mt pp.MessageType) pp.Message {
return pp.Message{
Type: mt,
Index: r.Index,
Begin: r.Begin,
Length: r.Length,
}
}
func newRequest(index, begin, length pp.Integer) request {
return request{index, chunkSpec{begin, length}}
}