tcpServer.h
Go to the documentation of this file.
1 
7 #include <ctime>
8 #include <iostream>
9 #include <string>
10 #include <boost/bind.hpp>
11 #include <boost/shared_ptr.hpp>
12 #include <boost/enable_shared_from_this.hpp>
13 #include <boost/asio.hpp>
14 #include <boost/function.hpp>
15 #include <boost/thread.hpp>
16 
17 #define MAX_LENGHT 1024
18 
19 
24 class TcpConnection: public boost::enable_shared_from_this<TcpConnection>
25 {
26 private:
27  boost::function<void(const boost::system::error_code& error, const char* receiveData, std::string& sendData)> mReceiveSendHandler;
28  boost::function<void(const boost::system::error_code& error, std::size_t size)> mWriteStatusHandler;
29  boost::asio::ip::tcp::socket mSocket;
30 
31  bool mDisconnecting;
32  char mReadBuffer[MAX_LENGHT];
33 
34 private:
39  explicit TcpConnection(boost::asio::io_context& io_context);
45  void handleWrite(const boost::system::error_code& error, size_t bytes_transferred);
51  void handleReceive(const boost::system::error_code& error, std::size_t bytes_transferred);
52 
53 public:
54 
58  typedef boost::shared_ptr<TcpConnection> pointer;
59 
65  static pointer create(boost::asio::io_context& io_context)
66  {
67  return pointer(new TcpConnection(io_context));
68  }
69 
74  boost::asio::ip::tcp::socket& socket();
75 public:
76 
77 
82  bool isDisconnected();
86  void disconnect();
91  void write(std::string sendData);
95  void receive();
100  void setWriteStatusHandler(boost::function<void(const boost::system::error_code& error, std::size_t size)> writeStatusHandler);
105  void setReceiveSendHandler(boost::function<void(const boost::system::error_code& error, const char* receiveData, std::string& sendData)> receiveSendHandler);
106 
107 };
108 
115 {
116 private:
117 
118  boost::asio::io_context mIoContext;
119  boost::shared_ptr<boost::asio::ip::tcp::acceptor> mAcceptor;
120  boost::thread mBt;
121  boost::function<void(const boost::system::error_code& error, const char* receiveData, std::string& sendData)> mReceiveSendHandler;
122  boost::function<void(const boost::system::error_code& error, std::size_t size)> mWriteStatusHandler;
123 
124 private:
130  void handleAccept(const boost::system::error_code& error, boost::shared_ptr<TcpConnection> new_connection);
131 
135  std::set<TcpConnection::pointer> sConnections;
136 
137 public:
138 
142  void startAccept();
143 
150  TcpAsyncServer(boost::asio::io_context& io_context, std::string ip, unsigned short port);
151 
155  virtual ~TcpAsyncServer();
156 
161  void close(boost::system::error_code& rEc);
162 
166  void stopAll();
167 
172  void publishMsg(const std::string &message);
173 
178  void setWriteStatusHandler(boost::function<void(const boost::system::error_code& error, std::size_t size)> writeStatusHandler);
183  void setReceiveSendHandler(boost::function<void(const boost::system::error_code& error, const char* receiveData, std::string& sendData)> receiveSendHandler);
184 };
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:58
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:24
#define MAX_LENGHT
Max length for read buffer.
Definition: tcpServer.h:17
static pointer create(boost::asio::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:65
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:114
void write(std::string sendData)
Function responsible of send the data through the socket.
bool isDisconnected()
returns if it is disconected
void receive()
Asynchronous receiving.