2017-08-08 18:43:25 +08:00
|
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2017-2018
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* AUTHOR(S)
|
|
|
|
|
* Zheng Shuxin
|
|
|
|
|
* E-mail: zhengshuxin@qiyi.com
|
|
|
|
|
*
|
|
|
|
|
* VERSION
|
|
|
|
|
* Tue 08 Aug 2017 03:09:21 PM CST
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
namespace acl
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
class tcp_manager;
|
|
|
|
|
class tcp_pool;
|
|
|
|
|
class string;
|
|
|
|
|
|
2017-08-09 16:04:54 +08:00
|
|
|
|
/**
|
2017-08-09 16:15:01 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD><EFBFBD> tcp_manager <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>࣬<EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD>̬<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD>ַ<EFBFBD><EFBFBD>ͬʱ<EFBFBD><EFBFBD>̬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* ÿһ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD>
|
2017-08-09 16:04:54 +08:00
|
|
|
|
*/
|
|
|
|
|
class ACL_CPP_API tcp_ipc
|
2017-08-08 18:43:25 +08:00
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
tcp_ipc(void);
|
|
|
|
|
~tcp_ipc(void);
|
|
|
|
|
|
2017-08-09 16:04:54 +08:00
|
|
|
|
/**
|
2017-08-09 16:15:01 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳص<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param max {int} ÿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳص<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD><EFBFBD><EFBFBD> <= 0 ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2017-08-09 16:04:54 +08:00
|
|
|
|
* @return {tcp_ipc&}
|
|
|
|
|
*/
|
2017-08-08 18:43:25 +08:00
|
|
|
|
tcp_ipc& set_limit(int max);
|
2017-08-09 16:04:54 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2017-08-09 16:15:01 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˿<EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>䣬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӿ<EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>䳬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD>
|
|
|
|
|
* @param ttl {int} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱʱ<EFBFBD><EFBFBD>
|
2017-08-09 16:04:54 +08:00
|
|
|
|
* @return {tcp_ipc&}
|
|
|
|
|
*/
|
2017-08-08 18:43:25 +08:00
|
|
|
|
tcp_ipc& set_idle(int ttl);
|
2017-08-09 16:04:54 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2017-08-09 16:15:01 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD>ʱʱ<EFBFBD><EFBFBD>
|
|
|
|
|
* @param conn_timeout {int} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD>ʱʱ<EFBFBD>䣨<EFBFBD>룩
|
2017-08-09 16:04:54 +08:00
|
|
|
|
* @return {tcp_ipc&}
|
|
|
|
|
*/
|
2017-08-08 18:43:25 +08:00
|
|
|
|
tcp_ipc& set_conn_timeout(int conn_timeout);
|
2017-08-09 16:04:54 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2017-08-09 16:15:01 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD>ʱʱ<EFBFBD><EFBFBD>
|
|
|
|
|
* @param timeout {int} <EFBFBD><EFBFBD>д<EFBFBD><EFBFBD>ʱʱ<EFBFBD>䣨<EFBFBD>룩
|
2017-08-09 16:04:54 +08:00
|
|
|
|
* @return {tcp_ipc&}
|
|
|
|
|
*/
|
2017-08-08 18:43:25 +08:00
|
|
|
|
tcp_ipc& set_rw_timeout(int timeout);
|
|
|
|
|
|
2017-08-09 16:04:54 +08:00
|
|
|
|
/**
|
2017-08-09 16:15:01 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> TCP <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2017-08-09 16:04:54 +08:00
|
|
|
|
* @return {tcp_manager&}
|
|
|
|
|
*/
|
|
|
|
|
tcp_manager& get_manager(void) const;
|
|
|
|
|
|
|
|
|
|
/**
|
2017-08-09 16:15:01 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD>Ե<EFBFBD><EFBFBD>ñ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD>ֻ<EFBFBD>е<EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>Ż<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param addr {const char*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD>IP:PORT
|
2017-08-09 16:04:54 +08:00
|
|
|
|
* @return {tcp_ipc&}
|
|
|
|
|
*/
|
|
|
|
|
tcp_ipc& add_addr(const char* addr);
|
|
|
|
|
|
|
|
|
|
/**
|
2017-08-09 16:15:01 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD>ݷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַɾ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳض<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳض<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>ö<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD>ᱻɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Dz<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD>ɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӱ<EFBFBD><EFBFBD>黹<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳض<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* <EFBFBD>Żᱻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><EFBFBD>
|
|
|
|
|
* @param addr {const char*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD>IP:PORT
|
2017-08-09 16:04:54 +08:00
|
|
|
|
* @return {tcp_ipc&}
|
|
|
|
|
*/
|
|
|
|
|
tcp_ipc& del_addr(const char* addr);
|
|
|
|
|
|
|
|
|
|
/**
|
2017-08-09 16:15:01 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD>ķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>Ƿ<EFBFBD><EFBFBD>ɹ<EFBFBD>
|
|
|
|
|
* @param addr {const char*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD>IP:PORT
|
2017-08-09 16:04:54 +08:00
|
|
|
|
* @return {bool}
|
|
|
|
|
*/
|
|
|
|
|
bool addr_exist(const char* addr);
|
|
|
|
|
|
|
|
|
|
/**
|
2017-08-09 16:15:01 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD>еķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param addrs {std::vector<string>&} <EFBFBD>洢<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2017-08-09 16:04:54 +08:00
|
|
|
|
*/
|
|
|
|
|
void get_addrs(std::vector<string>& addrs);
|
|
|
|
|
|
|
|
|
|
/**
|
2017-08-09 16:15:01 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD>
|
|
|
|
|
* @param addr {const char*} ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
|
|
|
|
|
* @param {const void*} Ҫ<EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD><EFBFBD><EFBFBD>ַ
|
|
|
|
|
* @param len {unsigned int} <EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param out {string*} <EFBFBD><EFBFBD><EFBFBD>ö<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> NULL ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>ӷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>
|
|
|
|
|
* <EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڸû<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ö<EFBFBD><EFBFBD><EFBFBD>Ϊ NULL<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @return {bool} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD>ɹ<EFBFBD>
|
2017-08-09 16:04:54 +08:00
|
|
|
|
*/
|
2017-08-08 18:43:25 +08:00
|
|
|
|
bool send(const char* addr, const void* data, unsigned int len,
|
|
|
|
|
string* out = NULL);
|
|
|
|
|
|
2017-08-09 16:04:54 +08:00
|
|
|
|
/**
|
2017-08-09 16:15:01 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>з<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD>
|
|
|
|
|
* @param {const void*} Ҫ<EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD><EFBFBD><EFBFBD>ַ
|
|
|
|
|
* @param len {unsigned int} <EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param exclusive {bool} <EFBFBD><EFBFBD><EFBFBD>㲥<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Է<EFBFBD>ֹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD><EFBFBD><EFBFBD>Դ
|
|
|
|
|
* @param check_result {bool} <EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>֤<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param nerr {unsigned *} <EFBFBD><EFBFBD> NULL ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD>ܵķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @return {size_t} <EFBFBD><EFBFBD><EFBFBD>ط<EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD>ķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2017-08-09 16:04:54 +08:00
|
|
|
|
*/
|
|
|
|
|
size_t broadcast(const void* data, unsigned int len,
|
|
|
|
|
bool exclusive = true, bool check_result = false,
|
|
|
|
|
unsigned* nerr = NULL);
|
|
|
|
|
|
2017-08-08 18:43:25 +08:00
|
|
|
|
private:
|
|
|
|
|
tcp_manager* manager_;
|
|
|
|
|
int max_;
|
|
|
|
|
int ttl_;
|
|
|
|
|
int conn_timeout_;
|
|
|
|
|
int rw_timeout_;
|
|
|
|
|
|
|
|
|
|
bool send(tcp_pool&, const void*, unsigned int, string*);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace acl
|