2015-01-18 05:57:16 -05:00
|
|
|
|
|
|
|
// ServerHandleImpl.h
|
|
|
|
|
|
|
|
// Declares the cServerHandleImpl class implementing the TCP server functionality
|
|
|
|
|
|
|
|
// This is an internal header, no-one outside OSSupport should need to include it; use Network.h instead
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "Network.h"
|
|
|
|
#include <event2/listener.h>
|
|
|
|
#include "CriticalSection.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// fwd:
|
|
|
|
class cTCPLinkImpl;
|
2017-07-20 07:19:18 -04:00
|
|
|
typedef std::shared_ptr<cTCPLinkImpl> cTCPLinkImplPtr;
|
2015-01-18 05:57:16 -05:00
|
|
|
typedef std::vector<cTCPLinkImplPtr> cTCPLinkImplPtrs;
|
|
|
|
class cServerHandleImpl;
|
2017-07-20 07:19:18 -04:00
|
|
|
typedef std::shared_ptr<cServerHandleImpl> cServerHandleImplPtr;
|
2015-01-18 05:57:16 -05:00
|
|
|
typedef std::vector<cServerHandleImplPtr> cServerHandleImplPtrs;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class cServerHandleImpl:
|
|
|
|
public cServerHandle
|
|
|
|
{
|
2020-04-13 12:38:06 -04:00
|
|
|
using Super = cServerHandle;
|
2015-01-18 05:57:16 -05:00
|
|
|
friend class cTCPLinkImpl;
|
|
|
|
|
|
|
|
public:
|
2020-04-13 12:38:06 -04:00
|
|
|
|
2015-01-18 05:57:16 -05:00
|
|
|
/** Closes the server, dropping all the connections. */
|
2017-05-20 02:16:28 -04:00
|
|
|
virtual ~cServerHandleImpl() override;
|
2015-01-18 05:57:16 -05:00
|
|
|
|
|
|
|
/** Creates a new server instance listening on the specified port.
|
|
|
|
Both IPv4 and IPv6 interfaces are used, if possible.
|
|
|
|
Always returns a server instance; in the event of a failure, the instance holds the error details. Use IsListening() to query success. */
|
|
|
|
static cServerHandleImplPtr Listen(
|
|
|
|
UInt16 a_Port,
|
2015-01-21 15:12:11 -05:00
|
|
|
cNetwork::cListenCallbacksPtr a_ListenCallbacks
|
2015-01-18 05:57:16 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
// cServerHandle overrides:
|
|
|
|
virtual void Close(void) override;
|
|
|
|
virtual bool IsListening(void) const override { return m_IsListening; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/** The callbacks used to notify about incoming connections. */
|
|
|
|
cNetwork::cListenCallbacksPtr m_ListenCallbacks;
|
|
|
|
|
|
|
|
/** The LibEvent handle representing the main listening socket. */
|
|
|
|
evconnlistener * m_ConnListener;
|
|
|
|
|
|
|
|
/** The LibEvent handle representing the secondary listening socket (only when side-by-side listening is needed, such as WinXP). */
|
|
|
|
evconnlistener * m_SecondaryConnListener;
|
|
|
|
|
|
|
|
/** Set to true when the server is initialized successfully and is listening for incoming connections. */
|
|
|
|
bool m_IsListening;
|
|
|
|
|
|
|
|
/** Container for all currently active connections on this server. */
|
|
|
|
cTCPLinkImplPtrs m_Connections;
|
|
|
|
|
|
|
|
/** Mutex protecting m_Connections againt multithreaded access. */
|
|
|
|
cCriticalSection m_CS;
|
|
|
|
|
|
|
|
/** Contains the error code for the failure to listen. Only valid for non-listening instances. */
|
|
|
|
int m_ErrorCode;
|
|
|
|
|
|
|
|
/** Contains the error message for the failure to listen. Only valid for non-listening instances. */
|
|
|
|
AString m_ErrorMsg;
|
|
|
|
|
2015-01-23 17:01:18 -05:00
|
|
|
/** The SharedPtr to self, so that it can be passed to created links. */
|
|
|
|
cServerHandleImplPtr m_SelfPtr;
|
|
|
|
|
2015-01-18 05:57:16 -05:00
|
|
|
|
|
|
|
|
|
|
|
/** Creates a new instance with the specified callbacks.
|
|
|
|
Initializes the internals, but doesn't start listening yet. */
|
2015-01-21 15:12:11 -05:00
|
|
|
cServerHandleImpl(cNetwork::cListenCallbacksPtr a_ListenCallbacks);
|
2015-01-18 05:57:16 -05:00
|
|
|
|
|
|
|
/** Starts listening on the specified port.
|
|
|
|
Returns true if successful, false on failure. On failure, sets m_ErrorCode and m_ErrorMsg. */
|
|
|
|
bool Listen(UInt16 a_Port);
|
|
|
|
|
|
|
|
/** The callback called by LibEvent upon incoming connection. */
|
|
|
|
static void Callback(evconnlistener * a_Listener, evutil_socket_t a_Socket, sockaddr * a_Addr, int a_Len, void * a_Self);
|
|
|
|
|
|
|
|
/** Removes the specified link from m_Connections.
|
|
|
|
Called by cTCPLinkImpl when the link is terminated. */
|
|
|
|
void RemoveLink(const cTCPLinkImpl * a_Link);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|