Update NEWS from git log
[xapian.git] / xapian-core / net / tcpserver.h
blob209c5611a0b97aca0f8e6671d493c2e1e0ffe676
1 /** @file
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 = NULL;
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 static int get_listening_socket(const std::string & host, int port,
66 bool tcp_nodelay
67 #if defined __CYGWIN__ || defined __WIN32__
68 , HANDLE &mutex
69 #endif
72 protected:
73 /** Should we produce output when connections are made or lost? */
74 bool verbose;
76 /** Accept a connection and return the file descriptor for it. */
77 int accept_connection();
79 public:
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
87 * made or lost?
89 TcpServer(const std::string &host, int port, bool tcp_nodelay,
90 bool verbose);
92 /** Destructor. */
93 virtual ~TcpServer();
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.
101 void run();
103 /** Accept a single connection, service requests on it, then stop. */
104 void run_once();
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