FREEDM DGI
CTcpServer.hpp
Go to the documentation of this file.
1 
23 #ifndef C_TCP_SERVER_HPP
24 #define C_TCP_SERVER_HPP
25 
26 #include <boost/asio.hpp>
27 #include <boost/function.hpp>
28 #include <boost/shared_ptr.hpp>
29 #include <boost/noncopyable.hpp>
30 
31 namespace freedm {
32 namespace broker {
33 namespace device {
34 
42  : private boost::noncopyable
43 {
44 public:
46  typedef boost::shared_ptr<CTcpServer> Pointer;
47 
49  typedef boost::shared_ptr<boost::asio::ip::tcp::socket> Connection;
50 
52  typedef boost::function<void ()> ConnectionHandler;
53 
55  virtual ~CTcpServer();
56 
58  static Pointer Create(boost::asio::io_service & ios, unsigned short port,
59  const std::string address="");
60 
62  void RegisterHandler(ConnectionHandler h);
63 
65  void Stop();
66 
68  void StartAccept();
69 
71  Connection GetClient() { return m_client; }
72 private:
74  CTcpServer(boost::asio::io_service & ios, unsigned short port,
75  const std::string address="");
76 
78  void HandleAccept(const boost::system::error_code & error);
79 
81  std::string hdr() const;
82 
84  boost::asio::ip::tcp::acceptor m_acceptor;
85 
87  unsigned short m_port;
88 
90  ConnectionHandler m_handler;
91 protected:
93  Connection m_client;
94 };
95 
96 } // namespace device
97 } // namespace broker
98 } // namespace freedm
99 
100 #endif // C_TCP_SERVER_HPP
TCP server that handles a single client connection.
Definition: CTcpServer.hpp:41
boost::asio::ip::tcp::acceptor m_acceptor
Acceptor for new client connections.
Definition: CTcpServer.hpp:84
void Stop()
Stops the TCP server.
Definition: CTcpServer.cpp:133
unsigned short m_port
Port number of the server.
Definition: CTcpServer.hpp:87
void HandleAccept(const boost::system::error_code &error)
Handles an accepted client connection.
Definition: CTcpServer.cpp:212
ConnectionHandler m_handler
Callback function to handle clients.
Definition: CTcpServer.hpp:90
Connection m_client
Socket for the current client.
Definition: CTcpServer.hpp:93
static Pointer Create(boost::asio::io_service &ios, unsigned short port, const std::string address="")
Creates a new TCP server on the specified port number.
Definition: CTcpServer.cpp:117
boost::function< void()> ConnectionHandler
Type of the callback function for client connections.
Definition: CTcpServer.hpp:52
std::string hdr() const
Gets a log header.
Definition: CTcpServer.cpp:241
CTcpServer(boost::asio::io_service &ios, unsigned short port, const std::string address="")
Constructs the TCP server on the specified port number.
Definition: CTcpServer.cpp:65
void RegisterHandler(ConnectionHandler h)
Registers a callback function for client connections.
Definition: CTcpServer.cpp:157
void StartAccept()
Prepares to accept the next client.
Definition: CTcpServer.cpp:184
boost::shared_ptr< CTcpServer > Pointer
Convenience type for a shared pointer to self.
Definition: CTcpServer.hpp:46
General FREEDM Namespace.
Definition: CBroker.cpp:53
Connection GetClient()
Gets the current client.
Definition: CTcpServer.hpp:71
boost::shared_ptr< boost::asio::ip::tcp::socket > Connection
Convenient type for the client socket.
Definition: CTcpServer.hpp:49
virtual ~CTcpServer()
Virtual destructor for derived classes.
Definition: CTcpServer.cpp:99