tcpServer.h
Go to the documentation of this file.
1 
7 #include <ctime>
8 #include <iostream>
9 #include <string>
10 #include <set>
11 #include <boost/bind/bind.hpp>
12 #include <boost/shared_ptr.hpp>
13 #include <boost/enable_shared_from_this.hpp>
14 #include <boost/asio.hpp>
15 #include <boost/function.hpp>
16 #include <boost/thread.hpp>
17 #include "dpyError.h"
18 
19 #define MAX_LENGHT 1024
20 
21 
26 class TcpConnection: public boost::enable_shared_from_this<TcpConnection>
27 {
28 private:
29  boost::function<void(const boost::system::error_code& error, const char* receiveData, std::string& sendData)> mReceiveSendHandler;
30  boost::function<void(const boost::system::error_code& error, std::size_t size)> mWriteStatusHandler;
31  boost::asio::ip::tcp::socket mSocket;
32 
33  bool mDisconnecting;
34  char mReadBuffer[MAX_LENGHT];
35 
36 private:
41  explicit TcpConnection(BOOST_IO_CONTEXT &io_context);
47  void handleWrite(const boost::system::error_code& error, size_t bytes_transferred);
53  void handleReceive(const boost::system::error_code& error, std::size_t bytes_transferred);
54 
55 public:
56 
60  typedef boost::shared_ptr<TcpConnection> pointer;
61 
67  static pointer create(BOOST_IO_CONTEXT& io_context)
68  {
69  return pointer(new TcpConnection(io_context));
70  }
71 
76  boost::asio::ip::tcp::socket& socket();
77 public:
78 
79 
84  bool isDisconnected();
88  void disconnect();
93  void write(std::string sendData);
97  void receive();
102  void setWriteStatusHandler(boost::function<void(const boost::system::error_code& error, std::size_t size)> writeStatusHandler);
107  void setReceiveSendHandler(boost::function<void(const boost::system::error_code& error, const char* receiveData, std::string& sendData)> receiveSendHandler);
108 
109 };
110 
117 {
118 private:
119 
120  boost::asio::io_context mIoContext;
121  boost::shared_ptr<boost::asio::ip::tcp::acceptor> mAcceptor;
122  boost::thread mBt;
123  boost::function<void(const boost::system::error_code& error, const char* receiveData, std::string& sendData)> mReceiveSendHandler;
124  boost::function<void(const boost::system::error_code& error, std::size_t size)> mWriteStatusHandler;
125 
126 private:
132  void handleAccept(const boost::system::error_code& error, boost::shared_ptr<TcpConnection> new_connection);
133 
137  std::set<TcpConnection::pointer> sConnections;
138 
139 public:
140 
144  void startAccept();
145 
152  TcpAsyncServer(boost::asio::io_context& io_context, std::string ip, unsigned short port);
153 
157  virtual ~TcpAsyncServer();
158 
163  void close(boost::system::error_code& rEc);
164 
168  void stopAll();
169 
174  void publishMsg(const std::string &message);
175 
180  void setWriteStatusHandler(boost::function<void(const boost::system::error_code& error, std::size_t size)> writeStatusHandler);
185  void setReceiveSendHandler(boost::function<void(const boost::system::error_code& error, const char* receiveData, std::string& sendData)> receiveSendHandler);
186 };
struct to keep a message from a sender
Definition: defTypes.h:24
void setWriteStatusHandler(boost::function< void(const boost::system::error_code &error, std::size_t size)> writeStatusHandler)
To set the write handler.
void disconnect()
Disconnect function for TCP socket.
boost::shared_ptr< TcpConnection > pointer
Pointer to TCP connection.
Definition: tcpServer.h:60
boost::asio::ip::tcp::socket & socket()
Boost TCP socket.
Connection class, responsible of send/receive data from any client or server. Regardless its communic...
Definition: tcpServer.h:26
#define MAX_LENGHT
Max length for read buffer.
Definition: tcpServer.h:19
void setReceiveSendHandler(boost::function< void(const boost::system::error_code &error, const char *receiveData, std::string &sendData)> receiveSendHandler)
To set the read handler.
TCP Server Class that allows to create a server to interact with send and receive operations...
Definition: tcpServer.h:116
void write(std::string sendData)
Function responsible of send the data through the socket.
static pointer create(BOOST_IO_CONTEXT &io_context)
Static function to create the boost share object of Connection related to an io context. It is one way to hide the Connection class only available for Server class.
Definition: tcpServer.h:67
bool isDisconnected()
returns if it is disconected
void receive()
Asynchronous receiving.