Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add automatic constructors for TCP and UDP transports. #512

Merged
merged 20 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions chronos/transports/common.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
{.push raises: [].}

import std/[strutils]
import results
import stew/[base10, byteutils]
import ".."/[config, asyncloop, osdefs, oserrno, handles]

Expand All @@ -18,7 +19,7 @@ from std/net import Domain, `==`, IpAddress, IpAddressFamily, parseIpAddress,
from std/nativesockets import toInt, `$`

export Domain, `==`, IpAddress, IpAddressFamily, parseIpAddress, SockType,
Protocol, Port, toInt, `$`
Protocol, Port, toInt, `$`, results

const
DefaultStreamBufferSize* = chronosTransportDefaultBufferSize
Expand All @@ -29,7 +30,7 @@ type
ServerFlags* = enum
## Server's flags
ReuseAddr, ReusePort, TcpNoDelay, NoAutoRead, GCUserData, FirstPipe,
NoPipeFlash, Broadcast
NoPipeFlash, Broadcast, V4Mapped

DualStackType* {.pure.} = enum
Auto, Enabled, Disabled, Default
Expand Down Expand Up @@ -200,6 +201,15 @@ proc `$`*(address: TransportAddress): string =
of AddressFamily.None:
"None"

proc toIpAddress*(address: TransportAddress): IpAddress =
case address.family
of AddressFamily.IPv4:
IpAddress(family: IpAddressFamily.IPv4, address_v4: address.address_v4)
of AddressFamily.IPv6:
IpAddress(family: IpAddressFamily.IPv6, address_v6: address.address_v6)
else:
raiseAssert "IpAddress do not support address family " & $address.family

proc toHex*(address: TransportAddress): string =
## Returns hexadecimal representation of ``address``.
case address.family
Expand Down Expand Up @@ -783,3 +793,25 @@ proc setDualstack*(socket: AsyncFD,
else:
? getDomain(socket)
setDualstack(socket, family, flag)

proc getAutoAddress*(port: Port): TransportAddress =
var res =
if isAvailable(AddressFamily.IPv6):
AnyAddress6
else:
AnyAddress
res.port = port
res

proc getAutoAddresses*(
localPort: Port,
remotePort: Port
): tuple[local: TransportAddress, remote: TransportAddress] =
var (local, remote) =
if isAvailable(AddressFamily.IPv6):
(AnyAddress6, AnyAddress6)
else:
(AnyAddress, AnyAddress)
local.port = localPort
remote.port = remotePort
(local, remote)
Loading
Loading