This commit is contained in:
2026-01-14 22:03:11 +01:00
commit 70bb065fbe
16 changed files with 935 additions and 0 deletions

57
include/common/dynarr.h Normal file
View File

@@ -0,0 +1,57 @@
#ifndef DYNARR_H
#define DYNARR_H
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define DYNARR_MAX_CAPACITY ((size_t)0x7FFFFFFF)
typedef struct {
size_t size;
size_t elemSize;
size_t capacity;
void* data;
} DynArr;
// Do not use; Use DYNARR_CREATE macro instead.
DynArr* DynArr_create(size_t elemSize, size_t capacity);
// Reserve n blocks in arary; New size will be n, NOT size + n; Reserving less memory that current will fail, use prune instead.
void DynArr_reserve(DynArr* p, size_t n);
// Push data into a new block at the end of the array
void* DynArr_push_back(DynArr* p, void* value);
// Remove the last block in the array.
void DynArr_pop_back(DynArr* p);
// Remove first block from array.
void DynArr_pop_front(DynArr* p);
// Remove index from array. This moves all blocks after the index block.
void DynArr_remove(DynArr* p, size_t index);
// Erase the array. This will not free unused blocks.
void DynArr_erase(DynArr* p);
// Prune and free unused blocks. If pruning to zero, ensure to reserve after.
void DynArr_prune(DynArr* p);
// Get a pointer to a block by index
void* DynArr_at(DynArr* p, size_t index);
// Get the index by block pointer
size_t DynArr_at_ptr(DynArr* p, void* ptr);
// Get size
size_t DynArr_size(DynArr* p);
// Get capacity
size_t DynArr_capacity(DynArr* p);
void DynArr_destroy(DynArr* p);
#define DYNARR_CREATE(T, initialCapacity) DynArr_create(sizeof(T), initialCapacity)
#endif

12
include/common/numgen.h Normal file
View File

@@ -0,0 +1,12 @@
#ifndef NUMGEN_H
#define NUMGEN_H
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
#include <string.h>
unsigned char random_byte(void);
uint32_t random_four_byte(void);
#endif

View File

@@ -0,0 +1,46 @@
#ifndef TASK_H
#define TASK_H
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <common/dynarr.h>
typedef enum {
TASK_NONE,
TASK_PENDING,
TASK_ASSIGNED,
TASK_DONE,
TASK_FAILED
} task_state_t;
extern int TASK_ERR_CODE;
typedef enum {
TASK_ERR_NONE,
TASK_ERR_INVPTR,
TASK_ERR_INVARGS,
TASK_ERR_INVQUEUE,
TASK_ERR_INVIDX,
TASK_ERR_INVTASK
} task_err;
typedef struct {
char name[32]; // string
DynArr* data;
} task_arg_t;
typedef struct {
uint32_t taskId;
char binary[64];
DynArr* args;
task_state_t state;
uint32_t assigned_to;
time_t assigned_at;
} task_t;
void Task_Create(task_t* tsk);
void Task_DestroyArgs(task_t* task);
#endif

View File

@@ -0,0 +1,27 @@
#ifndef TCPCLIENT_H
#define TCPCLIENT_H
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <stdint.h>
#define MTU 1500
struct TcpClient {
int clientFd;
struct sockaddr_in clientAddr;
uint32_t clientId;
unsigned char dataBuf[MTU];
void (*on_data)(struct TcpClient* client);
void (*on_disconnect)(struct TcpClient* client);
pthread_t clientThread;
};
typedef struct TcpClient TcpClient;
#endif

View File

@@ -0,0 +1,54 @@
#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 <common/tcpd/tcpclient.h>
#include <common/numgen.h>
typedef struct {
int sockFd;
struct sockaddr_in addr;
int opt;
// Called before the client thread runs
void (*on_connect)(TcpClient* client);
// Called when data is received
void (*on_data)(TcpClient* client);
// Called before the socket and client thread are killed; Do NOT free client manually
void (*on_disconnect)(TcpClient* client);
// max clients
size_t clients;
TcpClient** clientsArrPtr;
pthread_t svrThread;
} TcpServer;
struct tcpclient_thread_args {
TcpClient* clientPtr;
TcpServer* serverPtr;
};
typedef struct tcpclient_thread_args tcpclient_thread_args;
TcpServer* TcpServer_Create();
void TcpServer_Destroy(TcpServer* ptr);
void TcpServer_Init(TcpServer* ptr, unsigned short port, const char* addr);
void TcpServer_Start(TcpServer* ptr, int maxcons);
void TcpServer_Stop(TcpServer* ptr);
void TcpServer_Send(TcpServer* ptr, TcpClient* cli, void* data, size_t len);
void TcpServer_Disconnect(TcpServer* ptr, TcpClient* cli);
void TcpServer_KillClient(TcpServer* ptr, TcpClient* cli);
size_t Generic_FindClientInArrayByPtr(TcpClient** arr, TcpClient* ptr, size_t len);
#endif

View File

@@ -0,0 +1,17 @@
#ifndef TASKQUEUE_H
#define TASKQUEUE_H
#include <common/task/task.h>
#include <common/dynarr.h>
typedef struct {
DynArr* tasks;
} task_queue_t;
void TaskQueue_Init(task_queue_t* queue);
void TaskQueue_DestroyQueue(task_queue_t* queue);
void TaskQueue_AddTask(task_queue_t* queue, task_t* task);
void TaskQueue_RemoveTask(task_queue_t* queue, size_t idx);
void TaskQueue_RemoveTaskByPtr(task_queue_t* queue, task_t* task);
#endif