Added new sLaika_peer member, 'uData'

- pktHandler was updated to pass 'uData'
This commit is contained in:
CPunch 2022-01-24 20:57:54 -06:00
parent ed3efdaf11
commit 0dee6fe3fc
3 changed files with 15 additions and 11 deletions

View File

@ -4,11 +4,10 @@
#include "cnc.h"
size_t laikaC_pktSizeTbl[LAIKAPKT_MAXNONE] = {
[LAIKAPKT_HANDSHAKE_REQ] = LAIKA_MAGICLEN + sizeof(uint8_t) + sizeof(uint8_t),
[LAIKAPKT_HANDSHAKE_RES] = sizeof(uint8_t)
[LAIKAPKT_HANDSHAKE_REQ] = LAIKA_MAGICLEN + sizeof(uint8_t) + sizeof(uint8_t)
};
void laikaC_pktHandler(struct sLaika_peer *peer, LAIKAPKT_ID id) {
void laikaC_pktHandler(struct sLaika_peer *peer, LAIKAPKT_ID id, void *uData) {
printf("got %d packet id!\n", id);
}
@ -24,6 +23,8 @@ struct sLaika_cnc *laikaC_newCNC(uint16_t port) {
/* add sock to pollList */
laikaP_addSock(&cnc->pList, &cnc->sock);
return cnc;
}
void laikaC_freeCNC(struct sLaika_cnc *cnc) {
@ -54,8 +55,9 @@ bool laikaC_pollPeers(struct sLaika_cnc *cnc, int timeout) {
if (evnts[i].sock == &cnc->sock) { /* event on listener? */
peer = laikaS_newPeer(
laikaC_pktHandler,
laikaC_pktSizeTbl,
&cnc->pList,
laikaC_pktSizeTbl
(void*)cnc
);
/* setup and accept new peer */
@ -63,7 +65,7 @@ bool laikaC_pollPeers(struct sLaika_cnc *cnc, int timeout) {
laikaS_setNonBlock(&peer->sock);
/* add to our pollList */
laikaP_addSock(&cnc->pList, (struct sLaika_sock*)peer);
laikaP_addSock(&cnc->pList, &peer->sock);
continue;
}

View File

@ -9,14 +9,15 @@
struct sLaika_peer {
struct sLaika_socket sock; /* DO NOT MOVE THIS. this member HAS TO BE FIRST so that typecasting sLaika_peer* to sLaika_sock* works as intended */
struct sLaika_pollList *pList; /* pollList we're active in */
void (*pktHandler)(struct sLaika_peer *peer, LAIKAPKT_ID id);
void (*pktHandler)(struct sLaika_peer *peer, LAIKAPKT_ID id, void *uData);
void *uData; /* data to be passed to pktHandler */
size_t *pktSizeTable; /* const table to pull pkt size data from */
size_t pktSize; /* current pkt size */
LAIKAPKT_ID pktID; /* current pkt ID */
size_t *pktSizeTable; /* const table to pull pkt size data from */
bool setPollOut; /* is EPOLLOUT/POLLOUT is set on sock's pollfd ? */
};
struct sLaika_peer *laikaS_newPeer(void (*pktHandler)(struct sLaika_peer *peer, LAIKAPKT_ID id), size_t *pktSizeTable, struct sLaika_pollList *pList);
struct sLaika_peer *laikaS_newPeer(void (*pktHandler)(struct sLaika_peer *peer, LAIKAPKT_ID id, void *uData), size_t *pktSizeTable, struct sLaika_pollList *pList, void *uData);
void laikaS_freePeer(struct sLaika_peer *peer);
bool laikaS_handlePeerIn(struct sLaika_peer *peer);

View File

@ -2,13 +2,14 @@
#include "lmem.h"
#include "lpeer.h"
struct sLaika_peer *laikaS_newPeer(void (*pktHandler)(struct sLaika_peer *peer, LAIKAPKT_ID id), size_t *pktSizeTable, struct sLaika_pollList *pList) {
struct sLaika_peer *laikaS_newPeer(void (*pktHandler)(struct sLaika_peer *peer, LAIKAPKT_ID id, void *uData), size_t *pktSizeTable, struct sLaika_pollList *pList, void *uData) {
struct sLaika_peer *peer = laikaM_malloc(sizeof(struct sLaika_peer));
laikaS_initSocket(&peer->sock);
peer->pktHandler = pktHandler;
peer->pList = pList;
peer->pktSizeTable = pktSizeTable;
peer->pList = pList;
peer->uData = uData;
peer->pktSize = 0;
peer->pktID = LAIKAPKT_MAXNONE;
peer->setPollOut = false;
@ -45,7 +46,7 @@ bool laikaS_handlePeerIn(struct sLaika_peer *peer) {
/* have we received the full packet? */
if (peer->pktSize == peer->sock.inCount) {
peer->pktHandler(peer, peer->pktID); /* dispatch to packet handler */
peer->pktHandler(peer, peer->pktID, peer->uData); /* dispatch to packet handler */
/* reset */
peer->sock.inCount = 0;