2
0
mirror of synced 2025-02-23 22:28:11 +00:00

Export PeerImpl and all its methods

This commit is contained in:
Matt Joiner 2020-05-31 13:22:36 +10:00
parent 3f8f6e1733
commit 317146dec9
4 changed files with 49 additions and 49 deletions

View File

@ -914,7 +914,7 @@ func (cl *Client) runHandshookConn(c *PeerConn, t *Torrent) error {
} }
c.conn.SetWriteDeadline(time.Time{}) c.conn.SetWriteDeadline(time.Time{})
c.r = deadlineReader{c.conn, c.r} c.r = deadlineReader{c.conn, c.r}
completedHandshakeConnectionFlags.Add(c.connectionFlags(), 1) completedHandshakeConnectionFlags.Add(c.ConnectionFlags(), 1)
if connIsIpv6(c.conn) { if connIsIpv6(c.conn) {
torrent.Add("completed handshake over ipv6", 1) torrent.Add("completed handshake over ipv6", 1)
} }
@ -1294,7 +1294,7 @@ func (cl *Client) newConnection(nc net.Conn, outgoing bool, remoteAddr net.Addr,
conn: nc, conn: nc,
writeBuffer: new(bytes.Buffer), writeBuffer: new(bytes.Buffer),
} }
c.peerImpl = c c.PeerImpl = c
c.logger = cl.logger.WithValues(c).WithDefaultLevel(log.Debug).WithText(func(m log.Msg) string { c.logger = cl.logger.WithValues(c).WithDefaultLevel(log.Debug).WithText(func(m log.Msg) string {
return fmt.Sprintf("%v: %s", c, m.Text()) return fmt.Sprintf("%v: %s", c, m.Text())
}) })

View File

