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.

136 lines
3.0 KiB

  1. #include "../includes/server.hpp"
  2. namespace betacore
  3. {
  4. Server::Server(int port)
  5. {
  6. this->port = port;
  7. std::cout << "And so it begins..." << std::endl;
  8. this->start();
  9. }
  10. void Server::start()
  11. {
  12. this->server_socket = socket(AF_INET, SOCK_STREAM, 0);
  13. if (this->server_socket < 0)
  14. {
  15. std::cout << "ERROR on opening socket" << std::endl;
  16. return;
  17. }
  18. std::cout << "Socket Connected" << std::endl;
  19. bzero((char *)&server_address, sizeof(server_address));
  20. server_address.sin_family = AF_INET;
  21. server_address.sin_addr.s_addr = INADDR_ANY;
  22. server_address.sin_port = htons(this->port);
  23. if (bind(
  24. server_socket,
  25. (struct sockaddr *)&server_address,
  26. sizeof(server_address)) < 0)
  27. {
  28. std::cout << "ERROR on binding " << std::endl;
  29. return;
  30. }
  31. std::cout << "Bound" << std::endl;
  32. online = true;
  33. server_running = true;
  34. std::cout << "Server is online" << std::endl;
  35. signal(SIGPIPE, SIG_IGN); // Prevents issue with sig kill on client
  36. std::thread listen_thread([this] { this->listener(); });
  37. listen_thread.detach();
  38. }
  39. void Server::listener()
  40. {
  41. std::cout << "Listener " << online << std::endl;
  42. struct sockaddr_in client_address;
  43. listen(this->server_socket, 10);
  44. while (online)
  45. {
  46. socklen_t client_socket_length = sizeof(client_address);
  47. int client_number =
  48. accept(server_socket, (struct sockaddr *)&client_address, &client_socket_length);
  49. this->clients.push_back(client_number);
  50. std::cout << "New Conenction" << std::endl;
  51. if (client_number < 0)
  52. {
  53. std::cout << "ERROR on accept" << std::endl;
  54. continue;
  55. }
  56. std::thread socket_thread([this, client_number] { this->read_socket(client_number); });
  57. socket_thread.detach();
  58. }
  59. this->shutdown();
  60. }
  61. void Server::read_socket(int client)
  62. {
  63. std::cout
  64. << "Ready To Read "
  65. << client
  66. << std::endl;
  67. int n;
  68. char buffer[BUFFER_LENGTH];
  69. while (online &&
  70. std::find(clients.begin(), clients.end(), client) != clients.end())
  71. {
  72. bzero(buffer, BUFFER_LENGTH);
  73. n = read(client, buffer, BUFFER_LENGTH - 1);
  74. if (n < 0)
  75. {
  76. std::cout << "ERROR reading from socket" << std::endl;
  77. break;
  78. }
  79. this->forward_message(client, buffer);
  80. }
  81. std::cout << "Closing Connection for " << client << std::endl;
  82. clients.erase(
  83. std::remove(clients.begin(), clients.end(), client), clients.end());
  84. close(client);
  85. }
  86. void Server::forward_message(int client, char buffer[BUFFER_LENGTH])
  87. {
  88. std::cout << "forward message form " << client << std::endl;
  89. int n;
  90. int error = 0;
  91. for (int c : clients)
  92. {
  93. if (c == client)
  94. continue;
  95. std::cout
  96. << "forward message form "
  97. << client
  98. << "to "
  99. << c
  100. << std::endl;
  101. n = write(c, buffer, strlen(buffer));
  102. if (n < 0)
  103. {
  104. std::cout << "ERROR Writing socket to " << c << std::endl;
  105. }
  106. }
  107. }
  108. void Server::shutdown()
  109. {
  110. std::cout << "Server is shuting down" << std::endl;
  111. std::cout << "Closing server socket" << std::endl;
  112. close(this->server_socket);
  113. server_running = false;
  114. }
  115. void Server::off()
  116. {
  117. this->online = false;
  118. }
  119. bool Server::running()
  120. {
  121. return this->server_running;
  122. }
  123. } // namespace betacore