connection.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * connection.cpp
  3. *
  4. * Created on: Sep 23, 2017
  5. * Author: root
  6. */
  7. #include "connection.h"
  8. //const int disable_conv_clear=0;//a udp connection in the multiplexer is called conversation in this program,conv for short.
  9. const int disable_conn_clear=0;//a raw connection is called conn.
  10. int report_interval=0;
  11. void server_clear_function(u64_t u64)//used in conv_manager in server mode.for server we have to use one udp fd for one conv(udp connection),
  12. //so we have to close the fd when conv expires
  13. {
  14. fd64_t fd64=u64;
  15. assert(fd_manager.exist(fd64));
  16. ev_io &watcher= fd_manager.get_info(fd64).io_watcher;
  17. address_t &addr=fd_manager.get_info(fd64).addr;//
  18. assert(conn_manager.exist(addr));//
  19. ev_loop *loop =conn_manager.find_insert(addr).loop; // overkill ? should we just use ev_default_loop(0)?
  20. ev_io_stop(loop,&watcher);
  21. fd_manager.fd64_close(fd64);
  22. }
  23. ////////////////////////////////////////////////////////////////////
  24. conn_manager_t::conn_manager_t()
  25. {
  26. mp.reserve(10007);
  27. last_clear_time=0;
  28. }
  29. int conn_manager_t::exist(address_t addr)
  30. {
  31. if(mp.find(addr)!=mp.end())
  32. {
  33. return 1;
  34. }
  35. return 0;
  36. }
  37. conn_info_t *& conn_manager_t::find_insert_p(address_t addr) //be aware,the adress may change after rehash
  38. {
  39. // u64_t u64=0;
  40. //u64=ip;
  41. //u64<<=32u;
  42. //u64|=port;
  43. unordered_map<address_t,conn_info_t*>::iterator it=mp.find(addr);
  44. if(it==mp.end())
  45. {
  46. mp[addr]=new conn_info_t;
  47. //lru.new_key(addr);
  48. }
  49. else
  50. {
  51. //lru.update(addr);
  52. }
  53. return mp[addr];
  54. }
  55. conn_info_t & conn_manager_t::find_insert(address_t addr) //be aware,the adress may change after rehash
  56. {
  57. //u64_t u64=0;
  58. //u64=ip;
  59. //u64<<=32u;
  60. //u64|=port;
  61. unordered_map<address_t,conn_info_t*>::iterator it=mp.find(addr);
  62. if(it==mp.end())
  63. {
  64. mp[addr]=new conn_info_t;
  65. //lru.new_key(addr);
  66. }
  67. else
  68. {
  69. //lru.update(addr);
  70. }
  71. return *mp[addr];
  72. }
  73. int conn_manager_t::erase(unordered_map<address_t,conn_info_t*>::iterator erase_it)
  74. {
  75. delete(erase_it->second);
  76. mp.erase(erase_it->first);
  77. return 0;
  78. }
  79. int conn_manager_t::clear_inactive()
  80. {
  81. if(get_current_time()-last_clear_time>conn_clear_interval)
  82. {
  83. last_clear_time=get_current_time();
  84. return clear_inactive0();
  85. }
  86. return 0;
  87. }
  88. int conn_manager_t::clear_inactive0()
  89. {
  90. //mylog(log_info,"called\n");
  91. unordered_map<address_t,conn_info_t*>::iterator it;
  92. unordered_map<address_t,conn_info_t*>::iterator old_it;
  93. if(disable_conn_clear) return 0;
  94. //map<uint32_t,uint64_t>::iterator it;
  95. int cnt=0;
  96. it=clear_it;//TODO,write it back
  97. int size=mp.size();
  98. int num_to_clean=size/conn_clear_ratio+conn_clear_min; //clear 1/10 each time,to avoid latency glitch
  99. //mylog(log_trace,"mp.size() %d\n", size);
  100. num_to_clean=min(num_to_clean,(int)mp.size());
  101. u64_t current_time=get_current_time();
  102. //mylog(log_info,"here size=%d\n",(int)mp.size());
  103. for(;;)
  104. {
  105. if(cnt>=num_to_clean) break;
  106. if(mp.begin()==mp.end()) break;
  107. if(it==mp.end())
  108. {
  109. it=mp.begin();
  110. }
  111. if(it->second->conv_manager.s.get_size() >0)
  112. {
  113. //mylog(log_info,"[%s:%d]size %d \n",my_ntoa(get_u64_h(it->first)),get_u64_l(it->first),(int)it->second->conv_manager.get_size());
  114. it++;
  115. }
  116. else if(current_time<it->second->last_active_time+server_conn_timeout)
  117. {
  118. it++;
  119. }
  120. else
  121. {
  122. address_t tmp_addr=it->first;// avoid making get_str() const;
  123. mylog(log_info,"{%s} inactive conn cleared \n",tmp_addr.get_str());
  124. old_it=it;
  125. it++;
  126. erase(old_it);
  127. }
  128. cnt++;
  129. }
  130. clear_it=it;
  131. return 0;
  132. }