2015-01-17 17:33:59 -05:00
|
|
|
|
|
|
|
// NetworkSingleton.h
|
|
|
|
|
|
|
|
// Declares the cNetworkSingleton class representing the storage for global data pertaining to network API
|
|
|
|
// such as a list of all connections, all listening sockets and the LibEvent dispatch thread.
|
|
|
|
|
2015-01-26 08:46:20 -05:00
|
|
|
// This is an internal header, no-one outside OSSupport should need to include it; use Network.h instead;
|
|
|
|
// the only exception being the main app entrypoint that needs to call Terminate before quitting.
|
2015-01-18 05:57:16 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-17 17:33:59 -05:00
|
|
|
#pragma once
|
|
|
|
|
2016-06-17 10:25:31 -04:00
|
|
|
#include <event2/event.h>
|
2017-06-15 05:03:49 -04:00
|
|
|
#include "NetworkLookup.h"
|
2015-01-17 17:33:59 -05:00
|
|
|
#include "CriticalSection.h"
|
2015-01-18 06:35:02 -05:00
|
|
|
#include "Event.h"
|
2015-01-17 17:33:59 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// fwd:
|
|
|
|
struct event_base;
|
2018-02-20 12:08:46 -05:00
|
|
|
class cTCPLink;
|
|
|
|
typedef std::shared_ptr<cTCPLink> cTCPLinkPtr;
|
|
|
|
typedef std::vector<cTCPLinkPtr> cTCPLinkPtrs;
|
|
|
|
class cServerHandle;
|
|
|
|
typedef std::shared_ptr<cServerHandle> cServerHandlePtr;
|
|
|
|
typedef std::vector<cServerHandlePtr> cServerHandlePtrs;
|
2015-01-17 17:33:59 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class cNetworkSingleton
|
|
|
|
{
|
|
|
|
public:
|
2015-06-17 10:38:00 -04:00
|
|
|
cNetworkSingleton();
|
2020-05-10 12:16:38 -04:00
|
|
|
~cNetworkSingleton() noexcept(false);
|
2015-01-18 06:35:02 -05:00
|
|
|
|
2015-01-17 17:33:59 -05:00
|
|
|
/** Returns the singleton instance of this class */
|
|
|
|
static cNetworkSingleton & Get(void);
|
|
|
|
|
2015-06-17 10:38:00 -04:00
|
|
|
/** Initialises all network-related threads.
|
|
|
|
To be called on first run or after app restart. */
|
|
|
|
void Initialise(void);
|
|
|
|
|
2015-01-26 08:46:20 -05:00
|
|
|
/** Terminates all network-related threads.
|
2015-06-17 10:38:00 -04:00
|
|
|
To be used only on app shutdown or restart.
|
2015-01-26 08:46:20 -05:00
|
|
|
MSVC runtime requires that the LibEvent networking be shut down before the main() function is exitted; this is the way to do it. */
|
|
|
|
void Terminate(void);
|
|
|
|
|
2015-01-17 17:33:59 -05:00
|
|
|
/** Returns the main LibEvent handle for event registering. */
|
|
|
|
event_base * GetEventBase(void) { return m_EventBase; }
|
|
|
|
|
2017-06-15 05:03:49 -04:00
|
|
|
/** Returns the thread used to perform hostname and IP lookups */
|
|
|
|
cNetworkLookup & GetLookupThread() { return m_LookupThread; }
|
2015-01-17 17:33:59 -05:00
|
|
|
|
|
|
|
/** Adds the specified link to m_Connections.
|
|
|
|
Used by the underlying link implementation when a new link is created. */
|
2020-05-14 18:15:35 -04:00
|
|
|
void AddLink(const cTCPLinkPtr & a_Link);
|
2015-01-17 17:33:59 -05:00
|
|
|
|
|
|
|
/** Removes the specified link from m_Connections.
|
|
|
|
Used by the underlying link implementation when the link is closed / errored. */
|
2018-02-20 12:08:46 -05:00
|
|
|
void RemoveLink(const cTCPLink * a_Link);
|
2015-01-17 17:33:59 -05:00
|
|
|
|
|
|
|
/** Adds the specified link to m_Servers.
|
|
|
|
Used by the underlying server handle implementation when a new listening server is created.
|
|
|
|
Only servers that succeed in listening are added. */
|
2020-05-14 18:15:35 -04:00
|
|
|
void AddServer(const cServerHandlePtr & a_Server);
|
2015-01-17 17:33:59 -05:00
|
|
|
|
|
|
|
/** Removes the specified server from m_Servers.
|
|
|
|
Used by the underlying server handle implementation when the server is closed. */
|
2018-02-20 12:08:46 -05:00
|
|
|
void RemoveServer(const cServerHandle * a_Server);
|
2015-01-17 17:33:59 -05:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
/** The main LibEvent container for driving the event loop. */
|
|
|
|
event_base * m_EventBase;
|
|
|
|
|
|
|
|
/** Container for all client connections, including ones with pending-connect. */
|
2018-02-20 12:08:46 -05:00
|
|
|
cTCPLinkPtrs m_Connections;
|
2015-01-17 17:33:59 -05:00
|
|
|
|
|
|
|
/** Container for all servers that are currently active. */
|
2018-02-20 12:08:46 -05:00
|
|
|
cServerHandlePtrs m_Servers;
|
2015-01-17 17:33:59 -05:00
|
|
|
|
|
|
|
/** Mutex protecting all containers against multithreaded access. */
|
|
|
|
cCriticalSection m_CS;
|
|
|
|
|
2015-01-26 08:46:20 -05:00
|
|
|
/** Set to true if Terminate has been called. */
|
2017-06-15 05:03:49 -04:00
|
|
|
std::atomic<bool> m_HasTerminated;
|
2015-01-26 08:46:20 -05:00
|
|
|
|
2015-02-18 16:41:22 -05:00
|
|
|
/** The thread in which the main LibEvent loop runs. */
|
|
|
|
std::thread m_EventLoopThread;
|
|
|
|
|
2016-06-17 10:25:31 -04:00
|
|
|
/** Event that is signalled once the startup is finished and the LibEvent loop is running. */
|
2016-06-26 09:51:12 -04:00
|
|
|
cEvent m_StartupEvent;
|
2016-06-17 10:25:31 -04:00
|
|
|
|
2017-06-15 05:03:49 -04:00
|
|
|
/** The thread on which hostname and ip address lookup is performed. */
|
|
|
|
cNetworkLookup m_LookupThread;
|
|
|
|
|
2016-06-17 10:25:31 -04:00
|
|
|
|
2015-01-17 17:33:59 -05:00
|
|
|
/** Converts LibEvent-generated log events into log messages in MCS log. */
|
|
|
|
static void LogCallback(int a_Severity, const char * a_Msg);
|
|
|
|
|
|
|
|
/** Implements the thread that runs LibEvent's event dispatcher loop. */
|
|
|
|
static void RunEventLoop(cNetworkSingleton * a_Self);
|
2016-06-17 10:25:31 -04:00
|
|
|
|
|
|
|
/** Callback called by LibEvent when the event loop is started. */
|
|
|
|
static void SignalizeStartup(evutil_socket_t a_Socket, short a_Events, void * a_Self);
|
2015-01-17 17:33:59 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|