ryu_ldn::network::Socket
ryu_ldn::network::Socket
Section titled “ryu_ldn::network::Socket”TCPwrapper.
Provides connect/send/recv operations with timeout support. Non-copyable, move-only.Usage:
Members
Section titled “Members”Type: int
m_connected
Section titled “m_connected”Type: bool
Methods
Section titled “Methods”Socket
Section titled “Socket”void Socket()Default constructor - creates invalid socket.
Default constructor - creates an uninitialized socket.After construction, the socket is in an invalid state (m_fd == -1). Callto establish a connection, which will create the underlying socket automatically.
~Socket
Section titled “~Socket”void ~Socket()Destructor - closes socket if open.
Destructor - ensures socket is properly closed.Automatically closes the socket if still open. This ensures no resource leaks even ifwasn’t explicitly called.
Socket
Section titled “Socket”void Socket(const&)Parameters:
param(const&)
operator=
Section titled “operator=”& operator=(const&)Parameters:
param(const&)
Returns: &
Socket
Section titled “Socket”void Socket(&& other)Move constructor.
Transfers ownership of the socket from another instance. The source socket becomes invalid after the move.otherto move fromparam
Parameters:
other(&&)
operator=
Section titled “operator=”& operator=(&& other)Move assignment operator.
Closes any existing socket, then transfers ownership from the source. The source socket becomes invalid after the move.otherto move fromparamReference to this socketreturn
Parameters:
other(&&)
Returns: &
connect
Section titled “connect”SocketResult connect(const char * host, uint16_t port, uint32_t timeout_ms)Connect to a remote host.
Establishes a TCP connection to the specified host and port. Creates the underlying socket if not already created.Connection ProcessResolve hostname to IP addressCreate socket if neededSet non-blocking mode (if timeout specified)Initiate connectionWait for connection with poll() (if timeout specified)Verify connection succeededRestore blocking modehostHostname or IP address (e.g., “ldn.ryujinx.app” or “192.168.1.1”)portTCP port number (e.g., 30456)timeout_msConnection timeout in milliseconds (0 = blocking/no timeout)paramon successful connectionreturnifwasn’t calledreturnif already connectedreturnif hostname resolution failsreturnif connection times outreturnif server not listeningreturnif host can’t be reachedreturnIf connection fails, the socket is automatically closednoteRecommended timeout: 5000ms (5 seconds) for typical usenoteclassryu__ldn_1_1network_1_1Socket_1
Parameters:
host(const char *)port(uint16_t)timeout_ms(uint32_t)
Returns: SocketResult
SocketResult send(const uint8_t * data, size_t size, size_t & sent)Send data (blocking)
Send data over the socket.Attempts to send data. May not send all data in one call - check the ‘sent’ parameter to see how much was actually sent.dataPointer to data buffer to sendsizeNumber of bytes to sendsentNumber of bytes actually sent (may be less than size)paramif some data was sentreturnif socket is non-blocking and would blockreturnif not connectedreturnif connection was closed by peerreturnif connection was resetreturnUseif you need to send all data reliablynoteUses MSG_NOSIGNAL to prevent SIGPIPE on broken connectionsnote
Parameters:
data(const uint8_t *)size(size_t)sent(size_t &)
Returns: SocketResult
send_all
Section titled “send_all”SocketResult send_all(const uint8_t * data, size_t size)Send all data (loops until complete or error)
Send all data reliably.Loops until all data is sent or an error occurs. Handles partial sends and WouldBlock conditions automatically.dataPointer to data buffer to sendsizeNumber of bytes to sendparamif all data was sentreturnif waiting too long for socket to be writablereturnOther error codes on failurereturnThis function may block for extended periodsnoteUses 5 second timeout per send chunknote
Parameters:
data(const uint8_t *)size(size_t)
Returns: SocketResult
SocketResult recv(uint8_t * buffer, size_t buffer_size, size_t & received, int32_t timeout_ms)Receive data (non-blocking or with timeout)
Receive data from the socket.Attempts to receive data from the socket. Behavior depends on timeout:bufferBuffer to receive data intobuffer_sizeMaximum bytes to receive (size of buffer)receivedNumber of bytes actually receivedtimeout_msReceive timeout in millisecondsparamif data was receivedreturnif non-blocking and no data availablereturnif timed out waiting for datareturnif connection was closed by peerreturnif not connectedreturnreceived may be less than buffer_size (TCP is a stream, not messages)note
Parameters:
buffer(uint8_t *)buffer_size(size_t)received(size_t &)timeout_ms(int32_t)
Returns: SocketResult
void close()Close the socket.
Closes the underlying socket file descriptor and resets state. Safe to call multiple times (subsequent calls are no-ops). Also called automatically by the destructor.
is_connected
Section titled “is_connected”bool is_connected()Check if socket is connected.
true if connectedreturntrue if a connection has been established and not yet closedreturnfalse otherwisereturnThis only tracks local state. The remote end may have closed the connection; this will be detected on the next send/recv.note
Returns: bool
is_valid
Section titled “is_valid”bool is_valid()Check if socket is valid (has file descriptor)
Check if socket is valid.true if validreturntrue if the socket has a valid file descriptorreturnfalse if socket was never created or has been closedreturn
Returns: bool
get_fd
Section titled “get_fd”int get_fd()Get the native socket file descriptor.
File descriptor or -1 if invalidreturn
Returns: int
set_non_blocking
Section titled “set_non_blocking”SocketResult set_non_blocking(bool non_blocking)Set socket to non-blocking mode.
Set socket blocking mode.non_blockingtrue for non-blocking, false for blockingparamnon_blockingtrue to set non-blocking, false for blockingparamor errorreturnon successreturnif socket is invalid or fcntl failsreturnGenerally you don’t need to call this directly - the timeout parameters on connect/recv handle non-blocking behavior.note
Parameters:
non_blocking(bool)
Returns: SocketResult
set_nodelay
Section titled “set_nodelay”SocketResult set_nodelay(bool nodelay)Set TCP_NODELAY option (disable Nagle’s algorithm)
Set TCP_NODELAY option.Disables Nagle’s algorithm, which buffers small packets to reduce overhead. For latency-sensitive protocols like gaming, disabling this can reduce latency at the cost of more network packets.nodelaytrue to disable Nagle (lower latency), false to enableparamon successreturnon failurereturnRecommended: true for gaming/realtime applicationsnote
Parameters:
nodelay(bool)
Returns: SocketResult
set_recv_buffer_size
Section titled “set_recv_buffer_size”SocketResult set_recv_buffer_size(int size)Set socket receive buffer size.
Adjusts the kernel receive buffer size. Larger buffers can help with high-bandwidth transfers but consume more memory.sizeBuffer size in bytesparamon successreturnon failurereturnThe kernel may not honor the exact size requestednote
Parameters:
size(int)
Returns: SocketResult
set_send_buffer_size
Section titled “set_send_buffer_size”SocketResult set_send_buffer_size(int size)Set socket send buffer size.
Adjusts the kernel send buffer size. Larger buffers can help with high-bandwidth transfers but consume more memory.sizeBuffer size in bytesparamon successreturnon failurereturnThe kernel may not honor the exact size requestednote
Parameters:
size(int)
Returns: SocketResult
Methods
Section titled “Methods”create
Section titled “create”SocketResult create()Create TCP socket.
Create the underlying TCP socket.Internal function to create the BSD socket. Called automatically byif the socket hasn’t been created yet.if socket created or already existsreturnon failurereturn
Returns: SocketResult
wait_ready
Section titled “wait_ready”SocketResult wait_ready(uint32_t timeout_ms, bool for_write)Wait for socket to be ready (using poll)
Wait for socket to be ready for I/O.Uses poll() to wait for the socket to become readable or writable. This is used internally for timeout support on connect and recv.timeout_msMaximum time to wait in millisecondsfor_writetrue to wait for write-ready, false for read-readyparamif socket is readyreturnif timeout expiredreturnif poll indicates an errorreturnpoll() is preferred over select() for simplicity and efficiencynote
Parameters:
timeout_ms(uint32_t)for_write(bool)
Returns: SocketResult