mirror of
https://github.com/wiiu-env/wut.git
synced 2024-12-13 04:22:37 +01:00
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:
parent
20c7dbf2fb
commit
a2247e7b1e
23
src/libwhb/include/whb/socketinit.h
Normal file
23
src/libwhb/include/whb/socketinit.h
Normal 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
|
||||
|
||||
/** @} */
|
@ -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);
|
||||
}
|
||||
|
29
src/libwhb/src/socketinit.c
Normal file
29
src/libwhb/src/socketinit.c
Normal 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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user