@ -35,15 +35,15 @@ const (
PeerSourcePex = "X" PeerSourcePex = "X"
) )
type peerImpl interface { type PeerImpl interface {
updateRequests() UpdateRequests()
writeInterested(interested bool) bool WriteInterested(interested bool) bool
cancel(request) bool Cancel(request) bool
request(request) bool Request(request) bool
connectionFlags() string ConnectionFlags() string
_close() Close()
postCancel(request) PostCancel(request)
drop() Drop()
} }
type peer struct { type peer struct {
@ -52,7 +52,7 @@ type peer struct {
t *Torrent t *Torrent
peerImpl PeerImpl
connString string connString string
outgoing bool outgoing bool
@ -248,7 +248,7 @@ func eventAgeString(t time.Time) string {
return fmt.Sprintf("%.2fs ago", time.Since(t).Seconds()) return fmt.Sprintf("%.2fs ago", time.Since(t).Seconds())
} }
func (cn *PeerConn) connectionFlags() (ret string) { func (cn *PeerConn) ConnectionFlags() (ret string) {
c := func(b byte) { c := func(b byte) {
ret += string([]byte{b}) ret += string([]byte{b})
} }
@ -280,7 +280,7 @@ func (cn *peer) statusFlags() (ret string) {
c('c') c('c')
} }
c('-') c('-')
ret += cn.connectionFlags() ret += cn.ConnectionFlags()
c('-') c('-')
if cn.peerInterested { if cn.peerInterested {
c('i') c('i')
@ -343,10 +343,10 @@ func (cn *peer) close() {
} }
cn.discardPieceInclination() cn.discardPieceInclination()
cn._pieceRequestOrder.Clear() cn._pieceRequestOrder.Clear()
cn.peerImpl._close() cn.PeerImpl.Close()
} }
func (cn *PeerConn) _close() { func (cn *PeerConn) Close() {
if cn.pex.IsEnabled() { if cn.pex.IsEnabled() {
cn.pex.Close() cn.pex.Close()
} }
@ -486,10 +486,10 @@ func (cn *peer) setInterested(interested bool) bool {
} }
cn.updateExpectingChunks() cn.updateExpectingChunks()
// log.Printf("%p: setting interest: %v", cn, interested) // log.Printf("%p: setting interest: %v", cn, interested)
return cn.writeInterested(interested) return cn.WriteInterested(interested)
} }
func (pc *PeerConn) writeInterested(interested bool) bool { func (pc *PeerConn) WriteInterested(interested bool) bool {
return pc.write(pp.Message{ return pc.write(pp.Message{
Type: func() pp.MessageType { Type: func() pp.MessageType {
if interested { if interested {
@ -542,10 +542,10 @@ func (cn *peer) request(r request) bool {
cn.t.pendingRequests[r]++ cn.t.pendingRequests[r]++
cn.t.requestStrategy.hooks().sentRequest(r) cn.t.requestStrategy.hooks().sentRequest(r)
cn.updateExpectingChunks() cn.updateExpectingChunks()
return cn.peerImpl.request(r) return cn.PeerImpl.Request(r)
} }
func (me *PeerConn) request(r request) bool { func (me *PeerConn) Request(r request) bool {
return me.write(pp.Message{ return me.write(pp.Message{
Type: pp.Request, Type: pp.Request,
Index: r.Index, Index: r.Index,
@ -554,7 +554,7 @@ func (me *PeerConn) request(r request) bool {
}) })
} }
func (me *PeerConn) cancel(r request) bool { func (me *PeerConn) Cancel(r request) bool {
return me.write(makeCancelMessage(r)) return me.write(makeCancelMessage(r))
} }
@ -567,7 +567,7 @@ func (cn *peer) doRequestState() bool {
for r := range cn.requests { for r := range cn.requests {
cn.deleteRequest(r) cn.deleteRequest(r)
// log.Printf("%p: cancelling request: %v", cn, r) // log.Printf("%p: cancelling request: %v", cn, r)
if !cn.peerImpl.cancel(r) { if !cn.PeerImpl.Cancel(r) {
return false return false
} }
} }
@ -703,7 +703,7 @@ func (cn *PeerConn) postBitfield() {
cn.sentHaves = cn.t._completedPieces.Copy() cn.sentHaves = cn.t._completedPieces.Copy()
} }
func (cn *PeerConn) updateRequests() { func (cn *PeerConn) UpdateRequests() {
// log.Print("update requests") // log.Print("update requests")
cn.tickleWriter() cn.tickleWriter()
} }
@ -826,7 +826,7 @@ func (cn *PeerConn) peerPiecesChanged() {
} }
} }
if prioritiesChanged { if prioritiesChanged {
cn.updateRequests() cn.UpdateRequests()
} }
} }
} }
@ -847,7 +847,7 @@ func (cn *PeerConn) peerSentHave(piece pieceIndex) error {
cn.raisePeerMinPieces(piece + 1) cn.raisePeerMinPieces(piece + 1)
cn._peerPieces.Set(bitmap.BitIndex(piece), true) cn._peerPieces.Set(bitmap.BitIndex(piece), true)
if cn.updatePiecePriority(piece) { if cn.updatePiecePriority(piece) {
cn.updateRequests() cn.UpdateRequests()
} }
return nil return nil
} }
@ -1090,7 +1090,7 @@ func (c *PeerConn) mainReadLoop() (err error) {
c.deleteAllRequests() c.deleteAllRequests()
} }
// We can then reset our interest. // We can then reset our interest.
c.updateRequests() c.UpdateRequests()
c.updateExpectingChunks() c.updateExpectingChunks()
case pp.Unchoke: case pp.Unchoke:
c.peerChoking = false c.peerChoking = false
@ -1140,7 +1140,7 @@ func (c *PeerConn) mainReadLoop() (err error) {
case pp.Suggest: case pp.Suggest:
torrent.Add("suggests received", 1) torrent.Add("suggests received", 1)
log.Fmsg("peer suggested piece %d", msg.Index).AddValues(c, msg.Index).SetLevel(log.Debug).Log(c.t.logger) log.Fmsg("peer suggested piece %d", msg.Index).AddValues(c, msg.Index).SetLevel(log.Debug).Log(c.t.logger)
c.updateRequests() c.UpdateRequests()
case pp.HaveAll: case pp.HaveAll:
err = c.onPeerSentHaveAll() err = c.onPeerSentHaveAll()
case pp.HaveNone: case pp.HaveNone:
@ -1152,7 +1152,7 @@ func (c *PeerConn) mainReadLoop() (err error) {
torrent.Add("allowed fasts received", 1) torrent.Add("allowed fasts received", 1)
log.Fmsg("peer allowed fast: %d", msg.Index).AddValues(c).SetLevel(log.Debug).Log(c.t.logger) log.Fmsg("peer allowed fast: %d", msg.Index).AddValues(c).SetLevel(log.Debug).Log(c.t.logger)
c.peerAllowedFast.Add(int(msg.Index)) c.peerAllowedFast.Add(int(msg.Index))
c.updateRequests() c.UpdateRequests()
case pp.Extended: case pp.Extended:
err = c.onReadExtendedMsg(msg.ExtendedID, msg.ExtendedPayload) err = c.onReadExtendedMsg(msg.ExtendedID, msg.ExtendedPayload)
default: default:
@ -1309,7 +1309,7 @@ func (c *peer) receiveChunk(msg *pp.Message) error {
// Cancel pending requests for this chunk. // Cancel pending requests for this chunk.
for c := range t.conns { for c := range t.conns {
c.postCancel(req) c.PostCancel(req)
} }
err := func() error { err := func() error {
@ -1440,7 +1440,7 @@ another:
return c.choke(msg) return c.choke(msg)
} }
func (cn *PeerConn) drop() { func (cn *PeerConn) Drop() {
cn.t.dropConnection(cn) cn.t.dropConnection(cn)
} }
@ -1472,10 +1472,10 @@ func (c *peer) deleteRequest(r request) bool {
if n < 0 { if n < 0 {
panic(n) panic(n)
} }
c.updateRequests() c.UpdateRequests()
c.t.iterPeers(func(_c *peer) { c.t.iterPeers(func(_c *peer) {
if !_c.interested && _c != c && c.peerHasPiece(pieceIndex(r.Index)) { if !_c.interested && _c != c && c.peerHasPiece(pieceIndex(r.Index)) {
_c.updateRequests() _c.UpdateRequests()
} }
}) })
return true return true
@ -1501,11 +1501,11 @@ func (c *peer) postCancel(r request) bool {
if !c.deleteRequest(r) { if !c.deleteRequest(r) {
return false return false
} }
c.peerImpl.postCancel(r) c.PeerImpl.PostCancel(r)
return true return true
} }
func (c *PeerConn) postCancel(r request) { func (c *PeerConn) PostCancel(r request) {
c.post(makeCancelMessage(r)) c.post(makeCancelMessage(r))
} }

View File

@ -947,7 +947,7 @@ func (t *Torrent) piecePriorityChanged(piece pieceIndex) {
t.iterPeers(func(c *peer) { t.iterPeers(func(c *peer) {
if c.updatePiecePriority(piece) { if c.updatePiecePriority(piece) {
// log.Print("conn piece priority changed") // log.Print("conn piece priority changed")
c.updateRequests() c.UpdateRequests()
} }
}) })
t.maybeNewConns() t.maybeNewConns()
@ -1725,7 +1725,7 @@ func (t *Torrent) pieceHashed(piece pieceIndex, passed bool, hashIoErr error) {
if len(bannableTouchers) >= 1 { if len(bannableTouchers) >= 1 {
c := bannableTouchers[0] c := bannableTouchers[0]
t.cl.banPeerIP(c.remoteIp()) t.cl.banPeerIP(c.remoteIp())
c.drop() c.Drop()
} }
} }
t.onIncompletePiece(piece) t.onIncompletePiece(piece)
@ -1771,7 +1771,7 @@ func (t *Torrent) onIncompletePiece(piece pieceIndex) {
// } // }
t.iterPeers(func(conn *peer) { t.iterPeers(func(conn *peer) {
if conn.peerHasPiece(piece) { if conn.peerHasPiece(piece) {
conn.updateRequests() conn.UpdateRequests()
} }
}) })
} }
@ -1935,7 +1935,7 @@ func (cb torrentRequestStrategyCallbacks) requestTimedOut(r request) {
defer cb.t.cl.unlock() defer cb.t.cl.unlock()
cb.t.iterPeers(func(cn *peer) { cb.t.iterPeers(func(cn *peer) {
if cn.peerHasPiece(pieceIndex(r.Index)) { if cn.peerHasPiece(pieceIndex(r.Index)) {
cn.updateRequests() cn.UpdateRequests()
} }
}) })
@ -1963,7 +1963,7 @@ func (t *Torrent) disallowDataDownloadLocked() {
log.Printf("disallowing data download") log.Printf("disallowing data download")
t.dataDownloadDisallowed = true t.dataDownloadDisallowed = true
t.iterPeers(func(c *peer) { t.iterPeers(func(c *peer) {
c.updateRequests() c.UpdateRequests()
}) })
} }
@ -1973,7 +1973,7 @@ func (t *Torrent) AllowDataDownload() {
log.Printf("AllowDataDownload") log.Printf("AllowDataDownload")
t.dataDownloadDisallowed = false t.dataDownloadDisallowed = false
t.iterPeers(func(c *peer) { t.iterPeers(func(c *peer) {
c.updateRequests() c.UpdateRequests()
}) })
} }
@ -2007,7 +2007,7 @@ func (t *Torrent) addWebSeed(url string) {
ws := webSeed{ ws := webSeed{
peer: p, peer: p,
} }
p.peerImpl = &ws p.PeerImpl = &ws
t.webSeeds[url] = p t.webSeeds[url] = p
} }

View File

@ -10,31 +10,31 @@ type webSeed struct {
url string url string
} }
func (ws *webSeed) postCancel(r request) { func (ws *webSeed) PostCancel(r request) {
panic("implement me") panic("implement me")
} }
func (ws *webSeed) writeInterested(interested bool) bool { func (ws *webSeed) WriteInterested(interested bool) bool {
return true return true
} }
func (ws *webSeed) cancel(r request) bool { func (ws *webSeed) Cancel(r request) bool {
panic("implement me") panic("implement me")
} }
func (ws *webSeed) request(r request) bool { func (ws *webSeed) Request(r request) bool {
panic("implement me") panic("implement me")
} }
func (ws *webSeed) connectionFlags() string { func (ws *webSeed) ConnectionFlags() string {
return "WS" return "WS"
} }
func (ws *webSeed) drop() { func (ws *webSeed) Drop() {
} }
func (ws *webSeed) updateRequests() { func (ws *webSeed) UpdateRequests() {
ws.peer.doRequestState() ws.peer.doRequestState()
} }
func (ws *webSeed) _close() {} func (ws *webSeed) Close() {}