summaryrefslogtreecommitdiffstats
path: root/src/OSSupport/NetworkSingleton.h
blob: d2af6d3c81c9db550281a85ef89c404b50b77448 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

// 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.

// 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.





#pragma once

#include <event2/event.h>
#include "NetworkLookup.h"
#include "CriticalSection.h"
#include "Event.h"





// fwd:
struct event_base;
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;





class cNetworkSingleton
{
  public:
	cNetworkSingleton();
	~cNetworkSingleton() noexcept(false);

	/** Returns the singleton instance of this class */
	static cNetworkSingleton & Get(void);

	/** Initialises all network-related threads.
	To be called on first run or after app restart. */
	void Initialise(void);

	/** Terminates all network-related threads.
	To be used only on app shutdown or restart.
	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);

	/** Returns the main LibEvent handle for event registering. */
	event_base * GetEventBase(void) { return m_EventBase; }

	/** Returns the thread used to perform hostname and IP lookups */
	cNetworkLookup & GetLookupThread() { return m_LookupThread; }

	/** Adds the specified link to m_Connections.
	Used by the underlying link implementation when a new link is created. */
	void AddLink(const cTCPLinkPtr & a_Link);

	/** Removes the specified link from m_Connections.
	Used by the underlying link implementation when the link is closed / errored. */
	void RemoveLink(const cTCPLink * a_Link);

	/** 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. */
	void AddServer(const cServerHandlePtr & a_Server);

	/** Removes the specified server from m_Servers.
	Used by the underlying server handle implementation when the server is closed. */
	void RemoveServer(const cServerHandle * a_Server);

  protected:
	/** The main LibEvent container for driving the event loop. */
	event_base * m_EventBase;

	/** Container for all client connections, including ones with pending-connect. */
	cTCPLinkPtrs m_Connections;

	/** Container for all servers that are currently active. */
	cServerHandlePtrs m_Servers;

	/** Mutex protecting all containers against multithreaded access. */
	cCriticalSection m_CS;

	/** Set to true if Terminate has been called. */
	std::atomic<bool> m_HasTerminated;

	/** The thread in which the main LibEvent loop runs. */
	std::thread m_EventLoopThread;

	/** Event that is signalled once the startup is finished and the LibEvent loop is running. */
	cEvent m_StartupEvent;

	/** The thread on which hostname and ip address lookup is performed. */
	cNetworkLookup m_LookupThread;


	/** 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);

	/** Callback called by LibEvent when the event loop is started. */
	static void SignalizeStartup(evutil_socket_t a_Socket, short a_Events, void * a_Self);
};