msgba/include/msgba/packet.h

68 lines
1.6 KiB
C

#pragma once
#include <stddef.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include <msgba/client_connection_data.h>
/**
* The possible values for a packet id.
*/
enum {
//! Packet id for get hello.
PACKET_GET_HELLO,
//! Packet id for send frame.
PACKET_SEND_FRAME,
//! Packet id for pressing a key.
PACKET_GET_KEY_DOWN,
//! Packet id for requesting a save to the server.
PACKET_GET_SAVE_REQUEST,
//! Packet id for sending a save to the client.
PACKET_SEND_SAVE_RESPONSE,
//! The number of recognized packets.
PACKETS_NUMBER
};
/**
* Struct representing a generic packet.
*/
struct msPacket {
//! The id of the packet.
size_t id;
//! The size of the data contained in the packet.
size_t size;
//! The data as a byte array. (Not null terminated.)
unsigned char *rawData;
};
/**
* Asks the code to handle a concrete packet comming from a client.
*/
bool
msPacketHandle(struct msPacket *packet, int clientFd, struct msClientConnectionData *const data);
/**
* Creates a new packet object.
*/
struct msPacket *
msPacketNew(const size_t id, const size_t size, unsigned char *rawData);
/**
* Sends the packet to the client.
*/
bool
msPacketSend(const struct msPacket *const packet, struct msClientConnectionData *const data);
/**
* When done with a packet it must be destroyed using this method.
*/
void
msPacketDestroy(struct msPacket **packet);
/**
* Tries to retrieve a packet from the client, returns NULL in case of error.
* Blocks the current thread.
*/
struct msPacket *
msPacketRead(int clientFd);