From a2247e7b1e567a3680221166cf9af4cd427c38ea Mon Sep 17 00:00:00 2001 From: CreeperMario Date: Sun, 20 Aug 2017 19:16:27 +0930 Subject: [PATCH] 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(); + } +}