source port may take a bit to become available again, so retry binding three times

This commit is contained in:
Christian Ulrich 2020-07-29 20:15:01 +02:00
parent d1b104c666
commit 7f40ab0082
No known key found for this signature in database
GPG Key ID: 8241BE099775A097
1 changed files with 10 additions and 2 deletions

View File

@ -166,10 +166,18 @@ proc punchHole(punchdConn: PunchdConnection, serverConn: ServerConnection,
proc initServerConnection(serverHostname: string, serverPort: Port,
probingPort: Port): Future[ServerConnection] {.async.} =
for i in 0 .. 1:
var failCount = 0
while result.probedPorts.len < 2:
# FIXME: error handling
let sock = newAsyncSocket()
try:
sock.bindAddr(probingPort)
except OSError as e:
if failCount == 3:
raise e
failCount.inc
await sleepAsync(1000)
continue
await sock.connect(serverHostname, serverPort)
let id = rand(uint32)
await sock.send(&"get-endpoint|{id}\n")