server.hpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #ifndef SERVER_HPP
  2. #define SERVER_HPP
  3. #include <iostream>
  4. #include <sys/socket.h>
  5. #include <sys/fcntl.h>
  6. #include <netdb.h>
  7. #include <netinet/in.h>
  8. #include <unistd.h>
  9. #include <strings.h>
  10. #include <sys/time.h>
  11. #include <sys/resource.h>
  12. #include "message.hpp"
  13. #include "task.hpp"
  14. using namespace std;
  15. struct ClientInformation{
  16. int socket;
  17. bool has_message;
  18. Message message;
  19. void* data;
  20. bool is_worker;
  21. Task* current_task;
  22. };
  23. class Server{
  24. private:
  25. void send_message(size_t c,Message& msg);
  26. public:
  27. bool (*treat)(Task& t);
  28. size_t nb_clients;
  29. size_t max_clients;
  30. size_t nb_workers;
  31. Task* tasks;
  32. size_t nb_tasks;
  33. size_t nb_finished_tasks;
  34. ClientInformation* clients;
  35. int connection_socket;
  36. char Buffer[MAX_MSG_SIZE];
  37. Server(size_t max_clients,int port);
  38. ~Server();
  39. void listen_for_new_clients();
  40. void listen_clients();
  41. void remove_client(size_t c);
  42. void get_message(size_t c);
  43. void send_code(size_t c,char code);
  44. void send_string(size_t c,string str);
  45. void treat_messages();
  46. void treat_message(size_t c);
  47. void send_informations(size_t c);
  48. void affect_task(size_t c);
  49. void affect_tasks();
  50. void get_task(size_t c);
  51. bool has_unfinished_tasks();
  52. void set_tasks(Task* tasks,size_t nb_tasks);
  53. };
  54. inline void
  55. Server::send_message(size_t c,Message& msg){
  56. send(clients[c].socket,msg.buffer,msg.size,0);
  57. }
  58. inline void
  59. Server::send_code(size_t c,char code){
  60. Message msg;
  61. msg.set_code(code);
  62. send_message(c,msg);
  63. }
  64. inline void
  65. Server::treat_messages(){
  66. for(size_t c=0;c<nb_clients;++c){
  67. if(clients[c].has_message){
  68. treat_message(c);
  69. }
  70. }
  71. }
  72. inline bool
  73. Server::has_unfinished_tasks(){
  74. return nb_finished_tasks<nb_tasks;
  75. }
  76. #endif