TCPServer.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #ifndef TCP_SERVER_H
  2. #define TCP_SERVER_H
  3. #include <iostream>
  4. #include <vector>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include <sys/types.h>
  10. #include <sys/socket.h>
  11. #include <netinet/in.h>
  12. #include <string.h>
  13. #include <arpa/inet.h>
  14. #include <pthread.h>
  15. #include <thread>
  16. #include <algorithm>
  17. #include <cctype>
  18. #include <mutex>
  19. using namespace std;
  20. #define MAXPACKETSIZE 40960
  21. #define MAX_CLIENT 1000
  22. //#define CODA_MSG 4
  23. struct descript_socket{
  24. int socket = -1;
  25. string ip = "";
  26. int id = -1;
  27. std::string message;
  28. bool enable_message_runtime = false;
  29. };
  30. class TCPServer
  31. {
  32. public:
  33. int setup(int port, vector<int> opts = vector<int>());
  34. vector<descript_socket*> getMessage();
  35. void accepted();
  36. void Send(string msg, int id);
  37. void detach(int id);
  38. void clean(int id);
  39. bool is_online();
  40. string get_ip_addr(int id);
  41. int get_last_closed_sockets();
  42. void closed();
  43. protected:
  44. int sockfd, n, pid;
  45. struct sockaddr_in serverAddress;
  46. struct sockaddr_in clientAddress;
  47. pthread_t serverThread[ MAX_CLIENT ];
  48. static vector<descript_socket*> newsockfd;
  49. static char msg[ MAXPACKETSIZE ];
  50. static vector<descript_socket*> Message;//[CODA_MSG];
  51. static bool isonline;
  52. static int last_closed;
  53. static int num_client;
  54. static std::mutex mt;
  55. static std::mutex mt_sock_;
  56. static void * Task(void * argv);
  57. };
  58. #endif