Make Request private
This commit is contained in:
parent
036fd126e8
commit
0dee5d9952
36
client.go
36
client.go
|
@ -58,7 +58,7 @@ func (me *Client) PrioritizeDataRegion(ih InfoHash, off, len_ int64) error {
|
||||||
if t == nil {
|
if t == nil {
|
||||||
return errors.New("no such active torrent")
|
return errors.New("no such active torrent")
|
||||||
}
|
}
|
||||||
newPriorities := make([]Request, 0, (len_+chunkSize-1)/chunkSize)
|
newPriorities := make([]request, 0, (len_+chunkSize-1)/chunkSize)
|
||||||
for len_ > 0 {
|
for len_ > 0 {
|
||||||
req, ok := t.offsetRequest(off)
|
req, ok := t.offsetRequest(off)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -93,7 +93,7 @@ func (me *Client) PrioritizeDataRegion(ih InfoHash, off, len_ int64) error {
|
||||||
|
|
||||||
type dataSpec struct {
|
type dataSpec struct {
|
||||||
InfoHash
|
InfoHash
|
||||||
Request
|
request
|
||||||
}
|
}
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
|
@ -403,9 +403,9 @@ func (me *Client) connectionLoop(torrent *torrent, conn *connection) error {
|
||||||
me.peerGotPiece(torrent, conn, int(msg.Index))
|
me.peerGotPiece(torrent, conn, int(msg.Index))
|
||||||
case peer_protocol.Request:
|
case peer_protocol.Request:
|
||||||
if conn.PeerRequests == nil {
|
if conn.PeerRequests == nil {
|
||||||
conn.PeerRequests = make(map[Request]struct{}, maxRequests)
|
conn.PeerRequests = make(map[request]struct{}, maxRequests)
|
||||||
}
|
}
|
||||||
request := Request{
|
request := request{
|
||||||
Index: msg.Index,
|
Index: msg.Index,
|
||||||
chunkSpec: chunkSpec{msg.Begin, msg.Length},
|
chunkSpec: chunkSpec{msg.Begin, msg.Length},
|
||||||
}
|
}
|
||||||
|
@ -446,7 +446,7 @@ func (me *Client) connectionLoop(torrent *torrent, conn *connection) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case peer_protocol.Piece:
|
case peer_protocol.Piece:
|
||||||
request_ := Request{msg.Index, chunkSpec{msg.Begin, peer_protocol.Integer(len(msg.Piece))}}
|
request_ := request{msg.Index, chunkSpec{msg.Begin, peer_protocol.Integer(len(msg.Piece))}}
|
||||||
if _, ok := conn.Requests[request_]; !ok {
|
if _, ok := conn.Requests[request_]; !ok {
|
||||||
err = fmt.Errorf("unexpected piece: %s", request_)
|
err = fmt.Errorf("unexpected piece: %s", request_)
|
||||||
break
|
break
|
||||||
|
@ -667,7 +667,7 @@ func (me *Client) WaitAll() bool {
|
||||||
|
|
||||||
func (me *Client) replenishConnRequests(torrent *torrent, conn *connection) {
|
func (me *Client) replenishConnRequests(torrent *torrent, conn *connection) {
|
||||||
requestHeatMap := torrent.requestHeat()
|
requestHeatMap := torrent.requestHeat()
|
||||||
addRequest := func(req Request) (again bool) {
|
addRequest := func(req request) (again bool) {
|
||||||
piece := torrent.Pieces[req.Index]
|
piece := torrent.Pieces[req.Index]
|
||||||
if piece.Hashing {
|
if piece.Hashing {
|
||||||
// We can't be sure we want this.
|
// We can't be sure we want this.
|
||||||
|
@ -686,7 +686,7 @@ func (me *Client) replenishConnRequests(torrent *torrent, conn *connection) {
|
||||||
// First request prioritized chunks.
|
// First request prioritized chunks.
|
||||||
if torrent.Priorities != nil {
|
if torrent.Priorities != nil {
|
||||||
for e := torrent.Priorities.Front(); e != nil; e = e.Next() {
|
for e := torrent.Priorities.Front(); e != nil; e = e.Next() {
|
||||||
if !addRequest(e.Value.(Request)) {
|
if !addRequest(e.Value.(request)) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -697,7 +697,7 @@ func (me *Client) replenishConnRequests(torrent *torrent, conn *connection) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for chunkSpec := range torrent.Pieces[index].PendingChunkSpecs {
|
for chunkSpec := range torrent.Pieces[index].PendingChunkSpecs {
|
||||||
if !addRequest(Request{index, chunkSpec}) {
|
if !addRequest(request{index, chunkSpec}) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -708,27 +708,27 @@ func (me *Client) replenishConnRequests(torrent *torrent, conn *connection) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (me *Client) downloadedChunk(torrent *torrent, msg *peer_protocol.Message) (err error) {
|
func (me *Client) downloadedChunk(torrent *torrent, msg *peer_protocol.Message) (err error) {
|
||||||
request := Request{msg.Index, chunkSpec{msg.Begin, peer_protocol.Integer(len(msg.Piece))}}
|
req := request{msg.Index, chunkSpec{msg.Begin, peer_protocol.Integer(len(msg.Piece))}}
|
||||||
if _, ok := torrent.Pieces[request.Index].PendingChunkSpecs[request.chunkSpec]; !ok {
|
if _, ok := torrent.Pieces[req.Index].PendingChunkSpecs[req.chunkSpec]; !ok {
|
||||||
log.Printf("got unnecessary chunk: %s", request)
|
log.Printf("got unnecessary chunk: %s", req)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = torrent.WriteChunk(int(msg.Index), int64(msg.Begin), msg.Piece)
|
err = torrent.WriteChunk(int(msg.Index), int64(msg.Begin), msg.Piece)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
delete(torrent.Pieces[request.Index].PendingChunkSpecs, request.chunkSpec)
|
delete(torrent.Pieces[req.Index].PendingChunkSpecs, req.chunkSpec)
|
||||||
if len(torrent.Pieces[request.Index].PendingChunkSpecs) == 0 {
|
if len(torrent.Pieces[req.Index].PendingChunkSpecs) == 0 {
|
||||||
me.queuePieceCheck(torrent, request.Index)
|
me.queuePieceCheck(torrent, req.Index)
|
||||||
}
|
}
|
||||||
var next *list.Element
|
var next *list.Element
|
||||||
for e := torrent.Priorities.Front(); e != nil; e = next {
|
for e := torrent.Priorities.Front(); e != nil; e = next {
|
||||||
next = e.Next()
|
next = e.Next()
|
||||||
if e.Value.(Request) == request {
|
if e.Value.(request) == req {
|
||||||
torrent.Priorities.Remove(e)
|
torrent.Priorities.Remove(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
me.dataReady(dataSpec{torrent.InfoHash, request})
|
me.dataReady(dataSpec{torrent.InfoHash, req})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -760,14 +760,14 @@ func (me *Client) pieceHashed(t *torrent, piece peer_protocol.Integer, correct b
|
||||||
if t.Priorities != nil {
|
if t.Priorities != nil {
|
||||||
for e := t.Priorities.Front(); e != nil; e = next {
|
for e := t.Priorities.Front(); e != nil; e = next {
|
||||||
next = e.Next()
|
next = e.Next()
|
||||||
if e.Value.(Request).Index == piece {
|
if e.Value.(request).Index == piece {
|
||||||
t.Priorities.Remove(e)
|
t.Priorities.Remove(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
me.dataReady(dataSpec{
|
me.dataReady(dataSpec{
|
||||||
t.InfoHash,
|
t.InfoHash,
|
||||||
Request{
|
request{
|
||||||
peer_protocol.Integer(piece),
|
peer_protocol.Integer(piece),
|
||||||
chunkSpec{0, peer_protocol.Integer(t.PieceLength(piece))},
|
chunkSpec{0, peer_protocol.Integer(t.PieceLength(piece))},
|
||||||
},
|
},
|
||||||
|
|
|
@ -22,13 +22,13 @@ type connection struct {
|
||||||
// Stuff controlled by the local peer.
|
// Stuff controlled by the local peer.
|
||||||
Interested bool
|
Interested bool
|
||||||
Choked bool
|
Choked bool
|
||||||
Requests map[Request]struct{}
|
Requests map[request]struct{}
|
||||||
|
|
||||||
// Stuff controlled by the remote peer.
|
// Stuff controlled by the remote peer.
|
||||||
PeerId [20]byte
|
PeerId [20]byte
|
||||||
PeerInterested bool
|
PeerInterested bool
|
||||||
PeerChoked bool
|
PeerChoked bool
|
||||||
PeerRequests map[Request]struct{}
|
PeerRequests map[request]struct{}
|
||||||
PeerExtensions [8]byte
|
PeerExtensions [8]byte
|
||||||
PeerPieces []bool
|
PeerPieces []bool
|
||||||
}
|
}
|
||||||
|
@ -62,7 +62,7 @@ func (c *connection) Post(msg encoding.BinaryMarshaler) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if more requests can be sent.
|
// Returns true if more requests can be sent.
|
||||||
func (c *connection) Request(chunk Request) bool {
|
func (c *connection) Request(chunk request) bool {
|
||||||
if len(c.Requests) >= maxRequests {
|
if len(c.Requests) >= maxRequests {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -82,14 +82,14 @@ func (c *connection) Request(chunk Request) bool {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
if c.Requests == nil {
|
if c.Requests == nil {
|
||||||
c.Requests = make(map[Request]struct{}, maxRequests)
|
c.Requests = make(map[request]struct{}, maxRequests)
|
||||||
}
|
}
|
||||||
c.Requests[chunk] = struct{}{}
|
c.Requests[chunk] = struct{}{}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if an unsatisfied request was canceled.
|
// Returns true if an unsatisfied request was canceled.
|
||||||
func (c *connection) PeerCancel(r Request) bool {
|
func (c *connection) PeerCancel(r request) bool {
|
||||||
if c.PeerRequests == nil {
|
if c.PeerRequests == nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
6
misc.go
6
misc.go
|
@ -60,13 +60,13 @@ type chunkSpec struct {
|
||||||
Begin, Length peer_protocol.Integer
|
Begin, Length peer_protocol.Integer
|
||||||
}
|
}
|
||||||
|
|
||||||
type Request struct {
|
type request struct {
|
||||||
Index peer_protocol.Integer
|
Index peer_protocol.Integer
|
||||||
chunkSpec
|
chunkSpec
|
||||||
}
|
}
|
||||||
|
|
||||||
func newRequest(index, begin, length peer_protocol.Integer) Request {
|
func newRequest(index, begin, length peer_protocol.Integer) request {
|
||||||
return Request{index, chunkSpec{begin, length}}
|
return request{index, chunkSpec{begin, length}}
|
||||||
}
|
}
|
||||||
|
|
||||||
type pieceByBytesPendingSlice struct {
|
type pieceByBytesPendingSlice struct {
|
||||||
|
|
12
torrent.go
12
torrent.go
|
@ -77,7 +77,7 @@ func (t *torrent) piecesByPendingBytesDesc() (indices []peer_protocol.Integer) {
|
||||||
|
|
||||||
// Return the request that would include the given offset into the torrent data.
|
// Return the request that would include the given offset into the torrent data.
|
||||||
func torrentOffsetRequest(torrentLength, pieceSize, chunkSize, offset int64) (
|
func torrentOffsetRequest(torrentLength, pieceSize, chunkSize, offset int64) (
|
||||||
r Request, ok bool) {
|
r request, ok bool) {
|
||||||
if offset < 0 || offset >= torrentLength {
|
if offset < 0 || offset >= torrentLength {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -93,7 +93,7 @@ func torrentOffsetRequest(torrentLength, pieceSize, chunkSize, offset int64) (
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func torrentRequestOffset(torrentLength, pieceSize int64, r Request) (off int64) {
|
func torrentRequestOffset(torrentLength, pieceSize int64, r request) (off int64) {
|
||||||
off = int64(r.Index)*pieceSize + int64(r.Begin)
|
off = int64(r.Index)*pieceSize + int64(r.Begin)
|
||||||
if off < 0 || off >= torrentLength {
|
if off < 0 || off >= torrentLength {
|
||||||
panic("invalid request")
|
panic("invalid request")
|
||||||
|
@ -101,12 +101,12 @@ func torrentRequestOffset(torrentLength, pieceSize int64, r Request) (off int64)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *torrent) requestOffset(r Request) int64 {
|
func (t *torrent) requestOffset(r request) int64 {
|
||||||
return torrentRequestOffset(t.Length(), t.MetaInfo.PieceLength, r)
|
return torrentRequestOffset(t.Length(), t.MetaInfo.PieceLength, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the request that would include the given offset into the torrent data.
|
// Return the request that would include the given offset into the torrent data.
|
||||||
func (t *torrent) offsetRequest(off int64) (req Request, ok bool) {
|
func (t *torrent) offsetRequest(off int64) (req request, ok bool) {
|
||||||
return torrentOffsetRequest(t.Length(), t.MetaInfo.PieceLength, chunkSize, off)
|
return torrentOffsetRequest(t.Length(), t.MetaInfo.PieceLength, chunkSize, off)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -144,8 +144,8 @@ func (t *torrent) pendAllChunkSpecs(index peer_protocol.Integer) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *torrent) requestHeat() (ret map[Request]int) {
|
func (t *torrent) requestHeat() (ret map[request]int) {
|
||||||
ret = make(map[Request]int)
|
ret = make(map[request]int)
|
||||||
for _, conn := range t.Conns {
|
for _, conn := range t.Conns {
|
||||||
for req, _ := range conn.Requests {
|
for req, _ := range conn.Requests {
|
||||||
ret[req]++
|
ret[req]++
|
||||||
|
|
|
@ -5,8 +5,8 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func r(i, b, l peer_protocol.Integer) Request {
|
func r(i, b, l peer_protocol.Integer) request {
|
||||||
return Request{i, chunkSpec{b, l}}
|
return request{i, chunkSpec{b, l}}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check the given Request is correct for various torrent offsets.
|
// Check the given Request is correct for various torrent offsets.
|
||||||
|
@ -14,15 +14,15 @@ func TestTorrentRequest(t *testing.T) {
|
||||||
const s = 472183431 // Length of torrent.
|
const s = 472183431 // Length of torrent.
|
||||||
for _, _case := range []struct {
|
for _, _case := range []struct {
|
||||||
off int64 // An offset into the torrent.
|
off int64 // An offset into the torrent.
|
||||||
req Request // The expected Request. The zero value means !ok.
|
req request // The expected Request. The zero value means !ok.
|
||||||
}{
|
}{
|
||||||
// Invalid offset.
|
// Invalid offset.
|
||||||
{-1, Request{}},
|
{-1, request{}},
|
||||||
{0, r(0, 0, 16384)},
|
{0, r(0, 0, 16384)},
|
||||||
// One before the end of a piece.
|
// One before the end of a piece.
|
||||||
{1<<18 - 1, r(0, 1<<18-16384, 16384)},
|
{1<<18 - 1, r(0, 1<<18-16384, 16384)},
|
||||||
// Offset beyond torrent length.
|
// Offset beyond torrent length.
|
||||||
{472 * 1 << 20, Request{}},
|
{472 * 1 << 20, request{}},
|
||||||
// One before the end of the torrent. Complicates the chunk length.
|
// One before the end of the torrent. Complicates the chunk length.
|
||||||
{s - 1, r((s-1)/(1<<18), (s-1)%(1<<18)/(16384)*(16384), 12935)},
|
{s - 1, r((s-1)/(1<<18), (s-1)%(1<<18)/(16384)*(16384), 12935)},
|
||||||
{1, r(0, 0, 16384)},
|
{1, r(0, 0, 16384)},
|
||||||
|
@ -32,7 +32,7 @@ func TestTorrentRequest(t *testing.T) {
|
||||||
{16384, r(0, 16384, 16384)},
|
{16384, r(0, 16384, 16384)},
|
||||||
} {
|
} {
|
||||||
req, ok := torrentOffsetRequest(472183431, 1<<18, 16384, _case.off)
|
req, ok := torrentOffsetRequest(472183431, 1<<18, 16384, _case.off)
|
||||||
if (_case.req == Request{}) == ok {
|
if (_case.req == request{}) == ok {
|
||||||
t.Fatalf("expected %v, got %v", _case.req, req)
|
t.Fatalf("expected %v, got %v", _case.req, req)
|
||||||
}
|
}
|
||||||
if req != _case.req {
|
if req != _case.req {
|
||||||
|
|
Loading…
Reference in New Issue