1 /* Copyright (C) 2019 MariaDB Corporation
2 
3    This program is free software; you can redistribute it and/or
4    modify it under the terms of the GNU General Public License
5    as published by the Free Software Foundation; version 2 of
6    the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
16    MA 02110-1301, USA. */
17 
18 #ifndef _SOCKETPOOL_H_
19 #define _SOCKETPOOL_H_
20 
21 #include <boost/utility.hpp>
22 #include <boost/thread/mutex.hpp>
23 #include <boost/thread/condition_variable.hpp>
24 
25 #include "bytestream.h"
26 
27 namespace idbdatafile
28 {
29 
30 /* This should be renamed; it's more than a pool, it also does the low-level communication.  TBD. */
31 class SocketPool : public boost::noncopyable
32 {
33     public:
34         SocketPool();
35 
36         // the dtor will immediately close all sockets
37         virtual ~SocketPool();
38 
39         // 0 = success, -1 = failure.  Should this throw instead?
40         int send_recv(messageqcpp::ByteStream &to_send, messageqcpp::ByteStream *to_recv);
41 
42     private:
43         int getSocket();
44         void returnSocket(const int sock);
45         void remoteClosed(const int sock);
46 
47         std::vector<int> allSockets;
48         std::deque<int> freeSockets;
49         boost::mutex mutex;
50         boost::condition_variable socketAvailable;
51         uint maxSockets;
52         static const uint defaultSockets = 20;
53 };
54 
55 }
56 
57 
58 
59 #endif
60