80 lines
2.0 KiB
C
80 lines
2.0 KiB
C
/**
|
|
* Copyright (c) 2025 Dominic Masters
|
|
*
|
|
* This software is released under the MIT License.
|
|
* https://opensource.org/licenses/MIT
|
|
*/
|
|
|
|
#pragma once
|
|
#include "dusk.h"
|
|
#include "packetwelcome.h"
|
|
#include "packetdisconnect.h"
|
|
#include "packetping.h"
|
|
|
|
typedef enum {
|
|
PACKET_TYPE_INVALID = 0,
|
|
PACKET_TYPE_WELCOME = 1,
|
|
PACKET_TYPE_DISCONNECT = 2,
|
|
PACKET_TYPE_PING = 3,
|
|
} packettype_t;
|
|
|
|
#define PACKET_TYPE_COUNT 4
|
|
|
|
typedef union {
|
|
packetwelcome_t welcome;
|
|
packetdisconnect_t disconnect;
|
|
packetping_t ping;
|
|
} packetdata_t;
|
|
|
|
typedef struct packet_s {
|
|
packettype_t type;
|
|
uint32_t length;
|
|
packetdata_t data;
|
|
} packet_t;
|
|
|
|
typedef struct {
|
|
errorret_t (*client)(const packet_t *packet, client_t *client);
|
|
errorret_t (*server)(const packet_t *packet, serverclient_t *client);
|
|
} packethandler_t;
|
|
|
|
extern packethandler_t PACKET_HANDLERS[];
|
|
|
|
/**
|
|
* Initializes a packet with the given type. This is only to be used by sub
|
|
* initializers, such as packetWelcomeCreate for example.
|
|
*
|
|
* @param packet Pointer to the packet structure to initialize.
|
|
* @param type The type of the packet.
|
|
* @param length The length of the packet data.
|
|
*/
|
|
void packetInit(
|
|
packet_t *packet,
|
|
const packettype_t type,
|
|
const uint32_t length
|
|
);
|
|
|
|
/**
|
|
* Processes a packet for a given client. Will auto-decide the correct method to
|
|
* handle the process
|
|
*
|
|
* @param packet Pointer to the packet structure to process.
|
|
* @param client Pointer to the client structure.
|
|
* @return ERROR_OK on success, or an error code on failure.
|
|
*/
|
|
errorret_t packetClientProcess(
|
|
const packet_t *packet,
|
|
client_t *client
|
|
);
|
|
|
|
/**
|
|
* Processes a packet for a given server client. Will auto-decide the correct
|
|
* method to handle the process
|
|
*
|
|
* @param packet Pointer to the packet structure to process.
|
|
* @param client Pointer to the server client structure.
|
|
* @return ERROR_OK on success, or an error code on failure.
|
|
*/
|
|
errorret_t packetServerProcess(
|
|
const packet_t *packet,
|
|
serverclient_t *client
|
|
); |