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.

115 lines
2.5 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(server_socket, (struct sockaddr *)&server_address, sizeof(server_address)) < 0)
  24. {
  25. std::cout << "ERROR on binding "<< std::endl;
  26. return;
  27. }
  28. std::cout << "Bound"<< std::endl;
  29. online = true;
  30. server_running=true;
  31. std::cout << "Server is online"<< std::endl;
  32. signal(SIGPIPE,SIG_IGN);
  33. std::thread listen_thread([this] { this->listener(); } );
  34. listen_thread.detach();
  35. }
  36. void Server::listener()
  37. {
  38. std::cout << "Listener "<< online << std::endl;
  39. struct sockaddr_in client_address;
  40. listen(this->server_socket, 10);
  41. while (online) {
  42. socklen_t client_socket_length = sizeof(client_address);
  43. int client_number =
  44. accept(server_socket, (struct sockaddr *)&client_address, &client_socket_length);
  45. this->clients.push_back(client_number);
  46. std::cout << "New Conenction"<< std::endl;
  47. if (client_number < 0)
  48. {
  49. std::cout << "ERROR on accept"<< std::endl;
  50. continue;
  51. }
  52. std::thread socket_thread([this,client_number]{this->read_socket(client_number);});
  53. socket_thread.detach();
  54. }
  55. this->shutdown();
  56. }
  57. void Server::read_socket(int client)
  58. {
  59. std::cout
  60. << "Ready To Read "
  61. << client
  62. << std::endl;
  63. int n;
  64. char buffer[1024];
  65. while (online &&
  66. std::find(clients.begin(), clients.end(), client) != clients.end())
  67. {
  68. bzero(buffer, 1024);
  69. n = read(client, buffer, 1024-1);
  70. if (n < 0)
  71. {
  72. std::cout << "ERROR reading from socket"<< std::endl;
  73. break;
  74. }
  75. printf("Here is the message: %s\n", buffer);
  76. n = write(client, "I got your mesage\n", 18);
  77. if (n < 0)
  78. {
  79. std::cout << "ERROR Writing socket"<< std::endl;
  80. break;
  81. }
  82. }
  83. clients.erase(
  84. std::remove(clients.begin(), clients.end(), client), clients.end());
  85. close(client);
  86. }
  87. void Server::shutdown()
  88. {
  89. std::cout << "Server is shuting down"<< std::endl;
  90. std::cout << "Closing server socket" << std::endl;
  91. close(this->server_socket);
  92. server_running = false;
  93. }
  94. void Server::off(){
  95. this-> online = false;
  96. }
  97. bool Server::running(){
  98. return this->server_running;
  99. }
  100. } // namespace betacore