Packet system basically working

This commit is contained in:
2025-04-22 22:52:45 -05:00
parent d0a8f3813d
commit 44ebe1e1da
9 changed files with 90 additions and 28 deletions

View File

@ -93,9 +93,9 @@ errorret_t networkedClientConnect(
if(err) return err; if(err) return err;
switch(packet.type) { switch(packet.type) {
case PACKET_TYPE_DISCONNECT: case PACKET_TYPE_DISCONNECT:
err = packetDisconnectClient(&packet); err = packetDisconnectClientProcess(&packet, client);
if(err) return err; if(err) return err;
break; return error("Server disconnected");
case PACKET_TYPE_WELCOME: case PACKET_TYPE_WELCOME:
err = packetWelcomeClient(&packet); err = packetWelcomeClient(&packet);
@ -335,6 +335,21 @@ void * networkedClientReadThread(void *arg) {
if(err) { if(err) {
consolePrint("Failed to read packet %s", errorString()); consolePrint("Failed to read packet %s", errorString());
errorFlush(); errorFlush();
pthread_mutex_unlock(&client->networked.readLock);
break;
}
// Handle disconnect packets here
if(packet.type == PACKET_TYPE_DISCONNECT) {
err = packetDisconnectClientProcess(&packet, client);
if(err) {
consolePrint(errorString());
errorFlush();
} else {
consolePrint("Server disconnected");
}
client->state = CLIENT_STATE_DISCONNECTING;
pthread_mutex_unlock(&client->networked.readLock);
break; break;
} }

View File

@ -11,6 +11,13 @@
#include "client/client.h" #include "client/client.h"
#include "server/server.h" #include "server/server.h"
packethandler_t PACKET_HANDLERS[] = {
{ NULL, NULL },
{ NULL, NULL },
{ packetDisconnectClientProcess, NULL },
{ packetPingClientProcess, packetPingServerProcess },
};
void packetInit( void packetInit(
packet_t *packet, packet_t *packet,
const packettype_t type, const packettype_t type,
@ -42,13 +49,15 @@ errorret_t packetClientProcess(
); );
assertIsMainThread("Client process must be on main thread"); assertIsMainThread("Client process must be on main thread");
switch(packet->type) { if(packet->type >= PACKET_TYPE_COUNT) {
case PACKET_TYPE_PING:
return packetPingClientProcess(packet, client);
default:
return error("Unknown packet type %d", packet->type); return error("Unknown packet type %d", packet->type);
} }
if(PACKET_HANDLERS[packet->type].client == NULL) {
return error("Packet type %d has no client handler", packet->type);
}
return PACKET_HANDLERS[packet->type].client(packet, client);
} }
errorret_t packetServerProcess( errorret_t packetServerProcess(
@ -63,11 +72,13 @@ errorret_t packetServerProcess(
); );
assertIsMainThread("Server client process must be on main thread"); assertIsMainThread("Server client process must be on main thread");
switch(packet->type) { if(packet->type >= PACKET_TYPE_COUNT) {
case PACKET_TYPE_PING:
return packetPingServerProcess(packet, client);
default:
return error("Unknown packet type %d", packet->type); return error("Unknown packet type %d", packet->type);
} }
if(PACKET_HANDLERS[packet->type].server == NULL) {
return error("Packet type %d has no server handler", packet->type);
}
return PACKET_HANDLERS[packet->type].server(packet, client);
} }

View File

@ -18,6 +18,8 @@ typedef enum {
PACKET_TYPE_PING = 3, PACKET_TYPE_PING = 3,
} packettype_t; } packettype_t;
#define PACKET_TYPE_COUNT 4
typedef union { typedef union {
packetwelcome_t welcome; packetwelcome_t welcome;
packetdisconnect_t disconnect; packetdisconnect_t disconnect;
@ -30,6 +32,13 @@ typedef struct packet_s {
packetdata_t data; packetdata_t data;
} packet_t; } 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 * Initializes a packet with the given type. This is only to be used by sub
* initializers, such as packetWelcomeCreate for example. * initializers, such as packetWelcomeCreate for example.

13
src/packet/packetbase.h Normal file
View File

@ -0,0 +1,13 @@
/**
* 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 client_s client_t;
typedef struct serverclient_s serverclient_t;
typedef struct packet_s packet_t;

View File

@ -8,6 +8,7 @@
#include "packet.h" #include "packet.h"
#include "util/memory.h" #include "util/memory.h"
#include "assert/assert.h" #include "assert/assert.h"
#include "client/client.h"
void packetDisconnectCreate( void packetDisconnectCreate(
packet_t *packet, packet_t *packet,
@ -17,8 +18,10 @@ void packetDisconnectCreate(
packet->data.disconnect.reason = reason; packet->data.disconnect.reason = reason;
} }
errorret_t packetDisconnectClient(packet_t *packet) { errorret_t packetDisconnectClientProcess(
assertNotNull(packet, "Packet is NULL"); const packet_t *packet,
client_t *client
) {
assertTrue( assertTrue(
packet->type == PACKET_TYPE_DISCONNECT, packet->type == PACKET_TYPE_DISCONNECT,
"Packet type is not DISCONNECT" "Packet type is not DISCONNECT"
@ -43,4 +46,6 @@ errorret_t packetDisconnectClient(packet_t *packet) {
default: default:
return error("Server disconnected: Unknown reason"); return error("Server disconnected: Unknown reason");
} }
return ERROR_OK;
} }

View File

@ -6,9 +6,7 @@
*/ */
#pragma once #pragma once
#include "error/error.h" #include "packetbase.h"
typedef struct packet_s packet_t;
typedef enum { typedef enum {
PACKET_DISCONNECT_REASON_UNKNOWN = 0, PACKET_DISCONNECT_REASON_UNKNOWN = 0,
@ -34,9 +32,12 @@ void packetDisconnectCreate(
); );
/** /**
* Handles a disconnect packet received FROM a server INTO a client. * Handles disconnect packet client side.
* *
* @param packet Pointer to the packet structure to handle. * @param packet Pointer to the packet structure to handle.
* @param client Pointer to the client structure.
* @return ERROR_OK on success, or an error code on failure. * @return ERROR_OK on success, or an error code on failure.
*/ */
errorret_t packetDisconnectClient(packet_t *packet); errorret_t packetDisconnectClientProcess(
const packet_t *packet, client_t *client
);

View File

@ -19,7 +19,13 @@ errorret_t packetPingClientProcess(
const packet_t *packet, const packet_t *packet,
client_t *client client_t *client
) { ) {
assertTrue(
packet->type == PACKET_TYPE_PING,
"Packet type is not PING"
);
printf("Client got Pong!\n"); printf("Client got Pong!\n");
return ERROR_OK; return ERROR_OK;
} }
@ -27,14 +33,21 @@ errorret_t packetPingServerProcess(
const packet_t *packet, const packet_t *packet,
serverclient_t *client serverclient_t *client
) { ) {
assertTrue(
packet->type == PACKET_TYPE_PING,
"Packet type is not PING"
);
if(packet->length != sizeof(packetping_t)) { if(packet->length != sizeof(packetping_t)) {
return error("Ping packet length is not %d", sizeof(packetping_t)); return error("Ping packet length is not %d", sizeof(packetping_t));
} }
printf("Server got Ping!\n"); printf("Server got Ping!\n");
// Send ping back to the client.
packet_t pong; packet_t pong;
packetPingCreate(&pong); packetPingCreate(&pong);
packetQueuePushOut(&client->packetQueue, &pong); packetQueuePushOut(&client->packetQueue, &pong);
return ERROR_OK; return ERROR_OK;
} }

View File

@ -6,10 +6,7 @@
*/ */
#pragma once #pragma once
#include "error/error.h" #include "packetbase.h"
typedef struct client_s client_t;
typedef struct serverclient_s serverclient_t;
typedef struct { typedef struct {
int32_t number; int32_t number;

View File

@ -6,9 +6,7 @@
*/ */
#pragma once #pragma once
#include "error/error.h" #include "packetbase.h"
typedef struct packet_s packet_t;
#define PACKET_WELCOME_STRING "DUSK" #define PACKET_WELCOME_STRING "DUSK"
#define PACKET_WELCOME_SIZE 4 #define PACKET_WELCOME_SIZE 4