1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
| #include "PoolSocket.h" #include <string.h> #include <pthread.h> #include <queue> #include <iostream> #include <unistd.h> #include <string.h> using namespace std;
struct PoolHandle { queue<TcpSocket*> sockList; int bounds;
string serverip; unsigned short serverport;
int connecttime; int sTimeout; pthread_mutex_t foo_mutex; };
PoolSocket::PoolSocket() { }
PoolSocket::~PoolSocket() { }
int PoolSocket::poolInit(PoolParam * param) { int ret = 0; PoolHandle *hdl = new PoolHandle; m_handle = hdl; if (hdl == NULL) { ret = MallocErr; return ret; }
hdl->serverip = param->serverip; hdl->serverport = param->serverport; hdl->connecttime = param->connecttime; hdl->bounds = param->bounds; hdl->sTimeout = 100;
pthread_mutex_init(&(hdl->foo_mutex), NULL);
pthread_mutex_lock(&(hdl->foo_mutex)); connectServer(); pthread_mutex_unlock(&(hdl->foo_mutex));
return ret; }
TcpSocket* PoolSocket::getConnect() { PoolHandle *hdl = static_cast<PoolHandle*>(m_handle); pthread_mutex_lock(&(hdl->foo_mutex));
if (hdl->sockList.size() == 0) { usleep(hdl->sTimeout); if (hdl->sockList.size() == 0) { return NULL; } } TcpSocket* sock = hdl->sockList.front(); hdl->sockList.pop(); cout << "取出一条连接, 剩余连接数: " << curConnSize() << endl; pthread_mutex_unlock(&(hdl->foo_mutex));
return sock; }
int PoolSocket::putConnect(TcpSocket* sock, bool isValid) { int ret = 0; PoolHandle *hdl = static_cast<PoolHandle*>(m_handle); pthread_mutex_lock(&(hdl->foo_mutex));
if (isValid) { hdl->sockList.push(sock); cout << "放回一条连接, 剩余连接数: " << curConnSize() << endl; } else { sock->disConnect(); delete sock; connectServer(false); cout << "修复一条连接, 剩余连接数: " << curConnSize() << endl; } pthread_mutex_unlock(&(hdl->foo_mutex));
return ret; }
void PoolSocket::poolDestory() { PoolHandle *hdl = static_cast<PoolHandle*>(m_handle); while (hdl->sockList.size() != 0) { TcpSocket* sock = hdl->sockList.front(); hdl->sockList.pop(); delete sock; } delete hdl; }
int PoolSocket::curConnSize() { PoolHandle *hdl = static_cast<PoolHandle*>(m_handle); return hdl->sockList.size(); }
void PoolSocket::connectServer(bool recursion) { PoolHandle *hdl = static_cast<PoolHandle*>(m_handle); if ((int)hdl->sockList.size() == hdl->bounds) { cout << "连接池对象初始化完毕, ^_^ ..." << endl; cout << "Poll Size: " << hdl->sockList.size() << endl; cout << "Poll bounds: " << hdl->bounds << endl; return; } TcpSocket* socket = new TcpSocket; char* ip = const_cast<char*>(hdl->serverip.data()); int ret = socket->connectToHost(ip, hdl->serverport, hdl->connecttime); if (ret == 0) { hdl->sockList.push(socket); cout << "Connect count: " << hdl->sockList.size() << endl; } else { cout << "连接服务器失败 - index: " << hdl->sockList.size()+1 << endl; delete socket; } if (recursion) { connectServer(); } }
|