10 #include <event2/thread.h>
11 #include <event2/bufferevent.h>
12 #include <event2/listener.h>
55 memset(&wsaData, 0,
sizeof(wsaData));
56 int res = WSAStartup (MAKEWORD(2, 2), &wsaData);
59 int err = WSAGetLastError();
60 LOGWARNING(
"WSAStartup failed: %d, WSAGLE = %d (%s)", res, err, evutil_socket_error_to_string(err));
69 #if defined(EVTHREAD_USE_WINDOWS_THREADS_IMPLEMENTED)
70 evthread_use_windows_threads();
71 #elif defined(EVTHREAD_USE_PTHREADS_IMPLEMENTED)
72 evthread_use_pthreads();
74 #error No threading implemented for EVTHREAD
78 event_config * config = event_config_new();
79 event_config_set_flag(config, EVENT_BASE_FLAG_STARTUP_IOCP);
83 LOGERROR(
"Failed to initialize LibEvent. The server will now terminate.");
86 event_config_free(config);
114 for (
auto & Conn : Conns)
120 for (
auto & Server : Servers)
133 libevent_global_shutdown();
148 case _EVENT_LOG_DEBUG:
LOGD (
"LibEvent: %s", a_Msg);
break;
149 case _EVENT_LOG_MSG:
LOG (
"LibEvent: %s", a_Msg);
break;
150 case _EVENT_LOG_WARN:
LOGWARNING(
"LibEvent: %s", a_Msg);
break;
151 case _EVENT_LOG_ERR:
LOGERROR (
"LibEvent: %s", a_Msg);
break;
154 LOGWARNING(
"LibEvent: Unknown log severity (%d): %s", a_Severity, a_Msg);
168 evtimer_add(timer, &timeout);
169 event_base_loop(a_Self->
m_EventBase, EVLOOP_NO_EXIT_ON_EMPTY);
181 self->m_StartupEvent.Set();
205 if (itr->get() == a_Link)
234 if (itr->get() == a_Server)
void LOGERROR(std::string_view a_Format, const Args &... args)
void LOGWARNING(std::string_view a_Format, const Args &... args)
void LOG(std::string_view a_Format, const Args &... args)
std::shared_ptr< cTCPLink > cTCPLinkPtr
std::shared_ptr< cServerHandle > cServerHandlePtr
RAII for cCriticalSection - locks the CS on creation, unlocks on destruction.
void Wait(void)
Waits until the event has been set.
void Start(void)
Starts the thread; returns without waiting for the actual start.
Interface that provides the methods available on a single TCP connection.
Interface that provides the methods available on a listening server socket.
void Stop()
Cancels any scheduled lookups and joins the lookup thread.
std::atomic< bool > m_HasTerminated
Set to true if Terminate has been called.
void Terminate(void)
Terminates all network-related threads.
static void SignalizeStartup(evutil_socket_t a_Socket, short a_Events, void *a_Self)
Callback called by LibEvent when the event loop is started.
std::thread m_EventLoopThread
The thread in which the main LibEvent loop runs.
static cNetworkSingleton & Get(void)
Returns the singleton instance of this class.
void Initialise(void)
Initialises all network-related threads.
event_base * m_EventBase
The main LibEvent container for driving the event loop.
cServerHandlePtrs m_Servers
Container for all servers that are currently active.
void AddLink(const cTCPLinkPtr &a_Link)
Adds the specified link to m_Connections.
cEvent m_StartupEvent
Event that is signalled once the startup is finished and the LibEvent loop is running.
void AddServer(const cServerHandlePtr &a_Server)
Adds the specified link to m_Servers.
cCriticalSection m_CS
Mutex protecting all containers against multithreaded access.
static void RunEventLoop(cNetworkSingleton *a_Self)
Implements the thread that runs LibEvent's event dispatcher loop.
void RemoveLink(const cTCPLink *a_Link)
Removes the specified link from m_Connections.
cNetworkLookup m_LookupThread
The thread on which hostname and ip address lookup is performed.
void RemoveServer(const cServerHandle *a_Server)
Removes the specified server from m_Servers.
static void LogCallback(int a_Severity, const char *a_Msg)
Converts LibEvent-generated log events into log messages in MCS log.
~cNetworkSingleton() noexcept(false)
cTCPLinkPtrs m_Connections
Container for all client connections, including ones with pending-connect.