1
0
mirror of https://github.com/CPunch/Laika.git synced 2025-09-26 03:40:05 +00:00

Lib: Minor refactoring, boilerplate content packet handlers

- content contexts now have events
- minor comments
This commit is contained in:
2022-05-18 12:04:19 -05:00
parent 83002faa62
commit 3e60cc3c0f
5 changed files with 130 additions and 80 deletions

View File

@@ -37,5 +37,7 @@ struct sLaika_socket;
struct sLaika_pollList;
struct sLaika_task;
struct sLaika_taskService;
struct sLaika_content;
struct sLaika_contentContext;
#endif

View File

@@ -8,19 +8,28 @@
#include <inttypes.h>
enum {
CONTENT_FILE,
CONTENT_PAYLOAD
CONTENT_IN = true, /* being recv'd from peer */
CONTENT_OUT = false /* being sent to peer */
};
enum {
CONTENT_TYPE_FILE
};
enum {
CONTENT_ERR_ID_IN_USE,
CONTENT_ERR_INVALID_ID
CONTENT_ERR_INVALID_ID,
CONTENT_ERR_REJECTED
};
typedef uint8_t CONTENT_TYPE;
typedef uint8_t CONTENT_ERRCODE;
typedef uint16_t CONTENT_ID;
typedef void (*contentRecvEvent)(struct sLaika_contentContext *context, struct sLaika_content *content);
typedef bool (*contentNewEvent)(struct sLaika_contentContext *context, struct sLaika_content *content);
typedef void (*contentErrorEvent)(struct sLaika_contentContext *context, struct sLaika_content *content, CONTENT_ERRCODE err);
struct sLaika_content {
struct sLaika_content *next;
FILE *fd;
@@ -28,22 +37,28 @@ struct sLaika_content {
uint32_t processed;
CONTENT_ID id;
CONTENT_TYPE type;
bool direction;
};
struct sLaika_contentContext {
struct sLaika_content *headIn; /* receiving from peer */
struct sLaika_content *headOut; /* sending to peer */
struct sLaika_content *head;
CONTENT_ID nextID;
contentRecvEvent onReceived;
contentNewEvent onNew;
contentErrorEvent onError;
};
void laikaF_initContext(struct sLaika_contentContext *context);
void laikaF_cleanContext(struct sLaika_contentContext *context);
int laikaF_newOutContent(struct sLaika_peer *peer, FILE *fd, CONTENT_TYPE type);
void laikaF_newInContent(struct sLaika_peer *peer, CONTENT_ID id, uint32_t sz, CONTENT_TYPE type);
void laikaF_setupEvents(struct sLaika_contentContext *context, contentRecvEvent onRecv, contentNewEvent onNew, contentErrorEvent onError);
int laikaF_sendContent(struct sLaika_peer *peer, FILE *fd, CONTENT_TYPE type);
void laikaF_pollContent(struct sLaika_peer *peer);
void laikaF_handleContentNew(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData);
void laikaF_handleContentError(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData);
void laikaF_handleContentChunk(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData);
#endif