]> Sergey Matveev's repositories - btrtrc.git/blobdiff - torrent.go
Test fixes for Windows
[btrtrc.git] / torrent.go
index ad89b45391cc9df92616888a369c67b09245b0e2..a2b85a4d474c044addfb827e0624b7ae9e68add8 100644 (file)
@@ -830,7 +830,10 @@ func (t *Torrent) newMetaInfo() metainfo.MetaInfo {
        }
 }
 
-// Get bytes left
+// Returns a count of bytes that are not complete in storage, and not pending being written to
+// storage. This value is from the perspective of the download manager, and may not agree with the
+// actual state in storage. If you want read data synchronously you should use a Reader. See
+// https://github.com/anacrolix/torrent/issues/828.
 func (t *Torrent) BytesMissing() (n int64) {
        t.cl.rLock()
        n = t.bytesMissingLocked()
@@ -1057,7 +1060,7 @@ func (t *Torrent) havePiece(index pieceIndex) bool {
 func (t *Torrent) maybeDropMutuallyCompletePeer(
        // I'm not sure about taking peer here, not all peer implementations actually drop. Maybe that's
        // okay?
-       p *Peer,
+       p *PeerConn,
 ) {
        if !t.cl.config.DropMutuallyCompletePeers {
                return
@@ -1071,7 +1074,7 @@ func (t *Torrent) maybeDropMutuallyCompletePeer(
        if p.useful() {
                return
        }
-       t.logger.WithDefaultLevel(log.Debug).Printf("dropping %v, which is mutually complete", p)
+       p.logger.Levelf(log.Debug, "is mutually complete; dropping")
        p.drop()
 }
 
@@ -2217,7 +2220,7 @@ func (t *Torrent) onPieceCompleted(piece pieceIndex) {
        t.piece(piece).readerCond.Broadcast()
        for conn := range t.conns {
                conn.have(piece)
-               t.maybeDropMutuallyCompletePeer(&conn.Peer)
+               t.maybeDropMutuallyCompletePeer(conn)
        }
 }
 
@@ -2739,18 +2742,11 @@ func (t *Torrent) peerConnsWithDialAddrPort(target netip.AddrPort) (ret []*PeerC
        return
 }
 
-func makeUtHolepunchMsgForPeerConn(
+func wrapUtHolepunchMsgForPeerConn(
        recipient *PeerConn,
-       msgType utHolepunch.MsgType,
-       addrPort netip.AddrPort,
-       errCode utHolepunch.ErrCode,
+       msg utHolepunch.Msg,
 ) pp.Message {
-       utHolepunchMsg := utHolepunch.Msg{
-               MsgType:  msgType,
-               AddrPort: addrPort,
-               ErrCode:  errCode,
-       }
-       extendedPayload, err := utHolepunchMsg.MarshalBinary()
+       extendedPayload, err := msg.MarshalBinary()
        if err != nil {
                panic(err)
        }
@@ -2767,10 +2763,38 @@ func sendUtHolepunchMsg(
        addrPort netip.AddrPort,
        errCode utHolepunch.ErrCode,
 ) {
-       pc.write(makeUtHolepunchMsgForPeerConn(pc, msgType, addrPort, errCode))
+       holepunchMsg := utHolepunch.Msg{
+               MsgType:  msgType,
+               AddrPort: addrPort,
+               ErrCode:  errCode,
+       }
+       incHolepunchMessagesSent(holepunchMsg)
+       ppMsg := wrapUtHolepunchMsgForPeerConn(pc, holepunchMsg)
+       pc.write(ppMsg)
+}
+
+func incHolepunchMessages(msg utHolepunch.Msg, verb string) {
+       torrent.Add(
+               fmt.Sprintf(
+                       "holepunch %v %v messages %v",
+                       msg.MsgType,
+                       addrPortProtocolStr(msg.AddrPort),
+                       verb,
+               ),
+               1,
+       )
+}
+
+func incHolepunchMessagesReceived(msg utHolepunch.Msg) {
+       incHolepunchMessages(msg, "received")
+}
+
+func incHolepunchMessagesSent(msg utHolepunch.Msg) {
+       incHolepunchMessages(msg, "sent")
 }
 
 func (t *Torrent) handleReceivedUtHolepunchMsg(msg utHolepunch.Msg, sender *PeerConn) error {
+       incHolepunchMessagesReceived(msg)
        switch msg.MsgType {
        case utHolepunch.Rendezvous:
                t.logger.Printf("got holepunch rendezvous request for %v from %p", msg.AddrPort, sender)
@@ -2801,6 +2825,7 @@ func (t *Torrent) handleReceivedUtHolepunchMsg(msg utHolepunch.Msg, sender *Peer
                }
                return nil
        case utHolepunch.Connect:
+               t.logger.Printf("got holepunch connect request for %v from %p", msg.AddrPort, sender)
                opts := outgoingConnOpts{
                        peerInfo: PeerInfo{
                                Addr:         msg.AddrPort,
@@ -2818,6 +2843,7 @@ func (t *Torrent) handleReceivedUtHolepunchMsg(msg utHolepunch.Msg, sender *Peer
                initiateConn(opts, true)
                return nil
        case utHolepunch.Error:
+               torrent.Add("holepunch error messages received", 1)
                t.logger.Levelf(log.Debug, "received ut_holepunch error message from %v: %v", sender, msg.ErrCode)
                return nil
        default:
@@ -2825,7 +2851,19 @@ func (t *Torrent) handleReceivedUtHolepunchMsg(msg utHolepunch.Msg, sender *Peer
        }
 }
 
-func (t *Torrent) startHolepunchRendezvous(addrPort netip.AddrPort) error {
+func addrPortProtocolStr(addrPort netip.AddrPort) string {
+       addr := addrPort.Addr()
+       switch {
+       case addr.Is4():
+               return "ipv4"
+       case addr.Is6():
+               return "ipv6"
+       default:
+               panic(addrPort)
+       }
+}
+
+func (t *Torrent) trySendHolepunchRendezvous(addrPort netip.AddrPort) error {
        rzsSent := 0
        for pc := range t.conns {
                if !pc.supportsExtension(utHolepunch.ExtensionName) {