[honey] Fix portability to systems without pread()
[xapian.git] / xapian-core / net / tcpserver.h
blob043db0cfeea9c368c0ce91cfb0d8d31aa79e70d2
1 /** @file tcpserver.h
2 * @brief Generic TCP/IP socket based server base class.
3 */
4 /* Copyright (C) 2007,2008 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_TCPSERVER_H
22 #define XAPIAN_INCLUDED_TCPSERVER_H
24 #ifdef __WIN32__
25 # include "remoteconnection.h"
26 # define SOCKET_INITIALIZER_MIXIN : private WinsockInitializer
27 #else
28 # define SOCKET_INITIALIZER_MIXIN
29 #endif
31 #if defined __CYGWIN__ || defined __WIN32__
32 # include "safewindows.h" // Only for HANDLE!
33 #endif
35 #include <xapian/visibility.h>
37 #include <string>
39 /** TCP/IP socket based server for RemoteDatabase.
41 * This class implements the server used by xapian-tcpsrv.
43 class XAPIAN_VISIBILITY_DEFAULT TcpServer SOCKET_INITIALIZER_MIXIN {
44 /// Don't allow assignment.
45 void operator=(const TcpServer &);
47 /// Don't allow copying.
48 TcpServer(const TcpServer &);
50 #if defined __CYGWIN__ || defined __WIN32__
51 /// Mutex to stop two TcpServers running on the same port.
52 HANDLE mutex;
53 #endif
55 /** The socket we're listening on. */
56 int listen_socket;
58 /** Create a listening socket ready to accept connections.
60 * @param host hostname or address to listen on or an empty string to
61 * accept connections on any interface.
62 * @param port TCP port to listen on.
63 * @param tcp_nodelay If true, enable TCP_NODELAY option.
65 XAPIAN_VISIBILITY_INTERNAL
66 static int get_listening_socket(const std::string & host, int port,
67 bool tcp_nodelay
68 #if defined __CYGWIN__ || defined __WIN32__
69 , HANDLE &mutex
70 #endif
73 protected:
74 /** Should we produce output when connections are made or lost? */
75 bool verbose;
77 /** Accept a connection and return the filedescriptor for it. */
78 XAPIAN_VISIBILITY_INTERNAL
79 int accept_connection();
81 public:
82 /** Construct a TcpServer and start listening for connections.
84 * @param host The hostname or address for the interface to listen on
85 * (or "" to listen on all interfaces).
86 * @param port The TCP port number to listen on.
87 * @param tcp_nodelay If true, enable TCP_NODELAY option.
88 * @param verbose Should we produce output when connections are
89 * made or lost?
91 TcpServer(const std::string &host, int port, bool tcp_nodelay,
92 bool verbose);
94 /** Destructor. */
95 virtual ~TcpServer();
97 /** Accept connections and service requests indefinitely.
99 * This method runs the TcpServer as a daemon which accepts a connection
100 * and forks itself (or creates a new thread under Windows) to serve the
101 * request while continuing to listen for more connections.
103 void run();
105 /** Accept a single connection, service requests on it, then stop. */
106 void run_once();
108 /// Handle a single connection on an already connected socket.
109 virtual void handle_one_connection(int socket) = 0;
112 #endif // XAPIAN_INCLUDED_TCPSERVER_H