[honey] Fix portability to systems without pread()
[xapian.git] / xapian-core / net / remotetcpclient.h
blob9da59a82fdc2ac4a2a22c9058e6eec032084ba0e
1 /** @file remotetcpclient.h
2 * @brief TCP/IP socket based RemoteDatabase implementation
3 */
4 /* Copyright (C) 2007,2008,2010,2011,2014 Olly Betts
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifndef XAPIAN_INCLUDED_REMOTETCPCLIENT_H
22 #define XAPIAN_INCLUDED_REMOTETCPCLIENT_H
24 #include "backends/remote/remote-database.h"
26 #ifdef __WIN32__
27 # define SOCKET_INITIALIZER_MIXIN private WinsockInitializer,
28 #else
29 # define SOCKET_INITIALIZER_MIXIN
30 #endif
32 /** TCP/IP socket based RemoteDatabase implementation.
34 * Connects via TCP/IP to an instance of xapian-tcpsrv.
36 class RemoteTcpClient : SOCKET_INITIALIZER_MIXIN public RemoteDatabase {
37 /// Don't allow assignment.
38 void operator=(const RemoteTcpClient &);
40 /// Don't allow copying.
41 RemoteTcpClient(const RemoteTcpClient &);
43 /** Attempt to open a TCP/IP socket connection to xapian-tcpsrv.
45 * Connect to xapian-tcpsrv running on port @a port of host @a hostname.
46 * Give up trying to connect after @a timeout_connect seconds.
48 * Note: this method is called early on during class construction before
49 * any member variables or even the base class have been initialised.
50 * To help avoid accidentally trying to use member variables or call other
51 * methods which do, this method has been deliberately made "static".
53 static int open_socket(const std::string & hostname, int port,
54 double timeout_connect);
56 /** Get a context string for use when constructing Xapian::NetworkError.
58 * Note: this method is used from constructors so has been made static to
59 * avoid problems with trying to use uninitialised member variables. In
60 * particular, it can't be made a virtual method of the base class.
62 static std::string get_tcpcontext(const std::string & hostname, int port);
64 public:
65 /** Constructor.
67 * Attempts to open a TCP/IP connection to xapian-tcpsrv running on port
68 * @a port of host @a hostname.
70 * @param timeout_connect Timeout for trying to connect (in seconds).
71 * @param timeout Timeout during communication after successfully
72 * connecting (in seconds).
73 * @param writable Is this a WritableDatabase?
74 * @param flags Xapian::DB_RETRY_LOCK or 0.
76 RemoteTcpClient(const std::string & hostname, int port,
77 double timeout_, double timeout_connect, bool writable,
78 int flags)
79 : RemoteDatabase(open_socket(hostname, port, timeout_connect),
80 timeout_, get_tcpcontext(hostname, port),
81 writable, flags) { }
83 /** Destructor. */
84 ~RemoteTcpClient();
87 #endif // XAPIAN_INCLUDED_REMOTETCPCLIENT_H