Implemented basic connection logic

This commit is contained in:
Nikita Tyukalov, ASUS, Linux
2026-03-25 00:25:47 +03:00
parent 7eaf35ea4c
commit 8e90c4c152
6 changed files with 572 additions and 1 deletions

55
connection.h Normal file
View File

@@ -0,0 +1,55 @@
#ifndef __CONNECTION_H
#define __CONNECTION_H
#include <stdbool.h>
#include <stdint.h>
// Add socket to managed sockets.
// Parameters:
// - fd - socket fd
// Returns:
// - on success: ID of connection (>0)
// - on failure: 0
// Remarks:
// - in case of success, you may forget about
// the socket you added because it is completely
// managed by this module
// - in case of failure you still need to close
// the socket
// - does not check if fd is already added
uint32_t connection_add(int fd);
// Cleanup.
// Remarks:
// - closes all connections
void connection_cleanup();
// Check if socket is managed.
// Parameters:
// - fd - socket fd
// Returns:
// - true if the socket is managed
// - false if the socket is not managed
bool connection_is_socket_managed(int fd);
// Handle socket events.
// Parameters:
// - fd - socket fd
// - events - events as returned by epoll_wait
// Returns:
// - true on success
// - false on failure
// Remarks:
// - the app should be terminated if this function returns false
void connection_event(int fd, uint32_t events);
// Send data over connection.
// Parameters:
// - id - connection ID
// - data - data to send
// - size - size of data
// Returns:
// - count of bytes sent
uint32_t connection_send(uint32_t id, const void *data, uint32_t size);
#endif