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.

81 lines
1.2 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. #ifndef __BETACORE_SERVER_HPP__
  2. #define __BETACORE_SERVER_HPP__
  3. #include <vector>
  4. #include <algorithm>
  5. #include <iostream>
  6. #include <sys/types.h>
  7. #include <signal.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <thread>
  12. #include <utility>
  13. #include <functional>
  14. #include "common.hpp"
  15. // LINUX SYSTEM CALLS :)
  16. #include <sys/socket.h>
  17. #include <sys/poll.h>
  18. #include <netinet/in.h>
  19. #include <unistd.h>
  20. namespace betacore
  21. {
  22. class Server
  23. {
  24. private:
  25. fd_set rset;
  26. bool online = false;
  27. bool server_running = false;
  28. int port;
  29. int server_socket;
  30. std::vector<int> clients;
  31. struct sockaddr_in server_address;
  32. /*
  33. * Starts the server
  34. */
  35. void start();
  36. /*
  37. * Shutdown Routine
  38. */
  39. void shutdown();
  40. /*
  41. * Listen for connection and spawn a new thread
  42. */
  43. void listener();
  44. /*
  45. * Read the socket, is blocking, but will block its own thread
  46. */
  47. void read_socket(int client);
  48. /*
  49. * This will forward to all connected clients except the client fd given
  50. */
  51. void forward_message(int client, char buffer[BUFFER_LENGTH]);
  52. public:
  53. /*
  54. * Create a server on port
  55. */
  56. Server(int port);
  57. /*
  58. * Turn it off
  59. */
  60. void off();
  61. /*
  62. * Return true if the server is running
  63. */
  64. bool running();
  65. };
  66. } // namespace betacore
  67. #endif