TcpServer
TCP server for accepting and managing client connections
It
Platform-specific TCP server handle
#if defined(_WIN32) || defined(_WIN64)
struct It {
SOCKET sockfd = INVALID_SOCKET
U8 running = 0
U16 slots = 0
}
#else
struct It {
I32 sockfd = -1
U8 running = 0
U16 slots = 0
}
#endifcreate
Create a TCP server bound to a port
TcpServer create(I16 port, U16 slots)Parameters
port— Port to listen onslots— Maximum simultaneous connections
Returns — Initialized server object
destroy
Destroy a TCP server and close all connections
U0 destroy(TcpServer server)Parameters
server— The TCP server
listen
Start accepting connections, calling a callback for each new client
U0 listen(TcpServer server, U0 (*callback)(TcpClient*))Parameters
server— The TCP servercallback— Function called with each new client
write
Send data to a specific client
U8 write(TcpClient client, I8* buffer, U32 size)Parameters
client— Target clientbuffer— Data to sendsize— Number of bytes to send
Returns — Non-zero on success
read
Read data from a client (non-blocking, may be partial)
U8 read(TcpClient client, I8* buffer, U32 size)Parameters
client— Source clientbuffer— Receive buffersize— Maximum bytes to read
Returns — Non-zero on success
readAll
Read data from a client, blocking until all bytes arrive
U8 readAll(TcpClient client, I8* buffer, U32 size)Parameters
client— Source clientbuffer— Receive buffersize— Exact number of bytes to read
Returns — Non-zero on success
broadcast
Broadcast data to all clients in a list
U0 broadcast(List<TcpClient> clients, I8* buffer, U32 size)Parameters
clients— List of clients to send tobuffer— Data to broadcastsize— Number of bytes to send