-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
5ecbe67
commit 7298850
Showing
4 changed files
with
32 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
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)) | ||
name.sin_port = htons(uint16(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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters