Add connection.hasPreferredNetworkOver and friends

This commit is contained in:
Matt Joiner 2018-06-16 17:14:47 +10:00
parent 7a5aa2c42f
commit 4c471be802
1 changed files with 26 additions and 0 deletions

View File

@ -106,6 +106,32 @@ type connection struct {
writerCond sync.Cond
}
// Returns true if the connection is over IPv6.
func (cn *connection) ipv6() bool {
ip := missinggo.AddrIP(cn.remoteAddr())
if ip.To4() != nil {
return false
}
return len(ip) == net.IPv6len
}
// Returns true the dialer has the lower client peer ID. TODO: Find the
// specification for this.
func (cn *connection) isPreferredDirection() bool {
return bytes.Compare(cn.t.cl.peerID[:], cn.PeerID[:]) < 0 == cn.outgoing
}
// Returns whether the left connection should be preferred over the right one,
// considering only their networking properties. If ok is false, we can't
// decide.
func (l *connection) hasPreferredNetworkOver(r *connection) (left, ok bool) {
var ml multiLess
ml.NextBool(l.isPreferredDirection(), r.isPreferredDirection())
ml.NextBool(!l.utp(), !r.utp())
ml.NextBool(l.ipv6(), r.ipv6())
return ml.FinalOk()
}
func (cn *connection) cumInterest() time.Duration {
ret := cn.priorInterest
if cn.Interested {