Cuberite
A lightweight, fast and extensible game server for Minecraft
ServerHandleImpl.h
Go to the documentation of this file.
1 
2 // ServerHandleImpl.h
3 
4 // Declares the cServerHandleImpl class implementing the TCP server functionality
5 
6 // This is an internal header, no-one outside OSSupport should need to include it; use Network.h instead
7 
8 
9 
10 
11 
12 #pragma once
13 
14 #include "Network.h"
15 #include <event2/listener.h>
16 #include "CriticalSection.h"
17 
18 
19 
20 
21 
22 // fwd:
23 class cTCPLinkImpl;
24 typedef std::shared_ptr<cTCPLinkImpl> cTCPLinkImplPtr;
25 typedef std::vector<cTCPLinkImplPtr> cTCPLinkImplPtrs;
26 class cServerHandleImpl;
27 typedef std::shared_ptr<cServerHandleImpl> cServerHandleImplPtr;
28 typedef std::vector<cServerHandleImplPtr> cServerHandleImplPtrs;
29 
30 
31 
32 
33 
35  public cServerHandle
36 {
38  friend class cTCPLinkImpl;
39 
40 public:
41 
43  virtual ~cServerHandleImpl() override;
44 
49  UInt16 a_Port,
50  cNetwork::cListenCallbacksPtr a_ListenCallbacks
51  );
52 
53  // cServerHandle overrides:
54  virtual void Close(void) override;
55  virtual bool IsListening(void) const override { return m_IsListening; }
56 
57 protected:
60 
62  evconnlistener * m_ConnListener;
63 
65  evconnlistener * m_SecondaryConnListener;
66 
69 
72 
75 
78 
81 
84 
85 
86 
90 
93  bool Listen(UInt16 a_Port);
94 
96  static void Callback(evconnlistener * a_Listener, evutil_socket_t a_Socket, sockaddr * a_Addr, int a_Len, void * a_Self);
97 
100  void RemoveLink(const cTCPLinkImpl * a_Link);
101 };
102 
103 
104 
105 
106 
unsigned short UInt16
Definition: Globals.h:158
std::shared_ptr< cTCPLinkImpl > cTCPLinkImplPtr
std::vector< cServerHandleImplPtr > cServerHandleImplPtrs
std::vector< cTCPLinkImplPtr > cTCPLinkImplPtrs
std::shared_ptr< cServerHandleImpl > cServerHandleImplPtr
std::string AString
Definition: StringUtils.h:11
Interface that provides the methods available on a listening server socket.
Definition: Network.h:155
std::shared_ptr< cListenCallbacks > cListenCallbacksPtr
Definition: Network.h:273
cServerHandleImpl(cNetwork::cListenCallbacksPtr a_ListenCallbacks)
Creates a new instance with the specified callbacks.
evconnlistener * m_ConnListener
The LibEvent handle representing the main listening socket.
static void Callback(evconnlistener *a_Listener, evutil_socket_t a_Socket, sockaddr *a_Addr, int a_Len, void *a_Self)
The callback called by LibEvent upon incoming connection.
evconnlistener * m_SecondaryConnListener
The LibEvent handle representing the secondary listening socket (only when side-by-side listening is ...
AString m_ErrorMsg
Contains the error message for the failure to listen.
cServerHandleImplPtr m_SelfPtr
The SharedPtr to self, so that it can be passed to created links.
virtual bool IsListening(void) const override
Returns true if the server has been started correctly and is currently listening for incoming connect...
cTCPLinkImplPtrs m_Connections
Container for all currently active connections on this server.
cCriticalSection m_CS
Mutex protecting m_Connections againt multithreaded access.
virtual void Close(void) override
Stops the server, no more incoming connections will be accepted.
static cServerHandleImplPtr Listen(UInt16 a_Port, cNetwork::cListenCallbacksPtr a_ListenCallbacks)
Creates a new server instance listening on the specified port.
virtual ~cServerHandleImpl() override
Closes the server, dropping all the connections.
bool m_IsListening
Set to true when the server is initialized successfully and is listening for incoming connections.
int m_ErrorCode
Contains the error code for the failure to listen.
cNetwork::cListenCallbacksPtr m_ListenCallbacks
The callbacks used to notify about incoming connections.
void RemoveLink(const cTCPLinkImpl *a_Link)
Removes the specified link from m_Connections.