From 53a9d0fc246ef09ed2e12c3ff671b995aeef72e0 Mon Sep 17 00:00:00 2001 From: Christian Ulrich Date: Fri, 25 Sep 2020 21:05:57 +0200 Subject: [PATCH] use random source port for initiating connection too --- examples/app/app.nim | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/examples/app/app.nim b/examples/app/app.nim index 1c54fd3..1673b90 100644 --- a/examples/app/app.nim +++ b/examples/app/app.nim @@ -178,6 +178,7 @@ proc initServerConnection(serverHostname: string, serverPort: Port, sock.bindAddr(probingPort) except OSError as e: if failCount == 3: + echo "raising error" raise e failCount.inc await sleepAsync(100) @@ -210,15 +211,15 @@ proc runApp(serverHostname: string, serverPort: Port, peerId: string, punchdConn.inConnections = newFutureStream[AsyncSocket]("runApp") await punchdConn.sock.connectUnix("/tmp/punchd.socket") asyncCheck handlePunchdMessages(punchdConn) + let srcPort = rand(Port(1024) .. Port.high) if otherPeerId.len == 0: # register and wait for connections - let acceptPort = rand(Port(1024) .. Port.high) - echo &"init server connection, probing port: {acceptPort}" + echo &"init server connection, probing port: {srcPort}" let serverConn = await initServerConnection(serverHostname, serverPort, - acceptPort) + srcPort) asyncCheck handleServerMessages(serverConn) asyncCheck handlePeerNotifications(serverConn, punchdConn, peerId) - let myPorts = (@[acceptPort] & serverConn.probedPorts).join(",") + let myPorts = (@[srcPort] & serverConn.probedPorts).join(",") let req = &"{peerId}|{serverConn.probedIp}|{myPorts}" echo "registering: ", req discard await serverConn.sendRequest("register", req) @@ -234,7 +235,7 @@ proc runApp(serverHostname: string, serverPort: Port, peerId: string, else: # initiate a new connection var serverConn = await initServerConnection(serverHostname, serverPort, - Port(1234)) + srcPort) asyncCheck handleServerMessages(serverConn) let sock = await punchHole(punchdConn, serverConn, peerId, otherPeerId) echo "connected!"