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.

114 lines
2.6 KiB

  1. #include "../includes/server.hpp"
  2. #include <iostream>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. namespace betacore
  8. {
  9. Server::Server(int port)
  10. {
  11. this->port = port;
  12. std::cout << "And so it begins..." << std::endl;
  13. this->start();
  14. }
  15. void Server::start()
  16. {
  17. this->server_socket = socket(AF_INET, SOCK_STREAM, 0);
  18. if (this->server_socket < 0)
  19. {
  20. std::cout << "ERROR on opening socket"<< std::endl;
  21. return;
  22. }
  23. std::cout << "Socket Connected"<< std::endl;
  24. bzero((char *)&server_address, sizeof(server_address));
  25. server_address.sin_family = AF_INET;
  26. server_address.sin_addr.s_addr = INADDR_ANY;
  27. server_address.sin_port = htons(this->port);
  28. if (bind(server_socket, (struct sockaddr *)&server_address, sizeof(server_address)) < 0)
  29. {
  30. std::cout << "ERROR on binding "<< std::endl;
  31. return;
  32. }
  33. std::cout << "Bound"<< std::endl;
  34. online = true;
  35. server_running=true;
  36. std::cout << "Server is online"<< std::endl;
  37. std::thread listen_thread([this] { this->listener(); } );
  38. listen_thread.detach();
  39. }
  40. void Server::listener()
  41. {
  42. std::cout << "Listener "<< online << std::endl;
  43. while (online) {
  44. struct sockaddr_in client_address;
  45. listen(this->server_socket, 5);
  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->client_socket_collection.push_back(client_address);
  50. this->clients.push_back(client_number);
  51. std::cout << "New Conenction"<< std::endl;
  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. while (online)
  65. {
  66. if (client < 0)
  67. {
  68. std::cout << "ERROR on accept"<< std::endl;
  69. }
  70. bzero(buffer, 256);
  71. n = read(client, buffer, 1024);
  72. if (n < 0)
  73. {
  74. std::cout << "ERROR reading from socket"<< std::endl;
  75. }
  76. printf("Here is the message: %s\n", buffer);
  77. n = write(client, "I got your mesage\n", 18);
  78. if (n < 0)
  79. {
  80. std::cout << "ERROR Writing socket"<< std::endl;
  81. }
  82. }
  83. }
  84. void Server::shutdown()
  85. {
  86. std::cout << "Server is shuting down"<< std::endl;
  87. for (auto socket : clients){
  88. std::cout
  89. << "Closing new socket :: "
  90. << socket
  91. << std::endl;
  92. close(socket);
  93. }
  94. std::cout << "Closing server socket" << std::endl;
  95. close(this->server_socket);
  96. server_running = false;
  97. }
  98. void Server::off(){
  99. this-> online = false;
  100. }
  101. bool Server::running(){
  102. return this->server_running;
  103. }
  104. } // namespace betacore