2 * @brief Generic TCP/IP socket based server base class.
4 /* Copyright (C) 2007,2008,2023 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 "socket_utils.h"
26 # define SOCKET_INITIALIZER_MIXIN : private WinsockInitializer
28 # define SOCKET_INITIALIZER_MIXIN
31 #include <xapian/visibility.h>
35 /** Generic TCP/IP socket based server base class. */
36 class XAPIAN_VISIBILITY_DEFAULT TcpServer SOCKET_INITIALIZER_MIXIN
{
37 /// Don't allow assignment.
38 TcpServer
& operator=(const TcpServer
&) = delete;
40 /// Don't allow copying.
41 TcpServer(const TcpServer
&) = delete;
43 /** The socket we're listening on. */
47 /** Should we produce output when connections are made or lost? */
50 /** Accept a connection and return the file descriptor for it. */
51 XAPIAN_VISIBILITY_INTERNAL
52 int accept_connection();
55 /** Construct a TcpServer and start listening for connections.
57 * @param host The hostname or address for the interface to listen on
58 * (or "" to listen on all interfaces).
59 * @param port The TCP port number to listen on.
60 * @param tcp_nodelay If true, enable TCP_NODELAY option.
61 * @param verbose Should we produce output when connections are
64 TcpServer(const std::string
& host
, int port
, bool tcp_nodelay
,
69 * Note: We don't need a virtual destructor despite having a virtual
70 * method as we don't ever delete a subclass using a pointer to the
75 /** Accept connections and service requests indefinitely.
77 * This method runs the TcpServer as a daemon which accepts a connection
78 * and forks itself (or creates a new thread under Windows) to serve the
79 * request while continuing to listen for more connections.
83 /** Accept a single connection, service requests on it, then stop. */
86 /// Should we produce output when connections are made or lost?
87 bool get_verbose() const { return verbose
; }
89 /// Handle a single connection on an already connected socket.
90 virtual void handle_one_connection(int socket
) = 0;
93 #endif // XAPIAN_INCLUDED_TCPSERVER_H