[ci] Fix clang-santisers job for GHA change
[xapian.git] / xapian-core / net / tcpserver.h
blob838f632923d7e986143b203917fd8482ad9adea1
1 /** @file
2 * @brief Generic TCP/IP socket based server base class.
3 */
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
24 #ifdef __WIN32__
25 # include "socket_utils.h"
26 # define SOCKET_INITIALIZER_MIXIN : private WinsockInitializer
27 #else
28 # define SOCKET_INITIALIZER_MIXIN
29 #endif
31 #include <xapian/visibility.h>
33 #include <string>
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. */
44 int listener;
46 protected:
47 /** Should we produce output when connections are made or lost? */
48 bool verbose;
50 /** Accept a connection and return the file descriptor for it. */
51 XAPIAN_VISIBILITY_INTERNAL
52 int accept_connection();
54 public:
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
62 * made or lost?
64 TcpServer(const std::string& host, int port, bool tcp_nodelay,
65 bool verbose_);
67 /** Destructor.
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
71 * base class.
73 ~TcpServer();
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.
81 void run();
83 /** Accept a single connection, service requests on it, then stop. */
84 void run_once();
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