Async 1.8.0
AsyncTcpServerBase.h
Go to the documentation of this file.
1
27#ifndef ASYNC_TCP_SERVER_BASE_INCLUDED
28#define ASYNC_TCP_SERVER_BASE_INCLUDED
29
30
31/****************************************************************************
32 *
33 * System Includes
34 *
35 ****************************************************************************/
36
37#include <string>
38#include <vector>
39#include <map>
40#include <sigc++/sigc++.h>
41
42
43/****************************************************************************
44 *
45 * Project Includes
46 *
47 ****************************************************************************/
48
49#include <AsyncTimer.h>
50#include <AsyncTcpConnection.h>
51#include <AsyncSslContext.h>
52
53
54/****************************************************************************
55 *
56 * Local Includes
57 *
58 ****************************************************************************/
59
60
61
62/****************************************************************************
63 *
64 * Forward declarations
65 *
66 ****************************************************************************/
67
68
69
70/****************************************************************************
71 *
72 * Namespace
73 *
74 ****************************************************************************/
75
76namespace Async
77{
78
79/****************************************************************************
80 *
81 * Forward declarations of classes inside of the declared namespace
82 *
83 ****************************************************************************/
84
85class FdWatch;
86
87
88/****************************************************************************
89 *
90 * Defines & typedefs
91 *
92 ****************************************************************************/
93
94
95
96/****************************************************************************
97 *
98 * Exported Global Variables
99 *
100 ****************************************************************************/
101
102
103
104/****************************************************************************
105 *
106 * Class definitions
107 *
108 ****************************************************************************/
109
115class TcpServerBase : public sigc::trackable
116{
117 public:
123 TcpServerBase(const std::string& port_str,
124 const Async::IpAddress &bind_ip);
125
129 virtual ~TcpServerBase(void);
130
136
142 TcpConnection *getClient(unsigned int index);
143
150 int writeAll(const void *buf, int count);
151
159 int writeOnly(TcpConnection *con, const void *buf, int count);
160
168 int writeExcept(TcpConnection *con, const void *buf, int count);
169
185
205 void setConnectionThrottling(unsigned bucket_max, float bucket_inc,
206 int inc_interval_ms);
207
208 protected:
209 virtual void createConnection(int sock, const IpAddress& remote_addr,
210 uint16_t remote_port) = 0;
213 virtual void emitClientConnected(TcpConnection *con) = 0;
214
215 private:
216 using TcpConnectionList = std::vector<TcpConnection*>;
217 struct ConThrotItem
218 {
219 ConThrotItem(float bucket_max) : m_bucket(bucket_max) {}
220 float m_bucket;
221 TcpConnectionList m_pending_connections;
222 };
223 using ConThrotMap = std::map<IpAddress, ConThrotItem>;
224
225 int m_sock;
226 FdWatch* m_rd_watch;
227 TcpConnectionList m_tcpConnectionList;
228 SslContext* m_ssl_ctx = nullptr;
229
230 ConThrotMap m_con_throt_map;
231 Timer m_con_throt_timer;
232 unsigned m_con_throt_bucket_max = 0;
233 unsigned m_con_throt_bucket_inc = 0;
234
235 void cleanup(void);
236 void onConnection(FdWatch *watch);
237 void updateConnThrotMap(Timer*);
238
239}; /* class TcpServerBase */
240
241
242} /* namespace */
243
244#endif /* ASYNC_TCP_SERVER_BASE_INCLUDED */
245
246
247
248/*
249 * This file has not been truncated
250 */
251
252
253
SSL context meant to be used with TcpConnection and friends.
Contains a class for handling exiting TCP connections.
Contains a single shot or periodic timer that emits a signal on timeout.
A class for watching file descriptors.
A class for representing an IP address in an OS independent way.
SSL context meant to be used with TcpConnection and friends.
A class for handling exiting TCP connections.
The base class for creating a TCP server.
int writeAll(const void *buf, int count)
Write data to all connected clients.
virtual void emitClientConnected(TcpConnection *con)=0
virtual void createConnection(int sock, const IpAddress &remote_addr, uint16_t remote_port)=0
int numberOfClients(void)
Get the number of clients that is connected to the server.
void setConnectionThrottling(unsigned bucket_max, float bucket_inc, int inc_interval_ms)
Enable connection throttling.
TcpConnection * getClient(unsigned int index)
Get the client object pointer from the server.
void removeConnection(TcpConnection *con)
int writeExcept(TcpConnection *con, const void *buf, int count)
Send data to all connected clients except the given client.
int writeOnly(TcpConnection *con, const void *buf, int count)
Send data only to the given client.
void addConnection(TcpConnection *con)
virtual ~TcpServerBase(void)
Destructor.
TcpServerBase(const std::string &port_str, const Async::IpAddress &bind_ip)
Default constuctor.
void setSslContext(SslContext &ctx)
Set the SSL context for all new connections.
A class that produces timer events.
Definition AsyncTimer.h:117
Namespace for the asynchronous programming classes.