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.} = 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"

View File

@ -34,28 +34,28 @@ method init(p: TestProto) {.gcsafe.} =
p.codec = TestCodec p.codec = TestCodec
p.handler = handle p.handler = handle
proc createSwitch(ma: MultiAddress): (Switch, PeerInfo) =
let seckey = PrivateKey.random(RSA)
var peerInfo: PeerInfo = PeerInfo.init(PrivateKey.random(RSA))
peerInfo.addrs.add(ma)
let identify = newIdentify(peerInfo)
proc createMplex(conn: Connection): Muxer =
result = newMplex(conn)
let mplexProvider = newMuxerProvider(createMplex, MplexCodec)
let transports = @[Transport(newTransport(TcpTransport))]
let muxers = [(MplexCodec, mplexProvider)].toTable()
let secureManagers = [(SecioCodec, Secure(newSecio(peerInfo.privateKey)))].toTable()
let switch = newSwitch(peerInfo,
transports,
identify,
muxers,
secureManagers)
result = (switch, peerInfo)
suite "Switch": suite "Switch":
test "e2e use switch": test "e2e use switch dial proto string":
proc createSwitch(ma: MultiAddress): (Switch, PeerInfo) {.gcsafe.}=
let seckey = PrivateKey.random(RSA)
var peerInfo: PeerInfo = PeerInfo.init(PrivateKey.random(RSA))
peerInfo.addrs.add(ma)
let identify = newIdentify(peerInfo)
proc createMplex(conn: Connection): Muxer =
result = newMplex(conn)
let mplexProvider = newMuxerProvider(createMplex, MplexCodec)
let transports = @[Transport(newTransport(TcpTransport))]
let muxers = [(MplexCodec, mplexProvider)].toTable()
let secureManagers = [(SecioCodec, Secure(newSecio(peerInfo.privateKey)))].toTable()
let switch = newSwitch(peerInfo,
transports,
identify,
muxers,
secureManagers)
result = (switch, peerInfo)
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