TCP Node boilerplate; CLI interface

This commit is contained in:
2026-04-23 16:24:26 +02:00
parent d631eb190d
commit 9c99eec3a8
13 changed files with 1635 additions and 578 deletions

33
include/tcpd/tcpclient.h Normal file
View File

@@ -0,0 +1,33 @@
#ifndef TCPCLIENT_H
#define TCPCLIENT_H
#include <arpa/inet.h>
#include <stddef.h>
#include <tcpd/tcpconnection.h>
typedef struct {
tcp_connection_t* connection;
void (*on_connect)(tcp_connection_t* conn);
void (*on_data)(tcp_connection_t* conn);
void (*on_disconnect)(tcp_connection_t* conn);
void* owner;
} tcp_client_t;
int TcpClient_Init(tcp_client_t* client);
void TcpClient_Destroy(tcp_client_t* client);
int TcpClient_Connect(
tcp_client_t* client,
const char* peerIp,
unsigned short peerPort,
void (*on_connect)(tcp_connection_t* conn),
void (*on_data)(tcp_connection_t* conn),
void (*on_disconnect)(tcp_connection_t* conn),
void* owner
);
int TcpClient_Send(tcp_client_t* client, const void* data, size_t len);
void TcpClient_Disconnect(tcp_client_t* client);
#endif

View File

@@ -1,29 +1,64 @@
#ifndef TCPCLIENT_H
#define TCPCLIENT_H
#ifndef TCPCONNECTION_H
#define TCPCONNECTION_H
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <dynarr.h>
#define MTU 1500
#define TCP_IO_BUFFER_SIZE 1500
#define TCP_FRAME_HEADER_SIZE 4U
#define TCP_MAX_FRAME_PAYLOAD (1024U * 1024U)
struct tcp_connection_t {
int clientFd;
struct sockaddr_in clientAddr;
uint32_t clientId;
unsigned char dataBuf[MTU];
ssize_t dataBufLen;
void (*on_data)(struct tcp_connection_t* client);
void (*on_disconnect)(struct tcp_connection_t* client);
pthread_t clientThread;
};
typedef enum {
TCP_CONNECTION_ROLE_INBOUND = 0,
TCP_CONNECTION_ROLE_OUTBOUND = 1
} tcp_connection_role_t;
typedef struct tcp_connection_t tcp_connection_t;
struct tcp_connection_t {
int sockFd;
struct sockaddr_in peerAddr;
uint32_t connectionId;
tcp_connection_role_t role;
pthread_t ioThread;
pthread_mutex_t sendLock;
pthread_mutex_t stateLock;
bool closing;
bool disconnectedNotified;
unsigned char* dataBuf;
size_t dataBufLen;
size_t dataBufCap;
unsigned char headerBuf[TCP_FRAME_HEADER_SIZE];
size_t headerBytesRead;
uint32_t expectedPayloadLen;
unsigned char* frameBuf;
size_t frameBytesRead;
void (*on_data)(tcp_connection_t* conn);
void (*on_disconnect)(tcp_connection_t* conn);
void* owner;
};
int TcpConnection_Init(tcp_connection_t* conn, int sockFd, const struct sockaddr_in* peerAddr, tcp_connection_role_t role);
void TcpConnection_Destroy(tcp_connection_t* conn);
int TcpConnection_SetDataBuffer(tcp_connection_t* conn, const unsigned char* data, size_t len);
void TcpConnection_ResetFramingState(tcp_connection_t* conn);
int TcpConnection_FeedFramedData(tcp_connection_t* conn, const unsigned char* input, size_t inputLen);
int TcpConnection_SendRaw(int sockFd, const void* data, size_t len);
int TcpConnection_SendFramed(tcp_connection_t* conn, const void* payload, size_t payloadLen);
void TcpConnection_RequestClose(tcp_connection_t* conn);
void TcpConnection_MarkDisconnectNotified(tcp_connection_t* conn);
bool TcpConnection_IsDisconnectNotified(tcp_connection_t* conn);
#endif

View File

@@ -1,23 +1,18 @@
#ifndef TCPSERVER_H
#define TCPSERVER_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdint.h>
#include <stddef.h>
#include <tcpd/tcpconnection.h>
#include <numgen.h>
#include <dynarr.h>
typedef struct {
int sockFd;
struct sockaddr_in addr;
int opt;
int isRunning;
void* owner;
// Called before the client thread runs
void (*on_connect)(tcp_connection_t* client);
@@ -27,8 +22,9 @@ typedef struct {
void (*on_disconnect)(tcp_connection_t* client);
// max clients
size_t clients;
size_t maxClients;
tcp_connection_t** clientsArrPtr;
pthread_mutex_t clientsMutex;
pthread_t svrThread;
} tcp_server_t;
@@ -46,8 +42,8 @@ void TcpServer_Destroy(tcp_server_t* ptr);
void TcpServer_Init(tcp_server_t* ptr, unsigned short port, const char* addr);
void TcpServer_Start(tcp_server_t* ptr, int maxcons);
void TcpServer_Stop(tcp_server_t* ptr);
void TcpServer_Send(tcp_server_t* ptr, tcp_connection_t* cli, void* data, size_t len);
void Generic_SendSocket(int sock, void* data, size_t len);
int TcpServer_Send(tcp_server_t* ptr, tcp_connection_t* cli, const void* data, size_t len);
void Generic_SendSocket(int sock, const void* data, size_t len);
void TcpServer_Disconnect(tcp_server_t* ptr, tcp_connection_t* cli);
void TcpServer_KillClient(tcp_server_t* ptr, tcp_connection_t* cli);