You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
3.3 KiB

  1. #include "../includes/client.hpp"
  2. namespace betacore
  3. {
  4. Client::Client(MODE mode, int port, std::string address, std::function<void(SHAPE &S)> &update) : _mode(mode),
  5. _port(port),
  6. _address(address),
  7. _update(update)
  8. {
  9. this->start();
  10. }
  11. void Client::start()
  12. {
  13. std::cout
  14. << "Start"
  15. << "\nConnecting to: " << _address << ":" <<_port
  16. << std::endl;
  17. //Create a socket point
  18. _client_socket = socket(AF_INET, SOCK_STREAM, 0);
  19. _server = gethostbyname(_address.c_str());
  20. if (_client_socket < 0)
  21. {
  22. std::cout << "ERROR opening socket" << std::endl;
  23. return;
  24. }
  25. std::cout
  26. << "Socket Open"
  27. << std::endl;
  28. bzero((char *)&_server_address, sizeof(_server_address));
  29. _server_address.sin_family = AF_INET;
  30. std::cout
  31. << "AF_INT SET"
  32. << std::endl;
  33. bcopy((char *)_server->h_addr, (char *)&_server_address.sin_addr.s_addr, _server->h_length);
  34. _server_address.sin_port = htons(_port);
  35. // Now connect to the server
  36. if (connect(_client_socket, (struct sockaddr *)&_server_address, sizeof(_server_address)) < 0)
  37. {
  38. std::cout << "ERROR connecting" << std::endl;
  39. return;
  40. }
  41. std::cout
  42. << "Client Connected"
  43. << std::endl;
  44. }
  45. void Client::stop()
  46. {
  47. std::cout
  48. << "Stop"
  49. << std::endl;
  50. }
  51. void Client::kill()
  52. {
  53. std::cout
  54. << "Kill"
  55. << std::endl;
  56. this->_online = false;
  57. }
  58. bool Client::running()
  59. {
  60. return this->_running;
  61. }
  62. SHAPE Client::parse(const std::string &message)
  63. {
  64. std::stringstream stream(message);
  65. std::string tmp;
  66. std::vector<std::string> words;
  67. while (std::getline(stream, tmp, ':'))
  68. words.push_back(tmp);
  69. if (words.size() < 2)
  70. return NONE;
  71. tmp = words.at(1);
  72. SHAPE result = decode(tmp);
  73. if (result != UNKOWN)
  74. return result;
  75. if (_mode == EVE)
  76. return UNKOWN;
  77. return decode(crypt(tmp));
  78. }
  79. SHAPE Client::decode(const std::string &shape)
  80. {
  81. if (shape == "TRIANGLE")
  82. return TRIANGLE;
  83. if (shape == "CIRCLE")
  84. return CIRCLE;
  85. if (shape == "SQUARE")
  86. return SQUARE;
  87. if (shape == "PENTAGON")
  88. return PENTAGON;
  89. return UNKOWN;
  90. }
  91. std::string Client::crypt(const std::string &message)
  92. {
  93. std::string output = message;
  94. for (int i = 0; i < message.size(); i++)
  95. output[i] = message[i] ^ key;
  96. return output;
  97. }
  98. std::string Client::encode(const SHAPE &shape)
  99. {
  100. std::string message;
  101. switch (shape)
  102. {
  103. case TRIANGLE:
  104. message = "TRIANGLE";
  105. break;
  106. case CIRCLE:
  107. message = "CIRCLE";
  108. break;
  109. case SQUARE:
  110. message = "SQUARE";
  111. break;
  112. case PENTAGON:
  113. message = "PENTAGON";
  114. break;
  115. default:
  116. message = "UNKOWN";
  117. break;
  118. }
  119. return message;
  120. }
  121. void Client::send(bool encrypt, SHAPE shape)
  122. {
  123. std::string message = "SHAPE:" + encrypt ? crypt(encode(shape)) : encode(shape) + ":END";
  124. std::cout << "Sending Message: " << message << std::endl;
  125. char buffer[BUFFER_SIZE];
  126. bzero(buffer, BUFFER_SIZE);
  127. strcpy(buffer, message.c_str());
  128. if (write(_client_socket, buffer, strlen(buffer) < 0))
  129. {
  130. std::cout << "Failed send to server" << std::endl;
  131. }
  132. }
  133. void Client::listener()
  134. {
  135. while (_online)
  136. {
  137. // Now read server response
  138. char buffer[BUFFER_SIZE];
  139. bzero(buffer, BUFFER_SIZE);
  140. if (read(_client_socket, buffer, BUFFER_SIZE-1) < 0)
  141. {
  142. std::cout << "ERROR reading from socket" << std::endl;
  143. }
  144. std::string message(buffer);
  145. SHAPE shape = parse(buffer);
  146. _update(shape);
  147. }
  148. }
  149. } // namespace betacore