connection.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * connection.h
  3. *
  4. * Created on: Sep 23, 2017
  5. * Author: root
  6. */
  7. #ifndef CONNECTION_H_
  8. #define CONNECTION_H_
  9. extern int disable_anti_replay;
  10. #include "connection.h"
  11. #include "common.h"
  12. #include "log.h"
  13. #include "delay_manager.h"
  14. #include "fd_manager.h"
  15. #include "fec_manager.h"
  16. /*
  17. struct anti_replay_t //its for anti replay attack,similar to openvpn/ipsec 's anti replay window
  18. {
  19. u64_t max_packet_received;
  20. char window[anti_replay_window_size];
  21. anti_replay_seq_t anti_replay_seq;
  22. anti_replay_seq_t get_new_seq_for_send();
  23. anti_replay_t();
  24. void re_init();
  25. int is_vaild(u64_t seq);
  26. };//anti_replay;
  27. */
  28. struct conv_manager_t // manage the udp connections
  29. {
  30. //typedef hash_map map;
  31. unordered_map<u64_t,u32_t> u64_to_conv; //conv and u64 are both supposed to be uniq
  32. unordered_map<u32_t,u64_t> conv_to_u64;
  33. unordered_map<u32_t,u64_t> conv_last_active_time;
  34. unordered_map<u32_t,u64_t>::iterator clear_it;
  35. unordered_map<u32_t,u64_t>::iterator it;
  36. unordered_map<u32_t,u64_t>::iterator old_it;
  37. //void (*clear_function)(uint64_t u64) ;
  38. long long last_clear_time;
  39. conv_manager_t();
  40. ~conv_manager_t();
  41. int get_size();
  42. void reserve();
  43. void clear();
  44. u32_t get_new_conv();
  45. int is_conv_used(u32_t conv);
  46. int is_u64_used(u64_t u64);
  47. u32_t find_conv_by_u64(u64_t u64);
  48. u64_t find_u64_by_conv(u32_t conv);
  49. int update_active_time(u32_t conv);
  50. int insert_conv(u32_t conv,u64_t u64);
  51. int erase_conv(u32_t conv);
  52. int clear_inactive(char * ip_port=0);
  53. int clear_inactive0(char * ip_port);
  54. };//g_conv_manager;
  55. struct conn_info_t //stores info for a raw connection.for client ,there is only one connection,for server there can be thousand of connection since server can
  56. //handle multiple clients
  57. {
  58. conv_manager_t conv_manager;
  59. fec_encode_manager_t fec_encode_manager;
  60. fec_decode_manager_t fec_decode_manager;
  61. fd64_t timer_fd;
  62. ip_port_t ip_port;
  63. };//g_conn_info;
  64. struct conn_manager_t //manager for connections. for client,we dont need conn_manager since there is only one connection.for server we use one conn_manager for all connections
  65. {
  66. unordered_map<u64_t,conn_info_t*> mp;//<ip,port> to conn_info_t;
  67. unordered_map<u64_t,conn_info_t*>::iterator clear_it;
  68. long long last_clear_time;
  69. conn_manager_t();
  70. int exist(ip_port_t);
  71. conn_info_t *& find_p(ip_port_t); //be aware,the adress may change after rehash
  72. conn_info_t & find(ip_port_t) ; //be aware,the adress may change after rehash
  73. int insert(ip_port_t);
  74. /*
  75. int exist_fd64(fd64_t fd64);
  76. void insert_fd64(fd64_t fd64,ip_port_t);
  77. ip_port_t find_by_fd64(fd64_t fd64);*/
  78. int erase(unordered_map<u64_t,conn_info_t*>::iterator erase_it);
  79. int clear_inactive();
  80. int clear_inactive0();
  81. };
  82. extern conn_manager_t conn_manager;
  83. #endif /* CONNECTION_H_ */