Support peers that don't have short IDs

This commit is contained in:
David Wragg committed 2015-10-14 11:11:34 +01:00
1 parent 1f1451e666
commit b36ffe1c9d
3 files changed
+33 -16

No files matched your search

+13 -6
View File
@@ -277,7 +277,7 @@ func (features features) Get(key string) string {
} }
func (conn *LocalConnection) parseFeatures(features features) (*Peer, error) { func (conn *LocalConnection) parseFeatures(features features) (*Peer, error) {
if err := features.MustHave([]string{"PeerNameFlavour", "Name", "NickName", "ShortID", "UID", "ConnID"}); err != nil { if err := features.MustHave([]string{"PeerNameFlavour", "Name", "NickName", "UID", "ConnID"}); err != nil {
return nil, err return nil, err
} }
@@ -293,10 +293,15 @@ func (conn *LocalConnection) parseFeatures(features features) (*Peer, error) {
nickName := features.Get("NickName") nickName := features.Get("NickName")
shortID, err := strconv.ParseUint(features.Get("ShortID"), 10, var shortID uint64
PeerShortIDBits) var hasShortID bool
if err != nil { if shortIDStr, present := features["ShortID"]; present {
return nil, err hasShortID = true
shortID, err = strconv.ParseUint(shortIDStr, 10,
PeerShortIDBits)
if err != nil {
return nil, err
}
} }
uid, err := ParsePeerUID(features.Get("UID")) uid, err := ParsePeerUID(features.Get("UID"))
@@ -310,7 +315,9 @@ func (conn *LocalConnection) parseFeatures(features features) (*Peer, error) {
} }
conn.uid ^= remoteConnID conn.uid ^= remoteConnID
return NewPeer(name, nickName, uid, 0, PeerShortID(shortID)), nil peer := NewPeer(name, nickName, uid, 0, PeerShortID(shortID))
peer.HasShortID = hasShortID
return peer, nil
} }
func (conn *LocalConnection) registerRemote(remote *Peer, acceptNewPeer bool) error { func (conn *LocalConnection) registerRemote(remote *Peer, acceptNewPeer bool) error {
+12 -10
View File
@@ -30,11 +30,12 @@ func randomPeerShortID() PeerShortID {
} }
type PeerSummary struct { type PeerSummary struct {
NameByte []byte NameByte []byte
NickName string NickName string
UID PeerUID UID PeerUID
Version uint64 Version uint64
ShortID PeerShortID ShortID PeerShortID
HasShortID bool
} }
type Peer struct { type Peer struct {
@@ -56,11 +57,12 @@ func NewPeerFromSummary(summary PeerSummary) *Peer {
func NewPeer(name PeerName, nickName string, uid PeerUID, version uint64, shortID PeerShortID) *Peer { func NewPeer(name PeerName, nickName string, uid PeerUID, version uint64, shortID PeerShortID) *Peer {
return NewPeerFromSummary(PeerSummary{ return NewPeerFromSummary(PeerSummary{
NameByte: name.Bin(), NameByte: name.Bin(),
NickName: nickName, NickName: nickName,
UID: uid, UID: uid,
Version: version, Version: version,
ShortID: shortID, ShortID: shortID,
HasShortID: true,
}) })
} }
+8
View File
@@ -114,6 +114,10 @@ func (peers *Peers) unlockAndNotify(pending *PeersPendingNotifications) {
} }
func (peers *Peers) addByShortID(peer *Peer, pending *PeersPendingNotifications) { func (peers *Peers) addByShortID(peer *Peer, pending *PeersPendingNotifications) {
if !peer.HasShortID {
return
}
entry, ok := peers.byShortID[peer.ShortID] entry, ok := peers.byShortID[peer.ShortID]
if !ok { if !ok {
entry = ShortIDPeers{peer: peer} entry = ShortIDPeers{peer: peer}
@@ -146,6 +150,10 @@ func (peers *Peers) addByShortID(peer *Peer, pending *PeersPendingNotifications)
} }
func (peers *Peers) deleteByShortID(peer *Peer, pending *PeersPendingNotifications) { func (peers *Peers) deleteByShortID(peer *Peer, pending *PeersPendingNotifications) {
if !peer.HasShortID {
return
}
entry := peers.byShortID[peer.ShortID] entry := peers.byShortID[peer.ShortID]
var otherIndex int var otherIndex int