From bc8ad9d1b1ce4a1561e2759c15bd1cf2d4322dc1 Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Tue, 5 Mar 2013 20:47:29 +0000 Subject: Made ListenThread an OSSupport part, as it's generic enough git-svn-id: http://mc-server.googlecode.com/svn/trunk@1258 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- source/OSSupport/ListenThread.h | 78 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 source/OSSupport/ListenThread.h (limited to 'source/OSSupport/ListenThread.h') diff --git a/source/OSSupport/ListenThread.h b/source/OSSupport/ListenThread.h new file mode 100644 index 000000000..952cc8a3f --- /dev/null +++ b/source/OSSupport/ListenThread.h @@ -0,0 +1,78 @@ + +// ListenThread.h + +// Declares the cListenThread class representing the thread that listens for client connections + + + + + +#pragma once + +#include "IsThread.h" +#include "Socket.h" + + + + + +// fwd: +class cServer; + + + + + +class cListenThread : + public cIsThread +{ + typedef cIsThread super; + +public: + /// Used as the callback for connection events + class cCallback + { + public: + /// This callback is called whenever a socket connection is accepted + virtual void OnConnectionAccepted(cSocket & a_Socket) = 0; + } ; + + cListenThread(cCallback & a_Callback, cSocket::eFamily a_Family); + ~cListenThread(); + + /// Creates all the sockets, returns trus if successful, false if not. + bool Initialize(const AString & a_PortsString); + + bool Start(void); + + void Stop(void); + + /// Call before Initialize() to set the "reuse" flag on the sockets + void SetReuseAddr(bool a_Reuse = true); + +protected: + typedef std::vector cSockets; + + /// The callback which to notify of incoming connections + cCallback & m_Callback; + + /// Socket address family to use + cSocket::eFamily m_Family; + + /// Sockets that are being monitored + cSockets m_Sockets; + + bool m_ShouldReuseAddr; + + /** Fills in m_Sockets with individual sockets, each for one port specified in a_PortsString. + Returns true if successful and at least one socket has been created + */ + bool CreateSockets(const AString & a_PortsString); + + // cIsThread override: + virtual void Execute(void) override; +} ; + + + + -- cgit v1.2.3