2019-07-27 22:44:32 +08:00
|
|
|
|
#pragma once
|
2017-06-02 14:47:24 +08:00
|
|
|
|
#include "../acl_cpp_define.hpp"
|
2019-05-09 13:57:51 +08:00
|
|
|
|
#include "../stdlib/noncopyable.hpp"
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
namespace acl
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
class connect_pool;
|
|
|
|
|
|
2019-05-09 13:57:51 +08:00
|
|
|
|
class ACL_CPP_API connect_client : public noncopyable
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
connect_client() : when_(0), pool_(NULL) {}
|
|
|
|
|
virtual ~connect_client() {}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 获得该连接对象最近一次被使用的时间截
|
2014-11-19 00:25:21 +08:00
|
|
|
|
* @return {time_t}
|
|
|
|
|
*/
|
|
|
|
|
time_t get_when()
|
|
|
|
|
{
|
|
|
|
|
return when_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 设置该连接对象当前被使用的时间截
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
|
|
|
|
void set_when(time_t when)
|
|
|
|
|
{
|
|
|
|
|
when_ = when;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 纯虚函数,子类必须实现此函数用于连接服务器
|
|
|
|
|
* @return {bool} 是否连接成功
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
|
|
|
|
virtual bool open() = 0;
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 获得连接池对象引用,在 connect_pool 内部创建
|
|
|
|
|
* 连接对象会调用 set_pool 设置连接池对象句柄
|
2014-11-19 00:25:21 +08:00
|
|
|
|
* @return {connect_pool*}
|
|
|
|
|
*/
|
|
|
|
|
connect_pool* get_pool() const
|
|
|
|
|
{
|
|
|
|
|
return pool_;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-13 16:26:30 +08:00
|
|
|
|
public:
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 虚函数,该函数设置网络连接超时时间及网络 IO 超时时间,子类可以重载该虚函数,
|
|
|
|
|
* 以便于设置内部的对象超时时间
|
|
|
|
|
* @param conn_timeout {int} 网络连接超时时间(秒)
|
|
|
|
|
* @param rw_timeout {int} 网络 IO 超时时间(秒)
|
2015-11-13 16:26:30 +08:00
|
|
|
|
*/
|
|
|
|
|
virtual void set_timeout(int conn_timeout, int rw_timeout)
|
|
|
|
|
{
|
|
|
|
|
conn_timeout_ = conn_timeout;
|
|
|
|
|
rw_timeout_ = rw_timeout;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
int conn_timeout_;
|
|
|
|
|
int rw_timeout_;
|
|
|
|
|
|
2014-11-19 00:25:21 +08:00
|
|
|
|
friend class connect_pool;
|
|
|
|
|
|
|
|
|
|
time_t when_;
|
|
|
|
|
connect_pool* pool_;
|
|
|
|
|
|
|
|
|
|
void set_pool(connect_pool* pool)
|
|
|
|
|
{
|
|
|
|
|
pool_ = pool;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace acl
|