dusk/src/packet/packet.c

73 lines
1.6 KiB
C

/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "packet.h"
#include "assert/assert.h"
#include "util/memory.h"
#include "client/client.h"
#include "server/server.h"
void packetInit(
packet_t *packet,
const packettype_t type,
const uint32_t length
) {
assertNotNull(packet, "Packet is NULL");
memoryZero(packet, sizeof(packet_t));
assertTrue(length > 0, "Packet length is 0");
assertTrue(
length <= sizeof(packetdata_t),
"Packet length is too large"
);
packet->type = type;
packet->length = length;
}
errorret_t packetClientProcess(
const packet_t *packet,
client_t *client
) {
assertNotNull(packet, "Packet is NULL");
assertNotNull(client, "Client is NULL");
assertTrue(
client->type == CLIENT_TYPE_NETWORKED,
"Client is not networked"
);
assertIsMainThread("Client process must be on main thread");
switch(packet->type) {
case PACKET_TYPE_PING:
return packetPingClientProcess(packet, client);
default:
return error("Unknown packet type %d", packet->type);
}
}
errorret_t packetServerProcess(
const packet_t *packet,
serverclient_t *client
) {
assertNotNull(packet, "Packet is NULL");
assertNotNull(client, "Client is NULL");
assertTrue(
client->server->type == SERVER_TYPE_NETWORKED,
"Server is not networked"
);
assertIsMainThread("Server client process must be on main thread");
switch(packet->type) {
case PACKET_TYPE_PING:
return packetPingServerProcess(packet, client);
default:
return error("Unknown packet type %d", packet->type);
}
}