add getPort to resolve Port(0) (#17559)
* add getPort to resolve Port(0) * fixup * use getPort in examples + tests * address comments: do not re-export Port
This commit is contained in:
parent
5ecbe67371
commit
72988509ba
4 changed files with 32 additions and 18 deletions
|
|
@ -1,6 +1,7 @@
|
|||
import std/[nativesockets, asyncdispatch, os]
|
||||
|
||||
proc bindAvailablePort*(handle: SocketHandle, port = Port(0)): Port =
|
||||
## See also `asynchttpserver.getPort`.
|
||||
block:
|
||||
var name: Sockaddr_in
|
||||
name.sin_family = typeof(name.sin_family)(toInt(AF_INET))
|
||||
|
|
@ -8,5 +9,5 @@ proc bindAvailablePort*(handle: SocketHandle, port = Port(0)): Port =
|
|||
name.sin_addr.s_addr = htonl(INADDR_ANY)
|
||||
if bindAddr(handle, cast[ptr SockAddr](addr(name)),
|
||||
sizeof(name).Socklen) < 0'i32:
|
||||
raiseOSError(osLastError())
|
||||
raiseOSError(osLastError(), $port)
|
||||
result = getLocalAddr(handle, AF_INET)[1]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue