client.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include <client.hpp>
  2. #include <arpa/inet.h>
  3. #include <netdb.h>
  4. #include <string.h>
  5. #include <sys/socket.h>
  6. #include <sys/types.h>
  7. #include <unistd.h>
  8. #include <functional>
  9. #include <cstring>
  10. #include <QDebug>
  11. #include <QByteArray>
  12. #include <string>
  13. #include <iostream>
  14. #include <vector>
  15. #include <future>
  16. #include <headers/kmessage_codec.hpp>
  17. #include <headers/json.hpp>
  18. using namespace KData;
  19. using json = nlohmann::json;
  20. static const int MAX_BUFFER_SIZE = 2048;
  21. flatbuffers::FlatBufferBuilder builder(1024);
  22. /**
  23. * @brief Client::createMessageHandler
  24. * @param cb
  25. * @return
  26. */
  27. Client::MessageHandler Client::createMessageHandler(
  28. std::function<void()> cb) {
  29. return MessageHandler(cb);
  30. }
  31. /**
  32. * @brief Client::Client
  33. * @constructor
  34. * @param parent
  35. * @param count
  36. * @param arguments
  37. */
  38. Client::Client(QWidget *parent, int count, char** arguments) : QDialog(parent), argc(count), argv(arguments), m_client_socket_fd(-1) {}
  39. /**
  40. * @brief Client::~Client
  41. * @destructor
  42. */
  43. Client::~Client() {
  44. closeConnection();
  45. }
  46. /**
  47. * @brief Client::handleMessages
  48. */
  49. void Client::handleMessages() {
  50. char receive_buffer[2048];
  51. for (;;) {
  52. memset(receive_buffer, 0, 2048);
  53. ssize_t bytes_received = 0;
  54. bytes_received = recv(m_client_socket_fd, receive_buffer, 2048 - 2, 0);
  55. receive_buffer[2047] = 0;
  56. if (bytes_received == 0) {
  57. break;
  58. }
  59. emit Client::messageReceived(QString::fromUtf8(receive_buffer, 2048));
  60. }
  61. memset(receive_buffer, 0, 2048);
  62. ::close(m_client_socket_fd);
  63. }
  64. /**
  65. * @brief Client::start
  66. * @return A meaningless integer
  67. */
  68. void Client::start() {
  69. m_client_socket_fd = socket(AF_INET, SOCK_STREAM, 0);
  70. if (m_client_socket_fd != -1) {
  71. sockaddr_in server_socket;
  72. char* end;
  73. server_socket.sin_family = AF_INET;
  74. auto port_value = strtol(argv[2], &end, 10);
  75. if (port_value < 0 || end == argv[2]) {
  76. return;
  77. }
  78. int socket_option = 1;
  79. // Free up the port to begin listening again
  80. setsockopt(m_client_socket_fd, SOL_SOCKET, SO_REUSEADDR, &socket_option,
  81. sizeof(socket_option));
  82. server_socket.sin_port = htons(port_value);
  83. inet_pton(AF_INET, argv[1], &server_socket.sin_addr.s_addr);
  84. if (::connect(m_client_socket_fd, reinterpret_cast<sockaddr*>(&server_socket),
  85. sizeof(server_socket)) != -1) {
  86. std::function<void()> message_send_fn = [this]() {
  87. this->handleMessages();
  88. };
  89. MessageHandler message_handler = createMessageHandler(message_send_fn);
  90. // Handle received messages on separate thread
  91. std::thread (message_handler).detach();
  92. } else {
  93. qDebug() << errno;
  94. ::close(m_client_socket_fd);
  95. }
  96. }
  97. }
  98. /**
  99. * @brief Client::sendMessage
  100. * @param s[in] <const QString&> The message to send
  101. */
  102. void Client::sendMessage(const QString& s) {
  103. if (m_client_socket_fd != -1) {
  104. // Convert QString to std byte vector
  105. // TODO: switch to using one or the other completely
  106. json data_json{};
  107. data_json["type"] = "custom";
  108. data_json["program"] = "placeholder";
  109. data_json["message"] = s.toUtf8().data();
  110. std::string json_string = data_json.dump();
  111. std::vector<uint8_t> fb_byte_vector{json_string.begin(), json_string.end()};
  112. // for (int i = 0; i < q_byte_array.size(); i++) {
  113. // fb_byte_vector.push_back(static_cast<uint8_t>(q_byte_array.at(i)));
  114. // }
  115. auto byte_vector = builder.CreateVector(fb_byte_vector);
  116. auto message = CreateMessage(builder, 69, byte_vector);
  117. builder.Finish(message);
  118. uint8_t* encoded_message_buffer = builder.GetBufferPointer();
  119. uint32_t size = builder.GetSize();
  120. qDebug() << "Size is " << size;
  121. uint8_t send_buffer[MAX_BUFFER_SIZE];
  122. memset(send_buffer, 0, MAX_BUFFER_SIZE);
  123. send_buffer[0] = (size & 0xFF) >> 24;
  124. send_buffer[1] = (size & 0xFF) >> 16;
  125. send_buffer[2] = (size & 0xFF) >> 8;
  126. send_buffer[3] = (size & 0xFF);
  127. std::memcpy(send_buffer + 4, encoded_message_buffer, size);
  128. qDebug() << "Ready to send:";
  129. std::string message_to_send{};
  130. for (unsigned int i = 0; i < (size + 4); i++) {
  131. message_to_send += (char)*(send_buffer + i);
  132. }
  133. qDebug() << message_to_send.c_str();
  134. ::send(m_client_socket_fd, send_buffer, size + 4, 0);
  135. memset(send_buffer, 0, MAX_BUFFER_SIZE);
  136. builder.Clear();
  137. } else {
  138. qDebug() << "You must first open a connection";
  139. }
  140. }
  141. void Client::closeConnection() {
  142. if (m_client_socket_fd != -1) {
  143. ::close(m_client_socket_fd);
  144. m_client_socket_fd = -1;
  145. }
  146. }