2019-07-27 22:44:32 +08:00
|
|
|
|
#pragma once
|
2018-12-28 21:46:01 +08:00
|
|
|
|
#include "fiber_cpp_define.hpp"
|
2019-01-14 14:34:14 +08:00
|
|
|
|
#include <string>
|
2018-12-28 21:46:01 +08:00
|
|
|
|
|
|
|
|
|
namespace acl {
|
|
|
|
|
|
2019-01-14 14:34:14 +08:00
|
|
|
|
class keeper_waiter;
|
2018-12-28 21:46:01 +08:00
|
|
|
|
class socket_stream;
|
2019-01-14 14:34:14 +08:00
|
|
|
|
class thread_mutex;
|
2018-12-28 21:46:01 +08:00
|
|
|
|
|
2019-01-15 11:57:19 +08:00
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 鐙珛绾跨▼鐢ㄤ簬棰勫厛涓庢湇鍔″櫒鍒涘缓绌洪棽杩炴帴锛屽鎴风鍙互鐩存帴浠庤杩炴帴姹犱腑鑾峰彇鏂拌繛鎺ワ紝
|
|
|
|
|
* 杩欏浜<EFBFBD> ping rtt 杈冮暱锛堝锛<EFBFBD>10ms 浠ヤ笂锛夋瘮杈冩湁浠峰€硷紝鍙互鏈夋晥鍦板噺灏戝洜缃戠粶 rtt
|
|
|
|
|
* 閫犳垚鐨勮繛鎺ユ椂闂存崯鑰<EFBFBD>
|
2019-01-15 11:57:19 +08:00
|
|
|
|
*/
|
2018-12-28 21:46:01 +08:00
|
|
|
|
class tcp_keeper : public thread
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
tcp_keeper(void);
|
|
|
|
|
~tcp_keeper(void);
|
|
|
|
|
|
2019-01-15 11:57:19 +08:00
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 璁剧疆寤虹珛缃戠粶杩炴帴鐨勮秴鏃舵椂闂达紙绉掞級
|
2019-01-15 11:57:19 +08:00
|
|
|
|
* @param n {int}
|
|
|
|
|
* @return {tcp_keeper&}
|
|
|
|
|
*/
|
2018-12-29 15:42:03 +08:00
|
|
|
|
tcp_keeper& set_conn_timeout(int n);
|
2019-01-15 11:57:19 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 璁剧疆缃戠粶濂楁帴瀛<EFBFBD> IO 璇诲啓瓒呮椂鏃堕棿锛堢锛<EFBFBD>
|
2019-01-15 11:57:19 +08:00
|
|
|
|
* @param n {int}
|
|
|
|
|
* @return {tcp_keeper&}
|
|
|
|
|
*/
|
2018-12-29 15:42:03 +08:00
|
|
|
|
tcp_keeper& set_rw_timeout(int n);
|
2019-01-15 11:57:19 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 璁剧疆杩炴帴姹犱腑绌洪棽杩炴帴鐨勬渶灏忚繛鎺ユ暟
|
2019-01-15 11:57:19 +08:00
|
|
|
|
* @param n {int}
|
|
|
|
|
* @return {tcp_keeper&}
|
|
|
|
|
*/
|
|
|
|
|
tcp_keeper& set_conn_min(int n);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 璁剧疆杩炴帴姹犱腑绌洪棽杩炴帴鐨勬渶澶ц繛鎺ユ暟
|
2019-01-15 11:57:19 +08:00
|
|
|
|
* @param n {int}
|
|
|
|
|
* @return {tcp_keeper&}
|
|
|
|
|
*/
|
2018-12-29 15:42:03 +08:00
|
|
|
|
tcp_keeper& set_conn_max(int n);
|
2019-01-15 11:57:19 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 璁剧疆缃戠粶杩炴帴鐨勭┖闂叉椂闂达紙绉掞級锛岀┖闂叉椂闂磋秴杩囨鍊兼椂杩炴帴灏嗚鍏抽棴
|
2019-01-15 11:57:19 +08:00
|
|
|
|
* @param ttl {int}
|
|
|
|
|
* @return {tcp_keeper&}
|
|
|
|
|
*/
|
2018-12-29 15:42:03 +08:00
|
|
|
|
tcp_keeper& set_conn_ttl(int ttl);
|
2019-01-15 11:57:19 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 璁剧疆姣忎釜杩炴帴姹犵殑绌洪棽鏃堕棿锛堢锛夛紝鍗冲綋璇ヨ繛鎺ユ睜鐨勭┖闂叉椂闂磋秴杩囨鍊兼椂
|
|
|
|
|
* 灏嗚閲婃斁锛屼粠鑰屼究浜庣郴缁熷洖鏀跺唴瀛樿祫婧<EFBFBD>
|
2019-01-15 11:57:19 +08:00
|
|
|
|
* @param ttl {int}
|
|
|
|
|
* @return {tcp_keeper&}
|
|
|
|
|
*/
|
2018-12-29 15:42:03 +08:00
|
|
|
|
tcp_keeper& set_pool_ttl(int ttl);
|
|
|
|
|
|
2019-01-15 11:57:19 +08:00
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 璁剧疆 rtt 闃€鍊硷紙绉掞級锛屽綋缃戠粶杩炴帴鏃堕棿瓒呰繃姝ゅ€兼椂鎵嶄細鍚敤浠庤繛鎺ユ睜鎻愬彇
|
|
|
|
|
* 杩炴帴鏂瑰紡锛屽鏋滅綉缁滆繛鎺ユ椂闂村皬浜庢鍊硷紝鍒欑洿鎺ヨ繛鎺ユ湇鍔″櫒
|
2019-01-15 11:57:19 +08:00
|
|
|
|
* @param rtt {double}
|
|
|
|
|
* @return {tcp_keeper&}
|
|
|
|
|
*/
|
2019-01-14 14:34:14 +08:00
|
|
|
|
tcp_keeper& set_rtt_min(double rtt);
|
|
|
|
|
|
2019-01-15 11:57:19 +08:00
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 浠<EFBFBD> tcp_keeper 瀵硅薄涓彁鍙栧搴斿湴鍧€鐨勭綉缁滆繛鎺ュ鎺<EFBFBD>
|
|
|
|
|
* @param addr {const char*} 鏈嶅姟鍣ㄥ湴鍧€锛屾牸寮忥細ip:port
|
|
|
|
|
* @param hit {bool*} 闈炵┖鏃讹紝灏嗗瓨鏀捐杩炴帴鏄惁鍦ㄨ繛鎺ユ睜鐨勭┖闂茶繛鎺ヤ腑鍛戒腑
|
|
|
|
|
* @param sync {bool} 鏄惁閲囩敤鐩磋繛妯″紡锛屽鏋滈噰鐢ㄧ洿杩炴ā寮忥紝鍒欏唴閮ㄤ笉浼<EFBFBD>
|
|
|
|
|
* 閽堝璇ュ湴鍧€棰勫垱杩炴帴姹<EFBFBD>
|
|
|
|
|
* @return {socket_stream*} 杩斿洖 NULL 琛ㄧず杩炴帴澶辫触
|
2019-01-15 11:57:19 +08:00
|
|
|
|
*/
|
2019-04-30 17:54:08 +08:00
|
|
|
|
socket_stream* peek(const char* addr, bool* hit = NULL,
|
|
|
|
|
bool sync = false);
|
2018-12-28 21:46:01 +08:00
|
|
|
|
|
2019-01-15 11:57:19 +08:00
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 鍋滄 tcp_keeper 绾跨▼杩愯
|
2019-01-15 11:57:19 +08:00
|
|
|
|
*/
|
2018-12-28 21:46:01 +08:00
|
|
|
|
void stop(void);
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
// @override
|
|
|
|
|
void* run(void);
|
|
|
|
|
|
|
|
|
|
private:
|
2019-01-14 14:34:14 +08:00
|
|
|
|
double rtt_min_;
|
|
|
|
|
keeper_waiter* waiter_;
|
|
|
|
|
std::map<std::string, double> addrs_;
|
|
|
|
|
thread_mutex* lock_;
|
|
|
|
|
|
|
|
|
|
bool direct(const char* addr, bool& found);
|
|
|
|
|
void remove(const char* addr);
|
|
|
|
|
void update(const char* addr, double cost);
|
2018-12-28 21:46:01 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace acl
|