Fix standard deviation estimation.
[bluesky.git] / kvstore / socket_pool.h
1 #ifndef _SOCKET_POOL_H_
2 #define _SOCKET_POOL_H_ 1
3
4 #include <iostream>
5 #include <set>
6 #include <queue>
7 #include <boost/shared_ptr.hpp>
8 #include <boost/thread.hpp>
9
10 #include "util.h"
11
12 using namespace std;
13 using namespace boost;
14
15
16 class SocketPool
17 {
18 public:
19     SocketPool(int max_streams,
20                asio::io_service &io_svc);
21     void setEndpoint(const tcp::endpoint &endpoint);
22     void cancelAndClear();
23     shared_ptr<tcp::socket> getSocket();
24     void putSocket(shared_ptr<tcp::socket> socket);
25 private:
26     int _issued;
27     int _max_sockets;
28     mutex _sockets_lock;
29     condition_variable _sockets_non_empty;
30     asio::io_service &_io_service;
31     tcp::endpoint _endpoint;
32     queue<shared_ptr<tcp::socket> > _queue;
33     set<shared_ptr<tcp::socket> > _set;
34 };
35
36 class SocketCheckout
37 {
38 public:
39     SocketCheckout(SocketPool *pool);
40     ~SocketCheckout();
41
42     tcp::socket& operator*();
43     tcp::socket* operator->();
44
45     shared_ptr<tcp::socket>& socket();
46
47 private:
48     shared_ptr<tcp::socket> _socket;
49     SocketPool *_pool;
50 };
51
52 #endif