Prepare to support sending reject messages
This commit is contained in:
parent
9f2a7ec3e4
commit
738a75bc1c
@ -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
|
||||
}
|
||||
|
||||
|
9
misc.go
9
misc.go
@ -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}}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user