31 if (!transport || !callbacks) {
48 bool enforce_encryption = (transport->
crypto_ctx != NULL);
72 return dispatch_result;
81 if (!transport || !frame_data) {
85 size_t frame_size = strlen(frame_data);
86 if (frame_size == 0) {
104 if (checked_size_add(
sizeof(header), frame_size, &total_size) !=
ASCIICHAT_OK) {
115 memcpy(buffer, &header,
sizeof(header));
116 memcpy(buffer +
sizeof(header), frame_data, frame_size);
134 if (!transport || !state) {
🗃️ Lock-Free Unified Memory Buffer Pool with Lazy Allocation
Hardware-Accelerated CRC32 Checksum Computation.
🔄 Network byte order conversion helpers
#define HOST_TO_NET_U32(val)
void buffer_pool_free(buffer_pool_t *pool, void *data, size_t size)
Free a buffer back to the pool (lock-free)
void * buffer_pool_alloc(buffer_pool_t *pool, size_t size)
Allocate a buffer from the pool (lock-free fast path)
#define SET_ERRNO(code, context_msg,...)
Set error code with custom context message and log it.
asciichat_error_t
Error and exit codes - unified status values (0-255)
uint32_t width
Terminal width in characters.
void * allocated_buffer
Buffer that needs to be freed by caller (may be NULL if not allocated)
uint32_t checksum
CRC32 checksum of original ASCII data.
uint32_t original_size
Size of original uncompressed ASCII data in bytes.
uint32_t height
Terminal height in characters.
uint32_t compressed_size
Size of compressed data (0 = not compressed)
size_t allocated_size
Size of allocated buffer in bytes.
size_t len
Length of payload data in bytes.
void * data
Packet payload data (decrypted and decompressed if applicable)
packet_recv_result_t receive_packet_secure(socket_t sockfd, void *crypto_ctx, bool enforce_encryption, packet_envelope_t *envelope)
Receive a packet with decryption and decompression support.
packet_type_t type
Packet type (from packet_types.h)
uint32_t flags
Frame flags bitmask (HAS_COLOR, IS_COMPRESSED, etc.)
packet_recv_result_t
Packet reception result codes.
@ PACKET_RECV_EOF
Connection closed (EOF)
@ PACKET_RECV_SECURITY_VIOLATION
Encryption policy violation (e.g., unencrypted packet when encryption required)
@ PACKET_RECV_SUCCESS
Packet received successfully.
@ PACKET_TYPE_SERVER_STATE
Server sends current state to clients.
@ PACKET_TYPE_CLEAR_CONSOLE
Server tells client to clear console.
@ PACKET_TYPE_ASCII_FRAME
Complete ASCII frame with all metadata.
#define asciichat_crc32(data, len)
Main CRC32 dispatcher macro - use this in application code.
asciichat_error_t acip_handle_server_packet(acip_transport_t *transport, packet_type_t type, const void *payload, size_t payload_len, void *client_ctx, const acip_server_callbacks_t *callbacks)
Handle incoming packet on server side.
ACIP protocol packet handlers (transport-agnostic)
asciichat_error_t acip_send_server_state(acip_transport_t *transport, const server_state_packet_t *state)
Send server state update to client (server → client)
asciichat_error_t acip_server_receive_and_dispatch(acip_transport_t *transport, void *client_ctx, const acip_server_callbacks_t *callbacks)
Receive packet from client and dispatch to callbacks.
asciichat_error_t acip_send_ascii_frame(acip_transport_t *transport, const char *frame_data, uint32_t width, uint32_t height)
Send ASCII frame to client (server → client)
asciichat_error_t acip_send_clear_console(acip_transport_t *transport)
Send clear console command to client (server → client)
ACIP server-side protocol library.
📝 Logging API with multiple log levels and terminal output control
✅ Safe Integer Arithmetic and Overflow Detection
Packet protocol implementation with encryption and compression support.
asciichat_error_t packet_send_via_transport(acip_transport_t *transport, packet_type_t type, const void *payload, size_t payload_len)
Send packet via transport with proper header (exported for generic wrappers)
ACIP shared/bidirectional packet sending functions.
Server-side packet handler callbacks.
bool(* is_connected)(acip_transport_t *transport)
Check if transport is connected.
socket_t(* get_socket)(acip_transport_t *transport)
Get underlying socket (if applicable)
Transport instance structure.
const acip_transport_methods_t * methods
Method table (virtual functions)
crypto_context_t * crypto_ctx
Optional encryption context.
ASCII frame packet structure (Packet Type 2)
Packet envelope containing received packet data.
Server state packet structure.