Async  1.5.0
AsyncTcpServer.h
Go to the documentation of this file.
1 
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 
80 namespace Async
81 {
82 
83 /****************************************************************************
84  *
85  * Forward declarations of classes inside of the declared namespace
86  *
87  ****************************************************************************/
88 
89 class 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 
126 template <typename ConT=TcpConnection>
127 class TcpServer : public TcpServerBase
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)
174  {
175  ConT *con = new ConT(sock, remote_addr, remote_port);
176  con->disconnected.connect(
177  mem_fun(*this, &TcpServer<ConT>::onDisconnected));
178  addConnection(con);
179  clientConnected(con);
180  }
181 
182  private:
183  void onDisconnected(ConT *con, typename ConT::DisconnectReason reason)
184  {
185  clientDisconnected(con, reason);
186  removeConnection(con);
187  }
188 
189 }; /* class TcpServer */
190 
191 
192 } /* namespace */
193 
194 #endif /* ASYNC_TCP_SERVER_INCLUDED */
195 
196 
197 
198 /*
199  * This file has not been truncated
200  */
201 
202 
203 
A class for handling exiting TCP connections.
virtual ~TcpServer(void)
Destructor.
sigc::signal< void, ConT *, typename ConT::DisconnectReason > clientDisconnected
A signal that is emitted when a client disconnect from the server.
virtual void createConnection(int sock, const IpAddress &remote_addr, uint16_t remote_port)
TcpServer(const std::string &port_str, const Async::IpAddress &bind_ip=IpAddress())
Default constuctor.
The base class for creating a TCP server.
TcpConnection * getClient(unsigned int index)
Get the client object pointer from the server.
void removeConnection(TcpConnection *con)
sigc::signal< void, ConT * > clientConnected
A signal that is emitted when a client connect to the server.
A class for creating a TCP server.
void addConnection(TcpConnection *con)
Namespace for the asynchronous programming classes.
ConT * getClient(unsigned int index)
Get the client object pointer from the server.
A class for representing an IP address in an OS independent way.
The base class for creating a TCP server.