AppChat  0.5.0
TCP client-server caht application with boost::asio library.
connection.h
1 #ifndef CONNECTION_H
2 #define CONNECTION_H
3 
4 #include <memory>
5 #include <deque>
6 #include <mutex>
7 #include "server/connection/isubscriber.h"
8 #include "server/channel/channels_manager.h"
9 #include "protocol/protocol.h"
10 #include "server/log/logger.h"
11 
16 class Connection : public ISubscriber, public std::enable_shared_from_this<Connection>
17 {
18 public:
24  explicit Connection(boost::asio::ip::tcp::socket&& _socket, database_ptr _db):
25  socket(std::move(_socket)),
26  db(_db),
27  busy(true)
28 
29  {
30  BOOST_LOG_TRIVIAL(info) << "new connection from " << socket.remote_endpoint().address().to_string()
31  << ":" << socket.remote_endpoint().port();
32  }
33 
38  void reuse(boost::asio::ip::tcp::socket&& _socket) override;
39 
43  virtual void start() override {
44  BOOST_LOG_TRIVIAL(info) << "Connection start read_request_header().";
45  read_request_header();
46  }
47 
52  virtual void sendme(text_response_ptr response) override;
53 
59  virtual identifier_t get_client_id() const override {
60  return client_id;
61  }
62 
68  virtual const std::string& get_login() const override { return login; }
69 
70  virtual bool is_busy() const noexcept override { return busy; }
71  virtual void set_busy(bool flag = true) noexcept override { busy = flag; }
72 
73  virtual void free_connection() override;
74 
75  ~Connection() {
76  free_connection();
77  }
78 private:
79 
80  boost::asio::ip::tcp::socket socket;
81  std::mutex mtx_sock;
82  std::deque<response_ptr> packets_to_client;
83 
84  identifier_t client_id;
85  std::string login;
86  std::string password;
87 
88  database_ptr db;
89  std::atomic<bool> busy;
90 
91 private:
97  void read_request_header();
98 
102  void read_request_body(registr_request_ptr);
103 
107  void read_request_body(autor_request_ptr);
108 
112  void read_request_body(text_request_ptr);
113 
117  void read_request_body(join_room_request_ptr);
118 
124  void send_response_header();
125 
129  void send_response_data();
130 
136  identifier_t generate_client_id() {
137  static identifier_t id = 0;
138  return ++id;
139  }
140 };
141 
142 using connection_ptr = std::shared_ptr<Connection>;
143 
144 #endif // CONNECTION_H
virtual void start() override
Entry point to handle incoming requests.
Definition: connection.h:43
void reuse(boost::asio::ip::tcp::socket &&_socket) override
reuse connection for Object Pool
Definition: connection.cpp:3
Channel Subscriber Interface.
Definition: isubscriber.h:13
virtual const std::string & get_login() const override
Get the login.
Definition: connection.h:68
virtual void sendme(text_response_ptr response) override
Send response message to the client.
Definition: connection.cpp:41
Connection class.
Definition: connection.h:16
Connection(boost::asio::ip::tcp::socket &&_socket, database_ptr _db)
Construct a new Connection object.
Definition: connection.h:24
virtual identifier_t get_client_id() const override
Get the client id object.
Definition: connection.h:59