TCP客户端任务线程池,主要封装了一个线程处理多个连接的线程池框架。用到了前文提到的TCPClientTask类。
class TCPClientTaskPool : private Nocopy
{
public:
bool insert_task(TCPClientTask* task);
void remove_task(TCPClientTask* task);
TCPClientTask* get_task(std::string&ip, int port);
...
private:
std::list<TCPClientTask* > tasks; //任务链表
CheckConThread * checkconthread; //连接检测线程
CheckWaitThread *checkwaitthread; //等待连接返回信息线程
ThreadPool mainthreads; //处理连接成功后主线程,使用ThreadPool的add添加TCPClientTaskThread指针
...
};
class CheckConThread : public Thread
{
public:
...
virtual void run();
private:
TCPClientTaskPool* pool;
};
class CheckWaitThread : public Thread, public TCPClientTaskQue
{
public:
...
virtual void run();
private:
TCPClientTaskPool* pool;
std::list<TCPClientTask* > tasks; //任务链表
void add_task(TCPClientTask* task);
void remove_task(TCPClientTask* task);
...
};
//和上面的CheckWaitThread类似
class TCPClientTaskThread : public Thread, public TCPClientTaskQue
{
public:
...
virtual void run();
private:
TCPClientTaskPool* pool;
std::list<TCPClientTask* > tasks; //任务链表
void add_task(TCPClientTask* task);
void remove_task(TCPClientTask* task);
...
};
//TCP任务客户端缓冲队列,把TCPClientTask*放进该缓冲队列
class TCPClientTaskQue
{
public:
void add_task(TCPClientTask* task);
...
private:
std::queue<TCPClientTask*, std::deque<TCPClientTask*> > queue;
...
};