AppChat  0.5.0
TCP client-server caht application with boost::asio library.
transport_response.h
1 #ifndef TRANSPORT_RESPONSE_H
2 #define TRANSPORT_RESPONSE_H
3 
4 #include "response.h"
5 
6 class TransportResponse : public Response {
7 public:
9  std::memcpy(header, &PROTOCOL_VERS, Block::VersionProtocol);
10  }
11 
12  virtual const void* get_data() const override { return __data; }
13  virtual void* get_data() override { return __data; }
14 
15  virtual const char* get_login() const {
16  return __data;
17  }
18 
19  virtual identifier_t get_roomid() const {
26  identifier_t roomid;
27  std::memcpy(&roomid, __data + Block::LoginName, sizeof(identifier_t));
28  return roomid;
29  }
30 
31  virtual uint32_t get_length_data() const override { return LengthResponse;}
32 
33 protected:
34  static constexpr auto LengthResponse = Block::LoginName + Block::RoomId + Block::TextMessage;
35  char __data[LengthResponse];
36 };
37 
39 public:
40  TextResponse(identifier_t roomid = 0) {
41  //std::memcpy(header, &PROTOCOL_VERS, Block::VersionProtocol);
42  std::memcpy(header+Block::VersionProtocol, &type_Response, Block::Command);
43 
44  std::memcpy(__data+Block::LoginName, &roomid, Block::RoomId);
45  }
46 
47  TextResponse(const std::string& login, const std::string& text, identifier_t roomid = 0) {
48  //std::memcpy(header, &PROTOCOL_VERS, Block::VersionProtocol);
49  std::memcpy(header+Block::VersionProtocol, &type_Response, Block::Command);
50  std::snprintf(__data, Block::LoginName, "%s", login.data());
51  std::memcpy(__data+Block::LoginName, &roomid, Block::RoomId);
52  std::snprintf(__data+Block::LoginName + Block::RoomId, Block::TextMessage, "%s", text.data());
53  }
54 
55  TextResponse(const std::string& login, DateTime dt, const std::string& text, identifier_t roomid = 0) {
56  //std::memcpy(header, &PROTOCOL_VERS, Block::VersionProtocol);
57  std::memcpy(header+Block::VersionProtocol, &type_Response, Block::Command);
58  std::memcpy(header+Block::VersionProtocol+Block::Command, &dt, Block::Datetime);
59  std::snprintf(__data, Block::LoginName, "%s", login.data());
60  std::memcpy(__data+Block::LoginName, &roomid, Block::RoomId);
61  std::snprintf(__data+Block::LoginName + Block::RoomId, Block::TextMessage, "%s", text.data());
62  }
63 
64  TextResponse(response_ptr response) {
65  std::memcpy(header, response->get_header(), Block::Header);
66 
67  std::memset(__data+Block::LoginName, 0, Block::RoomId);
68  }
69 
70  const char* get_message() const {
71  return __data+Block::LoginName+Block::RoomId;
72  }
73 private:
74  const TypeCommand type_Response = TypeCommand::EchoResponse;
75 };
76 
77 using transport_response_ptr = std::shared_ptr<TransportResponse>;
78 using text_response_ptr = std::shared_ptr<TextResponse>;
79 
80 #endif // TRANSPORT_RESPONSE_H
Definition: response.h:6
Definition: transport_response.h:38
virtual identifier_t get_roomid() const
Definition: transport_response.h:19
Definition: transport_response.h:6
Definition: datetime.h:6