connection.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. extern int report_interval;
  17. struct conv_manager_t // manage the udp connections
  18. {
  19. //typedef hash_map map;
  20. unordered_map<u64_t,u32_t> u64_to_conv; //conv and u64 are both supposed to be uniq
  21. unordered_map<u32_t,u64_t> conv_to_u64;
  22. unordered_map<u32_t,u64_t> conv_last_active_time;
  23. unordered_map<u32_t,u64_t>::iterator clear_it;
  24. //void (*clear_function)(uint64_t u64) ;
  25. long long last_clear_time;
  26. conv_manager_t();
  27. conv_manager_t(const conv_manager_t &b)
  28. {
  29. assert(0==1);
  30. }
  31. ~conv_manager_t();
  32. int get_size();
  33. void reserve();
  34. void clear();
  35. u32_t get_new_conv();
  36. int is_conv_used(u32_t conv);
  37. int is_u64_used(u64_t u64);
  38. u32_t find_conv_by_u64(u64_t u64);
  39. u64_t find_u64_by_conv(u32_t conv);
  40. int update_active_time(u32_t conv);
  41. int insert_conv(u32_t conv,u64_t u64);
  42. int erase_conv(u32_t conv);
  43. int clear_inactive(char * ip_port=0);
  44. int clear_inactive0(char * ip_port);
  45. };
  46. struct inner_stat_t
  47. {
  48. u64_t input_packet_num;
  49. u64_t input_packet_size;
  50. u64_t output_packet_num;
  51. u64_t output_packet_size;
  52. };
  53. struct stat_t
  54. {
  55. u64_t last_report_time;
  56. inner_stat_t normal_to_fec;
  57. inner_stat_t fec_to_normal;
  58. stat_t()
  59. {
  60. memset(this,0,sizeof(stat_t));
  61. }
  62. void report_as_client()
  63. {
  64. if(report_interval!=0 &&get_current_time()-last_report_time>u64_t(report_interval)*1000)
  65. {
  66. last_report_time=get_current_time();
  67. inner_stat_t &a=normal_to_fec;
  68. inner_stat_t &b=fec_to_normal;
  69. mylog(log_info,"[report]client-->server:(original:%llu pkt;%llu byte) (fec:%llu pkt,%llu byte) server-->client:(original:%llu pkt;%llu byte) (fec:%llu pkt;%llu byte)\n",
  70. a.input_packet_num,a.input_packet_size,a.output_packet_num,a.output_packet_size,
  71. b.output_packet_num,b.output_packet_size,b.input_packet_num,b.input_packet_size
  72. );
  73. }
  74. }
  75. void report_as_server(ip_port_t &ip_port)
  76. {
  77. if(report_interval!=0 &&get_current_time()-last_report_time>u64_t(report_interval)*1000)
  78. {
  79. last_report_time=get_current_time();
  80. inner_stat_t &a=fec_to_normal;
  81. inner_stat_t &b=normal_to_fec;
  82. mylog(log_info,"[report][%s]client-->server:(original:%llu pkt;%llu byte) (fec:%llu pkt;%llu byte) server-->client:(original:%llu pkt;%llu byte) (fec:%llu pkt;%llu byte)\n",
  83. ip_port.to_s(),
  84. a.output_packet_num,a.output_packet_size,a.input_packet_num,a.input_packet_size,
  85. b.input_packet_num,b.input_packet_size,b.output_packet_num,b.output_packet_size
  86. );
  87. }
  88. }
  89. };
  90. 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
  91. //handle multiple clients
  92. {
  93. conv_manager_t conv_manager;
  94. fec_encode_manager_t fec_encode_manager;
  95. fec_decode_manager_t fec_decode_manager;
  96. my_timer_t timer;
  97. ip_port_t ip_port;
  98. u64_t last_active_time;
  99. stat_t stat;
  100. conn_info_t()
  101. {
  102. }
  103. void update_active_time()
  104. {
  105. last_active_time=get_current_time();
  106. }
  107. conn_info_t(const conn_info_t &b)
  108. {
  109. assert(0==1);
  110. }
  111. };
  112. 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
  113. {
  114. unordered_map<u64_t,conn_info_t*> mp;//<ip,port> to conn_info_t;
  115. unordered_map<u64_t,conn_info_t*>::iterator clear_it;
  116. long long last_clear_time;
  117. conn_manager_t();
  118. conn_manager_t(const conn_info_t &b)
  119. {
  120. assert(0==1);
  121. }
  122. int exist(ip_port_t);
  123. conn_info_t *& find_p(ip_port_t); //be aware,the adress may change after rehash
  124. conn_info_t & find(ip_port_t) ; //be aware,the adress may change after rehash
  125. int insert(ip_port_t);
  126. int erase(unordered_map<u64_t,conn_info_t*>::iterator erase_it);
  127. int clear_inactive();
  128. int clear_inactive0();
  129. };
  130. extern conn_manager_t conn_manager;
  131. #endif /* CONNECTION_H_ */