From: Matt Joiner Date: Fri, 2 Feb 2018 02:26:04 +0000 (+1100) Subject: Move sendChunk onto connection X-Git-Tag: v1.0.0~233 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=97ffe39561dcf0a1aed204983a3f9270b5f27080;p=btrtrc.git Move sendChunk onto connection --- diff --git a/client.go b/client.go index ac133047..54a8b6ef 100644 --- a/client.go +++ b/client.go @@ -968,31 +968,6 @@ func (cl *Client) gotMetadataExtensionMsg(payload []byte, t *Torrent, c *connect } } -func (cl *Client) sendChunk(t *Torrent, c *connection, r request, msg func(pp.Message) bool) (more bool, err error) { - // Count the chunk being sent, even if it isn't. - b := make([]byte, r.Length) - p := t.info.Piece(int(r.Index)) - n, err := t.readAt(b, p.Offset()+int64(r.Begin)) - if n != len(b) { - if err == nil { - panic("expected error") - } - return - } else if err == io.EOF { - err = nil - } - more = msg(pp.Message{ - Type: pp.Piece, - Index: r.Index, - Begin: r.Begin, - Piece: b, - }) - c.chunksSent++ - uploadChunksPosted.Add(1) - c.lastChunkSent = time.Now() - return -} - func (cl *Client) openNewConns(t *Torrent) { defer t.updateWantPeersEvent() for len(t.peers) != 0 { diff --git a/connection.go b/connection.go index 4147cd76..426e202f 100644 --- a/connection.go +++ b/connection.go @@ -1247,3 +1247,28 @@ func (c *connection) postCancel(r request) bool { c.Post(makeCancelMessage(r)) return true } + +func (c *connection) sendChunk(r request, msg func(pp.Message) bool) (more bool, err error) { + // Count the chunk being sent, even if it isn't. + b := make([]byte, r.Length) + p := c.t.info.Piece(int(r.Index)) + n, err := c.t.readAt(b, p.Offset()+int64(r.Begin)) + if n != len(b) { + if err == nil { + panic("expected error") + } + return + } else if err == io.EOF { + err = nil + } + more = msg(pp.Message{ + Type: pp.Piece, + Index: r.Index, + Begin: r.Begin, + Piece: b, + }) + c.chunksSent++ + uploadChunksPosted.Add(1) + c.lastChunkSent = time.Now() + return +}