C++ wrapper around the MXNetwork socket API. More...
#include <MXNetwork/include/mxnetwork/socket.hpp>
Public Member Functions | |
| std::optional< Socket > | accept () |
| Accept an incoming connection. | |
| bool | bind (std::string_view port) |
| Bind an Internet socket to a local port. | |
| bool | bind_unix (std::string_view path) |
| Bind a UNIX-domain socket to a local path. | |
| void | close () |
| Close the socket if it is open. | |
| bool | connect (const std::string_view host, const std::string_view port) |
| Connect to a remote Internet endpoint. | |
| bool | connect_unix (const std::string_view path) |
| Connect to a UNIX-domain endpoint. | |
| bool | is_open () const |
| Return true when the socket is open. | |
| bool | listen (std::string_view port, int backlog) |
| Start listening on an Internet port. | |
| bool | listen_unix (std::string_view path, int backlog) |
| Start listening on a UNIX-domain socket path. | |
| Socket & | operator= (const Socket &s)=delete |
| Socket & | operator= (Socket &&s) noexcept |
| Move-assign from another socket wrapper. | |
| ssize_t | read (void *buf, size_t bytes, int flags) |
| Read bytes from the socket. | |
| ssize_t | read_all (void *buf, size_t bytes) |
| Read exactly the requested number of bytes. | |
| bool | readline (char **buffer, size_t *len) |
| Read a line of text from the socket. | |
| ssize_t | recvfrom (void *buf, size_t bytes) |
| Receive a datagram using the configured socket type. | |
| ssize_t | recvfrom (void *buf, size_t bytes, SocketAddress &address) |
| ssize_t | sendto (const void *buf, size_t bytes) |
| Send a datagram using the configured socket type. | |
| ssize_t | sendto (const void *buf, size_t bytes, const SocketAddress &address) |
| bool | setblocking (bool block) |
| Toggle blocking mode. | |
| Socket () noexcept | |
| Construct an invalid socket wrapper. | |
| Socket (const MXSocket &s, SocketType type) noexcept | |
| Copy socket state from a C API structure. | |
| Socket (const Socket &s)=delete | |
| Socket (mx_socket_fd sockfd, SocketType type) | |
| Adopt an existing socket handle. | |
| Socket (Socket &&s) noexcept | |
| Move-construct from another socket wrapper. | |
| Socket (SocketType type) noexcept | |
| Construct a socket wrapper for the given type. | |
| SocketType | socket_type () const |
| Return the socket family and transport type. | |
| mx_socket_fd | sockfd () const |
| Return the underlying socket handle. | |
| bool | valid () const |
| Return true when the socket handle is valid. | |
| ssize_t | write (const void *buf, size_t bytes, int flags) |
| Write bytes to the socket. | |
| ssize_t | write_all (const void *buf, size_t bytes) |
| Write exactly the requested number of bytes. | |
| ~Socket () noexcept | |
| Destroy the socket wrapper and close the socket if needed. | |
Protected Attributes | |
| MXSocket | sock |
| SocketType | type = SocketType::TYPE_INVALID |
C++ wrapper around the MXNetwork socket API.
Definition at line 57 of file socket.hpp.
|
noexcept |
Construct an invalid socket wrapper.
Definition at line 31 of file socket.cpp.
|
noexcept |
Construct a socket wrapper for the given type.
Definition at line 33 of file socket.cpp.
|
noexcept |
Destroy the socket wrapper and close the socket if needed.
Definition at line 42 of file socket.cpp.
| mxnetwork::Socket::Socket | ( | mx_socket_fd | sockfd, |
| SocketType | type ) |
Adopt an existing socket handle.
Definition at line 48 of file socket.cpp.
|
noexcept |
Copy socket state from a C API structure.
Definition at line 58 of file socket.cpp.
|
delete |
|
noexcept |
Move-construct from another socket wrapper.
Definition at line 63 of file socket.cpp.
|
nodiscard |
Accept an incoming connection.
Definition at line 120 of file socket.cpp.
| bool mxnetwork::Socket::bind | ( | std::string_view | port | ) |
Bind an Internet socket to a local port.
| port | Service or port name. |
Definition at line 138 of file socket.cpp.
| bool mxnetwork::Socket::bind_unix | ( | std::string_view | path | ) |
Bind a UNIX-domain socket to a local path.
| path | Filesystem path for the socket. |
Definition at line 153 of file socket.cpp.
| void mxnetwork::Socket::close | ( | ) |
Close the socket if it is open.
Definition at line 171 of file socket.cpp.
| bool mxnetwork::Socket::connect | ( | const std::string_view | host, |
| const std::string_view | port ) |
Connect to a remote Internet endpoint.
| host | Remote host name or address. |
| port | Remote service or port name. |
Definition at line 80 of file socket.cpp.
| bool mxnetwork::Socket::connect_unix | ( | const std::string_view | path | ) |
Connect to a UNIX-domain endpoint.
| path | Filesystem path for the socket. |
Definition at line 92 of file socket.cpp.
|
nodiscard |
Return true when the socket is open.
Definition at line 167 of file socket.cpp.
| bool mxnetwork::Socket::listen | ( | std::string_view | port, |
| int | backlog ) |
Start listening on an Internet port.
| port | Service or port name. |
| backlog | Maximum pending connection queue length. |
Definition at line 100 of file socket.cpp.
| bool mxnetwork::Socket::listen_unix | ( | std::string_view | path, |
| int | backlog ) |
Start listening on a UNIX-domain socket path.
| path | Filesystem path for the socket. |
| backlog | Maximum pending connection queue length. |
Definition at line 112 of file socket.cpp.
Move-assign from another socket wrapper.
Definition at line 69 of file socket.cpp.
| ssize_t mxnetwork::Socket::read | ( | void * | buf, |
| size_t | bytes, | ||
| int | flags ) |
Read bytes from the socket.
| buf | Output buffer. |
| bytes | Number of bytes to read. |
| flags | Platform socket flags. |
Definition at line 187 of file socket.cpp.
| ssize_t mxnetwork::Socket::read_all | ( | void * | buf, |
| size_t | bytes ) |
Read exactly the requested number of bytes.
| buf | Output buffer. |
| bytes | Number of bytes to read. |
Definition at line 199 of file socket.cpp.
| bool mxnetwork::Socket::readline | ( | char ** | buffer, |
| size_t * | len ) |
Read a line of text from the socket.
| buffer | Output buffer pointer. |
| len | Output length. |
Definition at line 191 of file socket.cpp.
| ssize_t mxnetwork::Socket::recvfrom | ( | void * | buf, |
| size_t | bytes ) |
Receive a datagram using the configured socket type.
| buf | Output buffer. |
| bytes | Maximum number of bytes to receive. |
Definition at line 216 of file socket.cpp.
| ssize_t mxnetwork::Socket::recvfrom | ( | void * | buf, |
| size_t | bytes, | ||
| SocketAddress & | address ) |
Definition at line 226 of file socket.cpp.
| ssize_t mxnetwork::Socket::sendto | ( | const void * | buf, |
| size_t | bytes ) |
Send a datagram using the configured socket type.
| buf | Input buffer. |
| bytes | Number of bytes to send. |
Definition at line 207 of file socket.cpp.
| ssize_t mxnetwork::Socket::sendto | ( | const void * | buf, |
| size_t | bytes, | ||
| const SocketAddress & | address ) |
Definition at line 230 of file socket.cpp.
| bool mxnetwork::Socket::setblocking | ( | bool | block | ) |
Toggle blocking mode.
| block | True to enable blocking I/O. |
Definition at line 159 of file socket.cpp.
|
nodiscard |
Return the socket family and transport type.
Definition at line 179 of file socket.cpp.
|
nodiscard |
Return the underlying socket handle.
Definition at line 183 of file socket.cpp.
|
nodiscard |
Return true when the socket handle is valid.
Definition at line 163 of file socket.cpp.
| ssize_t mxnetwork::Socket::write | ( | const void * | buf, |
| size_t | bytes, | ||
| int | flags ) |
Write bytes to the socket.
| buf | Input buffer. |
| bytes | Number of bytes to write. |
| flags | Platform socket flags. |
Definition at line 195 of file socket.cpp.
| ssize_t mxnetwork::Socket::write_all | ( | const void * | buf, |
| size_t | bytes ) |
Write exactly the requested number of bytes.
| buf | Input buffer. |
| bytes | Number of bytes to write. |
Definition at line 203 of file socket.cpp.
|
protected |
Definition at line 203 of file socket.hpp.
|
protected |
Definition at line 204 of file socket.hpp.