Fix conn status string for WebRTC connections

This commit is contained in:
Matt Joiner 2020-09-29 16:21:54 +10:00
parent 4dee11efe9
commit 1cdae13700
5 changed files with 24 additions and 10 deletions

View File

@ -495,7 +495,8 @@ func (cl *Client) acceptConnections(l net.Listener) {
}
}
func regularConnString(nc net.Conn) string {
// Creates the PeerConn.connString for a regular net.Conn PeerConn.
func regularNetConnPeerConnConnString(nc net.Conn) string {
return fmt.Sprintf("%s-%s", nc.LocalAddr(), nc.RemoteAddr())
}
@ -505,7 +506,7 @@ func (cl *Client) incomingConnection(nc net.Conn) {
tc.SetLinger(0)
}
c := cl.newConnection(nc, false, nc.RemoteAddr(), nc.RemoteAddr().Network(),
regularConnString(nc))
regularNetConnPeerConnConnString(nc))
c.Discovery = PeerSourceIncoming
cl.runReceivedConn(c)
}
@ -704,7 +705,7 @@ func (cl *Client) establishOutgoingConnEx(t *Torrent, addr net.Addr, obfuscatedH
}
return nil, errors.New("dial failed")
}
c, err := cl.initiateProtocolHandshakes(context.Background(), nc, t, true, obfuscatedHeader, addr, dr.Network, regularConnString(nc))
c, err := cl.initiateProtocolHandshakes(context.Background(), nc, t, true, obfuscatedHeader, addr, dr.Network, regularNetConnPeerConnConnString(nc))
if err != nil {
nc.Close()
}
@ -1338,8 +1339,8 @@ func (cl *Client) newConnection(nc net.Conn, outgoing bool, remoteAddr net.Addr,
RemoteAddr: remoteAddr,
network: network,
connString: connString,
},
connString: connString,
conn: nc,
writeBuffer: new(bytes.Buffer),
}

View File

@ -19,4 +19,5 @@ type peerImpl interface {
onGotInfo(*metainfo.Info)
drop()
String() string
connStatusString() string
}

View File

@ -44,7 +44,6 @@ type peer struct {
peerImpl
connString string
outgoing bool
network string
RemoteAddr net.Addr
@ -86,11 +85,9 @@ type peer struct {
pex pexConnState
// Stuff controlled by the remote peer.
PeerID PeerID
peerInterested bool
peerChoking bool
peerRequests map[request]struct{}
PeerExtensionBytes pp.PeerExtensionBits
PeerPrefersEncryption bool // as indicated by 'e' field in extension handshake
PeerListenPort int
// The pieces the peer has claimed to have.
@ -116,10 +113,18 @@ type peer struct {
logger log.Logger
}
// Maintains the state of a connection with a peer.
// Maintains the state of a BitTorrent-protocol based connection with a peer.
type PeerConn struct {
peer
// A string that should identify the PeerConn's net.Conn endpoints. The net.Conn could
// be wrapping WebRTC, uTP, or TCP etc. Used in writing the conn status for peers.
connString string
// See BEP 3 etc.
PeerID PeerID
PeerExtensionBytes pp.PeerExtensionBits
// The actual Conn, used for closing, and setting socket options.
conn net.Conn
// The Reader and Writer for this Conn, with hooks installed for stats,
@ -132,6 +137,10 @@ type PeerConn struct {
writerCond sync.Cond
}
func (cn *PeerConn) connStatusString() string {
return fmt.Sprintf("%+-55q %s %s\n", cn.PeerID, cn.PeerExtensionBytes, cn.connString)
}
func (cn *peer) updateExpectingChunks() {
if cn.expectingChunks() {
if cn.lastStartedExpectingToReceiveChunks.IsZero() {
@ -295,7 +304,7 @@ func (cn *peer) downloadRate() float64 {
func (cn *peer) writeStatus(w io.Writer, t *Torrent) {
// \t isn't preserved in <pre> blocks?
fmt.Fprintf(w, "%+-55q %s %s\n", cn.PeerID, cn.PeerExtensionBytes, cn.connString)
fmt.Fprintln(w, cn.connStatusString())
fmt.Fprintf(w, " last msg: %s, connected: %s, last helpful: %s, itime: %s, etime: %s\n",
eventAgeString(cn.lastMessageReceived),
eventAgeString(cn.completedHandshake),

View File

@ -2036,7 +2036,6 @@ func (t *Torrent) addWebSeed(url string) {
ws := webseedPeer{
peer: peer{
t: t,
connString: url,
outgoing: true,
network: "http",
reconciledHandshakeStats: true,

View File

@ -20,6 +20,10 @@ type webseedPeer struct {
var _ peerImpl = (*webseedPeer)(nil)
func (me *webseedPeer) connStatusString() string {
return me.client.Url
}
func (ws *webseedPeer) String() string {
return fmt.Sprintf("webseed peer for %q", ws.client.Url)
}