Async 1.9.1
AsyncTcpServer.h
Go to the documentation of this file.
1
28
32
33
34#ifndef ASYNC_TCP_SERVER_INCLUDED
35#define ASYNC_TCP_SERVER_INCLUDED
36
37
38/****************************************************************************
39 *
40 * System Includes
41 *
42 ****************************************************************************/
43
44#include <string>
45#include <vector>
46#include <sigc++/sigc++.h>
47
48
49/****************************************************************************
50 *
51 * Project Includes
52 *
53 ****************************************************************************/
54
55#include <AsyncTcpServerBase.h>
56
57
58/****************************************************************************
59 *
60 * Local Includes
61 *
62 ****************************************************************************/
63
64
65
66/****************************************************************************
67 *
68 * Forward declarations
69 *
70 ****************************************************************************/
71
72
73
74/****************************************************************************
75 *
76 * Namespace
77 *
78 ****************************************************************************/
79
80namespace Async
81{
82
83/****************************************************************************
84 *
85 * Forward declarations of classes inside of the declared namespace
86 *
87 ****************************************************************************/
88
89class FdWatch;
90
91
92/****************************************************************************
93 *
94 * Defines & typedefs
95 *
96 ****************************************************************************/
97
98
99
100/****************************************************************************
101 *
102 * Exported Global Variables
103 *
104 ****************************************************************************/
105
106
107
108/****************************************************************************
109 *
110 * Class definitions
111 *
112 ****************************************************************************/
113
126template <typename ConT=TcpConnection>
128{
129 public:
135 TcpServer(const std::string& port_str,
136 const Async::IpAddress &bind_ip=IpAddress())
137 : TcpServerBase(port_str, bind_ip)
138 {
139 }
140
144 virtual ~TcpServer(void) {}
145
151 ConT *getClient(unsigned int index)
152 {
154 return dynamic_cast<ConT*>(con);
155 }
156
161 sigc::signal<void(ConT*)> clientConnected;
162
168 sigc::signal<void(ConT*, typename ConT::DisconnectReason)>
170
171 protected:
172 virtual void createConnection(int sock, const IpAddress& remote_addr,
173 uint16_t remote_port) override
174 {
175 ConT *con = new TcpServerConnection(*this, sock, remote_addr, remote_port);
176 addConnection(con);
177 }
178
179 virtual void emitClientConnected(TcpConnection *con_base) override
180 {
181 auto con = dynamic_cast<TcpServerConnection*>(con_base);
182 con->m_is_connected = true;
183 clientConnected(reinterpret_cast<ConT*>(con));
184 }
185
186 private:
187 struct TcpServerConnection : public ConT
188 {
189 TcpServerConnection(TcpServer& srv, int sock,
190 const IpAddress& remote_addr, uint16_t remote_port)
191 : ConT(sock, remote_addr, remote_port), m_srv(srv)
192 {
193 }
194 virtual TcpConnection& operator=(TcpConnection&& other_base) override
195 {
196 this->TcpConnection::operator=(std::move(other_base));
197 auto& other = dynamic_cast<TcpServerConnection&>(other_base);
198 m_is_connected = other.m_is_connected;
199 m_is_connected = false;
200 return *this;
201 }
202 virtual void onDisconnected(
203 typename ConT::DisconnectReason reason) override
204 {
205 m_srv.onDisconnected(this, reason);
206 m_is_connected = false;
207 ConT::onDisconnected(reason);
208 }
209
210 TcpServer& m_srv;
211 bool m_is_connected {false};
212 };
213
214 void onDisconnected(ConT *con_base, typename ConT::DisconnectReason reason)
215 {
216 auto con = dynamic_cast<TcpServerConnection*>(con_base);
217 assert(con != nullptr);
218 if (con->m_is_connected &&
220 {
221 clientDisconnected(con_base, reason);
222 }
223 removeConnection(con_base);
224 }
225
226}; /* class TcpServer */
227
228
229} /* namespace */
230
231#endif /* ASYNC_TCP_SERVER_INCLUDED */
232
233
234
235/*
236 * This file has not been truncated
237 */
238
239
240
The base class for creating a TCP server.
A class for watching file descriptors.
A class for representing an IP address in an OS independent way.
A class for handling exiting TCP connections.
@ DR_ORDERED_DISCONNECT
Disconnect ordered locally.
virtual TcpConnection & operator=(TcpConnection &&other)
Move assignmnt operator.
TcpConnection * getClient(unsigned int index)
Get the client object pointer from the server.
void removeConnection(TcpConnection *con)
void addConnection(TcpConnection *con)
TcpServerBase(const std::string &port_str, const Async::IpAddress &bind_ip)
Default constuctor.
A class for creating a TCP server.
virtual void createConnection(int sock, const IpAddress &remote_addr, uint16_t remote_port) override
sigc::signal< void(ConT *, typename ConT::DisconnectReason)> clientDisconnected
A signal that is emitted when a client disconnect from the server.
sigc::signal< void(ConT *)> clientConnected
A signal that is emitted when a client connect to the server.
virtual void emitClientConnected(TcpConnection *con_base) override
ConT * getClient(unsigned int index)
Get the client object pointer from the server.
virtual ~TcpServer(void)
Destructor.
TcpServer(const std::string &port_str, const Async::IpAddress &bind_ip=IpAddress())
Default constuctor.
Namespace for the asynchronous programming classes.