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.
This commit is contained in:
CreeperMario 2017-08-20 19:16:27 +09:30
parent 20c7dbf2fb
commit a2247e7b1e
3 changed files with 55 additions and 2 deletions

View File

@ -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
/** @} */

View File

@ -4,6 +4,7 @@
#include <string.h>
#include <whb/log.h>
#include <whb/log_udp.h>
#include <whb/socketinit.h>
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);
}

View File

@ -0,0 +1,29 @@
#include <nsysnet/socket.h>
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();
}
}