C/C++编程:连接池类connect_pool
前置准备
- C/C++编程:连接客户端类connect_client
- 连接池指的是针对某一个服务器地址可以有多个连接
知识点:
1:纯虚类:不能创建实例,只能继承,而且子类必须实现其纯虚函数
class connect_pool : public noncopyable{
protected:
/**
* 纯虚函数,需要子类实现
* @return {connect_client*}
*/
virtual connect_client* create_connect() = 0;
}
class connect_pool : public oceanstar::connect_pool{
// 基类纯虚函数的实现
oceanstar::connect_client* create_connect(){
return new connect_client(addr_.c_str(), conn_timeout_, conn_timeout_);
}
}
2. 如果父类没有默认构造函数,那么子类必须显式调用其父类的构造函数
class connect_pool : public noncopyable{
public:
/**
* 构造函数(因为这里显式写了一个构造函数,所以编译器不会默认生成无参构造函数)
* @param addr {const char*} 服务器监听地址,格式:ip:port(domain:port)
* @param max {size_t} 连接池最大连接个数限制,如果该值设为 0,则不设置连接池的连接上限
*/
connect_pool(const char* addr, size_t max): max_(max){
strncpy(addr_, addr, sizeof(addr_));
}
class connect_pool : public oceanstar::connect_pool{
public:
connect_pool(const char* addr, size_t count)
: oceanstar::connect_pool(addr, count){
//必须实现这种构造函数,否则:使用了被删除的函数‘connect_pool::connect_pool()’
}
版本1
源码(纯虚类)
//
// Created by oceanstar on 2021/8/10.
//
#ifndef OCEANSTAR_HTTP_CONNECT_POOL_H
#define OCEANSTAR_HTTP_CONNECT_POOL_H
#include <list>
#include <noncopyable.h>
#include "connect_client.h"
#include <cstring>
namespace oceanstar{
class connect_client;
/**
* 客户端连接池类,实现对连接池的动态管理,该类为纯虚类,需要子类实现
* 纯虚函数 create_connect 用于创建与服务端的一个连接
*/
class connect_pool : public noncopyable{
public:
/**
* 构造函数
* @param addr {const char*} 服务器监听地址,格式:ip:port(domain:port)
* @param max {size_t} 连接池最大连接个数限制,如果该值设为 0,则不设置连接池的连接上限
*/
connect_pool(const char* addr, size_t max): max_(max){
strncpy(addr_, addr, sizeof(addr_));
}
~connect_pool(void)
{
std::list<connect_client*>::iterator it = pool_.begin();
for (; it != pool_.end(); ++it) {
delete *it;
}
}
protected:
/**
* 纯虚函数,需要子类实现
* @return {connect_client*}
*/
virtual connect_client* create_connect() = 0;
protected:
char addr_[256]; // 连接池对应的服务器地址,IP:PORT
size_t max_; // 最大连接数
std::list<connect_client*> pool_; // 连接池集合
};
}
#endif //OCEANSTAR_HTTP_CONNECT_POOL_H
使用
class connect_pool : public oceanstar::connect_pool{
public:
connect_pool(const char* addr, size_t max)
: oceanstar::connect_pool(addr, max){
//必须实现这种构造函数,否则:使用了被删除的函数‘connect_pool::connect_pool()’
}
protected:
// 基类纯虚函数的实现
oceanstar::connect_client* create_connect(){
return new connect_client(addr_.c_str(), conn_timeout_, rw_timeout_);
}
protected:
std::string addr_;
int conn_timeout_;
int rw_timeout_;
};
版本:添加key
因为连接池总是绑定到某一个地址【1V1】,所以可以为这个连接池设置一个key,用来标识其身份。最好的key是addr
//
// Created by oceanstar on 2021/8/10.
//
#ifndef OCEANSTAR_HTTP_CONNECT_POOL_H
#define OCEANSTAR_HTTP_CONNECT_POOL_H
#include <list>
#include <noncopyable.h>
#include "connect_client.h"
#include "acl_mystring.h"
#include <cstring>
namespace oceanstar{
class connect_client;
/**
* 客户端连接池类,实现对连接池的动态管理,该类为纯虚类,需要子类实现
* 纯虚函数 create_connect 用于创建与服务端的一个连接
*/
class connect_pool : public noncopyable{
public:
/**
* 构造函数
* @param addr {const char*} 服务器监听地址,格式:ip:port(domain:port)
* @param max {size_t} 连接池最大连接个数限制,如果该值设为 0,则不设置连接池的连接上限
*/
connect_pool(const char* addr, size_t max): max_(max){
strncpy(addr_, addr, sizeof(addr_));
strncpy(key_, addr_, sizeof(key_));
acl_lowercase(key_);
}
~connect_pool(void)
{
std::list<connect_client*>::iterator it = pool_.begin();
for (; it != pool_.end(); ++it) {
delete *it;
}
}
public:
void set_key(const char* key){
strncpy(key_, key, sizeof(key_));
acl_lowercase(key_);
}
const char* get_key(void) const
{
return key_;
}
protected:
/**
* 纯虚函数,需要子类实现
* @return {connect_client*}
*/
virtual connect_client* create_connect() = 0;
protected:
char key_[256]; // 与该连接池相关的 key
char addr_[256]; // 连接池对应的服务器地址,IP:PORT
size_t max_; // 最大连接数
std::list<connect_client*> pool_; // 连接池集合
};
}
#endif //OCEANSTAR_HTTP_CONNECT_POOL_H
版本:设置连接池是否可用
//
// Created by oceanstar on 2021/8/10.
//
#ifndef OCEANSTAR_HTTP_CONNECT_POOL_H
#define OCEANSTAR_HTTP_CONNECT_POOL_H
#include <list>
#include <noncopyable.h>
#include "connect_client.h"
#include "acl_mystring.h"
#include "thread_mutex.h"
#include <cstring>
namespace oceanstar{
class connect_client;
/**
* 客户端连接池类,实现对连接池的动态管理,该类为纯虚类,需要子类实现
* 纯虚函数 create_connect 用于创建与服务端的一个连接
*/
class connect_pool : public noncopyable{
public:
/**
* 构造函数
* @param addr {const char*} 服务器监听地址,格式:ip:port(domain:port)
* @param max {size_t} 连接池最大连接个数限制,如果该值设为 0,则不设置连接池的连接上限
*/
connect_pool(const char* addr, size_t max): max_(max), alive_(true), last_dead_(0){
strncpy(addr_, addr, sizeof(addr_));
strncpy(key_, addr_, sizeof(key_));
acl_lowercase(key_);
}
~connect_pool(void)
{
std::list<connect_client*>::iterator it = pool_.begin();
for (; it != pool_.end(); ++it) {
delete *it;
}
}
public:
void set_key(const char* key){
strncpy(key_, key, sizeof(key_));
acl_lowercase(key_);
}
const char* get_key(void) const
{
return key_;
}
/**
* 设置连接池的存活状态
*/
void set_alive(bool yes /* true | false */){
lock_.lock();
alive_ = yes;
if (yes == false) {
time(&last_dead_);
}
lock_.unlock();
}
/**
* 检查连接池是否正常
* @return {bool} 返回 true 表示当前连接池处于正常状态,否则表示当前
* 连接池不可用
*/
bool aliving(){
return alive_;
}
protected:
/**
* 纯虚函数,需要子类实现
* @return {connect_client*}
*/
virtual connect_client* create_connect() = 0;
protected:
char key_[256]; // 与该连接池相关的 key
char addr_[256]; // 连接池对应的服务器地址,IP:PORT
size_t max_; // 最大连接数
std::list<connect_client*> pool_; // 连接池集合
bool alive_; // 是否属正常
time_t last_dead_; // 该连接池对象上次不可用时的时间截
thread_mutex lock_; // 访问 pool_ 时的互斥锁
};
}
#endif //OCEANSTAR_HTTP_CONNECT_POOL_H
版本:添加超时时间
//
// Created by oceanstar on 2021/8/10.
//
#ifndef OCEANSTAR_HTTP_CONNECT_POOL_H
#define OCEANSTAR_HTTP_CONNECT_POOL_H
#include <list>
#include <noncopyable.h>
#include "connect_client.h"
#include "acl_mystring.h"
#include "thread_mutex.h"
#include <cstring>
namespace oceanstar{
class connect_client;
/**
* 客户端连接池类,实现对连接池的动态管理,该类为纯虚类,需要子类实现
* 纯虚函数 create_connect 用于创建与服务端的一个连接
*/
class connect_pool : public noncopyable{
public:
/**
* 构造函数
* @param addr {const char*} 服务器监听地址,格式:ip:port(domain:port)
* @param max {size_t} 连接池最大连接个数限制,如果该值设为 0,则不设置连接池的连接上限
*/
connect_pool(const char* addr, size_t max): max_(max), alive_(true), last_dead_(0){
strncpy(addr_, addr, sizeof(addr_));
strncpy(key_, addr_, sizeof(key_));
acl_lowercase(key_);
}
~connect_pool(void)
{
std::list<connect_client*>::iterator it = pool_.begin();
for (; it != pool_.end(); ++it) {
delete *it;
}
}
/**
* 此接口用来设置超时时间
* @param conn_timeout {int} 网络连接超时时间(秒)
* @param rw_timeout {int} 网络 IO 超时时间(秒)
*/
connect_pool& set_timeout(int conn_timeout, int rw_timeout){
conn_timeout_ = conn_timeout;
rw_timeout_ = rw_timeout;
return *this;
}
public:
void set_key(const char* key){
strncpy(key_, key, sizeof(key_));
acl_lowercase(key_);
}
const char* get_key(void) const
{
return key_;
}
/**
* 设置连接池的存活状态
*/
void set_alive(bool yes /* true | false */){
lock_.lock();
alive_ = yes;
if (yes == false) {
time(&last_dead_);
}
lock_.unlock();
}
/**
* 检查连接池是否正常
* @return {bool} 返回 true 表示当前连接池处于正常状态,否则表示当前
* 连接池不可用
*/
bool aliving(){
return alive_;
}
protected:
/**
* 纯虚函数,需要子类实现
* @return {connect_client*}
*/
virtual connect_client* create_connect() = 0;
protected:
char key_[256]; // 与该连接池相关的 key
char addr_[256]; // 连接池对应的服务器地址,IP:PORT
size_t max_; // 最大连接数
std::list<connect_client*> pool_; // 连接池集合
bool alive_; // 是否属正常
time_t last_dead_; // 该连接池对象上次不可用时的时间截
thread_mutex lock_; // 访问 pool_ 时的互斥锁
int conn_timeout_ = 30; // 网络连接超时时间(秒)
int rw_timeout_ = 30; // 网络 IO 超时时间(秒)
};
}
#endif //OCEANSTAR_HTTP_CONNECT_POOL_H
使用
//
// Created by oceanstar on 2021/8/18.
//
#ifndef OCEANSTAR_HTTP_HTTP_REQUEST_POOL_H
#define OCEANSTAR_HTTP_HTTP_REQUEST_POOL_H
#include <connpool/connect_pool.h>
#include "http_request.h"
namespace oceanstar{
class http_request_pool : public connect_pool{
public:
/**
* 构造函数
* @param addr {const char*} 服务器监听地址,格式:ip:port(domain:port)
* @param count {size_t} 连接池最大连接个数限制,当该值为 0 时则没有限制
* @param idx {size_t} 该连接池对象在集合中的下标位置(从 0 开始)
*/
http_request_pool(const char* addr, size_t count) : connect_pool(addr, count){
}
~http_request_pool() = default;;
protected:
// 基类纯虚函数,该函数返回后由基类设置该连接池的网络连接及网络 IO 超时时间
virtual connect_client* create_connect(){
http_request* req = new http_request(addr_, conn_timeout_, rw_timeout_);
return req;
}
};
}
#endif //OCEANSTAR_HTTP_HTTP_REQUEST_POOL_H
还没有评论,来说两句吧...