msgba/include/msgba/packet.h

59 lines
1.3 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_GET_HELLO, //! Packet id for get hello.
PACKET_SEND_FRAME, //! Packet id for send frame.
PACKETS_NUMBER //! The number of recognized packets.
};
/**
* 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 *raw_data;
};
/**
* Asks the code to handle a concrete packet comming from a client.
*/
bool
msPacketHandle(struct msPacket *packet, int client_fd, struct msClientConnectionData *const data);
/**
* Creates a new packet object.
*/
struct msPacket *
msPacketNew(const size_t id, const size_t size, unsigned char *raw_data);
/**
* 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 client_fd);