From 84d1957066a4bf364615efa11b93fdf6029b01fc Mon Sep 17 00:00:00 2001 From: CreeperMario Date: Sat, 19 Aug 2017 21:23:20 +0930 Subject: [PATCH 01/19] udplogserver: Do not automatically add a new line to log outputs. --- tools/udplogserver/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/udplogserver/main.cpp b/tools/udplogserver/main.cpp index 60bc9ba..0368950 100644 --- a/tools/udplogserver/main.cpp +++ b/tools/udplogserver/main.cpp @@ -90,7 +90,7 @@ int main(int argc, char **argv) if (recvd > 0) { buffer[recvd] = 0; - std::cout << buffer << std::endl; + std::cout << buffer; } } } From 45f3cd71ab8f6ced4b38c420cd8cc0586b7c39ab Mon Sep 17 00:00:00 2001 From: CreeperMario Date: Sun, 20 Aug 2017 16:57:43 +0930 Subject: [PATCH 02/19] whb: Modification of logging functions * WHBLogPrint and WHBLogPrint have been renamed to WHBLogWrite and WHBLogWrite respectively. * WHBLogPrint and WHBLogPrintf have been re-implemented to automatically add a new line to the end of the string, and then send the string to WHBLogWrite. --- src/libwhb/include/whb/log.h | 6 ++++++ src/libwhb/src/log.c | 40 +++++++++++++++++++++++++++++++++++- src/libwhb/src/log_cafe.c | 11 +++++++++- 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/libwhb/include/whb/log.h b/src/libwhb/include/whb/log.h index 10871b0..df3c6c2 100644 --- a/src/libwhb/include/whb/log.h +++ b/src/libwhb/include/whb/log.h @@ -16,9 +16,15 @@ typedef void (*LogHandlerFn)(const char *msg); BOOL WHBAddLogHandler(LogHandlerFn fn); +BOOL +WHBLogWrite(const char *str); + BOOL WHBLogPrint(const char *str); +BOOL +WHBLogWritef(const char *fmt, ...); + BOOL WHBLogPrintf(const char *fmt, ...); diff --git a/src/libwhb/src/log.c b/src/libwhb/src/log.c index 6b52c25..2ee8020 100644 --- a/src/libwhb/src/log.c +++ b/src/libwhb/src/log.c @@ -26,7 +26,7 @@ WHBAddLogHandler(LogHandlerFn fn) } BOOL -WHBLogPrint(const char *str) +WHBLogWrite(const char *str) { int i; @@ -39,6 +39,44 @@ WHBLogPrint(const char *str) return TRUE; } +BOOL +WHBLogPrint(const char *str) +{ + char *buf = MEMAllocFromDefaultHeapEx(PRINTF_BUFFER_LENGTH, 4); + BOOL result; + + if(!buf) { + return FALSE; + } + + snprintf(buf, PRINTF_BUFFER_LENGTH, "%s\n", str); + result = WHBLogWrite(buf); + + MEMFreeToDefaultHeap(buf); + return result; +} + +BOOL +WHBLogWritef(const char *fmt, ...) +{ + char *buf = MEMAllocFromDefaultHeapEx(PRINTF_BUFFER_LENGTH, 4); + va_list va; + BOOL result; + + if (!buf) { + return FALSE; + } + + va_start(va, fmt); + + vsnprintf(buf, PRINTF_BUFFER_LENGTH, fmt, va); + result = WHBLogWrite(buf); + + MEMFreeToDefaultHeap(buf); + va_end(va); + return result; +} + BOOL WHBLogPrintf(const char *fmt, ...) { diff --git a/src/libwhb/src/log_cafe.c b/src/libwhb/src/log_cafe.c index a1367d3..e3bea7a 100644 --- a/src/libwhb/src/log_cafe.c +++ b/src/libwhb/src/log_cafe.c @@ -1,10 +1,19 @@ #include #include +#include + static void cafeLogHandler(const char * msg) { - OSReport("%s\n", msg); + int length = strlen(msg); + + if(msg[length-1] != '\n') { + OSReport("%s\n", msg); + } + else { + OSReport(msg); + } } BOOL From ac4ce95f8477fc5cc55bcb50d57a2d80ccebeca6 Mon Sep 17 00:00:00 2001 From: CreeperMario Date: Sun, 20 Aug 2017 17:51:45 +0930 Subject: [PATCH 03/19] udplogserver: Flush output buffer when sent a message * Because we are no longer adding a new line, which automatically flushes the buffer. --- tools/udplogserver/main.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/udplogserver/main.cpp b/tools/udplogserver/main.cpp index 0368950..fdec456 100644 --- a/tools/udplogserver/main.cpp +++ b/tools/udplogserver/main.cpp @@ -91,6 +91,7 @@ int main(int argc, char **argv) if (recvd > 0) { buffer[recvd] = 0; std::cout << buffer; + std::cout.flush(); } } } From 20c7dbf2fbd9eb57694c19be99a744b87fb0bcca Mon Sep 17 00:00:00 2001 From: CreeperMario Date: Sun, 20 Aug 2017 18:17:07 +0930 Subject: [PATCH 04/19] whb: Add ability to remove logging functions * WHBRemoveLogHandler will look through the "registry" of logging functions, and will set the entry to NULL if it matches what the user passes. * WHBLogUdpDeinit will correctly close the socket and shut down the socket library in addition to removing its entry in the registry. --- samples/gx2/triangle/src/main.c | 1 + src/libwhb/include/whb/log.h | 3 +++ src/libwhb/include/whb/log_cafe.h | 3 +++ src/libwhb/include/whb/log_udp.h | 3 +++ src/libwhb/src/log.c | 15 +++++++++++++++ src/libwhb/src/log_cafe.c | 9 +++++++-- src/libwhb/src/log_udp.c | 14 ++++++++++++-- 7 files changed, 44 insertions(+), 4 deletions(-) diff --git a/samples/gx2/triangle/src/main.c b/samples/gx2/triangle/src/main.c index 02deaee..e0bd431 100644 --- a/samples/gx2/triangle/src/main.c +++ b/samples/gx2/triangle/src/main.c @@ -116,5 +116,6 @@ exit: WHBUnmountSdCard(); WHBGfxShutdown(); WHBProcShutdown(); + WHBLogUdpDeinit(); return result; } diff --git a/src/libwhb/include/whb/log.h b/src/libwhb/include/whb/log.h index df3c6c2..f2c1964 100644 --- a/src/libwhb/include/whb/log.h +++ b/src/libwhb/include/whb/log.h @@ -16,6 +16,9 @@ typedef void (*LogHandlerFn)(const char *msg); BOOL WHBAddLogHandler(LogHandlerFn fn); +BOOL +WHBRemoveLogHandler(LogHandlerFn fn); + BOOL WHBLogWrite(const char *str); diff --git a/src/libwhb/include/whb/log_cafe.h b/src/libwhb/include/whb/log_cafe.h index bc40515..023b5fe 100644 --- a/src/libwhb/include/whb/log_cafe.h +++ b/src/libwhb/include/whb/log_cafe.h @@ -14,6 +14,9 @@ extern "C" { BOOL WHBLogCafeInit(); +BOOL +WHBLogCafeDeinit(); + #ifdef __cplusplus } #endif diff --git a/src/libwhb/include/whb/log_udp.h b/src/libwhb/include/whb/log_udp.h index 28ba5d7..00900c6 100644 --- a/src/libwhb/include/whb/log_udp.h +++ b/src/libwhb/include/whb/log_udp.h @@ -14,6 +14,9 @@ extern "C" { BOOL WHBLogUdpInit(); +BOOL +WHBLogUdpDeinit(); + #ifdef __cplusplus } #endif diff --git a/src/libwhb/src/log.c b/src/libwhb/src/log.c index 2ee8020..9972bc9 100644 --- a/src/libwhb/src/log.c +++ b/src/libwhb/src/log.c @@ -25,6 +25,21 @@ WHBAddLogHandler(LogHandlerFn fn) return FALSE; } +BOOL +WHBRemoveLogHandler(LogHandlerFn fn) +{ + int i; + + for(i = 0; i < MAX_HANDLERS; ++i) { + if(sHandlers[i] == fn) { + sHandlers[i] = NULL; + return TRUE; + } + } + + return FALSE; +} + BOOL WHBLogWrite(const char *str) { diff --git a/src/libwhb/src/log_cafe.c b/src/libwhb/src/log_cafe.c index e3bea7a..34d067e 100644 --- a/src/libwhb/src/log_cafe.c +++ b/src/libwhb/src/log_cafe.c @@ -19,6 +19,11 @@ cafeLogHandler(const char * msg) BOOL WHBLogCafeInit() { - WHBAddLogHandler(cafeLogHandler); - return TRUE; + return WHBAddLogHandler(cafeLogHandler); +} + +BOOL +WHBLogCafeDeinit() +{ + return WHBRemoveLogHandler(cafeLogHandler); } diff --git a/src/libwhb/src/log_udp.c b/src/libwhb/src/log_udp.c index 713d4cc..ba405b1 100644 --- a/src/libwhb/src/log_udp.c +++ b/src/libwhb/src/log_udp.c @@ -42,6 +42,16 @@ WHBLogUdpInit() sSendAddr.sin_port = htons(SERVER_PORT); sSendAddr.sin_addr.s_addr = htonl(INADDR_BROADCAST); - WHBAddLogHandler(udpLogHandler); - return TRUE; + return WHBAddLogHandler(udpLogHandler); +} + +BOOL +WHBLogUdpDeinit() +{ + if(shutdown(sSocket, SHUT_WR) != 0) { + return FALSE; + } + + socket_lib_finish(); + return WHBRemoveLogHandler(udpLogHandler); } From a2247e7b1e567a3680221166cf9af4cd427c38ea Mon Sep 17 00:00:00 2001 From: CreeperMario Date: Sun, 20 Aug 2017 19:16:27 +0930 Subject: [PATCH 05/19] whb: Add simplistic socket library manager. * This manager counts how many times the program has requested the socket library be initialized, so that the socket library is not mistakenly closed while other things are still using sockets. * WHBInitializeSocketLibrary will call socket_lib_init() and also keep a count on how many times it has been called. * WHBDeinitializeSocketLibrary will call socket_lib_finish(), but only if it has been called as many times as WHBInitializeSocketLibrary. --- src/libwhb/include/whb/socketinit.h | 23 +++++++++++++++++++++++ src/libwhb/src/log_udp.c | 5 +++-- src/libwhb/src/socketinit.c | 29 +++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 src/libwhb/include/whb/socketinit.h create mode 100644 src/libwhb/src/socketinit.c diff --git a/src/libwhb/include/whb/socketinit.h b/src/libwhb/include/whb/socketinit.h new file mode 100644 index 0000000..f78bd1b --- /dev/null +++ b/src/libwhb/include/whb/socketinit.h @@ -0,0 +1,23 @@ +#pragma once + +/** + * \defgroup whb_socketinit Socket Library Initialization Manager + * \ingroup whb + * @{ + */ + +#ifdef __cplusplus +extern "C" { +#endif + +void +WHBInitializeSocketLibrary(); + +void +WHBDeinitializeSocketLibrary(); + +#ifdef __cplusplus +} +#endif + +/** @} */ diff --git a/src/libwhb/src/log_udp.c b/src/libwhb/src/log_udp.c index ba405b1..f6ebd11 100644 --- a/src/libwhb/src/log_udp.c +++ b/src/libwhb/src/log_udp.c @@ -4,6 +4,7 @@ #include #include #include +#include static int sSocket = -1; @@ -28,7 +29,7 @@ BOOL WHBLogUdpInit() { int broadcastEnable = 1; - socket_lib_init(); + WHBInitializeSocketLibrary(); sSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (sSocket < 0) { @@ -52,6 +53,6 @@ WHBLogUdpDeinit() return FALSE; } - socket_lib_finish(); + WHBDeinitializeSocketLibrary(); return WHBRemoveLogHandler(udpLogHandler); } diff --git a/src/libwhb/src/socketinit.c b/src/libwhb/src/socketinit.c new file mode 100644 index 0000000..1fbd3da --- /dev/null +++ b/src/libwhb/src/socketinit.c @@ -0,0 +1,29 @@ +#include + +static BOOL +isSocketInitialized = FALSE; + +static unsigned int +numberOfSocketClients = 0; + +void +WHBInitializeSocketLibrary() +{ + if(!isSocketInitialized) { + isSocketInitialized = TRUE; + socket_lib_init(); + } + numberOfSocketClients++; +} + +void +WHBDeinitializeSocketLibrary() +{ + if(numberOfSocketClients > 0) { + numberOfSocketClients--; + } + if(isSocketInitialized && numberOfSocketClients == 0) { + isSocketInitialized = FALSE; + socket_lib_finish(); + } +} From 75bb7b5d1033fdc0c14c1ac74030be96f254a333 Mon Sep 17 00:00:00 2001 From: CreeperMario Date: Sun, 20 Aug 2017 20:36:48 +0930 Subject: [PATCH 06/19] whb: Rename "socketinit" to "libmanager" --- src/libwhb/include/whb/{socketinit.h => libmanager.h} | 4 ++++ src/libwhb/src/{socketinit.c => libmanager.c} | 4 ++++ src/libwhb/src/log_udp.c | 2 +- 3 files changed, 9 insertions(+), 1 deletion(-) rename src/libwhb/include/whb/{socketinit.h => libmanager.h} (86%) rename src/libwhb/src/{socketinit.c => libmanager.c} (92%) diff --git a/src/libwhb/include/whb/socketinit.h b/src/libwhb/include/whb/libmanager.h similarity index 86% rename from src/libwhb/include/whb/socketinit.h rename to src/libwhb/include/whb/libmanager.h index f78bd1b..d088997 100644 --- a/src/libwhb/include/whb/socketinit.h +++ b/src/libwhb/include/whb/libmanager.h @@ -10,6 +10,10 @@ extern "C" { #endif +/** + * Socket Library (nsysnet.rpl) + */ + void WHBInitializeSocketLibrary(); diff --git a/src/libwhb/src/socketinit.c b/src/libwhb/src/libmanager.c similarity index 92% rename from src/libwhb/src/socketinit.c rename to src/libwhb/src/libmanager.c index 1fbd3da..3701b00 100644 --- a/src/libwhb/src/socketinit.c +++ b/src/libwhb/src/libmanager.c @@ -1,5 +1,9 @@ #include +/** + * Socket Library (nsysnet.rpl) + */ + static BOOL isSocketInitialized = FALSE; diff --git a/src/libwhb/src/log_udp.c b/src/libwhb/src/log_udp.c index f6ebd11..2e5cd5e 100644 --- a/src/libwhb/src/log_udp.c +++ b/src/libwhb/src/log_udp.c @@ -4,7 +4,7 @@ #include #include #include -#include +#include static int sSocket = -1; From 5829abb4ef33513c384c69bc1f8da578eaee4d67 Mon Sep 17 00:00:00 2001 From: CreeperMario Date: Sun, 27 Aug 2017 17:06:02 +0930 Subject: [PATCH 07/19] crt: Suppress warnings for pointer logic --- src/crt/fs_dev.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/crt/fs_dev.c b/src/crt/fs_dev.c index dab72a2..401c4af 100644 --- a/src/crt/fs_dev.c +++ b/src/crt/fs_dev.c @@ -16,12 +16,12 @@ static int fs_translate_error(FSStatus error); static int fs_open(struct _reent *r, void *fileStruct, const char *path, int flags, int mode); -static int fs_close(struct _reent *r, int fd); -static ssize_t fs_write(struct _reent *r, int fd, const char *ptr, size_t len); -static ssize_t fs_write_safe(struct _reent *r, int fd, const char *ptr, size_t len); -static ssize_t fs_read(struct _reent *r, int fd, char *ptr, size_t len); -static off_t fs_seek(struct _reent *r, int fd, off_t pos, int dir); -static int fs_fstat(struct _reent *r, int fd, struct stat *st); +static int fs_close(struct _reent *r, void *fd); +static ssize_t fs_write(struct _reent *r, void *fd, const char *ptr, size_t len); +static ssize_t fs_write_safe(struct _reent *r, void *fd, const char *ptr, size_t len); +static ssize_t fs_read(struct _reent *r, void *fd, char *ptr, size_t len); +static off_t fs_seek(struct _reent *r, void *fd, off_t pos, int dir); +static int fs_fstat(struct _reent *r, void *fd, struct stat *st); static int fs_stat(struct _reent *r, const char *file, struct stat *st); static int fs_link(struct _reent *r, const char *existing, const char *newLink); static int fs_unlink(struct _reent *r, const char *name); @@ -33,10 +33,10 @@ static int fs_dirreset(struct _reent *r, DIR_ITER *dirState); static int fs_dirnext(struct _reent *r, DIR_ITER *dirState, char *filename, struct stat *filestat); static int fs_dirclose(struct _reent *r, DIR_ITER *dirState); static int fs_statvfs(struct _reent *r, const char *path, struct statvfs *buf); -static int fs_ftruncate(struct _reent *r, int fd, off_t len); -static int fs_fsync(struct _reent *r, int fd); +static int fs_ftruncate(struct _reent *r, void *fd, off_t len); +static int fs_fsync(struct _reent *r, void *fd); static int fs_chmod(struct _reent *r, const char *path, mode_t mode); -static int fs_fchmod(struct _reent *r, int fd, mode_t mode); +static int fs_fchmod(struct _reent *r, void *fd, mode_t mode); static int fs_rmdir(struct _reent *r, const char *name); /** @@ -292,7 +292,7 @@ fs_open(struct _reent *r, static int fs_close(struct _reent *r, - int fd) + void *fd) { FSStatus rc; fs_file_t *file = (fs_file_t*)fd; @@ -313,7 +313,7 @@ fs_close(struct _reent *r, static ssize_t fs_write(struct _reent *r, - int fd, + void *fd, const char *ptr, size_t len) { @@ -373,7 +373,7 @@ fs_write(struct _reent *r, static ssize_t fs_write_safe(struct _reent *r, - int fd, + void *fd, const char *ptr, size_t len) { @@ -433,7 +433,7 @@ fs_write_safe(struct _reent *r, static ssize_t fs_read(struct _reent *r, - int fd, + void *fd, char *ptr, size_t len) { @@ -502,7 +502,7 @@ fs_read(struct _reent *r, static off_t fs_seek(struct _reent *r, - int fd, + void *fd, off_t pos, int whence) { @@ -565,7 +565,7 @@ fs_seek(struct _reent *r, static int fs_fstat(struct _reent *r, - int fd, + void *fd, struct stat *st) { FSStatus rc; @@ -613,7 +613,7 @@ fs_stat(struct _reent *r, if (rc >= 0) { fs_file_t tmpfd = { .fd = fd }; - rc = fs_fstat(r, (int)&tmpfd, st); + rc = fs_fstat(r, &tmpfd, st); FSCloseFile(fsClient, &fsCmd, fd, -1); return rc; } @@ -940,7 +940,7 @@ fs_statvfs(struct _reent *r, static int fs_ftruncate(struct _reent *r, - int fd, + void *fd, off_t len) { FSStatus rc; @@ -975,7 +975,7 @@ fs_ftruncate(struct _reent *r, static int fs_fsync(struct _reent *r, - int fd) + void *fd) { FSStatus rc; fs_file_t *file = (fs_file_t*)fd; @@ -1024,7 +1024,7 @@ fs_chmod(struct _reent *r, static int fs_fchmod(struct _reent *r, - int fd, + void *fd, mode_t mode) { //TODO: FSChangeMode and FSStatFile? From d44b7f774e0a6029f86611b9310dfd0e17d74b20 Mon Sep 17 00:00:00 2001 From: CreeperMario Date: Sun, 27 Aug 2017 18:41:08 +0930 Subject: [PATCH 08/19] Update cppformat v2 to fmtlib v4 * I did this mainly to remove some build warnings that were showing up in cppformat v2 underneath clang (the default macOS compiler) * At this point, WUT will build on macOS with no warnings whatsoever. --- .gitmodules | 6 ++--- externals/CMakeLists.txt | 40 +++++++++++++++++----------------- externals/cppformat | 1 - externals/fmt | 1 + tools/implcheck/CMakeLists.txt | 2 +- tools/implcheck/main.cpp | 2 +- tools/readrpl/CMakeLists.txt | 2 +- tools/readrpl/main.cpp | 2 +- 8 files changed, 28 insertions(+), 28 deletions(-) delete mode 160000 externals/cppformat create mode 160000 externals/fmt diff --git a/.gitmodules b/.gitmodules index d7271e4..0e1dc1b 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,9 +1,9 @@ [submodule "tools/ext/zlib"] path = externals/zlib url = https://github.com/madler/zlib.git -[submodule "tools/ext/cppformat"] - path = externals/cppformat - url = https://github.com/cppformat/cppformat.git [submodule "tools/ext/excmd"] path = externals/excmd url = https://github.com/exjam/excmd +[submodule "externals/fmt"] + path = externals/fmt + url = https://github.com/fmtlib/fmt diff --git a/externals/CMakeLists.txt b/externals/CMakeLists.txt index 5b9180f..be69048 100644 --- a/externals/CMakeLists.txt +++ b/externals/CMakeLists.txt @@ -1,10 +1,10 @@ project(libraries) include(ExternalProject) -# cppformat -set(CPPFORMAT_DIR "cppformat") -externalproject_add(cppformat - SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${CPPFORMAT_DIR}" +# fmtlib +set(FMTLIB_DIR "fmt") +externalproject_add(fmtlib + SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${FMTLIB_DIR}" CMAKE_CACHE_ARGS -DCMAKE_C_COMPILER:string=${CMAKE_C_COMPILER} -DCMAKE_C_FLAGS:string=${CMAKE_C_FLAGS} @@ -14,28 +14,28 @@ externalproject_add(cppformat -DFMT_INSTALL:string=off -DFMT_TEST:string=off INSTALL_COMMAND "") -externalproject_get_property(cppformat BINARY_DIR) -set_target_properties(cppformat PROPERTIES FOLDER libraries) +externalproject_get_property(fmtlib BINARY_DIR) +set_target_properties(fmtlib PROPERTIES FOLDER libraries) if(MSVC) - set(CPPFORMAT_IMPORTED_LOCATION - IMPORTED_LOCATION_DEBUG "${BINARY_DIR}/Debug/${CMAKE_FIND_LIBRARY_PREFIXES}cppformat${CMAKE_FIND_LIBRARY_SUFFIXES}" - IMPORTED_LOCATION_RELEASE "${BINARY_DIR}/Release/${CMAKE_FIND_LIBRARY_PREFIXES}cppformat${CMAKE_FIND_LIBRARY_SUFFIXES}" - IMPORTED_LOCATION_RELWITHDEBINFO "${BINARY_DIR}/RelWithDebInfo/${CMAKE_FIND_LIBRARY_PREFIXES}cppformat${CMAKE_FIND_LIBRARY_SUFFIXES}" - IMPORTED_LOCATION_MINSIZEREL "${BINARY_DIR}/MinSizeRel/${CMAKE_FIND_LIBRARY_PREFIXES}cppformat${CMAKE_FIND_LIBRARY_SUFFIXES}") + set(FMTLIB_IMPORTED_LOCATION + IMPORTED_LOCATION_DEBUG "${BINARY_DIR}/Debug/${CMAKE_FIND_LIBRARY_PREFIXES}fmt${CMAKE_FIND_LIBRARY_SUFFIXES}" + IMPORTED_LOCATION_RELEASE "${BINARY_DIR}/Release/${CMAKE_FIND_LIBRARY_PREFIXES}fmt${CMAKE_FIND_LIBRARY_SUFFIXES}" + IMPORTED_LOCATION_RELWITHDEBINFO "${BINARY_DIR}/RelWithDebInfo/${CMAKE_FIND_LIBRARY_PREFIXES}fmt${CMAKE_FIND_LIBRARY_SUFFIXES}" + IMPORTED_LOCATION_MINSIZEREL "${BINARY_DIR}/MinSizeRel/${CMAKE_FIND_LIBRARY_PREFIXES}fmt${CMAKE_FIND_LIBRARY_SUFFIXES}") else() - set(CPPFORMAT_IMPORTED_LOCATION - IMPORTED_LOCATION "${BINARY_DIR}/${CMAKE_FIND_LIBRARY_PREFIXES}cppformat.a") + set(FMTLIB_IMPORTED_LOCATION + IMPORTED_LOCATION "${BINARY_DIR}/fmt/${CMAKE_FIND_LIBRARY_PREFIXES}fmt.a") endif() -add_library(cppformat_import STATIC IMPORTED GLOBAL) -add_dependencies(cppformat_import cppformat) -set_target_properties(cppformat_import PROPERTIES ${CPPFORMAT_IMPORTED_LOCATION}) +add_library(fmtlib_import STATIC IMPORTED GLOBAL) +add_dependencies(fmtlib_import fmtlib) +set_target_properties(fmtlib_import PROPERTIES ${FMTLIB_IMPORTED_LOCATION}) -add_library(cppformat_final INTERFACE) -target_include_directories(cppformat_final INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/${CPPFORMAT_DIR}") -target_link_libraries(cppformat_final INTERFACE cppformat_import) -set(CPPFORMAT_LINK cppformat_final PARENT_SCOPE) +add_library(fmtlib_final INTERFACE) +target_include_directories(fmtlib_final INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/${FMTLIB_DIR}") +target_link_libraries(fmtlib_final INTERFACE fmtlib_import) +set(FMTLIB_LINK fmtlib_final PARENT_SCOPE) # excmd set(EXCMD_DIR "excmd") diff --git a/externals/cppformat b/externals/cppformat deleted file mode 160000 index 804ad8f..0000000 --- a/externals/cppformat +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 804ad8f4df7e01259374a1d2f458775f1b2d1164 diff --git a/externals/fmt b/externals/fmt new file mode 160000 index 0000000..3983438 --- /dev/null +++ b/externals/fmt @@ -0,0 +1 @@ +Subproject commit 398343897f98b88ade80bbebdcbe82a36c65a980 diff --git a/tools/implcheck/CMakeLists.txt b/tools/implcheck/CMakeLists.txt index 718a441..58d926a 100644 --- a/tools/implcheck/CMakeLists.txt +++ b/tools/implcheck/CMakeLists.txt @@ -7,8 +7,8 @@ add_executable(implcheck ${SOURCE_FILES} ${HEADER_FILES}) set_target_properties(implcheck PROPERTIES FOLDER tools) target_link_libraries(implcheck PRIVATE - ${CPPFORMAT_LINK} ${EXCMD_LINK} + ${FMTLIB_LINK} ${ZLIB_LINK}) install(TARGETS implcheck RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}/bin") diff --git a/tools/implcheck/main.cpp b/tools/implcheck/main.cpp index 8316b0d..dac6ce4 100644 --- a/tools/implcheck/main.cpp +++ b/tools/implcheck/main.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include #include diff --git a/tools/readrpl/CMakeLists.txt b/tools/readrpl/CMakeLists.txt index d9a490f..31a6b29 100644 --- a/tools/readrpl/CMakeLists.txt +++ b/tools/readrpl/CMakeLists.txt @@ -7,8 +7,8 @@ add_executable(readrpl ${SOURCE_FILES} ${HEADER_FILES}) set_target_properties(readrpl PROPERTIES FOLDER tools) target_link_libraries(readrpl PRIVATE - ${CPPFORMAT_LINK} ${EXCMD_LINK} + ${FMTLIB_LINK} ${ZLIB_LINK}) install(TARGETS readrpl RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}/bin") diff --git a/tools/readrpl/main.cpp b/tools/readrpl/main.cpp index 5519e41..8910f40 100644 --- a/tools/readrpl/main.cpp +++ b/tools/readrpl/main.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include #include From 40ff966159f16ca5aebca858f84ec1bd4a01ff99 Mon Sep 17 00:00:00 2001 From: CreeperMario Date: Sun, 27 Aug 2017 19:16:37 +0930 Subject: [PATCH 09/19] Re-organise .gitmodules file --- .gitmodules | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.gitmodules b/.gitmodules index 0e1dc1b..7b4acd7 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,9 +1,9 @@ -[submodule "tools/ext/zlib"] - path = externals/zlib - url = https://github.com/madler/zlib.git -[submodule "tools/ext/excmd"] +[submodule "externals/excmd"] path = externals/excmd url = https://github.com/exjam/excmd [submodule "externals/fmt"] path = externals/fmt url = https://github.com/fmtlib/fmt +[submodule "externals/zlib"] + path = externals/zlib + url = https://github.com/madler/zlib.git From 51f3d4bdd71687ed0c418109315ca6acaca7f320 Mon Sep 17 00:00:00 2001 From: CreeperMario Date: Mon, 28 Aug 2017 06:59:32 +0930 Subject: [PATCH 10/19] whb: Logging functions use less RAM at expense of binary size. --- src/libwhb/src/log.c | 51 +++++++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/src/libwhb/src/log.c b/src/libwhb/src/log.c index 9972bc9..5794ef8 100644 --- a/src/libwhb/src/log.c +++ b/src/libwhb/src/log.c @@ -58,17 +58,21 @@ BOOL WHBLogPrint(const char *str) { char *buf = MEMAllocFromDefaultHeapEx(PRINTF_BUFFER_LENGTH, 4); - BOOL result; - if(!buf) { return FALSE; } snprintf(buf, PRINTF_BUFFER_LENGTH, "%s\n", str); - result = WHBLogWrite(buf); + + int i; + for (i = 0; i < MAX_HANDLERS; ++i) { + if (sHandlers[i]) { + sHandlers[i](buf); + } + } MEMFreeToDefaultHeap(buf); - return result; + return TRUE; } BOOL @@ -76,39 +80,56 @@ WHBLogWritef(const char *fmt, ...) { char *buf = MEMAllocFromDefaultHeapEx(PRINTF_BUFFER_LENGTH, 4); va_list va; - BOOL result; if (!buf) { return FALSE; } va_start(va, fmt); - vsnprintf(buf, PRINTF_BUFFER_LENGTH, fmt, va); - result = WHBLogWrite(buf); + + int i; + for (i = 0; i < MAX_HANDLERS; ++i) { + if (sHandlers[i]) { + sHandlers[i](buf); + } + } MEMFreeToDefaultHeap(buf); va_end(va); - return result; + return TRUE; } BOOL WHBLogPrintf(const char *fmt, ...) { - char *buf = MEMAllocFromDefaultHeapEx(PRINTF_BUFFER_LENGTH, 4); + char *buf1 = MEMAllocFromDefaultHeapEx(PRINTF_BUFFER_LENGTH, 4); + char *buf2 = MEMAllocFromDefaultHeapEx(PRINTF_BUFFER_LENGTH, 4); va_list va; - BOOL result; - if (!buf) { + if (!buf1) { + return FALSE; + } + + if(!buf2) { + MEMFreeToDefaultHeap(buf1); return FALSE; } va_start(va, fmt); - vsnprintf(buf, PRINTF_BUFFER_LENGTH, fmt, va); - result = WHBLogPrint(buf); + vsnprintf(buf1, PRINTF_BUFFER_LENGTH, fmt, va); + snprintf(buf2, PRINTF_BUFFER_LENGTH, "%s\n", buf1); - MEMFreeToDefaultHeap(buf); + int i; + for (i = 0; i < MAX_HANDLERS; ++i) { + if (sHandlers[i]) { + sHandlers[i](buf2); + } + } + + MEMFreeToDefaultHeap(buf1); + MEMFreeToDefaultHeap(buf2); va_end(va); - return result; + return TRUE; } From ef34f600c79bc9530af826bc0c2adc52773ef3ab Mon Sep 17 00:00:00 2001 From: CreeperMario Date: Fri, 1 Sep 2017 14:30:08 +0930 Subject: [PATCH 11/19] whb: Use MEM2 heap instead of DefaultHeap in logging functions --- src/libwhb/src/log.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/libwhb/src/log.c b/src/libwhb/src/log.c index 5794ef8..cf3d6d3 100644 --- a/src/libwhb/src/log.c +++ b/src/libwhb/src/log.c @@ -1,4 +1,5 @@ -#include +#include +#include #include #include #include @@ -57,7 +58,8 @@ WHBLogWrite(const char *str) BOOL WHBLogPrint(const char *str) { - char *buf = MEMAllocFromDefaultHeapEx(PRINTF_BUFFER_LENGTH, 4); + MEMHeapHandle mem2_handle = MEMGetBaseHeapHandle(MEM_BASE_HEAP_MEM2); + char *buf = MEMAllocFromExpHeapEx(mem2_handle, PRINTF_BUFFER_LENGTH, 4); if(!buf) { return FALSE; } @@ -71,14 +73,15 @@ WHBLogPrint(const char *str) } } - MEMFreeToDefaultHeap(buf); + MEMFreeToExpHeap(mem2_handle, buf); return TRUE; } BOOL WHBLogWritef(const char *fmt, ...) { - char *buf = MEMAllocFromDefaultHeapEx(PRINTF_BUFFER_LENGTH, 4); + MEMHeapHandle mem2_handle = MEMGetBaseHeapHandle(MEM_BASE_HEAP_MEM2); + char *buf = MEMAllocFromExpHeapEx(mem2_handle, PRINTF_BUFFER_LENGTH, 4); va_list va; if (!buf) { @@ -95,7 +98,7 @@ WHBLogWritef(const char *fmt, ...) } } - MEMFreeToDefaultHeap(buf); + MEMFreeToExpHeap(mem2_handle, buf); va_end(va); return TRUE; } @@ -103,8 +106,9 @@ WHBLogWritef(const char *fmt, ...) BOOL WHBLogPrintf(const char *fmt, ...) { - char *buf1 = MEMAllocFromDefaultHeapEx(PRINTF_BUFFER_LENGTH, 4); - char *buf2 = MEMAllocFromDefaultHeapEx(PRINTF_BUFFER_LENGTH, 4); + MEMHeapHandle mem2_handle = MEMGetBaseHeapHandle(MEM_BASE_HEAP_MEM2); + char *buf1 = MEMAllocFromExpHeapEx(mem2_handle, PRINTF_BUFFER_LENGTH, 4); + char *buf2 = MEMAllocFromExpHeapEx(mem2_handle, PRINTF_BUFFER_LENGTH, 4); va_list va; if (!buf1) { @@ -112,7 +116,7 @@ WHBLogPrintf(const char *fmt, ...) } if(!buf2) { - MEMFreeToDefaultHeap(buf1); + MEMFreeToExpHeap(mem2_handle, buf1); return FALSE; } @@ -128,8 +132,8 @@ WHBLogPrintf(const char *fmt, ...) } } - MEMFreeToDefaultHeap(buf1); - MEMFreeToDefaultHeap(buf2); + MEMFreeToExpHeap(mem2_handle, buf1); + MEMFreeToExpHeap(mem2_handle, buf2); va_end(va); return TRUE; } From ace7c0b36445740a404e1204b2b423b02f9477fa Mon Sep 17 00:00:00 2001 From: CreeperMario Date: Fri, 1 Sep 2017 18:13:51 +0930 Subject: [PATCH 12/19] whb: Implement a basic TCP-based command server kit. * This server will listen for connections, and then listen on that connection stream for, well, text. It will then close the connection and send the string back to the main program. --- src/libwhb/include/whb/commandserver.h | 28 ++++++ src/libwhb/src/commandserver.c | 123 +++++++++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 src/libwhb/include/whb/commandserver.h create mode 100644 src/libwhb/src/commandserver.c diff --git a/src/libwhb/include/whb/commandserver.h b/src/libwhb/include/whb/commandserver.h new file mode 100644 index 0000000..4c6ece3 --- /dev/null +++ b/src/libwhb/include/whb/commandserver.h @@ -0,0 +1,28 @@ +#pragma once + +/** + * \defgroup whb_commandserver Network Command Server + * \ingroup whb + * @{ + */ + +#ifdef __cplusplus +extern "C" { +#endif + +#define WHB_SERVER_BUFFER_SIZE 1024 + +BOOL +WHBCommandServerInit(); + +void +WHBCommandServerStop(); + +BOOL +WHBCommandServerListen(); + +#ifdef __cplusplus +} +#endif + +/** @} */ diff --git a/src/libwhb/src/commandserver.c b/src/libwhb/src/commandserver.c new file mode 100644 index 0000000..79f71a3 --- /dev/null +++ b/src/libwhb/src/commandserver.c @@ -0,0 +1,123 @@ +#include +#include +#include +#include +#include +#include + +#include + +#define SERVER_PORT 4406 + +int +sSocket = -1; + +struct sockaddr_in +sAddr; + +void +closeSocket(const char * funcName) +{ + int ret = socketclose(sSocket); + if(ret < 0) { + WHBLogPrintf("%s: Error occurred closing socket: %d", funcName, socketlasterr()); + } +} + +void +closeClient(const char * funcName, int fd) +{ + int ret = socketclose(fd); + if(ret < 0) { + WHBLogPrintf("%s: Error occurred closing client socket: %d", funcName, socketlasterr()); + } +} + +BOOL +WHBCommandServerInit() +{ + int ret = 0; + + if(sSocket >= 0) { + WHBLogPrintf("%s: Command server is already running.", __FUNCTION__); + return TRUE; + } + + WHBInitializeSocketLibrary(); + + sSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if(sSocket < 0) { + WHBLogPrintf("%s: Error occurred while creating socket: %d", __FUNCTION__, socketlasterr()); + sSocket = -1; + return FALSE; + } + + memset(&sAddr, 0, sizeof(struct sockaddr_in)); + sAddr.sin_family = AF_INET; + sAddr.sin_port = htons(SERVER_PORT); + sAddr.sin_addr.s_addr = htonl(INADDR_ANY); + + ret = bind(sSocket, (struct sockaddr *)&sAddr, sizeof(struct sockaddr_in)); + if(ret < 0) { + WHBLogPrintf("%s: Error occurred while binding to socket: %d", __FUNCTION__, socketlasterr()); + closeSocket(__FUNCTION__); + sSocket = -1; + return FALSE; + } + + ret = listen(sSocket, 3); + if(ret < 0) { + WHBLogPrintf("%s: Error occurred while setting socket to listen mode: %d", __FUNCTION__, socketlasterr()); + closeSocket(__FUNCTION__); + sSocket = -1; + return FALSE; + } + + return TRUE; +} + +void +WHBCommandServerStop() +{ + if(sSocket < 0) { + WHBLogPrintf("%s: Socket is already closed.", __FUNCTION__); + return; + } + + closeSocket(__FUNCTION__); + sSocket = -1; + WHBDeinitializeSocketLibrary(); +} + +BOOL +WHBCommandServerListen(char * stringLocation) +{ + char buffer[WHB_SERVER_BUFFER_SIZE]; + memset(buffer, 0, WHB_SERVER_BUFFER_SIZE); + + int ret, sClient; + struct sockaddr_in sClientAddr; + socklen_t sClientAddrLen = sizeof(struct sockaddr_in); + + if(sSocket < 0) { + WHBLogPrintf("%s: Socket is not open. Please run WHBCommandServerInit() first.", __FUNCTION__); + return FALSE; + } + + sClient = accept(sSocket, (struct sockaddr *)&sClientAddr, &sClientAddrLen); + if(sClient < 0) { + WHBLogPrintf("%s: Error occurred while accepting a client connection: %d", __FUNCTION__, socketlasterr()); + return FALSE; + } + + ret = recv(sClient, buffer, WHB_SERVER_BUFFER_SIZE, 0); + if(ret < 0) { + WHBLogPrintf("%s: Error occurred while receiving data from client: %d", __FUNCTION__, socketlasterr()); + closeClient(__FUNCTION__, sClient); + return FALSE; + } + + closeClient(__FUNCTION__, sClient); + memcpy(stringLocation, buffer, WHB_SERVER_BUFFER_SIZE); + return TRUE; +} From 0b2ca7c05ed26cdefd522dcfacb6ad70edd60cab Mon Sep 17 00:00:00 2001 From: CreeperMario Date: Fri, 1 Sep 2017 18:26:10 +0930 Subject: [PATCH 13/19] travis: Add macOS build target * This is currently untested, and will be fixed if necessary during the PR process. --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index d7fa2c8..63f2b16 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,6 +13,8 @@ matrix: - zlib1g-dev env: - MATRIX_EVAL="CC=gcc-6 && CXX=g++-6" + - os: osx + osx_image: xcode8.3 cache: directories: From 5e84d0321d0c1dfd12f9f1ec339513de9db65341 Mon Sep 17 00:00:00 2001 From: CreeperMario Date: Fri, 1 Sep 2017 21:01:46 +0930 Subject: [PATCH 14/19] CI Ambulance --- .travis.yml | 3 ++- externals/CMakeLists.txt | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 63f2b16..12d5008 100644 --- a/.travis.yml +++ b/.travis.yml @@ -27,7 +27,8 @@ git: submodules: true before_script: - - wget https://freefr.dl.sourceforge.net/project/devkitpro/devkitPPC/devkitPPC_r29-1/devkitPPC_r29-1-x86_64-linux.tar.bz2 -O /tmp/devkitPPC.tar.bz2 + - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then wget https://freefr.dl.sourceforge.net/project/devkitpro/devkitPPC/devkitPPC_r29-1/devkitPPC_r29-1-x86_64-linux.tar.bz2 -O /tmp/devkitPPC.tar.bz2; fi + - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then wget https://freefr.dl.sourceforge.net/project/devkitpro/devkitPPC/devkitPPC_r29-1/devkitPPC_r29-1-x86_64-osx.tar.bz2 -O /tmp/devkitPPC.tar.bz2; fi - tar -xjf /tmp/devkitPPC.tar.bz2 - export DEVKITPPC=$PWD/devkitPPC diff --git a/externals/CMakeLists.txt b/externals/CMakeLists.txt index be69048..c078e31 100644 --- a/externals/CMakeLists.txt +++ b/externals/CMakeLists.txt @@ -19,10 +19,10 @@ set_target_properties(fmtlib PROPERTIES FOLDER libraries) if(MSVC) set(FMTLIB_IMPORTED_LOCATION - IMPORTED_LOCATION_DEBUG "${BINARY_DIR}/Debug/${CMAKE_FIND_LIBRARY_PREFIXES}fmt${CMAKE_FIND_LIBRARY_SUFFIXES}" - IMPORTED_LOCATION_RELEASE "${BINARY_DIR}/Release/${CMAKE_FIND_LIBRARY_PREFIXES}fmt${CMAKE_FIND_LIBRARY_SUFFIXES}" - IMPORTED_LOCATION_RELWITHDEBINFO "${BINARY_DIR}/RelWithDebInfo/${CMAKE_FIND_LIBRARY_PREFIXES}fmt${CMAKE_FIND_LIBRARY_SUFFIXES}" - IMPORTED_LOCATION_MINSIZEREL "${BINARY_DIR}/MinSizeRel/${CMAKE_FIND_LIBRARY_PREFIXES}fmt${CMAKE_FIND_LIBRARY_SUFFIXES}") + IMPORTED_LOCATION_DEBUG "${BINARY_DIR}/fmt/Debug/${CMAKE_FIND_LIBRARY_PREFIXES}fmt${CMAKE_FIND_LIBRARY_SUFFIXES}" + IMPORTED_LOCATION_RELEASE "${BINARY_DIR}/fmt/Release/${CMAKE_FIND_LIBRARY_PREFIXES}fmt${CMAKE_FIND_LIBRARY_SUFFIXES}" + IMPORTED_LOCATION_RELWITHDEBINFO "${BINARY_DIR}/fmt/RelWithDebInfo/${CMAKE_FIND_LIBRARY_PREFIXES}fmt${CMAKE_FIND_LIBRARY_SUFFIXES}" + IMPORTED_LOCATION_MINSIZEREL "${BINARY_DIR}/fmt/MinSizeRel/${CMAKE_FIND_LIBRARY_PREFIXES}fmt${CMAKE_FIND_LIBRARY_SUFFIXES}") else() set(FMTLIB_IMPORTED_LOCATION IMPORTED_LOCATION "${BINARY_DIR}/fmt/${CMAKE_FIND_LIBRARY_PREFIXES}fmt.a") From 23d6f0f5cbb1da2efa037f876ea5938067c94000 Mon Sep 17 00:00:00 2001 From: CreeperMario Date: Sat, 2 Sep 2017 07:16:57 +0930 Subject: [PATCH 15/19] whb: Correct header file for command server * Why did this NOT generate a build warning? --- src/libwhb/include/whb/commandserver.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libwhb/include/whb/commandserver.h b/src/libwhb/include/whb/commandserver.h index 4c6ece3..bc3fd9c 100644 --- a/src/libwhb/include/whb/commandserver.h +++ b/src/libwhb/include/whb/commandserver.h @@ -19,7 +19,7 @@ void WHBCommandServerStop(); BOOL -WHBCommandServerListen(); +WHBCommandServerListen(char * stringLocation); #ifdef __cplusplus } From 76a8b8e2923031a341cf900b467b34be84affb49 Mon Sep 17 00:00:00 2001 From: CreeperMario Date: Tue, 10 Oct 2017 09:12:54 +1030 Subject: [PATCH 16/19] readrpl: Return -1 instead of false in main() * This suppresses a compiler warning emitted by clang, about returning a boolean value when an integer is expected. --- tools/readrpl/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/readrpl/main.cpp b/tools/readrpl/main.cpp index 8910f40..836d6bb 100644 --- a/tools/readrpl/main.cpp +++ b/tools/readrpl/main.cpp @@ -700,7 +700,7 @@ int main(int argc, char **argv) if (header.magic != elf::HeaderMagic) { std::cout << "Invalid ELF magic header" << std::endl; - return false; + return -1; } // Read sections From 758e91f65f7ca4a39862ca23afcea08230f73efb Mon Sep 17 00:00:00 2001 From: CreeperMario Date: Tue, 10 Oct 2017 09:22:13 +1030 Subject: [PATCH 17/19] Revert "whb: Use MEM2 heap instead of DefaultHeap in logging functions" This reverts commit ef34f600c79bc9530af826bc0c2adc52773ef3ab, meaning that MEMAllocFromDefaultHeapEx is now used again, instead of MEMAllocFromExpHeapEx and MEMGetBaseHeapHandle. --- src/libwhb/src/log.c | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/libwhb/src/log.c b/src/libwhb/src/log.c index cf3d6d3..5794ef8 100644 --- a/src/libwhb/src/log.c +++ b/src/libwhb/src/log.c @@ -1,5 +1,4 @@ -#include -#include +#include #include #include #include @@ -58,8 +57,7 @@ WHBLogWrite(const char *str) BOOL WHBLogPrint(const char *str) { - MEMHeapHandle mem2_handle = MEMGetBaseHeapHandle(MEM_BASE_HEAP_MEM2); - char *buf = MEMAllocFromExpHeapEx(mem2_handle, PRINTF_BUFFER_LENGTH, 4); + char *buf = MEMAllocFromDefaultHeapEx(PRINTF_BUFFER_LENGTH, 4); if(!buf) { return FALSE; } @@ -73,15 +71,14 @@ WHBLogPrint(const char *str) } } - MEMFreeToExpHeap(mem2_handle, buf); + MEMFreeToDefaultHeap(buf); return TRUE; } BOOL WHBLogWritef(const char *fmt, ...) { - MEMHeapHandle mem2_handle = MEMGetBaseHeapHandle(MEM_BASE_HEAP_MEM2); - char *buf = MEMAllocFromExpHeapEx(mem2_handle, PRINTF_BUFFER_LENGTH, 4); + char *buf = MEMAllocFromDefaultHeapEx(PRINTF_BUFFER_LENGTH, 4); va_list va; if (!buf) { @@ -98,7 +95,7 @@ WHBLogWritef(const char *fmt, ...) } } - MEMFreeToExpHeap(mem2_handle, buf); + MEMFreeToDefaultHeap(buf); va_end(va); return TRUE; } @@ -106,9 +103,8 @@ WHBLogWritef(const char *fmt, ...) BOOL WHBLogPrintf(const char *fmt, ...) { - MEMHeapHandle mem2_handle = MEMGetBaseHeapHandle(MEM_BASE_HEAP_MEM2); - char *buf1 = MEMAllocFromExpHeapEx(mem2_handle, PRINTF_BUFFER_LENGTH, 4); - char *buf2 = MEMAllocFromExpHeapEx(mem2_handle, PRINTF_BUFFER_LENGTH, 4); + char *buf1 = MEMAllocFromDefaultHeapEx(PRINTF_BUFFER_LENGTH, 4); + char *buf2 = MEMAllocFromDefaultHeapEx(PRINTF_BUFFER_LENGTH, 4); va_list va; if (!buf1) { @@ -116,7 +112,7 @@ WHBLogPrintf(const char *fmt, ...) } if(!buf2) { - MEMFreeToExpHeap(mem2_handle, buf1); + MEMFreeToDefaultHeap(buf1); return FALSE; } @@ -132,8 +128,8 @@ WHBLogPrintf(const char *fmt, ...) } } - MEMFreeToExpHeap(mem2_handle, buf1); - MEMFreeToExpHeap(mem2_handle, buf2); + MEMFreeToDefaultHeap(buf1); + MEMFreeToDefaultHeap(buf2); va_end(va); return TRUE; } From 240514eff22aa8f4b3585ad23cc6a7ecac18e324 Mon Sep 17 00:00:00 2001 From: CreeperMario Date: Tue, 10 Oct 2017 09:43:59 +1030 Subject: [PATCH 18/19] whb: Separate log dispatching into a separate inline function --- src/libwhb/src/log.c | 44 +++++++++++++++----------------------------- 1 file changed, 15 insertions(+), 29 deletions(-) diff --git a/src/libwhb/src/log.c b/src/libwhb/src/log.c index 5794ef8..eccd15e 100644 --- a/src/libwhb/src/log.c +++ b/src/libwhb/src/log.c @@ -10,6 +10,17 @@ static LogHandlerFn sHandlers[MAX_HANDLERS] = { 0 }; +static inline void +dispatchMessage(const char * str) +{ + int i; + for (i = 0; i < MAX_HANDLERS; ++i) { + if (sHandlers[i]) { + sHandlers[i](str); + } + } +} + BOOL WHBAddLogHandler(LogHandlerFn fn) { @@ -43,14 +54,7 @@ WHBRemoveLogHandler(LogHandlerFn fn) BOOL WHBLogWrite(const char *str) { - int i; - - for (i = 0; i < MAX_HANDLERS; ++i) { - if (sHandlers[i]) { - sHandlers[i](str); - } - } - + dispatchMessage(str); return TRUE; } @@ -63,13 +67,7 @@ WHBLogPrint(const char *str) } snprintf(buf, PRINTF_BUFFER_LENGTH, "%s\n", str); - - int i; - for (i = 0; i < MAX_HANDLERS; ++i) { - if (sHandlers[i]) { - sHandlers[i](buf); - } - } + dispatchMessage(buf); MEMFreeToDefaultHeap(buf); return TRUE; @@ -87,13 +85,7 @@ WHBLogWritef(const char *fmt, ...) va_start(va, fmt); vsnprintf(buf, PRINTF_BUFFER_LENGTH, fmt, va); - - int i; - for (i = 0; i < MAX_HANDLERS; ++i) { - if (sHandlers[i]) { - sHandlers[i](buf); - } - } + dispatchMessage(buf); MEMFreeToDefaultHeap(buf); va_end(va); @@ -120,13 +112,7 @@ WHBLogPrintf(const char *fmt, ...) vsnprintf(buf1, PRINTF_BUFFER_LENGTH, fmt, va); snprintf(buf2, PRINTF_BUFFER_LENGTH, "%s\n", buf1); - - int i; - for (i = 0; i < MAX_HANDLERS; ++i) { - if (sHandlers[i]) { - sHandlers[i](buf2); - } - } + dispatchMessage(buf2); MEMFreeToDefaultHeap(buf1); MEMFreeToDefaultHeap(buf2); From 39d4599896223f19b8c7e0e0624a620a7eada071 Mon Sep 17 00:00:00 2001 From: CreeperMario Date: Tue, 10 Oct 2017 11:53:06 +1030 Subject: [PATCH 19/19] whb: Command Server connection stream now stays open. * The stream will stay open until either an error occurs, WHBCommandServerStop is called, or a null string is returned (the client socket had been closed). * The header file now includes wut.h, so that BOOL is defined. --- src/libwhb/include/whb/commandserver.h | 1 + src/libwhb/src/commandserver.c | 41 ++++++++++++++++++-------- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/libwhb/include/whb/commandserver.h b/src/libwhb/include/whb/commandserver.h index bc3fd9c..59f5d3f 100644 --- a/src/libwhb/include/whb/commandserver.h +++ b/src/libwhb/include/whb/commandserver.h @@ -1,4 +1,5 @@ #pragma once +#include /** * \defgroup whb_commandserver Network Command Server diff --git a/src/libwhb/src/commandserver.c b/src/libwhb/src/commandserver.c index 79f71a3..a2c1831 100644 --- a/src/libwhb/src/commandserver.c +++ b/src/libwhb/src/commandserver.c @@ -12,25 +12,34 @@ int sSocket = -1; +int +sClient = -1; + struct sockaddr_in sAddr; -void +static inline void closeSocket(const char * funcName) { int ret = socketclose(sSocket); if(ret < 0) { WHBLogPrintf("%s: Error occurred closing socket: %d", funcName, socketlasterr()); } + else { + sSocket = -1; + } } -void -closeClient(const char * funcName, int fd) +static inline void +closeClient(const char * funcName) { - int ret = socketclose(fd); + int ret = socketclose(sClient); if(ret < 0) { WHBLogPrintf("%s: Error occurred closing client socket: %d", funcName, socketlasterr()); } + else { + sClient = -1; + } } BOOL @@ -61,7 +70,6 @@ WHBCommandServerInit() if(ret < 0) { WHBLogPrintf("%s: Error occurred while binding to socket: %d", __FUNCTION__, socketlasterr()); closeSocket(__FUNCTION__); - sSocket = -1; return FALSE; } @@ -69,7 +77,6 @@ WHBCommandServerInit() if(ret < 0) { WHBLogPrintf("%s: Error occurred while setting socket to listen mode: %d", __FUNCTION__, socketlasterr()); closeSocket(__FUNCTION__); - sSocket = -1; return FALSE; } @@ -84,8 +91,10 @@ WHBCommandServerStop() return; } + if(sClient >= 0) { + closeClient(__FUNCTION__); + } closeSocket(__FUNCTION__); - sSocket = -1; WHBDeinitializeSocketLibrary(); } @@ -95,7 +104,7 @@ WHBCommandServerListen(char * stringLocation) char buffer[WHB_SERVER_BUFFER_SIZE]; memset(buffer, 0, WHB_SERVER_BUFFER_SIZE); - int ret, sClient; + int ret; struct sockaddr_in sClientAddr; socklen_t sClientAddrLen = sizeof(struct sockaddr_in); @@ -104,20 +113,26 @@ WHBCommandServerListen(char * stringLocation) return FALSE; } - sClient = accept(sSocket, (struct sockaddr *)&sClientAddr, &sClientAddrLen); if(sClient < 0) { - WHBLogPrintf("%s: Error occurred while accepting a client connection: %d", __FUNCTION__, socketlasterr()); - return FALSE; + sClient = accept(sSocket, (struct sockaddr *)&sClientAddr, &sClientAddrLen); + if(sClient < 0) { + WHBLogPrintf("%s: Error occurred while accepting a client connection: %d", __FUNCTION__, socketlasterr()); + return FALSE; + } } ret = recv(sClient, buffer, WHB_SERVER_BUFFER_SIZE, 0); if(ret < 0) { WHBLogPrintf("%s: Error occurred while receiving data from client: %d", __FUNCTION__, socketlasterr()); - closeClient(__FUNCTION__, sClient); + closeClient(__FUNCTION__); + return FALSE; + } + if(ret == 0) { + WHBLogPrintf("%s: Remote socket was closed. Closing client connection...", __FUNCTION__); + closeClient(__FUNCTION__); return FALSE; } - closeClient(__FUNCTION__, sClient); memcpy(stringLocation, buffer, WHB_SERVER_BUFFER_SIZE); return TRUE; }