55 lines
1.7 KiB
C
55 lines
1.7 KiB
C
#ifndef TCPSERVER_H
|
|
#define TCPSERVER_H
|
|
|
|
#include <arpa/inet.h>
|
|
#include <pthread.h>
|
|
#include <stddef.h>
|
|
#include <constants.h>
|
|
|
|
#include <tcpd/tcpconnection.h>
|
|
|
|
typedef struct {
|
|
int sockFd; // IPv6 listening socket (-1 if IPv6 unavailable)
|
|
int sockFdV4; // IPv4 listening socket (-1 on bind failure)
|
|
int opt;
|
|
int isRunning;
|
|
void* owner;
|
|
|
|
// Called before the client thread runs
|
|
void (*on_connect)(tcp_connection_t* client);
|
|
// Called when data is received
|
|
void (*on_data)(tcp_connection_t* client);
|
|
// Called before the socket and client thread are killed; Do NOT free client manually
|
|
void (*on_disconnect)(tcp_connection_t* client);
|
|
|
|
// max clients
|
|
size_t maxClients;
|
|
tcp_connection_t** clientsArrPtr;
|
|
pthread_mutex_t clientsMutex;
|
|
|
|
pthread_t svrThread; // IPv6 accept thread
|
|
pthread_t svrThreadV4; // IPv4 accept thread
|
|
} tcp_server_t;
|
|
|
|
struct tcpclient_thread_args {
|
|
tcp_connection_t* clientPtr;
|
|
tcp_server_t* serverPtr;
|
|
};
|
|
|
|
typedef struct tcpclient_thread_args tcpclient_thread_args;
|
|
|
|
tcp_server_t* TcpServer_Create();
|
|
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);
|
|
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);
|
|
|
|
size_t Generic_FindClientInArrayByPtr(tcp_connection_t** arr, tcp_connection_t* ptr, size_t len);
|
|
|
|
#endif
|