Basic packet system done.
This commit is contained in:
@ -8,6 +8,8 @@
|
||||
#include "packet.h"
|
||||
#include "assert/assert.h"
|
||||
#include "util/memory.h"
|
||||
#include "client/client.h"
|
||||
#include "server/server.h"
|
||||
|
||||
void packetInit(
|
||||
packet_t *packet,
|
||||
@ -26,4 +28,46 @@ void packetInit(
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
@ -42,4 +42,30 @@ 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
|
||||
);
|
@ -8,19 +8,33 @@
|
||||
#include "packet.h"
|
||||
#include "util/memory.h"
|
||||
#include "assert/assert.h"
|
||||
#include "server/server.h"
|
||||
|
||||
void packetPingCreate(packet_t *packet) {
|
||||
packetInit(packet, PACKET_TYPE_PING, sizeof(packetping_t));
|
||||
packet->data.ping.number = GetRandomValue(0, INT32_MAX);
|
||||
}
|
||||
|
||||
errorret_t packetPingClient(packet_t *packet) {
|
||||
assertNotNull(packet, "Packet is NULL");
|
||||
assertTrue(packet->type == PACKET_TYPE_PING, "Packet type is not PING");
|
||||
errorret_t packetPingClientProcess(
|
||||
const packet_t *packet,
|
||||
client_t *client
|
||||
) {
|
||||
printf("Client got Pong!\n");
|
||||
return ERROR_OK;
|
||||
}
|
||||
|
||||
errorret_t packetPingServerProcess(
|
||||
const packet_t *packet,
|
||||
serverclient_t *client
|
||||
) {
|
||||
if(packet->length != sizeof(packetping_t)) {
|
||||
return error("Ping packet length is not %d", sizeof(packetping_t));
|
||||
}
|
||||
|
||||
printf("Server got Ping!\n");
|
||||
|
||||
packet_t pong;
|
||||
packetPingCreate(&pong);
|
||||
packetQueuePushOut(&client->packetQueue, &pong);
|
||||
return ERROR_OK;
|
||||
}
|
@ -8,6 +8,9 @@
|
||||
#pragma once
|
||||
#include "error/error.h"
|
||||
|
||||
typedef struct client_s client_t;
|
||||
typedef struct serverclient_s serverclient_t;
|
||||
|
||||
typedef struct {
|
||||
int32_t number;
|
||||
} packetping_t;
|
||||
@ -20,9 +23,25 @@ typedef struct {
|
||||
void packetPingCreate(packet_t *packet);
|
||||
|
||||
/**
|
||||
* Handles a ping packet received FROM a server INTO a client.
|
||||
* Validates a ping packet received FROM a client INTO a server.
|
||||
*
|
||||
* @param packet Pointer to the packet structure to handle.
|
||||
* @param packet Pointer to the packet structure to validate.
|
||||
* @param client Pointer to the server client structure.
|
||||
* @return ERROR_OK on success, or an error code on failure.
|
||||
*/
|
||||
errorret_t packetPingClient(packet_t *packet);
|
||||
errorret_t packetPingClientProcess(
|
||||
const packet_t *packet,
|
||||
client_t *client
|
||||
);
|
||||
|
||||
/**
|
||||
* Handles a ping packet received FROM a client INTO a server.
|
||||
*
|
||||
* @param packet Pointer to the packet structure to handle.
|
||||
* @param client Pointer to the server client structure.
|
||||
* @return ERROR_OK on success, or an error code on failure.
|
||||
*/
|
||||
errorret_t packetPingServerProcess(
|
||||
const packet_t *packet,
|
||||
serverclient_t *client
|
||||
);
|
@ -12,52 +12,75 @@
|
||||
void packetQueueInit(packetqueue_t *queue) {
|
||||
assertNotNull(queue, "Packet queue is NULL");
|
||||
memoryZero(queue, sizeof(packetqueue_t));
|
||||
pthread_mutex_init(&queue->lock, NULL);
|
||||
}
|
||||
|
||||
void packetQueuePushIn(packetqueue_t *queue, const packet_t *packet) {
|
||||
assertNotNull(queue, "Packet queue is NULL");
|
||||
assertNotNull(packet, "Packet is NULL");
|
||||
pthread_mutex_lock(&queue->lock);
|
||||
assertTrue(
|
||||
queue->packetsInCount < PACKET_QUEUE_MAX_SIZE, "Inbound packet queue is full"
|
||||
);
|
||||
|
||||
queue->packetsIn[queue->packetsInCount++] = *packet;
|
||||
pthread_mutex_unlock(&queue->lock);
|
||||
}
|
||||
|
||||
void packetQueuePushOut(packetqueue_t *queue, const packet_t *packet) {
|
||||
assertNotNull(queue, "Packet queue is NULL");
|
||||
assertNotNull(packet, "Packet is NULL");
|
||||
pthread_mutex_lock(&queue->lock);
|
||||
assertTrue(
|
||||
queue->packetsOutCount < PACKET_QUEUE_MAX_SIZE, "Outbound packet queue is full"
|
||||
);
|
||||
|
||||
queue->packetsOut[queue->packetsOutCount++] = *packet;
|
||||
pthread_mutex_unlock(&queue->lock);
|
||||
}
|
||||
|
||||
int packetQueuePopIn(packetqueue_t *queue, packet_t *packet) {
|
||||
int32_t packetQueuePopIn(packetqueue_t *queue, packet_t *packet) {
|
||||
assertNotNull(queue, "Packet queue is NULL");
|
||||
assertNotNull(packet, "Packet is NULL");
|
||||
|
||||
if(queue->packetsInCount == 0) return 0;
|
||||
pthread_mutex_lock(&queue->lock);
|
||||
if(queue->packetsInCount == 0) {
|
||||
pthread_mutex_unlock(&queue->lock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
*packet = queue->packetsIn[0];
|
||||
for(uint32_t i = 1; i < queue->packetsInCount; i++) {
|
||||
queue->packetsIn[i - 1] = queue->packetsIn[i];
|
||||
if(queue->packetsInCount > 1) {
|
||||
memoryCopy(
|
||||
&queue->packetsIn[0],
|
||||
&queue->packetsIn[1],
|
||||
(queue->packetsInCount - 1) * sizeof(packet_t)
|
||||
);
|
||||
}
|
||||
queue->packetsInCount--;
|
||||
pthread_mutex_unlock(&queue->lock);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int packetQueuePopOut(packetqueue_t *queue, packet_t *packet) {
|
||||
int32_t packetQueuePopOut(packetqueue_t *queue, packet_t *packet) {
|
||||
assertNotNull(queue, "Packet queue is NULL");
|
||||
assertNotNull(packet, "Packet is NULL");
|
||||
|
||||
if(queue->packetsOutCount == 0) return 0;
|
||||
pthread_mutex_lock(&queue->lock);
|
||||
if(queue->packetsOutCount == 0) {
|
||||
pthread_mutex_unlock(&queue->lock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
*packet = queue->packetsOut[0];
|
||||
for(uint32_t i = 1; i < queue->packetsOutCount; i++) {
|
||||
queue->packetsOut[i - 1] = queue->packetsOut[i];
|
||||
if(queue->packetsOutCount > 1) {
|
||||
memoryCopy(
|
||||
&queue->packetsOut[0],
|
||||
&queue->packetsOut[1],
|
||||
(queue->packetsOutCount - 1) * sizeof(packet_t)
|
||||
);
|
||||
}
|
||||
queue->packetsOutCount--;
|
||||
pthread_mutex_unlock(&queue->lock);
|
||||
return 1;
|
||||
}
|
@ -7,6 +7,7 @@
|
||||
|
||||
#pragma once
|
||||
#include "packet/packet.h"
|
||||
#include <pthread.h>
|
||||
|
||||
#define PACKET_QUEUE_MAX_SIZE 512
|
||||
|
||||
@ -15,6 +16,7 @@ typedef struct {
|
||||
uint32_t packetsInCount;
|
||||
packet_t packetsOut[PACKET_QUEUE_MAX_SIZE];
|
||||
uint32_t packetsOutCount;
|
||||
pthread_mutex_t lock;
|
||||
} packetqueue_t;
|
||||
|
||||
/**
|
||||
@ -47,7 +49,7 @@ void packetQueuePushOut(packetqueue_t *queue, const packet_t *packet);
|
||||
* @param packet Pointer to the packet to store the popped packet.
|
||||
* @return 1 if a packet was popped, 0 otherwise.
|
||||
*/
|
||||
int packetQueuePopIn(packetqueue_t *queue, packet_t *packet);
|
||||
int32_t packetQueuePopIn(packetqueue_t *queue, packet_t *packet);
|
||||
|
||||
/**
|
||||
* Pops a packet from the outbound packet queue.
|
||||
@ -56,4 +58,4 @@ int packetQueuePopIn(packetqueue_t *queue, packet_t *packet);
|
||||
* @param packet Pointer to the packet to store the popped packet.
|
||||
* @return 1 if a packet was popped, 0 otherwise.
|
||||
*/
|
||||
int packetQueuePopOut(packetqueue_t *queue, packet_t *packet);
|
||||
int32_t packetQueuePopOut(packetqueue_t *queue, packet_t *packet);
|
Reference in New Issue
Block a user