MXVK Vulkan Framework 0.24.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
mxnetwork::Socket Class Reference

C++ wrapper around the MXNetwork socket API. More...

#include <MXNetwork/include/mxnetwork/socket.hpp>

Public Member Functions

std::optional< Socketaccept ()
 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.
Socketoperator= (const Socket &s)=delete
Socketoperator= (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

Detailed Description

C++ wrapper around the MXNetwork socket API.

Definition at line 57 of file socket.hpp.

Constructor & Destructor Documentation

◆ Socket() [1/6]

mxnetwork::Socket::Socket ( )
noexcept

Construct an invalid socket wrapper.

Definition at line 31 of file socket.cpp.

Socket() noexcept
Construct an invalid socket wrapper.
Definition socket.cpp:31
@ TYPE_INVALID
No socket type selected.
Definition socket.hpp:39

◆ Socket() [2/6]

mxnetwork::Socket::Socket ( SocketType type)
noexcept

Construct a socket wrapper for the given type.

Parameters
typeSocket family and transport type.

Definition at line 33 of file socket.cpp.

33 {
34 type = stype;
35 if (mx_socket_init(&sock))
36 return;
37 else
39 sock.sockfd = NULL_SOCKET;
40 }
SocketType type
Definition socket.hpp:204
#define NULL_SOCKET
Definition mxsocket.h:62
bool mx_socket_init(MXSocket *s)
Initialize a socket structure with default values.
Definition mxsocket.c:407

◆ ~Socket()

mxnetwork::Socket::~Socket ( )
noexcept

Destroy the socket wrapper and close the socket if needed.

Definition at line 42 of file socket.cpp.

42 {
43 if (mx_socket_valid(&sock)) {
44 close();
45 }
46 }
void close()
Close the socket if it is open.
Definition socket.cpp:171
bool mx_socket_valid(const MXSocket *s)
Check whether the socket handle is valid.
Definition mxsocket.c:416

◆ Socket() [3/6]

mxnetwork::Socket::Socket ( mx_socket_fd sockfd,
SocketType type )

Adopt an existing socket handle.

Parameters
sockfdSocket handle to wrap.
typeSocket family and transport type.

Definition at line 48 of file socket.cpp.

48 {
49 if (!mx_socket_init(&sock))
50 throw Exception("Error on socket init.\n");
51
52 sock.sockfd = sockfd;
53 sock.addrlen = 0;
54 sock.blocking = false;
55 type = stype;
56 }
mx_socket_fd sockfd() const
Return the underlying socket handle.
Definition socket.cpp:183

◆ Socket() [4/6]

mxnetwork::Socket::Socket ( const MXSocket & s,
SocketType type )
noexcept

Copy socket state from a C API structure.

Parameters
sSource socket state.
typeSocket family and transport type.

Definition at line 58 of file socket.cpp.

58 {
59 type = stype;
60 setsocket(s);
61 }

◆ Socket() [5/6]

mxnetwork::Socket::Socket ( const Socket & s)
delete

◆ Socket() [6/6]

mxnetwork::Socket::Socket ( Socket && s)
noexcept

Move-construct from another socket wrapper.

Definition at line 63 of file socket.cpp.

63 {
64 type = s.type;
65 setsocket(s.sock);
66 s.sock.sockfd = NULL_SOCKET;
67 }

Member Function Documentation

◆ accept()

std::optional< Socket > mxnetwork::Socket::accept ( )
nodiscard

Accept an incoming connection.

Returns
Accepted socket on success.

Definition at line 120 of file socket.cpp.

120 {
121 MXSocket newsocket;
122 if (!mx_socket_init(&newsocket))
123 return std::nullopt;
124
125 if (mx_socket_accept(&sock, &newsocket)) {
126 return Socket(newsocket, type);
127 }
128
129 if (!mx_socket_valid(&sock))
130 return std::nullopt;
131
133 return std::nullopt;
134
135 throw Exception("Error accept socket failed.\n");
136 }
#define SOCK_ESHUTDOWN
Definition mxsocket.h:59
#define SOCK_ERRNO
Definition mxsocket.h:51
#define SOCK_ECONNRESET
Definition mxsocket.h:60
#define SOCK_EINVAL
Definition mxsocket.h:56
#define SOCK_ECONNABORTED
Definition mxsocket.h:55
#define SOCK_EBADF
Definition mxsocket.h:57
#define SOCK_EWOULDBLOCK
Definition mxsocket.h:54
#define SOCK_EAGAIN
Definition mxsocket.h:53
bool mx_socket_accept(const MXSocket *input, MXSocket *output)
Accept an incoming connection.
Definition mxsocket.c:278
#define SOCK_ENOTSOCK
Definition mxsocket.h:58
#define SOCK_EINTR
Definition mxsocket.h:52

◆ bind()

bool mxnetwork::Socket::bind ( std::string_view port)

Bind an Internet socket to a local port.

Parameters
portService or port name.
Returns
True on success.

Definition at line 138 of file socket.cpp.

138 {
139 bool bound = false;
141 bound = mx_socket_bind(&sock, std::string(port).c_str());
143 bound = mx_socket_ipv6_bind(&sock, std::string(port).c_str());
144 else
145 bound = mx_socket_bind(&sock, std::string(port).c_str());
146
147 if (!bound) {
148 throw Exception("Could not bind UDP socket.");
149 }
150 return true;
151 }
bool mx_socket_ipv6_bind(MXSocket *s, const char *port)
Bind an IPv6 Internet socket to a local port.
Definition mxsocket.c:374
bool mx_socket_bind(MXSocket *s, const char *port)
Bind an Internet socket to a local port.
Definition mxsocket.c:370
@ TYPE_INET_DGRAM
IPv4 datagram socket.
Definition socket.hpp:47
@ TYPE_INET6_DGRAM
IPv6 datagram socket.
Definition socket.hpp:49
@ TYPE_INET6
IPv6 stream socket.
Definition socket.hpp:43
@ TYPE_INET
IPv4 stream socket.
Definition socket.hpp:41

◆ bind_unix()

bool mxnetwork::Socket::bind_unix ( std::string_view path)

Bind a UNIX-domain socket to a local path.

Parameters
pathFilesystem path for the socket.
Returns
True on success.

Definition at line 153 of file socket.cpp.

153 {
154 if (!mx_socket_unix_bind(&sock, std::string(path).c_str()))
155 return false;
156 return true;
157 }
bool mx_socket_unix_bind(MXSocket *s, const char *path)
Bind a UNIX-domain socket to a local path.
Definition mxsocket.c:378

◆ close()

void mxnetwork::Socket::close ( )

Close the socket if it is open.

Definition at line 171 of file socket.cpp.

171 {
172 if (mx_socket_valid(&sock)) {
173 std::cout << "Socket: " << sock.sockfd << " closed.\n";
175 }
177 }
void mx_socket_close(MXSocket *s)
Close a socket if it is open.
Definition mxsocket.c:323

◆ connect()

bool mxnetwork::Socket::connect ( const std::string_view host,
const std::string_view port )

Connect to a remote Internet endpoint.

Parameters
hostRemote host name or address.
portRemote service or port name.
Returns
True on success.

Definition at line 80 of file socket.cpp.

80 {
82 return mx_socket_connect(&sock, std::string(host).c_str(), std::string(port).c_str(), SOCK_STREAM);
83 else if (type == SocketType::TYPE_INET6)
84 return mx_socket_ipv6_connect(&sock, std::string(host).c_str(), std::string(port).c_str(), SOCK_STREAM);
86 return mx_socket_connect(&sock, std::string(host).c_str(), std::string(port).c_str(), SOCK_DGRAM);
88 return mx_socket_ipv6_connect(&sock, std::string(host).c_str(), std::string(port).c_str(), SOCK_DGRAM);
89 return false;
90 }
bool mx_socket_ipv6_connect(MXSocket *s, const char *host, const char *port, int type)
Connect an IPv6 Internet socket to a remote host.
Definition mxsocket.c:366
bool mx_socket_connect(MXSocket *s, const char *host, const char *port, int type)
Connect an Internet socket to a remote host.
Definition mxsocket.c:362

◆ connect_unix()

bool mxnetwork::Socket::connect_unix ( const std::string_view path)

Connect to a UNIX-domain endpoint.

Parameters
pathFilesystem path for the socket.
Returns
True on success.

Definition at line 92 of file socket.cpp.

92 {
94 return mx_socket_unix_connect(&sock, std::string(path).c_str(), SOCK_STREAM);
96 return mx_socket_unix_connect(&sock, std::string(path).c_str(), SOCK_DGRAM);
97 return false;
98 }
bool mx_socket_unix_connect(MXSocket *s, const char *path, int type)
Connect a UNIX-domain socket to a local path.
Definition mxsocket.c:241
@ TYPE_UNIX_DGRAM
UNIX-domain datagram socket.
Definition socket.hpp:51
@ TYPE_UNIX
UNIX-domain stream socket.
Definition socket.hpp:45

◆ is_open()

bool mxnetwork::Socket::is_open ( ) const
nodiscard

Return true when the socket is open.

Definition at line 167 of file socket.cpp.

167 {
168 return mx_socket_is_open(&sock);
169 }
bool mx_socket_is_open(const MXSocket *s)
Check whether the socket is open.
Definition mxsocket.c:442

◆ listen()

bool mxnetwork::Socket::listen ( std::string_view port,
int backlog )

Start listening on an Internet port.

Parameters
portService or port name.
backlogMaximum pending connection queue length.
Returns
True on success.

Definition at line 100 of file socket.cpp.

100 {
102 return mx_socket_listen(&sock, std::string(port).c_str(), backlog, SOCK_STREAM);
103 else if (type == SocketType::TYPE_INET6)
104 return mx_socket_ipv6_listen(&sock, std::string(port).c_str(), backlog, SOCK_STREAM);
106 return mx_socket_listen(&sock, std::string(port).c_str(), backlog, SOCK_DGRAM);
108 return mx_socket_ipv6_listen(&sock, std::string(port).c_str(), backlog, SOCK_DGRAM);
109 return false;
110 }
bool mx_socket_listen(MXSocket *s, const char *port, int backlog, int type)
Create and bind an Internet socket for listening.
Definition mxsocket.c:270
bool mx_socket_ipv6_listen(MXSocket *s, const char *port, int backlog, int type)
Create and bind an IPv6 Internet socket for listening.
Definition mxsocket.c:274

◆ listen_unix()

bool mxnetwork::Socket::listen_unix ( std::string_view path,
int backlog )

Start listening on a UNIX-domain socket path.

Parameters
pathFilesystem path for the socket.
backlogMaximum pending connection queue length.
Returns
True on success.

Definition at line 112 of file socket.cpp.

112 {
114 return mx_socket_unix_listen(&sock, std::string(path).c_str(), backlog, SOCK_STREAM);
116 return mx_socket_unix_listen(&sock, std::string(path).c_str(), backlog, SOCK_DGRAM);
117 return false;
118 }
bool mx_socket_unix_listen(MXSocket *s, const char *path, int backlog, int type)
Create and bind a UNIX-domain socket for listening.
Definition mxsocket.c:202

◆ operator=() [1/2]

Socket & mxnetwork::Socket::operator= ( const Socket & s)
delete

◆ operator=() [2/2]

Socket & mxnetwork::Socket::operator= ( Socket && s)
noexcept

Move-assign from another socket wrapper.

Definition at line 69 of file socket.cpp.

69 {
70 if (this != &s) {
71 type = s.type;
73 ::mx_close_socket(sock.sockfd);
74 setsocket(s.sock);
75 s.sock.sockfd = NULL_SOCKET;
76 }
77 return *this;
78 }
#define mx_close_socket(s)
Definition mxsocket.h:49

◆ read()

ssize_t mxnetwork::Socket::read ( void * buf,
size_t bytes,
int flags )

Read bytes from the socket.

Parameters
bufOutput buffer.
bytesNumber of bytes to read.
flagsPlatform socket flags.
Returns
Number of bytes read, or a negative error value.

Definition at line 187 of file socket.cpp.

187 {
188 return mx_socket_read(&sock, buf, bytes, flags);
189 }
ssize_t mx_socket_read(MXSocket *s, void *data, size_t len, int flags)
Read data from a socket.
Definition mxsocket.c:422

◆ read_all()

ssize_t mxnetwork::Socket::read_all ( void * buf,
size_t bytes )

Read exactly the requested number of bytes.

Parameters
bufOutput buffer.
bytesNumber of bytes to read.
Returns
Number of bytes read, or a negative error value.

Definition at line 199 of file socket.cpp.

199 {
200 return mx_socket_read_all(&sock, buf, bytes);
201 }
ssize_t mx_socket_read_all(MXSocket *sock, void *buf, size_t bytes)
Read the requested number of bytes from a socket.
Definition mxsocket.c:555

◆ readline()

bool mxnetwork::Socket::readline ( char ** buffer,
size_t * len )

Read a line of text from the socket.

Parameters
bufferOutput buffer pointer.
lenOutput length.
Returns
True on success.

Definition at line 191 of file socket.cpp.

191 {
192 return mx_socket_readline(&sock, buffer, len);
193 }
bool mx_socket_readline(MXSocket *s, char **buffer, size_t *len)
Read a line of text from a socket.
Definition mxsocket.c:474

◆ recvfrom() [1/2]

ssize_t mxnetwork::Socket::recvfrom ( void * buf,
size_t bytes )

Receive a datagram using the configured socket type.

Parameters
bufOutput buffer.
bytesMaximum number of bytes to receive.
Returns
Number of bytes received, or a negative error value.

Definition at line 216 of file socket.cpp.

216 {
218 return mx_socket_recvfrom(&sock, buf, bytes);
220 return mx_socket_ipv6_recvfrom(&sock, buf, bytes);
222 return mx_socket_unix_recvfrom(&sock, buf, bytes);
223 return 0;
224 }
ssize_t mx_socket_recvfrom(MXSocket *sock, void *buf, size_t bytes)
Receive a datagram using an Internet socket.
Definition mxsocket.c:601
ssize_t mx_socket_ipv6_recvfrom(MXSocket *sock, void *buf, size_t bytes)
Receive a datagram using an IPv6 Internet socket.
Definition mxsocket.c:632
ssize_t mx_socket_unix_recvfrom(MXSocket *sock, void *buf, size_t bytes)
Receive a datagram using a UNIX-domain socket.
Definition mxsocket.c:653

◆ recvfrom() [2/2]

ssize_t mxnetwork::Socket::recvfrom ( void * buf,
size_t bytes,
SocketAddress & address )

Definition at line 226 of file socket.cpp.

226 {
227 return mx_socket_recvfrom_address(&sock, buf, bytes, &address.value);
228 }
ssize_t mx_socket_recvfrom_address(MXSocket *sock, void *buf, size_t bytes, MXSocketAddress *address)
Definition mxsocket.c:614

◆ sendto() [1/2]

ssize_t mxnetwork::Socket::sendto ( const void * buf,
size_t bytes )

Send a datagram using the configured socket type.

Parameters
bufInput buffer.
bytesNumber of bytes to send.
Returns
Number of bytes sent, or a negative error value.

Definition at line 207 of file socket.cpp.

207 {
209 return mx_socket_sendto(&sock, buf, bytes);
211 return mx_socket_ipv6_sendto(&sock, buf, bytes);
213 return mx_socket_unix_sendto(&sock, buf, bytes);
214 return 0;
215 }
ssize_t mx_socket_unix_sendto(MXSocket *sock, const void *buf, size_t bytes)
Send a datagram using a UNIX-domain socket.
Definition mxsocket.c:645
ssize_t mx_socket_sendto(MXSocket *sock, const void *buf, size_t bytes)
Send a datagram using an Internet socket.
Definition mxsocket.c:583
ssize_t mx_socket_ipv6_sendto(MXSocket *sock, const void *buf, size_t bytes)
Send a datagram using an IPv6 Internet socket.
Definition mxsocket.c:592

◆ sendto() [2/2]

ssize_t mxnetwork::Socket::sendto ( const void * buf,
size_t bytes,
const SocketAddress & address )

Definition at line 230 of file socket.cpp.

230 {
231 return mx_socket_sendto_address(&sock, buf, bytes, &address.value);
232 }
ssize_t mx_socket_sendto_address(MXSocket *sock, const void *buf, size_t bytes, const MXSocketAddress *address)
Definition mxsocket.c:625

◆ setblocking()

bool mxnetwork::Socket::setblocking ( bool block)

Toggle blocking mode.

Parameters
blockTrue to enable blocking I/O.
Returns
True on success.

Definition at line 159 of file socket.cpp.

159 {
160 return mx_socket_set_blocking(&sock, block);
161 }
bool mx_socket_set_blocking(MXSocket *s, bool state)
Toggle blocking mode for a socket.
Definition mxsocket.c:331

◆ socket_type()

SocketType mxnetwork::Socket::socket_type ( ) const
nodiscard

Return the socket family and transport type.

Definition at line 179 of file socket.cpp.

179 {
180 return type;
181 }

◆ sockfd()

mx_socket_fd mxnetwork::Socket::sockfd ( ) const
nodiscard

Return the underlying socket handle.

Definition at line 183 of file socket.cpp.

183 {
184 return sock.sockfd;
185 }

◆ valid()

bool mxnetwork::Socket::valid ( ) const
nodiscard

Return true when the socket handle is valid.

Definition at line 163 of file socket.cpp.

163 {
164 return mx_socket_valid(&sock);
165 }

◆ write()

ssize_t mxnetwork::Socket::write ( const void * buf,
size_t bytes,
int flags )

Write bytes to the socket.

Parameters
bufInput buffer.
bytesNumber of bytes to write.
flagsPlatform socket flags.
Returns
Number of bytes written, or a negative error value.

Definition at line 195 of file socket.cpp.

195 {
196 return mx_socket_send(&sock, buf, bytes, flags);
197 }
ssize_t mx_socket_send(MXSocket *s, const void *data, size_t len, int flags)
Write data to a socket.
Definition mxsocket.c:432

◆ write_all()

ssize_t mxnetwork::Socket::write_all ( const void * buf,
size_t bytes )

Write exactly the requested number of bytes.

Parameters
bufInput buffer.
bytesNumber of bytes to write.
Returns
Number of bytes written, or a negative error value.

Definition at line 203 of file socket.cpp.

203 {
204 return mx_socket_write_all(&sock, buf, bytes);
205 }
ssize_t mx_socket_write_all(MXSocket *sock, const void *buf, size_t bytes)
Write the requested number of bytes to a socket.
Definition mxsocket.c:535

Member Data Documentation

◆ sock

MXSocket mxnetwork::Socket::sock
protected

Definition at line 203 of file socket.hpp.

◆ type

SocketType mxnetwork::Socket::type = SocketType::TYPE_INVALID
protected

Definition at line 204 of file socket.hpp.


The documentation for this class was generated from the following files: