Skip to content

Commit

Permalink
Make single source for addresses calculation.
Browse files Browse the repository at this point in the history
  • Loading branch information
cheatfate committed Apr 2, 2024
1 parent 74c2bdc commit d8e1213
Showing 1 changed file with 20 additions and 29 deletions.
49 changes: 20 additions & 29 deletions chronos/transports/datagram.nim
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,24 @@ proc close*(transp: DatagramTransport) =
transp.state.incl({WriteClosed, ReadClosed})
closeSocket(transp.fd, continuation)

proc getTransportAddresses(
local, remote: Opt[IpAddress],
localPort, remotePort: Port
): tuple[local: TransportAddress, remote: TransportAddress] =
let
(localAuto, remoteAuto) = getAutoAddresses(localPort, remotePort)
lres =
if local.isSome():
initTAddress(local.get(), localPort)
else:
localAuto
rres =
if remote.isSome():
initTAddress(remote.get(), remotePort)
else:
remoteAuto
(lres, rres)

proc newDatagramTransportCommon(cbproc: UnsafeDatagramCallback,
remote: TransportAddress,
local: TransportAddress,
Expand Down Expand Up @@ -924,21 +942,7 @@ proc newDatagramTransport*(cbproc: DatagramCallback,
## ``Broadcast`` option).
let
(localHost, remoteHost) =
block:
let
(localAuto, remoteAuto) = getAutoAddresses(localPort, remotePort)
lres =
if local.isSome():
initTAddress(local.get(), localPort)
else:
localAuto
rres =
if remote.isSome():
initTAddress(remote.get(), remotePort)
else:
remoteAuto
(lres, rres)

getTransportAddresses(local, remote, localPort, remotePort)
newDatagramTransportCommon(cbproc, remoteHost, localHost, asyncInvalidSocket,
flags, cast[pointer](udata), child, bufSize,
ttl, dualstack)
Expand Down Expand Up @@ -972,20 +976,7 @@ proc newDatagramTransport*[T](cbproc: DatagramCallback,
raises: [TransportOsError].} =
let
(localHost, remoteHost) =
block:
let
(localAuto, remoteAuto) = getAutoAddresses(localPort, remotePort)
lres =
if local.isSome():
initTAddress(local.get(), localPort)
else:
localAuto
rres =
if remote.isSome():
initTAddress(remote.get(), remotePort)
else:
remoteAuto
(lres, rres)
getTransportAddresses(local, remote, localPort, remotePort)
fflags = flags + {GCUserData}
GC_ref(udata)
newDatagramTransportCommon(cbproc, remoteHost, localHost, asyncInvalidSocket,
Expand Down

0 comments on commit d8e1213

Please sign in to comment.