msgba/src/packet.c

90 lines
2.3 KiB
C

#include <endian.h>
#include <msgba/packet.h>
#include <msgba/packet/hello.h>
#define PRINT_DEBUG(NAME, CLIENT_FD) \
printf ("Received packet %s from client fd %d\n", #NAME, CLIENT_FD);
bool
msPacketHandle(struct msPacket *packet, int client_fd,
struct msClientConnectionData *const data) {
bool result = false;
switch (packet->id) {
case PACKET_GET_HELLO:
PRINT_DEBUG(PACKET_HELLO, client_fd);
result = msPacketHelloGet(packet, client_fd, data);
break;
}
return result;
}
void
msPacketSend(const struct msPacket *const packet, int fd) {
write(fd, (const void *)packet->id, sizeof packet->id);
write(fd, (const void *)packet->size, sizeof packet->size);
write(fd, (const void *)packet->raw_data, sizeof *packet->raw_data * packet->size);
}
struct msPacket *
msPacketNew(const size_t id, const size_t size, char *raw_data) {
struct msPacket *packet = malloc(sizeof *packet);
packet->id = id;
packet->size = size;
packet->raw_data = raw_data;
return packet;
}
void
msPacketDestroy(struct msPacket **packet) {
if ((*packet)->raw_data) {
free((*packet)->raw_data);
}
free(*packet);
*packet=NULL;
}
struct msPacket *
msPacketRead(int client_fd) {
struct msPacket *packet = NULL;
size_t id = 0;
size_t size = 0;
ssize_t result;
char *raw_data = NULL;
result = read(client_fd, &id, sizeof id);
if (result < sizeof id) {
printf("Unable to read id\n");
goto return_read_packet;
}
result = read(client_fd, &size, sizeof size);
if (result < sizeof size) {
printf("Unable to read packet\n");
goto return_read_packet;
}
size = be64toh(size);
raw_data = malloc(size);
size_t to_read_size = size;
while (to_read_size > 0) {
result = read(client_fd, &raw_data[size - to_read_size], to_read_size);
if (result == -1) {
printf("Unable to read raw_data\n");
goto return_read_packet;
}
to_read_size -= result;
}
if (result < size) {
}
packet = calloc (1, sizeof *packet);
packet->id = id;
packet->size = size;
packet->raw_data = raw_data;
return_read_packet:
if (!packet && raw_data) {
free(raw_data);
}
return packet;
}