allow empty protocol string in dial

This commit is contained in:
Dmitriy Ryajov 2019-12-23 12:44:51 -06:00
parent 68cc57669e
commit 8714c66353
2 changed files with 54 additions and 33 deletions

View File

@ -206,30 +206,30 @@ proc dial*(s: Switch,
Future[Connection] {.async.} =
let id = peer.id
trace "Dialing peer", peer = id
result = s.connections.getOrDefault(id)
if result.isNil or result.closed:
var conn = s.connections.getOrDefault(id)
if conn.isNil or conn.closed:
for t in s.transports: # for each transport
for a in peer.addrs: # for each address
if t.handles(a): # check if it can dial it
trace "Dialing address", address = $a
result = await t.dial(a)
conn = await t.dial(a)
# make sure to assign the peer to the connection
result.peerInfo = peer
result = await s.upgradeOutgoing(result)
if isNil(result):
conn.peerInfo = peer
conn = await s.upgradeOutgoing(conn)
if isNil(conn):
continue
result.closeEvent.wait()
conn.closeEvent.wait()
.addCallback do (udata: pointer):
asyncCheck s.cleanupConn(result)
asyncCheck s.cleanupConn(conn)
break
else:
trace "Reusing existing connection"
if isNil(result):
raise newException(CatchableError, "unable to establish outgoing link!")
if isNil(conn):
raise newException(CatchableError, "Unable to establish outgoing link")
if proto.len > 0 and not result.closed:
if proto.len > 0 and not conn.closed:
let stream = await s.getMuxedStream(peer)
if not isNil(stream):
trace "Connection is muxed, return muxed stream"

View File

@ -34,9 +34,7 @@ method init(p: TestProto) {.gcsafe.} =
p.codec = TestCodec
p.handler = handle
suite "Switch":
test "e2e use switch":
proc createSwitch(ma: MultiAddress): (Switch, PeerInfo) {.gcsafe.}=
proc createSwitch(ma: MultiAddress): (Switch, PeerInfo) =
let seckey = PrivateKey.random(RSA)
var peerInfo: PeerInfo = PeerInfo.init(PrivateKey.random(RSA))
peerInfo.addrs.add(ma)
@ -56,6 +54,8 @@ suite "Switch":
secureManagers)
result = (switch, peerInfo)
suite "Switch":
test "e2e use switch dial proto string":
proc testSwitch(): Future[bool] {.async, gcsafe.} =
let ma1: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0")
let ma2: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0")
@ -82,3 +82,24 @@ suite "Switch":
check:
waitFor(testSwitch()) == true
test "e2e use switch no proto string":
proc testSwitch(): Future[bool] {.async, gcsafe.} =
let ma1: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0")
let ma2: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0")
var peerInfo1, peerInfo2: PeerInfo
var switch1, switch2: Switch
(switch1, peerInfo1) = createSwitch(ma1)
asyncCheck switch1.start()
(switch2, peerInfo2) = createSwitch(ma2)
asyncCheck switch2.start()
var conn = await switch2.dial(switch1.peerInfo)
check isNil(conn)
discard allFutures(switch1.stop(), switch2.stop())
result = true
check:
waitFor(testSwitch()) == true