conn_manager.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * conn_manager.cpp
  3. *
  4. * Created on: Sep 15, 2017
  5. * Author: root
  6. */
  7. #include "conn_manager.h"
  8. int disable_conv_clear=0;
  9. conn_manager_t::conn_manager_t() {
  10. clear_it = fd_last_active_time.begin();
  11. long long last_clear_time = 0;
  12. rehash();
  13. //clear_function=0;
  14. }
  15. conn_manager_t::~conn_manager_t() {
  16. clear();
  17. }
  18. int conn_manager_t::get_size() {
  19. return fd_to_u64.size();
  20. }
  21. void conn_manager_t::rehash() {
  22. u64_to_fd.rehash(10007);
  23. fd_to_u64.rehash(10007);
  24. fd_last_active_time.rehash(10007);
  25. }
  26. void conn_manager_t::clear() {
  27. if (disable_conv_clear)
  28. return;
  29. for (it = fd_to_u64.begin(); it != fd_to_u64.end(); it++) {
  30. //int fd=int((it->second<<32u)>>32u);
  31. close(it->first);
  32. }
  33. u64_to_fd.clear();
  34. fd_to_u64.clear();
  35. fd_last_active_time.clear();
  36. clear_it = fd_last_active_time.begin();
  37. }
  38. int conn_manager_t::exist_fd(u32_t fd) {
  39. return fd_to_u64.find(fd) != fd_to_u64.end();
  40. }
  41. int conn_manager_t::exist_u64(u64_t u64) {
  42. return u64_to_fd.find(u64) != u64_to_fd.end();
  43. }
  44. u32_t conn_manager_t::find_fd_by_u64(u64_t u64) {
  45. return u64_to_fd[u64];
  46. }
  47. u64_t conn_manager_t::find_u64_by_fd(u32_t fd) {
  48. return fd_to_u64[fd];
  49. }
  50. int conn_manager_t::update_active_time(u32_t fd) {
  51. return fd_last_active_time[fd] = get_current_time();
  52. }
  53. int conn_manager_t::insert_fd(u32_t fd, u64_t u64) {
  54. u64_to_fd[u64] = fd;
  55. fd_to_u64[fd] = u64;
  56. fd_last_active_time[fd] = get_current_time();
  57. return 0;
  58. }
  59. int conn_manager_t::erase_fd(u32_t fd) {
  60. if (disable_conv_clear)
  61. return 0;
  62. u64_t u64 = fd_to_u64[fd];
  63. u32_t ip = (u64 >> 32u);
  64. int port = uint16_t((u64 << 32u) >> 32u);
  65. mylog(log_info, "fd %d cleared,assocated adress %s,%d\n", fd, my_ntoa(ip),
  66. port);
  67. close(fd);
  68. fd_to_u64.erase(fd);
  69. u64_to_fd.erase(u64);
  70. fd_last_active_time.erase(fd);
  71. return 0;
  72. }
  73. void conn_manager_t::check_clear_list() {
  74. while (!clear_list.empty()) {
  75. int fd = *clear_list.begin();
  76. clear_list.pop_front();
  77. erase_fd(fd);
  78. }
  79. }
  80. int conn_manager_t::clear_inactive() {
  81. if (get_current_time() - last_clear_time > conv_clear_interval) {
  82. last_clear_time = get_current_time();
  83. return clear_inactive0();
  84. }
  85. return 0;
  86. }
  87. int conn_manager_t::clear_inactive0() {
  88. if (disable_conv_clear)
  89. return 0;
  90. //map<uint32_t,uint64_t>::iterator it;
  91. int cnt = 0;
  92. it = clear_it;
  93. int size = fd_last_active_time.size();
  94. int num_to_clean = size / conv_clear_ratio + conv_clear_min; //clear 1/10 each time,to avoid latency glitch
  95. u64_t current_time = get_current_time();
  96. for (;;) {
  97. if (cnt >= num_to_clean)
  98. break;
  99. if (fd_last_active_time.begin() == fd_last_active_time.end())
  100. break;
  101. if (it == fd_last_active_time.end()) {
  102. it = fd_last_active_time.begin();
  103. }
  104. if (current_time - it->second > conv_timeout) {
  105. //mylog(log_info,"inactive conv %u cleared \n",it->first);
  106. old_it = it;
  107. it++;
  108. u32_t fd = old_it->first;
  109. erase_fd(old_it->first);
  110. } else {
  111. it++;
  112. }
  113. cnt++;
  114. }
  115. return 0;
  116. }