xentara-utils v2.0.4
The Xentara Utility Library
|
A Windows socket that closes itself on destruct. More...
#include <xentara/utils/windows/Socket.hpp>
Public Types | |
using | NativeType = SOCKET |
A native Windows socket. | |
Public Member Functions | |
constexpr | Socket () noexcept=default |
Default constructor. | |
constexpr | Socket (SOCKET nativeSocket) noexcept |
Conversion from a native socket. | |
constexpr | Socket (Socket &&other) noexcept |
Move constructor. | |
~Socket () | |
Destructor. Silently closes the socket. | |
auto | operator= (Socket &&rhs) noexcept -> Socket & |
Move assignment operator. | |
constexpr | operator SOCKET () const noexcept |
Conversion to a native socket. | |
constexpr | operator bool () const noexcept |
Checks whether the object contains a socket. | |
auto | operator= (SOCKET nativeSocket) noexcept -> Socket & |
Assignment from a native socket. | |
constexpr auto | release () noexcept -> SOCKET |
Removes the socket and passes ownership to the caller. | |
auto | close () noexcept -> bool |
Closes the socket, if any. | |
auto | performOverlappedIo (functional::function_ref< auto(SOCKET, OVERLAPPED *) → BOOL > operation, HANDLE event, std::optional< std::chrono::nanoseconds > timeout=std::nullopt) -> std::optional< DWORD > |
Performs an overlapped I/O operation. | |
auto | performOverlappedIo (functional::function_ref< auto(SOCKET, OVERLAPPED *) → BOOL > operation, std::span< HANDLE > waitObjects, std::optional< std::chrono::nanoseconds > timeout=std::nullopt) -> std::optional< OverlappedResult > |
Performs an overlapped I/O operation. | |
Static Public Attributes | |
static constexpr SOCKET | kInvalid { INVALID_SOCKET } |
An invalid socket. | |
A Windows socket that closes itself on destruct.
This class can be moved but not copied.
using xentara::utils::windows::Socket::NativeType = SOCKET |
A native Windows socket.
|
constexprdefaultnoexcept |
Default constructor.
Creates an empty object that does not contain a socket.
|
constexprnoexcept |
Conversion from a native socket.
nativeSocket | The native socket, or INVALID_SOCKET for none. |
|
constexprnoexcept |
Move constructor.
Takes ownership of the other object's socket, if any.
other | The object to move. Will be reset and no longer contain a socket afterwards. |
xentara::utils::windows::Socket::~Socket | ( | ) |
Destructor. Silently closes the socket.
|
noexcept |
Closes the socket, if any.
|
explicitconstexprnoexcept |
Checks whether the object contains a socket.
|
constexprnoexcept |
Conversion to a native socket.
Move assignment operator.
Takes ownership of the other socket's value. Any existing socket is silently closed.
rhs | The object to move. Will be reset and no longer contain a socket afterwards. |
|
noexcept |
Assignment from a native socket.
Any existing socket is silently closed.
nativeSocket | The native socket, or INVALID_SOCKET for none. |
auto xentara::utils::windows::Socket::performOverlappedIo | ( | functional::function_ref< auto(SOCKET, OVERLAPPED *) → BOOL > | operation, |
HANDLE | event, | ||
std::optional< std::chrono::nanoseconds > | timeout = std::nullopt |
||
) | -> std::optional< DWORD > |
Performs an overlapped I/O operation.
This function creates an OVERLAPPED structure and calls a function object with it. The function object must call a Windows overlapped socket function like WSARecv() or WSASend(), passing the overlapped structure to it, and return the result. The function must also preserve the error code stored in GetLastError(), specifically the error code ERROR_IO_PENDING, which the function uses to determine if waiting is necessary.
operation | The operation to perform. operation will be called with the socket and an overlapped structure as parameters. It must return TRUE on success or FALSE on error. If FALSE is returned, the last error code must be set to an appropriate error. If an overlapped I/O operation is pending, the function must return FALSE and set the last error to ERROR_IO_PENDING in the manner of overlapped I/O functions like WSARecv() and WSASend(). |
event | The event to use in the OVERLAPPED structure. |
timeout | The timeout, or std::nullopt for infinite. Negative timeouts are treated the same as a timeout of 0. |
Returns the number of bytes transferred on success, or std::nullopt on error. To get extended error information, call GetLastError(). On timeout, the last error is set to ERROR_OPERATION_ABORTED.
The meaning of “number of bytes transferred” depends on the overlapped operation. See the documentation of the lpNumberOfBytesTransferred parameter of GetOverlappedResult().
auto xentara::utils::windows::Socket::performOverlappedIo | ( | functional::function_ref< auto(SOCKET, OVERLAPPED *) → BOOL > | operation, |
std::span< HANDLE > | waitObjects, | ||
std::optional< std::chrono::nanoseconds > | timeout = std::nullopt |
||
) | -> std::optional< OverlappedResult > |
Performs an overlapped I/O operation.
This function creates an OVERLAPPED structure and calls a function object with it. The function object must call a Windows overlapped socket function like WSARecv() or WSASend(), passing the overlapped structure to it, and return the result. The function must also preserve the error code stored in GetLastError(), specifically the error code ERROR_IO_PENDING, which the function uses to determine if waiting is necessary.
operation | The operation to perform. operation will be called with the socket and an overlapped structure as parameters. It must return TRUE on success or FALSE on error. If FALSE is returned, the last error code must be set to an appropriate error. If an overlapped I/O operation is pending, the function must return FALSE and set the last error to ERROR_IO_PENDING in the manner of overlapped I/O functions like WSARecv() and WSASend(). |
waitObjects | The objects to wait for. The first position in the span must contain the event to use in the OVERLAPPED structure. |
timeout | The timeout, or std::nullopt for infinite. Negative timeouts are treated the same as a timeout of 0. |
std::invalid_argument | waitObjects is empty, or contains more objects than Windows can handle. |
|
constexprnoexcept |
Removes the socket and passes ownership to the caller.
After this call, this object will no longer contain a socket.
|
staticconstexpr |
An invalid socket.