84 lines
1.8 KiB
C
84 lines
1.8 KiB
C
/**
|
|
* Copyright (c) 2025 Dominic Masters
|
|
*
|
|
* This software is released under the MIT License.
|
|
* https://opensource.org/licenses/MIT
|
|
*/
|
|
|
|
#pragma once
|
|
#include "serverclient.h"
|
|
#include <netinet/in.h>
|
|
#include <arpa/inet.h>
|
|
|
|
#define SERVER_MAX_CLIENTS 32
|
|
#define SERVER_MAX_CHANNELS 2
|
|
|
|
typedef enum {
|
|
SERVER_STATE_STOPPED,
|
|
SERVER_STATE_STARTING,
|
|
SERVER_STATE_RUNNING,
|
|
SERVER_STATE_STOPPING
|
|
} serverstate_t;
|
|
|
|
typedef struct {
|
|
serverstate_t state;
|
|
pthread_t thread;
|
|
int serverSocket;
|
|
struct sockaddr_in serverAddress;
|
|
serverclient_t clients[SERVER_MAX_CLIENTS];
|
|
} server_t;
|
|
|
|
extern server_t SERVER;
|
|
|
|
/**
|
|
* Initialize the server
|
|
*
|
|
* This function initializes the server by setting up the ENet library and
|
|
* creating a server host.
|
|
*/
|
|
void serverInit();
|
|
|
|
/**
|
|
* Start the server
|
|
*
|
|
* This function starts the server by creating a host, binding it to the
|
|
* specified address and port, and starting the server thread.
|
|
*
|
|
* @param port The port number to bind the server to.
|
|
* @return Returns an error code if the server fails to start.
|
|
*/
|
|
errorret_t serverStart(uint16_t port);
|
|
|
|
/**
|
|
* Server thread function
|
|
*
|
|
* This function runs in a separate thread and handles incoming connections,
|
|
* messages, and disconnections.
|
|
*
|
|
* @param arg Pointer to the argument passed to the thread.
|
|
*/
|
|
void * serverThread(void *arg);
|
|
|
|
/**
|
|
* Get the client count
|
|
*
|
|
* This function returns the number of connected clients to the server.
|
|
*
|
|
* @return The number of connected clients.
|
|
*/
|
|
uint8_t serverGetClientCount();
|
|
|
|
/**
|
|
* Stop the server
|
|
*
|
|
* This function stops the server by destroying the host and cleaning up
|
|
* resources.
|
|
*/
|
|
void serverStop();
|
|
|
|
/**
|
|
* Dispose of the server
|
|
*
|
|
* This function disposes of the server by deinitializing the ENet library.
|
|
*/
|
|
void serverDispose(); |