summaryrefslogtreecommitdiffstats
path: root/source/ListenThread.h
blob: 90523ea4f680006101222be010507099e6911eee (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

// ListenThread.h

// Declares the cListenThread class representing the thread that listens for client connections





#pragma once

#include "OSSupport/IsThread.h"
#include "OSSupport/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);
	~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<cSocket> cSockets;

	/// The callback which to notify of incoming connections	
	cCallback & m_Callback;
	
	/// 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;
} ;