1 #include "socket_pool.h"
3 SocketPool::SocketPool(int max_sockets,
4 asio::io_service &io_svc
7 _max_sockets(max_sockets),
12 void SocketPool::setEndpoint(const tcp::endpoint &endpoint)
17 void SocketPool::cancelAndClear()
19 for (set<shared_ptr<tcp::socket> >::iterator i = _set.begin();
26 while (!_queue.empty()) _queue.pop();
30 shared_ptr<tcp::socket> SocketPool::getSocket()
32 mutex::scoped_lock lock(_sockets_lock);
34 while (_queue.size() == 0 && _issued >= _max_sockets)
35 _sockets_non_empty.wait(lock);
39 shared_ptr<tcp::socket> socket = _queue.front();
47 error_code error = asio::error::host_not_found;
49 shared_ptr<tcp::socket> socket(new tcp::socket(_io_service));
50 socket->connect(_endpoint, error);
52 if (error) throw syserr::system_error(error);
60 void SocketPool::putSocket(shared_ptr<tcp::socket> socket)
62 mutex::scoped_lock lock(_sockets_lock);
64 if (!socket->is_open())
66 cerr << "socket closed\n";
75 _sockets_non_empty.notify_one();
78 SocketCheckout::SocketCheckout(SocketPool *pool)
79 :_socket(pool->getSocket()),
84 SocketCheckout::~SocketCheckout()
86 _pool->putSocket(_socket);
88 tcp::socket& SocketCheckout::operator*()
93 tcp::socket* SocketCheckout::operator->()
98 shared_ptr<tcp::socket>& SocketCheckout::socket()