allow empty protocol string in dial
This commit is contained in:
parent
68cc57669e
commit
8714c66353
|
@ -206,30 +206,30 @@ proc dial*(s: Switch,
|
||||||
Future[Connection] {.async.} =
|
Future[Connection] {.async.} =
|
||||||
let id = peer.id
|
let id = peer.id
|
||||||
trace "Dialing peer", peer = id
|
trace "Dialing peer", peer = id
|
||||||
result = s.connections.getOrDefault(id)
|
var conn = s.connections.getOrDefault(id)
|
||||||
if result.isNil or result.closed:
|
if conn.isNil or conn.closed:
|
||||||
for t in s.transports: # for each transport
|
for t in s.transports: # for each transport
|
||||||
for a in peer.addrs: # for each address
|
for a in peer.addrs: # for each address
|
||||||
if t.handles(a): # check if it can dial it
|
if t.handles(a): # check if it can dial it
|
||||||
trace "Dialing address", address = $a
|
trace "Dialing address", address = $a
|
||||||
result = await t.dial(a)
|
conn = await t.dial(a)
|
||||||
# make sure to assign the peer to the connection
|
# make sure to assign the peer to the connection
|
||||||
result.peerInfo = peer
|
conn.peerInfo = peer
|
||||||
result = await s.upgradeOutgoing(result)
|
conn = await s.upgradeOutgoing(conn)
|
||||||
if isNil(result):
|
if isNil(conn):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
result.closeEvent.wait()
|
conn.closeEvent.wait()
|
||||||
.addCallback do (udata: pointer):
|
.addCallback do (udata: pointer):
|
||||||
asyncCheck s.cleanupConn(result)
|
asyncCheck s.cleanupConn(conn)
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
trace "Reusing existing connection"
|
trace "Reusing existing connection"
|
||||||
|
|
||||||
if isNil(result):
|
if isNil(conn):
|
||||||
raise newException(CatchableError, "unable to establish outgoing link!")
|
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)
|
let stream = await s.getMuxedStream(peer)
|
||||||
if not isNil(stream):
|
if not isNil(stream):
|
||||||
trace "Connection is muxed, return muxed stream"
|
trace "Connection is muxed, return muxed stream"
|
||||||
|
|
|
@ -34,9 +34,7 @@ method init(p: TestProto) {.gcsafe.} =
|
||||||
p.codec = TestCodec
|
p.codec = TestCodec
|
||||||
p.handler = handle
|
p.handler = handle
|
||||||
|
|
||||||
suite "Switch":
|
proc createSwitch(ma: MultiAddress): (Switch, PeerInfo) =
|
||||||
test "e2e use switch":
|
|
||||||
proc createSwitch(ma: MultiAddress): (Switch, PeerInfo) {.gcsafe.}=
|
|
||||||
let seckey = PrivateKey.random(RSA)
|
let seckey = PrivateKey.random(RSA)
|
||||||
var peerInfo: PeerInfo = PeerInfo.init(PrivateKey.random(RSA))
|
var peerInfo: PeerInfo = PeerInfo.init(PrivateKey.random(RSA))
|
||||||
peerInfo.addrs.add(ma)
|
peerInfo.addrs.add(ma)
|
||||||
|
@ -56,6 +54,8 @@ suite "Switch":
|
||||||
secureManagers)
|
secureManagers)
|
||||||
result = (switch, peerInfo)
|
result = (switch, peerInfo)
|
||||||
|
|
||||||
|
suite "Switch":
|
||||||
|
test "e2e use switch dial proto string":
|
||||||
proc testSwitch(): Future[bool] {.async, gcsafe.} =
|
proc testSwitch(): Future[bool] {.async, gcsafe.} =
|
||||||
let ma1: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0")
|
let ma1: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0")
|
||||||
let ma2: 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:
|
check:
|
||||||
waitFor(testSwitch()) == true
|
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
|
||||||
|
|
Loading…
Reference in New Issue