17 int err = WSAStartup(MAKEWORD(2, 2), &wsaData);
19 fprintf(stderr,
"WSAStartup failed with error: %d\n", err);
50 throw Exception(
"Error on socket init.\n");
54 sock.blocking =
false;
135 throw Exception(
"Error accept socket failed.\n");
148 throw Exception(
"Could not bind UDP socket.");
173 std::cout <<
"Socket: " <<
sock.sockfd <<
" closed.\n";
234 void Socket::setsocket(
const MXSocket &s) {
Lightweight exception wrapper for MXNetwork failures.
bool listen_unix(std::string_view path, int backlog)
Start listening on a UNIX-domain socket path.
ssize_t write_all(const void *buf, size_t bytes)
Write exactly the requested number of bytes.
ssize_t write(const void *buf, size_t bytes, int flags)
Write bytes to the socket.
void close()
Close the socket if it is open.
bool valid() const
Return true when the socket handle is valid.
bool is_open() const
Return true when the socket is open.
bool bind_unix(std::string_view path)
Bind a UNIX-domain socket to a local path.
bool readline(char **buffer, size_t *len)
Read a line of text from the socket.
SocketType socket_type() const
Return the socket family and transport type.
~Socket() noexcept
Destroy the socket wrapper and close the socket if needed.
ssize_t sendto(const void *buf, size_t bytes)
Send a datagram using the configured socket type.
Socket() noexcept
Construct an invalid socket wrapper.
bool bind(std::string_view port)
Bind an Internet socket to a local port.
bool listen(std::string_view port, int backlog)
Start listening on an Internet port.
std::optional< Socket > accept()
Accept an incoming connection.
bool setblocking(bool block)
Toggle blocking mode.
ssize_t read_all(void *buf, size_t bytes)
Read exactly the requested number of bytes.
ssize_t read(void *buf, size_t bytes, int flags)
Read bytes from the socket.
mx_socket_fd sockfd() const
Return the underlying socket handle.
Socket & operator=(const Socket &s)=delete
bool connect_unix(const std::string_view path)
Connect to a UNIX-domain endpoint.
bool connect(const std::string_view host, const std::string_view port)
Connect to a remote Internet endpoint.
ssize_t recvfrom(void *buf, size_t bytes)
Receive a datagram using the configured socket type.
ssize_t mx_socket_read(MXSocket *s, void *data, size_t len, int flags)
Read data from a socket.
bool mx_socket_ipv6_connect(MXSocket *s, const char *host, const char *port, int type)
Connect an IPv6 Internet socket to a remote host.
ssize_t mx_socket_recvfrom_address(MXSocket *sock, void *buf, size_t bytes, MXSocketAddress *address)
bool mx_socket_unix_bind(MXSocket *s, const char *path)
Bind a UNIX-domain socket to a local path.
ssize_t mx_socket_read_all(MXSocket *sock, void *buf, size_t bytes)
Read the requested number of bytes from a socket.
void mx_socket_close(MXSocket *s)
Close a socket if it is open.
#define mx_close_socket(s)
ssize_t mx_socket_recvfrom(MXSocket *sock, void *buf, size_t bytes)
Receive a datagram using an Internet socket.
bool mx_socket_is_open(const MXSocket *s)
Check whether the socket is open.
bool mx_socket_set_blocking(MXSocket *s, bool state)
Toggle blocking mode for a socket.
ssize_t mx_socket_sendto_address(MXSocket *sock, const void *buf, size_t bytes, const MXSocketAddress *address)
#define SOCK_ECONNABORTED
ssize_t mx_socket_write_all(MXSocket *sock, const void *buf, size_t bytes)
Write the requested number of bytes to a socket.
bool mx_socket_unix_connect(MXSocket *s, const char *path, int type)
Connect a UNIX-domain socket to a local path.
bool mx_socket_valid(const MXSocket *s)
Check whether the socket handle is valid.
bool mx_socket_unix_listen(MXSocket *s, const char *path, int backlog, int type)
Create and bind a UNIX-domain socket for listening.
ssize_t mx_socket_unix_sendto(MXSocket *sock, const void *buf, size_t bytes)
Send a datagram using a UNIX-domain socket.
bool mx_socket_listen(MXSocket *s, const char *port, int backlog, int type)
Create and bind an Internet socket for listening.
bool mx_socket_accept(const MXSocket *input, MXSocket *output)
Accept an incoming connection.
ssize_t mx_socket_send(MXSocket *s, const void *data, size_t len, int flags)
Write data to a socket.
bool mx_socket_connect(MXSocket *s, const char *host, const char *port, int type)
Connect an Internet socket to a remote host.
ssize_t mx_socket_ipv6_recvfrom(MXSocket *sock, void *buf, size_t bytes)
Receive a datagram using an IPv6 Internet socket.
bool mx_socket_ipv6_bind(MXSocket *s, const char *port)
Bind an IPv6 Internet socket to a local port.
bool mx_socket_readline(MXSocket *s, char **buffer, size_t *len)
Read a line of text from a socket.
bool mx_socket_ipv6_listen(MXSocket *s, const char *port, int backlog, int type)
Create and bind an IPv6 Internet socket for listening.
bool mx_socket_bind(MXSocket *s, const char *port)
Bind an Internet socket to a local port.
ssize_t mx_socket_sendto(MXSocket *sock, const void *buf, size_t bytes)
Send a datagram using an Internet socket.
bool mx_socket_init(MXSocket *s)
Initialize a socket structure with default values.
ssize_t mx_socket_ipv6_sendto(MXSocket *sock, const void *buf, size_t bytes)
Send a datagram using an IPv6 Internet socket.
ssize_t mx_socket_unix_recvfrom(MXSocket *sock, void *buf, size_t bytes)
Receive a datagram using a UNIX-domain socket.
SocketType
Socket family and transport type.
@ TYPE_INVALID
No socket type selected.
@ TYPE_UNIX_DGRAM
UNIX-domain datagram socket.
@ TYPE_INET_DGRAM
IPv4 datagram socket.
@ TYPE_INET6_DGRAM
IPv6 datagram socket.
@ TYPE_UNIX
UNIX-domain stream socket.
@ TYPE_INET6
IPv6 stream socket.
@ TYPE_INET
IPv4 stream socket.
struct sockaddr_storage storage
Cross-platform socket state used by the C API.
struct sockaddr_in6 inet6
IPv6 address storage.
bool blocking
True when the socket is in blocking mode.
struct sockaddr_in inet
IPv4 address storage.
struct sockaddr_un sun
UNIX-domain address storage.
mx_socket_fd sockfd
Underlying socket handle.
socklen_t addrlen
Length of the active address structure.
MXNetworkInit()
Initialize the socket backend for the current process.
~MXNetworkInit()
Shut down the socket backend.
bool operator==(const SocketAddress &other) const