Skip to content

Commit

Permalink
netsocket: socket operations return error codes via return values
Browse files Browse the repository at this point in the history
Switch to returning socket error codes explicitly via return values
instead of using legacy functions `getSockErr()`, `setSockErr`,
`strSockError()` to get extended error context.

To facilitate that, use `tl::expected<T, std::error_code>` as the
return value type for all main socket operations: read, write, open,
listen.

Moving to this new approach has numerous benefits over the old one:

1. Instead of using POSIX constants directly, we wrap them into
   `std::error_code` instances, which allows to attach custom error
   categories to them. This can be used to customize error messages
   and error codes mapping to platform-independent
   `std::error_conditions`.
2. Calling separate `get/setSockErr()` functions is error-prone:
   one can easily forget to check the error condition from
   `getSockErr()` and the value will be overwritten by the next
   socket function without the ability to recover the former error.
   Conversely, one can forget to call `setSockErr()` to set
   the proper error code for the caller to check upon.
3. As mentioned above, `std::error_code:s` can be implicitly mapped
   to platform-independent `std::error_conditions`, allowing for this
   code to compile successfuly:

	if (errCode == std::errc::connection_reset) { ... }

   This allows for very convenient and portable error checking
   code, which completely hides implementation details of how
   a particular error code is implemented (but, if one really needs to,
   they still can extract the platform-dependent error code value
   to get the extended error context).

The `getSockErr()`, `setSockErr` and `strSockError()` functions
are still used in the `netsocket.cpp` implementation, but now they are
strictly confined to this particular translation unit, meaning
they have now become an implementation detail, rather than a part of
public API contract of `netplay` library.

Signed-off-by: Pavel Solodovnikov <[email protected]>
  • Loading branch information
ManManson authored and past-due committed Oct 19, 2024
1 parent 4c6302b commit d0296f1
Show file tree
Hide file tree
Showing 4 changed files with 249 additions and 227 deletions.
Loading

0 comments on commit d0296f1

Please sign in to comment.