2 * @brief Generic TCP/IP socket based server base class.
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
25 # include "remoteconnection.h"
26 # define SOCKET_INITIALIZER_MIXIN : private WinsockInitializer
28 # define SOCKET_INITIALIZER_MIXIN
31 #if defined __CYGWIN__ || defined __WIN32__
32 # include "safewindows.h" // Only for HANDLE!
35 #include <xapian/visibility.h>
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.
55 /** The socket we're listening on. */
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 static int get_listening_socket(const std::string
& host
, int port
,
67 #if defined __CYGWIN__ || defined __WIN32__
73 /** Should we produce output when connections are made or lost? */
76 /** Accept a connection and return the file descriptor for it. */
77 int accept_connection();
80 /** Construct a TcpServer and start listening for connections.
82 * @param host The hostname or address for the interface to listen on
83 * (or "" to listen on all interfaces).
84 * @param port The TCP port number to listen on.
85 * @param tcp_nodelay If true, enable TCP_NODELAY option.
86 * @param verbose Should we produce output when connections are
89 TcpServer(const std::string
&host
, int port
, bool tcp_nodelay
,
95 /** Accept connections and service requests indefinitely.
97 * This method runs the TcpServer as a daemon which accepts a connection
98 * and forks itself (or creates a new thread under Windows) to serve the
99 * request while continuing to listen for more connections.
103 /** Accept a single connection, service requests on it, then stop. */
106 /// Handle a single connection on an already connected socket.
107 virtual void handle_one_connection(int socket
) = 0;
110 #endif // XAPIAN_INCLUDED_TCPSERVER_H