2018-05-16 08:22:34 +00:00
|
|
|
#
|
2019-02-06 14:49:11 +00:00
|
|
|
# Chronos Transport Common Types
|
|
|
|
# (c) Copyright 2018-Present
|
2018-05-16 08:22:34 +00:00
|
|
|
# Status Research & Development GmbH
|
|
|
|
#
|
|
|
|
# Licensed under either of
|
|
|
|
# Apache License, version 2.0, (LICENSE-APACHEv2)
|
|
|
|
# MIT license (LICENSE-MIT)
|
exception tracking (#166)
* exception tracking
This PR adds minimal exception tracking to chronos, moving the goalpost
one step further.
In particular, it becomes invalid to raise exceptions from `callSoon`
callbacks: this is critical for writing correct error handling because
there's no reasonable way that a user of chronos can possibly _reason_
about exceptions coming out of there: the event loop will be in an
indeterminite state when the loop is executing an _random_ callback.
As expected, there are several issues in the error handling of chronos:
in particular, it will end up in an inconsistent internal state whenever
the selector loop operations fail, because the internal state update
functions are not written in an exception-safe way. This PR turns this
into a Defect, which probably is not the optimal way of handling things
- expect more work to be done here.
Some API have no way of reporting back errors to callers - for example,
when something fails in the accept loop, there's not much it can do, and
no way to report it back to the user of the API - this has been fixed
with the new accept flow - the old one should be deprecated.
Finally, there is information loss in the API: in composite operations
like `poll` and `waitFor` there's no way to differentiate internal
errors from user-level errors originating from callbacks.
* store `CatchableError` in future
* annotate proc's with correct raises information
* `selectors2` to avoid non-CatchableError IOSelectorsException
* `$` should never raise
* remove unnecessary gcsafe annotations
* fix exceptions leaking out of timer waits
* fix some imports
* functions must signal raising the union of all exceptions across all
platforms to enable cross-platform code
* switch to unittest2
* add `selectors2` which supercedes the std library version and fixes
several exception handling issues in there
* fixes
* docs, platform-independent eh specifiers for some functions
* add feature flag for strict exception mode
also bump version to 3.0.0 - _most_ existing code should be compatible
with this version of exception handling but some things might need
fixing - callbacks, existing raises specifications etc.
* fix AsyncCheck for non-void T
2021-03-24 09:08:33 +00:00
|
|
|
|
2022-08-06 10:56:06 +00:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
exception tracking (#166)
* exception tracking
This PR adds minimal exception tracking to chronos, moving the goalpost
one step further.
In particular, it becomes invalid to raise exceptions from `callSoon`
callbacks: this is critical for writing correct error handling because
there's no reasonable way that a user of chronos can possibly _reason_
about exceptions coming out of there: the event loop will be in an
indeterminite state when the loop is executing an _random_ callback.
As expected, there are several issues in the error handling of chronos:
in particular, it will end up in an inconsistent internal state whenever
the selector loop operations fail, because the internal state update
functions are not written in an exception-safe way. This PR turns this
into a Defect, which probably is not the optimal way of handling things
- expect more work to be done here.
Some API have no way of reporting back errors to callers - for example,
when something fails in the accept loop, there's not much it can do, and
no way to report it back to the user of the API - this has been fixed
with the new accept flow - the old one should be deprecated.
Finally, there is information loss in the API: in composite operations
like `poll` and `waitFor` there's no way to differentiate internal
errors from user-level errors originating from callbacks.
* store `CatchableError` in future
* annotate proc's with correct raises information
* `selectors2` to avoid non-CatchableError IOSelectorsException
* `$` should never raise
* remove unnecessary gcsafe annotations
* fix exceptions leaking out of timer waits
* fix some imports
* functions must signal raising the union of all exceptions across all
platforms to enable cross-platform code
* switch to unittest2
* add `selectors2` which supercedes the std library version and fixes
several exception handling issues in there
* fixes
* docs, platform-independent eh specifiers for some functions
* add feature flag for strict exception mode
also bump version to 3.0.0 - _most_ existing code should be compatible
with this version of exception handling but some things might need
fixing - callbacks, existing raises specifications etc.
* fix AsyncCheck for non-void T
2021-03-24 09:08:33 +00:00
|
|
|
|
2023-04-30 06:20:08 +00:00
|
|
|
import std/[strutils]
|
2023-02-21 10:48:36 +00:00
|
|
|
import stew/[base10, byteutils]
|
2023-04-30 06:20:08 +00:00
|
|
|
import ".."/[asyncloop, osdefs, oserrno]
|
|
|
|
|
|
|
|
from std/net import Domain, `==`, IpAddress, IpAddressFamily, parseIpAddress,
|
|
|
|
SockType, Protocol, Port, `$`
|
|
|
|
from std/nativesockets import toInt, `$`
|
|
|
|
|
|
|
|
export Domain, `==`, IpAddress, IpAddressFamily, parseIpAddress, SockType,
|
2023-05-15 16:45:26 +00:00
|
|
|
Protocol, Port, toInt, `$`
|
2018-06-07 06:17:59 +00:00
|
|
|
|
2018-05-16 08:22:34 +00:00
|
|
|
const
|
|
|
|
DefaultStreamBufferSize* = 4096 ## Default buffer size for stream
|
|
|
|
## transports
|
|
|
|
DefaultDatagramBufferSize* = 65536 ## Default buffer size for datagram
|
|
|
|
## transports
|
|
|
|
type
|
|
|
|
ServerFlags* = enum
|
|
|
|
## Server's flags
|
2018-10-25 10:19:19 +00:00
|
|
|
ReuseAddr, ReusePort, TcpNoDelay, NoAutoRead, GCUserData, FirstPipe,
|
2019-03-30 22:31:10 +00:00
|
|
|
NoPipeFlash, Broadcast
|
2018-10-25 10:19:19 +00:00
|
|
|
|
|
|
|
AddressFamily* {.pure.} = enum
|
|
|
|
None, IPv4, IPv6, Unix
|
2018-05-16 08:22:34 +00:00
|
|
|
|
|
|
|
TransportAddress* = object
|
|
|
|
## Transport network address
|
2018-10-25 10:19:19 +00:00
|
|
|
case family*: AddressFamily
|
|
|
|
of AddressFamily.None:
|
|
|
|
discard
|
|
|
|
of AddressFamily.IPv4:
|
|
|
|
address_v4*: array[4, uint8]
|
|
|
|
of AddressFamily.IPv6:
|
|
|
|
address_v6*: array[16, uint8]
|
|
|
|
of AddressFamily.Unix:
|
|
|
|
address_un*: array[108, uint8]
|
|
|
|
port*: Port # Port number
|
2018-05-16 08:22:34 +00:00
|
|
|
|
|
|
|
ServerCommand* = enum
|
|
|
|
## Server's commands
|
|
|
|
Start, # Start server
|
|
|
|
Pause, # Pause server
|
|
|
|
Stop # Stop server
|
|
|
|
|
|
|
|
ServerStatus* = enum
|
|
|
|
## Server's statuses
|
|
|
|
Starting, # Server created
|
|
|
|
Stopped, # Server stopped
|
|
|
|
Running, # Server running
|
2018-06-04 09:57:17 +00:00
|
|
|
Closed # Server closed
|
2018-05-16 08:22:34 +00:00
|
|
|
|
2022-01-04 17:00:17 +00:00
|
|
|
when defined(windows) or defined(nimdoc):
|
2018-06-04 09:57:17 +00:00
|
|
|
type
|
|
|
|
SocketServer* = ref object of RootRef
|
|
|
|
## Socket server object
|
|
|
|
sock*: AsyncFD # Socket
|
|
|
|
local*: TransportAddress # Address
|
|
|
|
status*: ServerStatus # Current server status
|
|
|
|
udata*: pointer # User-defined pointer
|
|
|
|
flags*: set[ServerFlags] # Flags
|
|
|
|
bufferSize*: int # Size of internal transports' buffer
|
|
|
|
loopFuture*: Future[void] # Server's main Future
|
|
|
|
domain*: Domain # Current server domain (IPv4 or IPv6)
|
|
|
|
apending*: bool
|
|
|
|
asock*: AsyncFD # Current AcceptEx() socket
|
2020-06-24 08:21:52 +00:00
|
|
|
errorCode*: OSErrorCode # Current error code
|
2018-06-04 09:57:17 +00:00
|
|
|
abuffer*: array[128, byte] # Windows AcceptEx() buffer
|
2022-01-04 17:00:17 +00:00
|
|
|
when defined(windows):
|
2023-02-21 10:48:36 +00:00
|
|
|
aovl*: CustomOverlapped # AcceptEx OVERLAPPED structure
|
2018-06-04 09:57:17 +00:00
|
|
|
else:
|
|
|
|
type
|
|
|
|
SocketServer* = ref object of RootRef
|
|
|
|
## Socket server object
|
|
|
|
sock*: AsyncFD # Socket
|
|
|
|
local*: TransportAddress # Address
|
|
|
|
status*: ServerStatus # Current server status
|
|
|
|
udata*: pointer # User-defined pointer
|
|
|
|
flags*: set[ServerFlags] # Flags
|
|
|
|
bufferSize*: int # Size of internal transports' buffer
|
|
|
|
loopFuture*: Future[void] # Server's main Future
|
2020-06-24 08:21:52 +00:00
|
|
|
errorCode*: OSErrorCode # Current error code
|
2018-05-16 08:22:34 +00:00
|
|
|
|
2018-06-04 09:57:17 +00:00
|
|
|
type
|
2018-07-20 08:58:01 +00:00
|
|
|
TransportError* = object of AsyncError
|
2018-05-16 08:22:34 +00:00
|
|
|
## Transport's specific exception
|
|
|
|
TransportOsError* = object of TransportError
|
|
|
|
## Transport's OS specific exception
|
2018-10-03 00:44:39 +00:00
|
|
|
code*: OSErrorCode
|
2018-05-16 08:22:34 +00:00
|
|
|
TransportIncompleteError* = object of TransportError
|
|
|
|
## Transport's `incomplete data received` exception
|
|
|
|
TransportLimitError* = object of TransportError
|
|
|
|
## Transport's `data limit reached` exception
|
2018-06-10 00:55:19 +00:00
|
|
|
TransportAddressError* = object of TransportError
|
2018-10-03 00:44:39 +00:00
|
|
|
## Transport's address specific exception
|
|
|
|
code*: OSErrorCode
|
2018-10-25 10:19:19 +00:00
|
|
|
TransportNoSupport* = object of TransportError
|
|
|
|
## Transport's capability not supported exception
|
2020-03-05 09:59:10 +00:00
|
|
|
TransportUseClosedError* = object of TransportError
|
|
|
|
## Usage after transport close exception
|
2020-06-24 08:21:52 +00:00
|
|
|
TransportTooManyError* = object of TransportError
|
|
|
|
## Too many open file descriptors exception
|
2021-11-12 16:13:56 +00:00
|
|
|
TransportAbortedError* = object of TransportError
|
|
|
|
## Remote client disconnected before server accepts connection
|
2018-05-16 08:22:34 +00:00
|
|
|
|
|
|
|
TransportState* = enum
|
|
|
|
## Transport's state
|
|
|
|
ReadPending, # Read operation pending (Windows)
|
|
|
|
ReadPaused, # Read operations paused
|
|
|
|
ReadClosed, # Read operations closed
|
|
|
|
ReadEof, # Read at EOF
|
|
|
|
ReadError, # Read error
|
|
|
|
WritePending, # Writer operation pending (Windows)
|
|
|
|
WritePaused, # Writer operations paused
|
|
|
|
WriteClosed, # Writer operations closed
|
2019-05-28 06:29:00 +00:00
|
|
|
WriteEof, # Remote peer disconnected
|
2018-05-16 08:22:34 +00:00
|
|
|
WriteError # Write error
|
|
|
|
|
|
|
|
var
|
2018-10-25 10:19:19 +00:00
|
|
|
AnyAddress* = TransportAddress(family: AddressFamily.IPv4, port: Port(0))
|
|
|
|
## Default INADDR_ANY address for IPv4
|
|
|
|
AnyAddress6* = TransportAddress(family: AddressFamily.IPv6, port: Port(0))
|
|
|
|
## Default INADDR_ANY address for IPv6
|
|
|
|
|
|
|
|
proc `==`*(lhs, rhs: TransportAddress): bool =
|
|
|
|
## Compare two transport addresses ``lhs`` and ``rhs``. Return ``true`` if
|
|
|
|
## addresses are equal.
|
2023-02-16 16:18:05 +00:00
|
|
|
if (lhs.family != rhs.family): return false
|
2021-04-09 21:39:54 +00:00
|
|
|
case lhs.family
|
|
|
|
of AddressFamily.None:
|
|
|
|
true
|
|
|
|
of AddressFamily.IPv4:
|
2023-02-16 16:18:05 +00:00
|
|
|
if lhs.port != rhs.port: return false
|
|
|
|
lhs.address_v4 == rhs.address_v4
|
2021-04-09 21:39:54 +00:00
|
|
|
of AddressFamily.IPv6:
|
2023-02-16 16:18:05 +00:00
|
|
|
if lhs.port != rhs.port: return false
|
|
|
|
lhs.address_v6 == rhs.address_v6
|
2021-04-09 21:39:54 +00:00
|
|
|
of AddressFamily.Unix:
|
|
|
|
equalMem(unsafeAddr lhs.address_un[0],
|
|
|
|
unsafeAddr rhs.address_un[0], sizeof(lhs.address_un))
|
2018-05-16 08:22:34 +00:00
|
|
|
|
2018-05-28 23:35:15 +00:00
|
|
|
proc getDomain*(address: TransportAddress): Domain =
|
|
|
|
## Returns OS specific Domain from TransportAddress.
|
2018-10-25 10:19:19 +00:00
|
|
|
case address.family
|
|
|
|
of AddressFamily.IPv4:
|
2021-04-09 21:39:54 +00:00
|
|
|
Domain.AF_INET
|
2018-10-25 10:19:19 +00:00
|
|
|
of AddressFamily.IPv6:
|
2021-04-09 21:39:54 +00:00
|
|
|
Domain.AF_INET6
|
2018-10-25 10:19:19 +00:00
|
|
|
of AddressFamily.Unix:
|
|
|
|
when defined(windows):
|
2021-04-09 21:39:54 +00:00
|
|
|
cast[Domain](1)
|
2018-10-25 10:19:19 +00:00
|
|
|
else:
|
2021-04-09 21:39:54 +00:00
|
|
|
Domain.AF_UNIX
|
2018-10-25 10:19:19 +00:00
|
|
|
else:
|
2021-04-09 21:39:54 +00:00
|
|
|
cast[Domain](0)
|
2018-05-28 23:35:15 +00:00
|
|
|
|
2018-05-16 08:22:34 +00:00
|
|
|
proc `$`*(address: TransportAddress): string =
|
|
|
|
## Returns string representation of ``address``.
|
2018-10-25 10:19:19 +00:00
|
|
|
case address.family
|
|
|
|
of AddressFamily.IPv4:
|
2021-04-09 21:39:54 +00:00
|
|
|
var a = IpAddress(family: IpAddressFamily.IPv4,
|
|
|
|
address_v4: address.address_v4)
|
|
|
|
var res = $a
|
|
|
|
res.add(":")
|
|
|
|
res.add(Base10.toString(uint16(address.port)))
|
|
|
|
res
|
2018-10-25 10:19:19 +00:00
|
|
|
of AddressFamily.IPv6:
|
|
|
|
var a = IpAddress(family: IpAddressFamily.IPv6,
|
|
|
|
address_v6: address.address_v6)
|
2021-04-09 21:39:54 +00:00
|
|
|
var res = "[" & $a & "]:"
|
|
|
|
res.add(Base10.toString(uint16(address.port)))
|
|
|
|
res
|
2018-10-25 10:19:19 +00:00
|
|
|
of AddressFamily.Unix:
|
|
|
|
const length = sizeof(address.address_un) + 1
|
|
|
|
var buffer: array[length, char]
|
|
|
|
if not equalMem(addr buffer[0], unsafeAddr address.address_un[0],
|
|
|
|
sizeof(address.address_un)):
|
|
|
|
copyMem(addr buffer[0], unsafeAddr address.address_un[0],
|
|
|
|
sizeof(address.address_un))
|
2021-04-09 21:39:54 +00:00
|
|
|
$cast[cstring](addr buffer)
|
2018-10-25 10:19:19 +00:00
|
|
|
else:
|
2021-04-09 21:39:54 +00:00
|
|
|
"/"
|
2023-02-16 16:18:05 +00:00
|
|
|
of AddressFamily.None:
|
|
|
|
"None"
|
|
|
|
|
|
|
|
proc toHex*(address: TransportAddress): string =
|
|
|
|
## Returns hexadecimal representation of ``address`.
|
|
|
|
case address.family
|
|
|
|
of AddressFamily.IPv4:
|
|
|
|
"0x" & address.address_v4.toHex()
|
|
|
|
of AddressFamily.IPv6:
|
|
|
|
"0x" & address.address_v6.toHex()
|
|
|
|
of AddressFamily.Unix:
|
|
|
|
"0x" & address.address_un.toHex()
|
|
|
|
of AddressFamily.None:
|
|
|
|
"None"
|
2018-05-16 08:22:34 +00:00
|
|
|
|
exception tracking (#166)
* exception tracking
This PR adds minimal exception tracking to chronos, moving the goalpost
one step further.
In particular, it becomes invalid to raise exceptions from `callSoon`
callbacks: this is critical for writing correct error handling because
there's no reasonable way that a user of chronos can possibly _reason_
about exceptions coming out of there: the event loop will be in an
indeterminite state when the loop is executing an _random_ callback.
As expected, there are several issues in the error handling of chronos:
in particular, it will end up in an inconsistent internal state whenever
the selector loop operations fail, because the internal state update
functions are not written in an exception-safe way. This PR turns this
into a Defect, which probably is not the optimal way of handling things
- expect more work to be done here.
Some API have no way of reporting back errors to callers - for example,
when something fails in the accept loop, there's not much it can do, and
no way to report it back to the user of the API - this has been fixed
with the new accept flow - the old one should be deprecated.
Finally, there is information loss in the API: in composite operations
like `poll` and `waitFor` there's no way to differentiate internal
errors from user-level errors originating from callbacks.
* store `CatchableError` in future
* annotate proc's with correct raises information
* `selectors2` to avoid non-CatchableError IOSelectorsException
* `$` should never raise
* remove unnecessary gcsafe annotations
* fix exceptions leaking out of timer waits
* fix some imports
* functions must signal raising the union of all exceptions across all
platforms to enable cross-platform code
* switch to unittest2
* add `selectors2` which supercedes the std library version and fixes
several exception handling issues in there
* fixes
* docs, platform-independent eh specifiers for some functions
* add feature flag for strict exception mode
also bump version to 3.0.0 - _most_ existing code should be compatible
with this version of exception handling but some things might need
fixing - callbacks, existing raises specifications etc.
* fix AsyncCheck for non-void T
2021-03-24 09:08:33 +00:00
|
|
|
proc initTAddress*(address: string): TransportAddress {.
|
|
|
|
raises: [Defect, TransportAddressError].} =
|
2018-10-25 10:19:19 +00:00
|
|
|
## Parses string representation of ``address``. ``address`` can be IPv4, IPv6
|
|
|
|
## or Unix domain address.
|
2018-05-29 09:59:39 +00:00
|
|
|
##
|
2018-05-21 21:52:57 +00:00
|
|
|
## IPv4 transport address format is ``a.b.c.d:port``.
|
|
|
|
## IPv6 transport address format is ``[::]:port``.
|
2018-10-25 10:19:19 +00:00
|
|
|
## Unix transport address format is ``/address``.
|
|
|
|
if len(address) > 0:
|
|
|
|
if address[0] == '/':
|
2021-04-09 21:39:54 +00:00
|
|
|
var res = TransportAddress(family: AddressFamily.Unix, port: Port(1))
|
|
|
|
let size = if len(address) < (sizeof(res.address_un) - 1): len(address)
|
|
|
|
else: (sizeof(res.address_un) - 1)
|
|
|
|
copyMem(addr res.address_un[0], unsafeAddr address[0], size)
|
|
|
|
res
|
2018-06-10 00:55:19 +00:00
|
|
|
else:
|
2021-04-09 21:39:54 +00:00
|
|
|
let parts =
|
|
|
|
block:
|
|
|
|
let res = address.rsplit(":", maxsplit = 1)
|
|
|
|
if len(res) != 2:
|
|
|
|
raise newException(TransportAddressError,
|
|
|
|
"Format is <address>:<port>!")
|
|
|
|
res
|
|
|
|
let port =
|
|
|
|
block:
|
|
|
|
let res = Base10.decode(uint16, parts[1])
|
|
|
|
if res.isErr():
|
|
|
|
raise newException(TransportAddressError,
|
|
|
|
"Invalid port number!")
|
|
|
|
res.get()
|
|
|
|
|
|
|
|
let ipaddr =
|
|
|
|
try:
|
|
|
|
if parts[0][0] == '[' and parts[0][^1] == ']':
|
|
|
|
parseIpAddress(parts[0][1..^2])
|
|
|
|
else:
|
|
|
|
parseIpAddress(parts[0])
|
|
|
|
except CatchableError as exc:
|
|
|
|
raise newException(TransportAddressError, exc.msg)
|
|
|
|
|
|
|
|
case ipaddr.family
|
|
|
|
of IpAddressFamily.IPv4:
|
|
|
|
TransportAddress(family: AddressFamily.IPv4,
|
|
|
|
address_v4: ipaddr.address_v4, port: Port(port))
|
|
|
|
of IpAddressFamily.IPv6:
|
|
|
|
TransportAddress(family: AddressFamily.IPv6,
|
|
|
|
address_v6: ipaddr.address_v6, port: Port(port))
|
2018-10-25 10:19:19 +00:00
|
|
|
else:
|
2021-04-09 21:39:54 +00:00
|
|
|
TransportAddress(family: AddressFamily.Unix)
|
2018-05-16 08:22:34 +00:00
|
|
|
|
exception tracking (#166)
* exception tracking
This PR adds minimal exception tracking to chronos, moving the goalpost
one step further.
In particular, it becomes invalid to raise exceptions from `callSoon`
callbacks: this is critical for writing correct error handling because
there's no reasonable way that a user of chronos can possibly _reason_
about exceptions coming out of there: the event loop will be in an
indeterminite state when the loop is executing an _random_ callback.
As expected, there are several issues in the error handling of chronos:
in particular, it will end up in an inconsistent internal state whenever
the selector loop operations fail, because the internal state update
functions are not written in an exception-safe way. This PR turns this
into a Defect, which probably is not the optimal way of handling things
- expect more work to be done here.
Some API have no way of reporting back errors to callers - for example,
when something fails in the accept loop, there's not much it can do, and
no way to report it back to the user of the API - this has been fixed
with the new accept flow - the old one should be deprecated.
Finally, there is information loss in the API: in composite operations
like `poll` and `waitFor` there's no way to differentiate internal
errors from user-level errors originating from callbacks.
* store `CatchableError` in future
* annotate proc's with correct raises information
* `selectors2` to avoid non-CatchableError IOSelectorsException
* `$` should never raise
* remove unnecessary gcsafe annotations
* fix exceptions leaking out of timer waits
* fix some imports
* functions must signal raising the union of all exceptions across all
platforms to enable cross-platform code
* switch to unittest2
* add `selectors2` which supercedes the std library version and fixes
several exception handling issues in there
* fixes
* docs, platform-independent eh specifiers for some functions
* add feature flag for strict exception mode
also bump version to 3.0.0 - _most_ existing code should be compatible
with this version of exception handling but some things might need
fixing - callbacks, existing raises specifications etc.
* fix AsyncCheck for non-void T
2021-03-24 09:08:33 +00:00
|
|
|
proc initTAddress*(address: string, port: Port): TransportAddress {.
|
|
|
|
raises: [Defect, TransportAddressError].} =
|
2018-10-25 10:19:19 +00:00
|
|
|
## Initialize ``TransportAddress`` with IP (IPv4 or IPv6) address ``address``
|
|
|
|
## and port number ``port``.
|
2021-04-09 21:39:54 +00:00
|
|
|
let ipaddr =
|
|
|
|
try:
|
|
|
|
parseIpAddress(address)
|
|
|
|
except CatchableError as exc:
|
|
|
|
raise newException(TransportAddressError, exc.msg)
|
|
|
|
|
|
|
|
case ipaddr.family
|
|
|
|
of IpAddressFamily.IPv4:
|
|
|
|
TransportAddress(family: AddressFamily.IPv4,
|
|
|
|
address_v4: ipaddr.address_v4, port: port)
|
|
|
|
of IpAddressFamily.IPv6:
|
|
|
|
TransportAddress(family: AddressFamily.IPv6,
|
|
|
|
address_v6: ipaddr.address_v6, port: port)
|
2018-06-02 14:25:26 +00:00
|
|
|
|
exception tracking (#166)
* exception tracking
This PR adds minimal exception tracking to chronos, moving the goalpost
one step further.
In particular, it becomes invalid to raise exceptions from `callSoon`
callbacks: this is critical for writing correct error handling because
there's no reasonable way that a user of chronos can possibly _reason_
about exceptions coming out of there: the event loop will be in an
indeterminite state when the loop is executing an _random_ callback.
As expected, there are several issues in the error handling of chronos:
in particular, it will end up in an inconsistent internal state whenever
the selector loop operations fail, because the internal state update
functions are not written in an exception-safe way. This PR turns this
into a Defect, which probably is not the optimal way of handling things
- expect more work to be done here.
Some API have no way of reporting back errors to callers - for example,
when something fails in the accept loop, there's not much it can do, and
no way to report it back to the user of the API - this has been fixed
with the new accept flow - the old one should be deprecated.
Finally, there is information loss in the API: in composite operations
like `poll` and `waitFor` there's no way to differentiate internal
errors from user-level errors originating from callbacks.
* store `CatchableError` in future
* annotate proc's with correct raises information
* `selectors2` to avoid non-CatchableError IOSelectorsException
* `$` should never raise
* remove unnecessary gcsafe annotations
* fix exceptions leaking out of timer waits
* fix some imports
* functions must signal raising the union of all exceptions across all
platforms to enable cross-platform code
* switch to unittest2
* add `selectors2` which supercedes the std library version and fixes
several exception handling issues in there
* fixes
* docs, platform-independent eh specifiers for some functions
* add feature flag for strict exception mode
also bump version to 3.0.0 - _most_ existing code should be compatible
with this version of exception handling but some things might need
fixing - callbacks, existing raises specifications etc.
* fix AsyncCheck for non-void T
2021-03-24 09:08:33 +00:00
|
|
|
proc initTAddress*(address: string, port: int): TransportAddress {.
|
|
|
|
raises: [Defect, TransportAddressError].} =
|
2018-10-25 10:19:19 +00:00
|
|
|
## Initialize ``TransportAddress`` with IP (IPv4 or IPv6) address ``address``
|
|
|
|
## and port number ``port``.
|
2021-04-09 21:39:54 +00:00
|
|
|
if port < 0 or port > 65535:
|
2018-06-10 00:55:19 +00:00
|
|
|
raise newException(TransportAddressError, "Illegal port number!")
|
2021-04-09 21:39:54 +00:00
|
|
|
initTAddress(address, Port(port))
|
2018-06-10 00:55:19 +00:00
|
|
|
|
2018-06-14 07:15:31 +00:00
|
|
|
proc initTAddress*(address: IpAddress, port: Port): TransportAddress =
|
|
|
|
## Initialize ``TransportAddress`` with net.nim ``IpAddress`` and
|
|
|
|
## port number ``port``.
|
2020-05-15 11:09:21 +00:00
|
|
|
case address.family
|
|
|
|
of IpAddressFamily.IPv4:
|
2021-04-09 21:39:54 +00:00
|
|
|
TransportAddress(family: AddressFamily.IPv4,
|
|
|
|
address_v4: address.address_v4, port: port)
|
2020-05-15 11:09:21 +00:00
|
|
|
of IpAddressFamily.IPv6:
|
2021-04-09 21:39:54 +00:00
|
|
|
TransportAddress(family: AddressFamily.IPv6,
|
|
|
|
address_v6: address.address_v6, port: port)
|
2018-06-14 07:15:31 +00:00
|
|
|
|
2018-06-10 00:55:19 +00:00
|
|
|
proc getAddrInfo(address: string, port: Port, domain: Domain,
|
|
|
|
sockType: SockType = SockType.SOCK_STREAM,
|
exception tracking (#166)
* exception tracking
This PR adds minimal exception tracking to chronos, moving the goalpost
one step further.
In particular, it becomes invalid to raise exceptions from `callSoon`
callbacks: this is critical for writing correct error handling because
there's no reasonable way that a user of chronos can possibly _reason_
about exceptions coming out of there: the event loop will be in an
indeterminite state when the loop is executing an _random_ callback.
As expected, there are several issues in the error handling of chronos:
in particular, it will end up in an inconsistent internal state whenever
the selector loop operations fail, because the internal state update
functions are not written in an exception-safe way. This PR turns this
into a Defect, which probably is not the optimal way of handling things
- expect more work to be done here.
Some API have no way of reporting back errors to callers - for example,
when something fails in the accept loop, there's not much it can do, and
no way to report it back to the user of the API - this has been fixed
with the new accept flow - the old one should be deprecated.
Finally, there is information loss in the API: in composite operations
like `poll` and `waitFor` there's no way to differentiate internal
errors from user-level errors originating from callbacks.
* store `CatchableError` in future
* annotate proc's with correct raises information
* `selectors2` to avoid non-CatchableError IOSelectorsException
* `$` should never raise
* remove unnecessary gcsafe annotations
* fix exceptions leaking out of timer waits
* fix some imports
* functions must signal raising the union of all exceptions across all
platforms to enable cross-platform code
* switch to unittest2
* add `selectors2` which supercedes the std library version and fixes
several exception handling issues in there
* fixes
* docs, platform-independent eh specifiers for some functions
* add feature flag for strict exception mode
also bump version to 3.0.0 - _most_ existing code should be compatible
with this version of exception handling but some things might need
fixing - callbacks, existing raises specifications etc.
* fix AsyncCheck for non-void T
2021-03-24 09:08:33 +00:00
|
|
|
protocol: Protocol = Protocol.IPPROTO_TCP): ptr AddrInfo {.
|
|
|
|
raises: [Defect, TransportAddressError].} =
|
2018-06-10 00:55:19 +00:00
|
|
|
## We have this one copy of ``getAddrInfo()`` because of AI_V4MAPPED in
|
|
|
|
## ``net.nim:getAddrInfo()``, which is not cross-platform.
|
|
|
|
var hints: AddrInfo
|
2021-04-09 21:39:54 +00:00
|
|
|
var res: ptr AddrInfo = nil
|
2018-06-10 00:55:19 +00:00
|
|
|
hints.ai_family = toInt(domain)
|
|
|
|
hints.ai_socktype = toInt(sockType)
|
|
|
|
hints.ai_protocol = toInt(protocol)
|
2022-11-02 07:09:15 +00:00
|
|
|
var gaiRes = getaddrinfo(address, cstring(Base10.toString(uint16(port))),
|
2021-04-09 21:39:54 +00:00
|
|
|
addr(hints), res)
|
|
|
|
if gaiRes != 0'i32:
|
2022-01-04 17:00:17 +00:00
|
|
|
when defined(windows) or defined(nimdoc):
|
2018-06-10 00:55:19 +00:00
|
|
|
raise newException(TransportAddressError, osErrorMsg(osLastError()))
|
|
|
|
else:
|
2021-04-09 21:39:54 +00:00
|
|
|
raise newException(TransportAddressError, $gai_strerror(gaiRes))
|
|
|
|
res
|
2018-06-02 14:25:26 +00:00
|
|
|
|
2021-12-08 14:58:24 +00:00
|
|
|
proc fromSAddr*(sa: ptr Sockaddr_storage, sl: SockLen,
|
2018-10-25 10:19:19 +00:00
|
|
|
address: var TransportAddress) =
|
|
|
|
## Set transport address ``address`` with value from OS specific socket
|
|
|
|
## address storage.
|
|
|
|
if int(sa.ss_family) == toInt(Domain.AF_INET) and
|
|
|
|
int(sl) == sizeof(Sockaddr_in):
|
|
|
|
address = TransportAddress(family: AddressFamily.IPv4)
|
|
|
|
let s = cast[ptr Sockaddr_in](sa)
|
|
|
|
copyMem(addr address.address_v4[0], addr s.sin_addr,
|
|
|
|
sizeof(address.address_v4))
|
|
|
|
address.port = Port(nativesockets.ntohs(s.sin_port))
|
|
|
|
elif int(sa.ss_family) == toInt(Domain.AF_INET6) and
|
|
|
|
int(sl) == sizeof(Sockaddr_in6):
|
|
|
|
address = TransportAddress(family: AddressFamily.IPv6)
|
|
|
|
let s = cast[ptr Sockaddr_in6](sa)
|
|
|
|
copyMem(addr address.address_v6[0], addr s.sin6_addr,
|
|
|
|
sizeof(address.address_v6))
|
|
|
|
address.port = Port(nativesockets.ntohs(s.sin6_port))
|
|
|
|
elif int(sa.ss_family) == toInt(Domain.AF_UNIX):
|
2022-01-04 17:00:17 +00:00
|
|
|
when not defined(windows) and not defined(nimdoc):
|
2018-10-25 10:19:19 +00:00
|
|
|
address = TransportAddress(family: AddressFamily.Unix)
|
|
|
|
if int(sl) > sizeof(sa.ss_family):
|
|
|
|
var length = int(sl) - sizeof(sa.ss_family)
|
|
|
|
if length > (sizeof(address.address_un) - 1):
|
|
|
|
length = sizeof(address.address_un) - 1
|
|
|
|
let s = cast[ptr Sockaddr_un](sa)
|
|
|
|
copyMem(addr address.address_un[0], addr s.sun_path[0], length)
|
|
|
|
address.port = Port(1)
|
|
|
|
else:
|
|
|
|
discard
|
|
|
|
|
|
|
|
proc toSAddr*(address: TransportAddress, sa: var Sockaddr_storage,
|
2021-12-08 14:58:24 +00:00
|
|
|
sl: var SockLen) =
|
2018-10-25 10:19:19 +00:00
|
|
|
## Set socket OS specific socket address storage with address from transport
|
|
|
|
## address ``address``.
|
|
|
|
case address.family
|
|
|
|
of AddressFamily.IPv4:
|
2021-12-08 14:58:24 +00:00
|
|
|
sl = SockLen(sizeof(Sockaddr_in))
|
2018-10-25 10:19:19 +00:00
|
|
|
let s = cast[ptr Sockaddr_in](addr sa)
|
|
|
|
s.sin_family = type(s.sin_family)(toInt(Domain.AF_INET))
|
|
|
|
s.sin_port = nativesockets.htons(uint16(address.port))
|
|
|
|
copyMem(addr s.sin_addr, unsafeAddr address.address_v4[0],
|
|
|
|
sizeof(s.sin_addr))
|
|
|
|
of AddressFamily.IPv6:
|
2021-12-08 14:58:24 +00:00
|
|
|
sl = SockLen(sizeof(Sockaddr_in6))
|
2018-10-25 10:19:19 +00:00
|
|
|
let s = cast[ptr Sockaddr_in6](addr sa)
|
|
|
|
s.sin6_family = type(s.sin6_family)(toInt(Domain.AF_INET6))
|
|
|
|
s.sin6_port = nativesockets.htons(uint16(address.port))
|
|
|
|
copyMem(addr s.sin6_addr, unsafeAddr address.address_v6[0],
|
|
|
|
sizeof(s.sin6_addr))
|
|
|
|
of AddressFamily.Unix:
|
2022-01-04 17:00:17 +00:00
|
|
|
when not defined(windows) and not defined(nimdoc):
|
2018-10-25 10:19:19 +00:00
|
|
|
if address.port == Port(0):
|
2021-12-08 14:58:24 +00:00
|
|
|
sl = SockLen(sizeof(sa.ss_family))
|
2018-10-25 10:19:19 +00:00
|
|
|
else:
|
|
|
|
let s = cast[ptr Sockaddr_un](addr sa)
|
|
|
|
var name = cast[cstring](unsafeAddr address.address_un[0])
|
2021-12-08 14:58:24 +00:00
|
|
|
sl = SockLen(sizeof(sa.ss_family) + len(name) + 1)
|
2018-10-25 10:19:19 +00:00
|
|
|
s.sun_family = type(s.sun_family)(toInt(Domain.AF_UNIX))
|
|
|
|
copyMem(addr s.sun_path, unsafeAddr address.address_un[0],
|
|
|
|
len(name) + 1)
|
|
|
|
else:
|
|
|
|
discard
|
|
|
|
|
2021-05-07 20:52:24 +00:00
|
|
|
proc address*(ta: TransportAddress): IpAddress {.
|
|
|
|
raises: [Defect, ValueError].} =
|
2018-10-27 14:19:58 +00:00
|
|
|
## Converts ``TransportAddress`` to ``net.IpAddress`` object.
|
2019-02-06 14:49:11 +00:00
|
|
|
##
|
2018-10-27 14:19:58 +00:00
|
|
|
## Note its impossible to convert ``TransportAddress`` of ``Unix`` family,
|
|
|
|
## because ``IpAddress`` supports only IPv4, IPv6 addresses.
|
2021-04-09 21:39:54 +00:00
|
|
|
case ta.family
|
|
|
|
of AddressFamily.IPv4:
|
|
|
|
IpAddress(family: IpAddressFamily.IPv4, address_v4: ta.address_v4)
|
|
|
|
of AddressFamily.IPv6:
|
|
|
|
IpAddress(family: IpAddressFamily.IPv6, address_v6: ta.address_v6)
|
2018-10-27 14:19:58 +00:00
|
|
|
else:
|
|
|
|
raise newException(ValueError, "IpAddress supports only IPv4/IPv6!")
|
|
|
|
|
2021-05-10 07:26:36 +00:00
|
|
|
proc host*(ta: TransportAddress): string {.raises: [Defect].} =
|
|
|
|
## Returns ``host`` of TransportAddress ``ta``.
|
|
|
|
##
|
|
|
|
## For IPv4 and IPv6 addresses it will return IP address as string, or empty
|
|
|
|
## string for Unix address.
|
|
|
|
case ta.family
|
|
|
|
of AddressFamily.IPv4:
|
|
|
|
$IpAddress(family: IpAddressFamily.IPv4, address_v4: ta.address_v4)
|
|
|
|
of AddressFamily.IPv6:
|
|
|
|
let a = $IpAddress(family: IpAddressFamily.IPv6,
|
|
|
|
address_v6: ta.address_v6)
|
|
|
|
"[" & a & "]"
|
|
|
|
else:
|
|
|
|
""
|
|
|
|
|
2021-04-09 21:39:54 +00:00
|
|
|
proc resolveTAddress*(address: string, port: Port,
|
|
|
|
domain: Domain): seq[TransportAddress] {.
|
|
|
|
raises: [Defect, TransportAddressError].} =
|
|
|
|
var res: seq[TransportAddress]
|
2021-09-15 13:55:15 +00:00
|
|
|
let aiList = getAddrInfo(address, port, domain)
|
2021-04-09 21:39:54 +00:00
|
|
|
var it = aiList
|
|
|
|
while not(isNil(it)):
|
|
|
|
var ta: TransportAddress
|
|
|
|
fromSAddr(cast[ptr Sockaddr_storage](it.ai_addr),
|
|
|
|
SockLen(it.ai_addrlen), ta)
|
|
|
|
# For some reason getAddrInfo() sometimes returns duplicate addresses,
|
|
|
|
# for example getAddrInfo(`localhost`) returns `127.0.0.1` twice.
|
|
|
|
if ta notin res:
|
|
|
|
res.add(ta)
|
|
|
|
it = it.ai_next
|
|
|
|
res
|
|
|
|
|
|
|
|
proc resolveTAddress*(address: string, domain: Domain): seq[TransportAddress] {.
|
|
|
|
raises: [Defect, TransportAddressError].} =
|
|
|
|
let parts =
|
|
|
|
block:
|
|
|
|
let res = address.rsplit(":", maxsplit = 1)
|
|
|
|
if len(res) != 2:
|
|
|
|
raise newException(TransportAddressError, "Format is <address>:<port>!")
|
|
|
|
res
|
|
|
|
let port =
|
|
|
|
block:
|
|
|
|
let res = Base10.decode(uint16, parts[1])
|
|
|
|
if res.isErr():
|
|
|
|
raise newException(TransportAddressError, "Invalid port number!")
|
|
|
|
res.get()
|
|
|
|
let hostname =
|
|
|
|
if parts[0][0] == '[' and parts[0][^1] == ']':
|
|
|
|
# IPv6 numeric addresses must be enclosed with `[]`.
|
|
|
|
parts[0][1..^2]
|
|
|
|
else:
|
|
|
|
parts[0]
|
|
|
|
resolveTAddress(hostname, Port(port), domain)
|
|
|
|
|
|
|
|
proc resolveTAddress*(address: string): seq[TransportAddress] {.
|
|
|
|
raises: [Defect, TransportAddressError].} =
|
2018-06-02 14:25:26 +00:00
|
|
|
## Resolve string representation of ``address``.
|
2018-06-05 22:48:03 +00:00
|
|
|
##
|
2018-06-02 14:25:26 +00:00
|
|
|
## Supported formats are:
|
|
|
|
## IPv4 numeric address ``a.b.c.d:port``
|
|
|
|
## IPv6 numeric address ``[::]:port``
|
|
|
|
## Hostname address ``hostname:port``
|
2018-06-05 22:48:03 +00:00
|
|
|
##
|
2018-06-02 14:25:26 +00:00
|
|
|
## If hostname address is detected, then network address translation via DNS
|
|
|
|
## will be performed.
|
2021-04-09 21:39:54 +00:00
|
|
|
resolveTAddress(address, Domain.AF_UNSPEC)
|
2018-06-10 00:55:19 +00:00
|
|
|
|
2021-04-09 21:39:54 +00:00
|
|
|
proc resolveTAddress*(address: string, port: Port): seq[TransportAddress] {.
|
|
|
|
raises: [Defect, TransportAddressError].} =
|
|
|
|
## Resolve string representation of ``address``.
|
|
|
|
##
|
|
|
|
## Supported formats are:
|
|
|
|
## IPv4 numeric address ``a.b.c.d:port``
|
|
|
|
## IPv6 numeric address ``[::]:port``
|
|
|
|
## Hostname address ``hostname:port``
|
|
|
|
##
|
|
|
|
## If hostname address is detected, then network address translation via DNS
|
|
|
|
## will be performed.
|
|
|
|
resolveTAddress(address, port, Domain.AF_UNSPEC)
|
2019-03-27 19:42:46 +00:00
|
|
|
|
2021-04-09 21:39:54 +00:00
|
|
|
proc resolveTAddress*(address: string,
|
|
|
|
family: AddressFamily): seq[TransportAddress] {.
|
|
|
|
raises: [Defect, TransportAddressError].} =
|
|
|
|
## Resolve string representation of ``address``.
|
|
|
|
##
|
|
|
|
## Supported formats are:
|
|
|
|
## IPv4 numeric address ``a.b.c.d:port``
|
|
|
|
## IPv6 numeric address ``[::]:port``
|
|
|
|
## Hostname address ``hostname:port``
|
|
|
|
##
|
|
|
|
## If hostname address is detected, then network address translation via DNS
|
|
|
|
## will be performed.
|
|
|
|
case family
|
|
|
|
of AddressFamily.IPv4:
|
|
|
|
resolveTAddress(address, Domain.AF_INET)
|
|
|
|
of AddressFamily.IPv6:
|
|
|
|
resolveTAddress(address, Domain.AF_INET6)
|
2018-06-02 14:25:26 +00:00
|
|
|
else:
|
2021-04-09 21:39:54 +00:00
|
|
|
raiseAssert("Unable to resolve non-internet address")
|
2018-06-02 14:25:26 +00:00
|
|
|
|
2018-06-06 22:15:31 +00:00
|
|
|
proc resolveTAddress*(address: string, port: Port,
|
2021-04-09 21:39:54 +00:00
|
|
|
family: AddressFamily): seq[TransportAddress] {.
|
exception tracking (#166)
* exception tracking
This PR adds minimal exception tracking to chronos, moving the goalpost
one step further.
In particular, it becomes invalid to raise exceptions from `callSoon`
callbacks: this is critical for writing correct error handling because
there's no reasonable way that a user of chronos can possibly _reason_
about exceptions coming out of there: the event loop will be in an
indeterminite state when the loop is executing an _random_ callback.
As expected, there are several issues in the error handling of chronos:
in particular, it will end up in an inconsistent internal state whenever
the selector loop operations fail, because the internal state update
functions are not written in an exception-safe way. This PR turns this
into a Defect, which probably is not the optimal way of handling things
- expect more work to be done here.
Some API have no way of reporting back errors to callers - for example,
when something fails in the accept loop, there's not much it can do, and
no way to report it back to the user of the API - this has been fixed
with the new accept flow - the old one should be deprecated.
Finally, there is information loss in the API: in composite operations
like `poll` and `waitFor` there's no way to differentiate internal
errors from user-level errors originating from callbacks.
* store `CatchableError` in future
* annotate proc's with correct raises information
* `selectors2` to avoid non-CatchableError IOSelectorsException
* `$` should never raise
* remove unnecessary gcsafe annotations
* fix exceptions leaking out of timer waits
* fix some imports
* functions must signal raising the union of all exceptions across all
platforms to enable cross-platform code
* switch to unittest2
* add `selectors2` which supercedes the std library version and fixes
several exception handling issues in there
* fixes
* docs, platform-independent eh specifiers for some functions
* add feature flag for strict exception mode
also bump version to 3.0.0 - _most_ existing code should be compatible
with this version of exception handling but some things might need
fixing - callbacks, existing raises specifications etc.
* fix AsyncCheck for non-void T
2021-03-24 09:08:33 +00:00
|
|
|
raises: [Defect, TransportAddressError].} =
|
2018-06-06 22:15:31 +00:00
|
|
|
## Resolve string representation of ``address``.
|
2018-06-10 00:55:19 +00:00
|
|
|
##
|
2018-06-06 22:15:31 +00:00
|
|
|
## ``address`` could be dot IPv4/IPv6 address or hostname.
|
2018-06-10 00:55:19 +00:00
|
|
|
##
|
2018-06-06 22:15:31 +00:00
|
|
|
## If hostname address is detected, then network address translation via DNS
|
|
|
|
## will be performed.
|
2021-04-09 21:39:54 +00:00
|
|
|
case family
|
|
|
|
of AddressFamily.IPv4:
|
|
|
|
resolveTAddress(address, port, Domain.AF_INET)
|
|
|
|
of AddressFamily.IPv6:
|
|
|
|
resolveTAddress(address, port, Domain.AF_INET6)
|
|
|
|
else:
|
|
|
|
raiseAssert("Unable to resolve non-internet address")
|
2018-06-06 22:15:31 +00:00
|
|
|
|
2018-10-27 13:14:55 +00:00
|
|
|
proc resolveTAddress*(address: string,
|
|
|
|
family: IpAddressFamily): seq[TransportAddress] {.
|
exception tracking (#166)
* exception tracking
This PR adds minimal exception tracking to chronos, moving the goalpost
one step further.
In particular, it becomes invalid to raise exceptions from `callSoon`
callbacks: this is critical for writing correct error handling because
there's no reasonable way that a user of chronos can possibly _reason_
about exceptions coming out of there: the event loop will be in an
indeterminite state when the loop is executing an _random_ callback.
As expected, there are several issues in the error handling of chronos:
in particular, it will end up in an inconsistent internal state whenever
the selector loop operations fail, because the internal state update
functions are not written in an exception-safe way. This PR turns this
into a Defect, which probably is not the optimal way of handling things
- expect more work to be done here.
Some API have no way of reporting back errors to callers - for example,
when something fails in the accept loop, there's not much it can do, and
no way to report it back to the user of the API - this has been fixed
with the new accept flow - the old one should be deprecated.
Finally, there is information loss in the API: in composite operations
like `poll` and `waitFor` there's no way to differentiate internal
errors from user-level errors originating from callbacks.
* store `CatchableError` in future
* annotate proc's with correct raises information
* `selectors2` to avoid non-CatchableError IOSelectorsException
* `$` should never raise
* remove unnecessary gcsafe annotations
* fix exceptions leaking out of timer waits
* fix some imports
* functions must signal raising the union of all exceptions across all
platforms to enable cross-platform code
* switch to unittest2
* add `selectors2` which supercedes the std library version and fixes
several exception handling issues in there
* fixes
* docs, platform-independent eh specifiers for some functions
* add feature flag for strict exception mode
also bump version to 3.0.0 - _most_ existing code should be compatible
with this version of exception handling but some things might need
fixing - callbacks, existing raises specifications etc.
* fix AsyncCheck for non-void T
2021-03-24 09:08:33 +00:00
|
|
|
deprecated, raises: [Defect, TransportAddressError].} =
|
2021-04-09 21:39:54 +00:00
|
|
|
case family
|
|
|
|
of IpAddressFamily.IPv4:
|
|
|
|
resolveTAddress(address, AddressFamily.IPv4)
|
|
|
|
of IpAddressFamily.IPv6:
|
|
|
|
resolveTAddress(address, AddressFamily.IPv6)
|
2018-10-27 13:14:55 +00:00
|
|
|
|
|
|
|
proc resolveTAddress*(address: string, port: Port,
|
|
|
|
family: IpAddressFamily): seq[TransportAddress] {.
|
exception tracking (#166)
* exception tracking
This PR adds minimal exception tracking to chronos, moving the goalpost
one step further.
In particular, it becomes invalid to raise exceptions from `callSoon`
callbacks: this is critical for writing correct error handling because
there's no reasonable way that a user of chronos can possibly _reason_
about exceptions coming out of there: the event loop will be in an
indeterminite state when the loop is executing an _random_ callback.
As expected, there are several issues in the error handling of chronos:
in particular, it will end up in an inconsistent internal state whenever
the selector loop operations fail, because the internal state update
functions are not written in an exception-safe way. This PR turns this
into a Defect, which probably is not the optimal way of handling things
- expect more work to be done here.
Some API have no way of reporting back errors to callers - for example,
when something fails in the accept loop, there's not much it can do, and
no way to report it back to the user of the API - this has been fixed
with the new accept flow - the old one should be deprecated.
Finally, there is information loss in the API: in composite operations
like `poll` and `waitFor` there's no way to differentiate internal
errors from user-level errors originating from callbacks.
* store `CatchableError` in future
* annotate proc's with correct raises information
* `selectors2` to avoid non-CatchableError IOSelectorsException
* `$` should never raise
* remove unnecessary gcsafe annotations
* fix exceptions leaking out of timer waits
* fix some imports
* functions must signal raising the union of all exceptions across all
platforms to enable cross-platform code
* switch to unittest2
* add `selectors2` which supercedes the std library version and fixes
several exception handling issues in there
* fixes
* docs, platform-independent eh specifiers for some functions
* add feature flag for strict exception mode
also bump version to 3.0.0 - _most_ existing code should be compatible
with this version of exception handling but some things might need
fixing - callbacks, existing raises specifications etc.
* fix AsyncCheck for non-void T
2021-03-24 09:08:33 +00:00
|
|
|
deprecated, raises: [Defect, TransportAddressError].} =
|
2021-04-09 21:39:54 +00:00
|
|
|
case family
|
|
|
|
of IpAddressFamily.IPv4:
|
|
|
|
resolveTAddress(address, port, AddressFamily.IPv4)
|
|
|
|
of IpAddressFamily.IPv6:
|
|
|
|
resolveTAddress(address, port, AddressFamily.IPv6)
|
2018-10-27 13:14:55 +00:00
|
|
|
|
exception tracking (#166)
* exception tracking
This PR adds minimal exception tracking to chronos, moving the goalpost
one step further.
In particular, it becomes invalid to raise exceptions from `callSoon`
callbacks: this is critical for writing correct error handling because
there's no reasonable way that a user of chronos can possibly _reason_
about exceptions coming out of there: the event loop will be in an
indeterminite state when the loop is executing an _random_ callback.
As expected, there are several issues in the error handling of chronos:
in particular, it will end up in an inconsistent internal state whenever
the selector loop operations fail, because the internal state update
functions are not written in an exception-safe way. This PR turns this
into a Defect, which probably is not the optimal way of handling things
- expect more work to be done here.
Some API have no way of reporting back errors to callers - for example,
when something fails in the accept loop, there's not much it can do, and
no way to report it back to the user of the API - this has been fixed
with the new accept flow - the old one should be deprecated.
Finally, there is information loss in the API: in composite operations
like `poll` and `waitFor` there's no way to differentiate internal
errors from user-level errors originating from callbacks.
* store `CatchableError` in future
* annotate proc's with correct raises information
* `selectors2` to avoid non-CatchableError IOSelectorsException
* `$` should never raise
* remove unnecessary gcsafe annotations
* fix exceptions leaking out of timer waits
* fix some imports
* functions must signal raising the union of all exceptions across all
platforms to enable cross-platform code
* switch to unittest2
* add `selectors2` which supercedes the std library version and fixes
several exception handling issues in there
* fixes
* docs, platform-independent eh specifiers for some functions
* add feature flag for strict exception mode
also bump version to 3.0.0 - _most_ existing code should be compatible
with this version of exception handling but some things might need
fixing - callbacks, existing raises specifications etc.
* fix AsyncCheck for non-void T
2021-03-24 09:08:33 +00:00
|
|
|
proc windowsAnyAddressFix*(a: TransportAddress): TransportAddress =
|
2022-01-26 19:56:22 +00:00
|
|
|
## BSD Sockets on \*nix systems are able to perform connections to
|
2020-02-25 21:50:39 +00:00
|
|
|
## `0.0.0.0` or `::0` which are equal to `127.0.0.1` or `::1`.
|
|
|
|
when defined(windows):
|
|
|
|
if (a.family == AddressFamily.IPv4 and
|
|
|
|
a.address_v4 == AnyAddress.address_v4):
|
2021-04-09 21:39:54 +00:00
|
|
|
try:
|
|
|
|
initTAddress("127.0.0.1", a.port)
|
|
|
|
except TransportAddressError as exc:
|
|
|
|
raiseAssert exc.msg
|
2020-02-25 21:50:39 +00:00
|
|
|
elif (a.family == AddressFamily.IPv6 and
|
|
|
|
a.address_v6 == AnyAddress6.address_v6):
|
2021-04-09 21:39:54 +00:00
|
|
|
try:
|
|
|
|
initTAddress("::1", a.port)
|
|
|
|
except TransportAddressError as exc:
|
|
|
|
raiseAssert exc.msg
|
2020-02-25 21:50:39 +00:00
|
|
|
else:
|
2021-04-09 21:39:54 +00:00
|
|
|
a
|
2020-02-25 21:50:39 +00:00
|
|
|
else:
|
2021-04-09 21:39:54 +00:00
|
|
|
a
|
2020-02-25 21:50:39 +00:00
|
|
|
|
2018-05-16 08:22:34 +00:00
|
|
|
template checkClosed*(t: untyped) =
|
|
|
|
if (ReadClosed in (t).state) or (WriteClosed in (t).state):
|
2020-05-13 19:45:40 +00:00
|
|
|
raise newException(TransportUseClosedError, "Transport is already closed!")
|
2018-05-16 08:22:34 +00:00
|
|
|
|
2018-06-05 20:21:07 +00:00
|
|
|
template checkClosed*(t: untyped, future: untyped) =
|
|
|
|
if (ReadClosed in (t).state) or (WriteClosed in (t).state):
|
2020-06-24 08:21:52 +00:00
|
|
|
future.fail(newException(TransportUseClosedError,
|
|
|
|
"Transport is already closed!"))
|
2019-10-23 11:13:23 +00:00
|
|
|
return future
|
|
|
|
|
|
|
|
template checkWriteEof*(t: untyped, future: untyped) =
|
|
|
|
if (WriteEof in (t).state):
|
|
|
|
future.fail(newException(TransportError,
|
|
|
|
"Transport connection is already dropped!"))
|
2018-06-05 20:21:07 +00:00
|
|
|
return future
|
|
|
|
|
exception tracking (#166)
* exception tracking
This PR adds minimal exception tracking to chronos, moving the goalpost
one step further.
In particular, it becomes invalid to raise exceptions from `callSoon`
callbacks: this is critical for writing correct error handling because
there's no reasonable way that a user of chronos can possibly _reason_
about exceptions coming out of there: the event loop will be in an
indeterminite state when the loop is executing an _random_ callback.
As expected, there are several issues in the error handling of chronos:
in particular, it will end up in an inconsistent internal state whenever
the selector loop operations fail, because the internal state update
functions are not written in an exception-safe way. This PR turns this
into a Defect, which probably is not the optimal way of handling things
- expect more work to be done here.
Some API have no way of reporting back errors to callers - for example,
when something fails in the accept loop, there's not much it can do, and
no way to report it back to the user of the API - this has been fixed
with the new accept flow - the old one should be deprecated.
Finally, there is information loss in the API: in composite operations
like `poll` and `waitFor` there's no way to differentiate internal
errors from user-level errors originating from callbacks.
* store `CatchableError` in future
* annotate proc's with correct raises information
* `selectors2` to avoid non-CatchableError IOSelectorsException
* `$` should never raise
* remove unnecessary gcsafe annotations
* fix exceptions leaking out of timer waits
* fix some imports
* functions must signal raising the union of all exceptions across all
platforms to enable cross-platform code
* switch to unittest2
* add `selectors2` which supercedes the std library version and fixes
several exception handling issues in there
* fixes
* docs, platform-independent eh specifiers for some functions
* add feature flag for strict exception mode
also bump version to 3.0.0 - _most_ existing code should be compatible
with this version of exception handling but some things might need
fixing - callbacks, existing raises specifications etc.
* fix AsyncCheck for non-void T
2021-03-24 09:08:33 +00:00
|
|
|
template getError*(t: untyped): ref CatchableError =
|
2018-05-16 08:22:34 +00:00
|
|
|
var err = (t).error
|
|
|
|
(t).error = nil
|
|
|
|
err
|
|
|
|
|
2020-06-24 08:21:52 +00:00
|
|
|
template getServerUseClosedError*(): ref TransportUseClosedError =
|
|
|
|
newException(TransportUseClosedError, "Server is already closed!")
|
|
|
|
|
2020-11-18 09:30:33 +00:00
|
|
|
template getTransportUseClosedError*(): ref TransportUseClosedError =
|
|
|
|
newException(TransportUseClosedError, "Transport is already closed!")
|
|
|
|
|
2018-10-03 00:44:39 +00:00
|
|
|
template getTransportOsError*(err: OSErrorCode): ref TransportOsError =
|
|
|
|
var msg = "(" & $int(err) & ") " & osErrorMsg(err)
|
|
|
|
var tre = newException(TransportOsError, msg)
|
|
|
|
tre.code = err
|
|
|
|
tre
|
|
|
|
|
|
|
|
template getTransportOsError*(err: cint): ref TransportOsError =
|
|
|
|
getTransportOsError(OSErrorCode(err))
|
2018-06-10 23:08:17 +00:00
|
|
|
|
exception tracking (#166)
* exception tracking
This PR adds minimal exception tracking to chronos, moving the goalpost
one step further.
In particular, it becomes invalid to raise exceptions from `callSoon`
callbacks: this is critical for writing correct error handling because
there's no reasonable way that a user of chronos can possibly _reason_
about exceptions coming out of there: the event loop will be in an
indeterminite state when the loop is executing an _random_ callback.
As expected, there are several issues in the error handling of chronos:
in particular, it will end up in an inconsistent internal state whenever
the selector loop operations fail, because the internal state update
functions are not written in an exception-safe way. This PR turns this
into a Defect, which probably is not the optimal way of handling things
- expect more work to be done here.
Some API have no way of reporting back errors to callers - for example,
when something fails in the accept loop, there's not much it can do, and
no way to report it back to the user of the API - this has been fixed
with the new accept flow - the old one should be deprecated.
Finally, there is information loss in the API: in composite operations
like `poll` and `waitFor` there's no way to differentiate internal
errors from user-level errors originating from callbacks.
* store `CatchableError` in future
* annotate proc's with correct raises information
* `selectors2` to avoid non-CatchableError IOSelectorsException
* `$` should never raise
* remove unnecessary gcsafe annotations
* fix exceptions leaking out of timer waits
* fix some imports
* functions must signal raising the union of all exceptions across all
platforms to enable cross-platform code
* switch to unittest2
* add `selectors2` which supercedes the std library version and fixes
several exception handling issues in there
* fixes
* docs, platform-independent eh specifiers for some functions
* add feature flag for strict exception mode
also bump version to 3.0.0 - _most_ existing code should be compatible
with this version of exception handling but some things might need
fixing - callbacks, existing raises specifications etc.
* fix AsyncCheck for non-void T
2021-03-24 09:08:33 +00:00
|
|
|
proc raiseTransportOsError*(err: OSErrorCode) {.
|
|
|
|
raises: [Defect, TransportOsError].} =
|
2018-10-25 10:19:19 +00:00
|
|
|
## Raises transport specific OS error.
|
|
|
|
raise getTransportOsError(err)
|
|
|
|
|
2018-06-15 10:54:26 +00:00
|
|
|
type
|
|
|
|
SeqHeader = object
|
|
|
|
length, reserved: int
|
|
|
|
|
|
|
|
proc isLiteral*(s: string): bool {.inline.} =
|
2021-03-03 18:04:09 +00:00
|
|
|
when defined(gcOrc) or defined(gcArc):
|
|
|
|
false
|
|
|
|
else:
|
|
|
|
(cast[ptr SeqHeader](s).reserved and (1 shl (sizeof(int) * 8 - 2))) != 0
|
2018-06-15 10:54:26 +00:00
|
|
|
|
|
|
|
proc isLiteral*[T](s: seq[T]): bool {.inline.} =
|
2021-03-03 18:04:09 +00:00
|
|
|
when defined(gcOrc) or defined(gcArc):
|
|
|
|
false
|
|
|
|
else:
|
|
|
|
(cast[ptr SeqHeader](s).reserved and (1 shl (sizeof(int) * 8 - 2))) != 0
|
2018-06-15 10:54:26 +00:00
|
|
|
|
2023-04-30 06:20:08 +00:00
|
|
|
template getTransportTooManyError*(
|
|
|
|
code = OSErrorCode(0)
|
|
|
|
): ref TransportTooManyError =
|
2021-11-12 16:13:56 +00:00
|
|
|
let msg =
|
|
|
|
when defined(posix):
|
2023-04-30 06:20:08 +00:00
|
|
|
if code == OSErrorCode(0):
|
2021-11-12 16:13:56 +00:00
|
|
|
"Too many open transports"
|
2023-04-30 06:20:08 +00:00
|
|
|
elif code == oserrno.EMFILE:
|
2021-11-12 16:13:56 +00:00
|
|
|
"[EMFILE] Too many open files in the process"
|
2023-04-30 06:20:08 +00:00
|
|
|
elif code == oserrno.ENFILE:
|
2021-11-12 16:13:56 +00:00
|
|
|
"[ENFILE] Too many open files in system"
|
2023-04-30 06:20:08 +00:00
|
|
|
elif code == oserrno.ENOBUFS:
|
2021-11-12 16:13:56 +00:00
|
|
|
"[ENOBUFS] No buffer space available"
|
2023-04-30 06:20:08 +00:00
|
|
|
elif code == oserrno.ENOMEM:
|
2021-11-12 16:13:56 +00:00
|
|
|
"[ENOMEM] Not enough memory availble"
|
|
|
|
else:
|
2023-04-30 06:20:08 +00:00
|
|
|
"[" & $int(code) & "] Too many open transports"
|
2021-11-12 16:13:56 +00:00
|
|
|
elif defined(windows):
|
|
|
|
case code
|
2023-04-30 06:20:08 +00:00
|
|
|
of OSErrorCode(0):
|
2021-11-12 16:13:56 +00:00
|
|
|
"Too many open transports"
|
2023-04-30 06:20:08 +00:00
|
|
|
of ERROR_TOO_MANY_OPEN_FILES:
|
2021-11-12 16:13:56 +00:00
|
|
|
"[ERROR_TOO_MANY_OPEN_FILES] Too many open files"
|
2023-04-30 06:20:08 +00:00
|
|
|
of WSAENOBUFS:
|
2021-11-12 16:13:56 +00:00
|
|
|
"[WSAENOBUFS] No buffer space available"
|
2023-04-30 06:20:08 +00:00
|
|
|
of WSAEMFILE:
|
2021-11-12 16:13:56 +00:00
|
|
|
"[WSAEMFILE] Too many open sockets"
|
|
|
|
else:
|
2023-04-30 06:20:08 +00:00
|
|
|
"[" & $int(code) & "] Too many open transports"
|
2021-11-12 16:13:56 +00:00
|
|
|
else:
|
2023-04-30 06:20:08 +00:00
|
|
|
"[" & $int(code) & "] Too many open transports"
|
2021-11-12 16:13:56 +00:00
|
|
|
newException(TransportTooManyError, msg)
|
|
|
|
|
|
|
|
template getConnectionAbortedError*(m: string = ""): ref TransportAbortedError =
|
|
|
|
let msg =
|
|
|
|
if len(m) == 0:
|
|
|
|
"[ECONNABORTED] Connection has been aborted before being accepted"
|
|
|
|
else:
|
|
|
|
"[ECONNABORTED] " & m
|
|
|
|
newException(TransportAbortedError, msg)
|
|
|
|
|
2023-04-30 06:20:08 +00:00
|
|
|
template getConnectionAbortedError*(
|
|
|
|
code: OSErrorCode
|
|
|
|
): ref TransportAbortedError =
|
2021-11-12 16:13:56 +00:00
|
|
|
let msg =
|
|
|
|
when defined(posix):
|
2023-04-30 06:20:08 +00:00
|
|
|
if code == OSErrorCode(0):
|
2021-11-12 16:13:56 +00:00
|
|
|
"[ECONNABORTED] Connection has been aborted before being accepted"
|
2023-04-30 06:20:08 +00:00
|
|
|
elif code == oserrno.EPERM:
|
2021-11-12 16:13:56 +00:00
|
|
|
"[EPERM] Firewall rules forbid connection"
|
2023-04-30 06:20:08 +00:00
|
|
|
elif code == oserrno.ETIMEDOUT:
|
2021-11-12 16:13:56 +00:00
|
|
|
"[ETIMEDOUT] Operation has been timed out"
|
|
|
|
else:
|
2023-04-30 06:20:08 +00:00
|
|
|
"[" & $int(code) & "] Connection has been aborted"
|
2021-11-12 16:13:56 +00:00
|
|
|
elif defined(windows):
|
|
|
|
case code
|
2023-04-30 06:20:08 +00:00
|
|
|
of OSErrorCode(0), oserrno.WSAECONNABORTED:
|
2021-11-12 16:13:56 +00:00
|
|
|
"[ECONNABORTED] Connection has been aborted before being accepted"
|
2023-04-30 06:20:08 +00:00
|
|
|
of WSAENETDOWN:
|
2021-11-12 16:13:56 +00:00
|
|
|
"[ENETDOWN] Network is down"
|
2023-04-30 06:20:08 +00:00
|
|
|
of oserrno.WSAENETRESET:
|
2021-11-12 16:13:56 +00:00
|
|
|
"[ENETRESET] Network dropped connection on reset"
|
2023-04-30 06:20:08 +00:00
|
|
|
of oserrno.WSAECONNRESET:
|
2021-11-12 16:13:56 +00:00
|
|
|
"[ECONNRESET] Connection reset by peer"
|
2023-04-30 06:20:08 +00:00
|
|
|
of WSAETIMEDOUT:
|
2021-11-12 16:13:56 +00:00
|
|
|
"[ETIMEDOUT] Connection timed out"
|
|
|
|
else:
|
2023-04-30 06:20:08 +00:00
|
|
|
"[" & $int(code) & "] Connection has been aborted"
|
2021-11-12 16:13:56 +00:00
|
|
|
else:
|
2023-04-30 06:20:08 +00:00
|
|
|
"[" & $int(code) & "] Connection has been aborted"
|
2021-11-12 16:13:56 +00:00
|
|
|
|
|
|
|
newException(TransportAbortedError, msg)
|