111 lines
2.8 KiB
C
111 lines
2.8 KiB
C
/**
|
|
* Copyright (c) 2025 Dominic Masters
|
|
*
|
|
* This software is released under the MIT License.
|
|
* https://opensource.org/licenses/MIT
|
|
*/
|
|
|
|
#pragma once
|
|
#include "error/error.h"
|
|
|
|
typedef struct serverclient_s serverclient_t;
|
|
typedef struct serverclientaccept_s serverclientaccept_t;
|
|
|
|
typedef struct {
|
|
int32_t socket;
|
|
} networkedserverclientaccept_t;
|
|
|
|
typedef struct {
|
|
int32_t socket;
|
|
pthread_t readThread;
|
|
pthread_t writeThread;
|
|
struct timeval timeout;
|
|
pthread_mutex_t readLock;
|
|
pthread_mutex_t writeLock;
|
|
} networkedserverclient_t;
|
|
|
|
/**
|
|
* Accepts an incoming connection from a networked server client.
|
|
*
|
|
* @param client Pointer to the server client structure.
|
|
* @param accept Accept structure containing client information.
|
|
* @return Error code indicating success or failure.
|
|
*/
|
|
errorret_t networkedServerClientAccept(
|
|
serverclient_t *client,
|
|
const serverclientaccept_t accept
|
|
);
|
|
|
|
/**
|
|
* Closes the connection to a networked server client. Waits for the thread to finish.
|
|
*
|
|
* @param client Pointer to the server client structure.
|
|
*/
|
|
void networkedServerClientClose(serverclient_t *client);
|
|
|
|
/**
|
|
* Closes the connection to a networked server client on the thread.
|
|
*
|
|
* @param client Pointer to the server client structure.
|
|
* @param reason Reason for closing the connection.
|
|
*/
|
|
void networkedServerClientCloseOnThread(
|
|
serverclient_t *client,
|
|
const char_t *reason
|
|
);
|
|
|
|
/**
|
|
* Receives data from a server client.
|
|
*
|
|
* @param client Pointer to the server client structure.
|
|
* @param buffer Pointer to the buffer to store received data.
|
|
* @param len Max length of the buffer.
|
|
* @return Number of bytes received. 0 or less indicates an error.
|
|
*/
|
|
ssize_t networkedServerClientRead(
|
|
const serverclient_t * client,
|
|
uint8_t *buffer,
|
|
const size_t len
|
|
);
|
|
|
|
/**
|
|
* Writes data to a server client.
|
|
*
|
|
* @param client Pointer to the server client structure.
|
|
* @param data Pointer to the data to send.
|
|
* @param len Length of the data to send.
|
|
* @return Error code indicating success or failure.
|
|
*/
|
|
errorret_t networkedServerClientWrite(
|
|
serverclient_t * client,
|
|
const uint8_t *data,
|
|
const size_t len
|
|
);
|
|
|
|
/**
|
|
* Writes a packet to a server client.
|
|
*
|
|
* @param client Pointer to the server client structure.
|
|
* @param packet Pointer to the packet to send.
|
|
* @return Error code indicating success or failure.
|
|
*/
|
|
errorret_t networkedServerClientWritePacket(
|
|
serverclient_t * client,
|
|
const packet_t *packet
|
|
);
|
|
|
|
/**
|
|
* Thread function for handling reading from a networked server client.
|
|
*
|
|
* @param arg Pointer to the server client structure.
|
|
* @return NULL.
|
|
*/
|
|
void * networkedServerClientReadThread(void *arg);
|
|
|
|
/**
|
|
* Thread function for handling writing to a networked server client.
|
|
*
|
|
* @param arg Pointer to the server client structure.
|
|
* @return NULL.
|
|
*/
|
|
void * networkedServerClientWriteThread(void *arg); |