aboutsummaryrefslogtreecommitdiffstats
path: root/p2p/peer.go
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2015-06-26 20:48:50 +0800
committerPéter Szilágyi <peterke@gmail.com>2015-06-26 20:48:50 +0800
commitd84638bd31878f772c6d1de3b491160319ddfc4b (patch)
treea89ab7e943253f69fb8323da3940011cb36d57d9 /p2p/peer.go
parentb0a5be4495962c291a25cbea793e43bad0781510 (diff)
downloaddexon-d84638bd31878f772c6d1de3b491160319ddfc4b.tar.gz
dexon-d84638bd31878f772c6d1de3b491160319ddfc4b.tar.zst
dexon-d84638bd31878f772c6d1de3b491160319ddfc4b.zip
p2p: support protocol version negotiation
Diffstat (limited to 'p2p/peer.go')
-rw-r--r--p2p/peer.go11
1 files changed, 9 insertions, 2 deletions
diff --git a/p2p/peer.go b/p2p/peer.go
index 40466cf84..e1bda1d03 100644
--- a/p2p/peer.go
+++ b/p2p/peer.go
@@ -249,15 +249,22 @@ func countMatchingProtocols(protocols []Protocol, caps []Cap) int {
// matchProtocols creates structures for matching named subprotocols.
func matchProtocols(protocols []Protocol, caps []Cap, rw MsgReadWriter) map[string]*protoRW {
- sort.Sort(capsByName(caps))
+ sort.Sort(capsByNameAndVersion(caps))
offset := baseProtocolLength
result := make(map[string]*protoRW)
+
outer:
for _, cap := range caps {
for _, proto := range protocols {
- if proto.Name == cap.Name && proto.Version == cap.Version && result[cap.Name] == nil {
+ if proto.Name == cap.Name && proto.Version == cap.Version {
+ // If an old protocol version matched, revert it
+ if old := result[cap.Name]; old != nil {
+ offset -= old.Length
+ }
+ // Assign the new match
result[cap.Name] = &protoRW{Protocol: proto, offset: offset, in: make(chan Msg), w: rw}
offset += proto.Length
+
continue outer
}
}