1
0
mirror of https://github.com/CPunch/Laika.git synced 2026-01-07 12:50:04 +00:00

Lib: minor refactoring, more lcontent.c boilerplate

This commit is contained in:
2022-05-17 10:37:58 -05:00
parent b02f48c006
commit 83002faa62
7 changed files with 108 additions and 31 deletions

View File

@@ -1,3 +1,4 @@
#include "laika.h"
#include "lcontent.h"
#include "lmem.h"
#include "lerror.h"
@@ -5,6 +6,8 @@
#include "lsocket.h"
#include "lpeer.h"
#define CONTENTCHUNK_MAX_BODY (LAIKA_MAX_PKTSIZE-sizeof(CONTENT_ID))
size_t getSize(FILE *fd) {
size_t sz;
fseek(fd, 0L, SEEK_END);
@@ -13,17 +16,17 @@ size_t getSize(FILE *fd) {
return sz;
}
bool isValidID(struct sLaika_contentContext *context, uint16_t id) {
struct sLaika_content* getContentByID(struct sLaika_contentContext *context, CONTENT_ID id) {
struct sLaika_content *curr = context->headIn;
while (curr) {
if (curr->id == id)
return true;
return curr;
curr = curr->next;
}
return false;
return NULL;
}
void freeContent(struct sLaika_content *content) {
@@ -51,15 +54,34 @@ void rmvFromOut(struct sLaika_contentContext *context, struct sLaika_content *co
}
}
void sendContentError(struct sLaika_peer *peer, uint16_t id, CONTENT_ERRCODE err) {
void rmvFromIn(struct sLaika_contentContext *context, struct sLaika_content *content) {
struct sLaika_content *last = NULL, *curr = context->headIn;
while (curr) {
/* if found, remove it! */
if (curr == content) {
if (last)
last->next = curr->next;
else
context->headIn = curr->next;
freeContent(curr);
break;
}
last = curr;
curr = curr->next;
}
}
void sendContentError(struct sLaika_peer *peer, CONTENT_ID id, CONTENT_ERRCODE err) {
laikaS_startOutPacket(peer, LAIKAPKT_CONTENT_ERROR);
laikaS_writeInt(&peer->sock, &id, sizeof(uint16_t));
laikaS_writeInt(&peer->sock, &id, sizeof(CONTENT_ID));
laikaS_writeByte(&peer->sock, err);
laikaS_endOutPacket(peer);
}
void laikaF_initContext(struct sLaika_contentContext *context, struct sLaika_peer *peer) {
context->peer = peer;
void laikaF_initContext(struct sLaika_contentContext *context) {
context->headIn = NULL;
context->headOut = NULL;
context->nextID = 0;
@@ -85,9 +107,9 @@ void laikaF_cleanContext(struct sLaika_contentContext *context) {
}
}
int laikaF_newOutContent(struct sLaika_contentContext *context, FILE *fd, CONTENT_TYPE type) {
int laikaF_newOutContent(struct sLaika_peer *peer, FILE *fd, CONTENT_TYPE type) {
struct sLaika_content *content = (struct sLaika_content*)laikaM_malloc(sizeof(struct sLaika_content));
struct sLaika_peer *peer = context->peer;
struct sLaika_contentContext *context = &peer->context;
/* init content struct */
content->fd = fd;
@@ -102,17 +124,19 @@ int laikaF_newOutContent(struct sLaika_contentContext *context, FILE *fd, CONTEN
/* let the peer know we're sending them some content */
laikaS_startOutPacket(peer, LAIKAPKT_CONTENT_NEW);
laikaS_writeInt(&peer->sock, &content->id, sizeof(uint16_t));
laikaS_writeInt(&peer->sock, &content->id, sizeof(CONTENT_ID));
laikaS_writeInt(&peer->sock, &content->sz, sizeof(uint32_t));
laikaS_writeByte(&peer->sock, type);
laikaS_endOutPacket(peer);
}
void laikaF_newInContent(struct sLaika_contentContext *context, uint16_t id, uint32_t sz, CONTENT_TYPE type) {
/* new content we're recieving from a peer */
void laikaF_newInContent(struct sLaika_peer *peer, CONTENT_ID id, uint32_t sz, CONTENT_TYPE type) {
struct sLaika_content *content = (struct sLaika_content*)laikaM_malloc(sizeof(struct sLaika_content));
struct sLaika_contentContext *context = &peer->context;
if (isValidID(context, id)) {
sendContentError(context->peer, id, CONTENT_ERR_ID_IN_USE);
if (getContentByID(context, id)) {
sendContentError(peer, id, CONTENT_ERR_ID_IN_USE);
LAIKA_ERROR("ID [%d] is in use!\n", id);
}
@@ -127,27 +151,56 @@ void laikaF_newInContent(struct sLaika_contentContext *context, uint16_t id, uin
context->headIn = content;
}
void laikaF_poll(struct sLaika_contentContext *context) {
uint8_t buff[LAIKA_MAX_PKTSIZE-sizeof(uint16_t)];
struct sLaika_peer *peer = context->peer;
void laikaF_pollContent(struct sLaika_peer *peer) {
uint8_t buff[CONTENTCHUNK_MAX_BODY];
struct sLaika_contentContext *context = &peer->context;
struct sLaika_content *tmp, *curr = context->headOut;
int rd;
/* traverse our out content, sending each chunk */
while (curr) {
/* if we've reached the end of the file stream, remove it! */
if (rd = fread(buff, sizeof(uint8_t), (curr->sz - curr->processed > (LAIKA_MAX_PKTSIZE-sizeof(uint16_t)) ? LAIKA_MAX_PKTSIZE-sizeof(uint16_t) : curr->sz - curr->processed), curr->fd) == 0) {
if (rd = fread(buff, sizeof(uint8_t), MIN(curr->sz - curr->processed, CONTENTCHUNK_MAX_BODY), curr->fd) == 0) {
tmp = curr->next;
rmvFromOut(context, curr);
curr = tmp;
continue;
}
/* send chunk */
laikaS_startVarPacket(peer, LAIKAPKT_CONTENT_CHUNK);
laikaS_writeInt(&peer->sock, &curr->id, sizeof(uint16_t));
laikaS_writeInt(&peer->sock, &curr->id, sizeof(CONTENT_ID));
laikaS_write(&peer->sock, buff, rd);
laikaS_endVarPacket(peer);
curr->processed += rd;
curr = curr->next;
}
}
/* ============================================[[ Packet Handlers ]]============================================= */
void laikaF_handleContentChunk(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) {
uint8_t buff[CONTENTCHUNK_MAX_BODY];
struct sLaika_contentContext *context = &peer->context;
struct sLaika_content *content;
CONTENT_ID id;
size_t bodySz = sz-sizeof(CONTENT_ID);
if (sz <= sizeof(CONTENT_ID))
LAIKA_ERROR("malformed chunk packet!\n");
/* read and sanity check id */
laikaS_readInt(&peer->sock, &id, sizeof(CONTENT_ID));
if ((content = getContentByID(context, id)) == NULL)
LAIKA_ERROR("chunk recieved with invalid id! [%d]\n", id);
/* read data & write to file */
laikaS_read(&peer->sock, buff, bodySz);
if (fwrite(buff, sizeof(uint8_t), bodySz, content->fd) != bodySz) {
rmvFromIn(context, content);
} else {
/* TODO: if content->processed == content->sz then handle full file received event */
content->processed += bodySz;
}
}

View File

@@ -27,10 +27,14 @@ struct sLaika_peer *laikaS_newPeer(struct sLaika_peerPacketInfo *pktTbl, struct
memset(peer->hostname, 0, LAIKA_HOSTNAME_LEN);
memset(peer->inet, 0, LAIKA_INET_LEN);
memset(peer->ipv4, 0, LAIKA_IPV4_LEN);
/* init content context */
laikaF_initContext(&peer->context);
return peer;
}
void laikaS_freePeer(struct sLaika_peer *peer) {
laikaF_cleanContext(&peer->context);
laikaS_cleanSocket(&peer->sock);
laikaM_free(peer);
}