Cuberite
A lightweight, fast and extensible game server for Minecraft
NetworkSingleton.h
Go to the documentation of this file.
1 
2 // NetworkSingleton.h
3 
4 // Declares the cNetworkSingleton class representing the storage for global data pertaining to network API
5 // such as a list of all connections, all listening sockets and the LibEvent dispatch thread.
6 
7 // This is an internal header, no-one outside OSSupport should need to include it; use Network.h instead;
8 // the only exception being the main app entrypoint that needs to call Terminate before quitting.
9 
10 
11 
12 
13 
14 #pragma once
15 
16 #include <event2/event.h>
17 #include "NetworkLookup.h"
18 #include "CriticalSection.h"
19 #include "Event.h"
20 
21 
22 
23 
24 
25 // fwd:
26 struct event_base;
27 class cTCPLink;
28 typedef std::shared_ptr<cTCPLink> cTCPLinkPtr;
29 typedef std::vector<cTCPLinkPtr> cTCPLinkPtrs;
30 class cServerHandle;
31 typedef std::shared_ptr<cServerHandle> cServerHandlePtr;
32 typedef std::vector<cServerHandlePtr> cServerHandlePtrs;
33 
34 
35 
36 
37 
39 {
40 public:
42  ~cNetworkSingleton() noexcept(false);
43 
45  static cNetworkSingleton & Get(void);
46 
49  void Initialise(void);
50 
54  void Terminate(void);
55 
57  event_base * GetEventBase(void) { return m_EventBase; }
58 
61 
64  void AddLink(const cTCPLinkPtr & a_Link);
65 
68  void RemoveLink(const cTCPLink * a_Link);
69 
73  void AddServer(const cServerHandlePtr & a_Server);
74 
77  void RemoveServer(const cServerHandle * a_Server);
78 
79 protected:
80 
82  event_base * m_EventBase;
83 
86 
89 
92 
94  std::atomic<bool> m_HasTerminated;
95 
97  std::thread m_EventLoopThread;
98 
101 
104 
105 
107  static void LogCallback(int a_Severity, const char * a_Msg);
108 
110  static void RunEventLoop(cNetworkSingleton * a_Self);
111 
113  static void SignalizeStartup(evutil_socket_t a_Socket, short a_Events, void * a_Self);
114 };
115 
116 
117 
118 
119 
120 
121 
122 
123 
std::shared_ptr< cTCPLink > cTCPLinkPtr
Definition: Network.h:25
std::vector< cTCPLinkPtr > cTCPLinkPtrs
Definition: Network.h:27
std::shared_ptr< cServerHandle > cServerHandlePtr
Definition: Network.h:28
std::vector< cServerHandlePtr > cServerHandlePtrs
Definition: Network.h:30
std::shared_ptr< cTCPLink > cTCPLinkPtr
std::vector< cTCPLinkPtr > cTCPLinkPtrs
std::shared_ptr< cServerHandle > cServerHandlePtr
std::vector< cServerHandlePtr > cServerHandlePtrs
Definition: Event.h:18
Interface that provides the methods available on a single TCP connection.
Definition: Network.h:42
Interface that provides the methods available on a listening server socket.
Definition: Network.h:155
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.
cNetworkLookup & GetLookupThread()
Returns the thread used to perform hostname and IP lookups.
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.
event_base * GetEventBase(void)
Returns the main LibEvent handle for event registering.
~cNetworkSingleton() noexcept(false)
cTCPLinkPtrs m_Connections
Container for all client connections, including ones with pending-connect.