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(); + } +}