File: tcpserver.h

package info (click to toggle)
xapian-core 1.4.3-2%2Bdeb9u3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 21,412 kB
  • sloc: cpp: 113,868; ansic: 8,723; sh: 4,433; perl: 836; makefile: 566; tcl: 317; python: 40
file content (110 lines) | stat: -rw-r--r-- 3,573 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/** @file tcpserver.h
 *  @brief Generic TCP/IP socket based server base class.
 */
/* Copyright (C) 2007,2008 Olly Betts
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

#ifndef XAPIAN_INCLUDED_TCPSERVER_H
#define XAPIAN_INCLUDED_TCPSERVER_H

#ifdef __WIN32__
# include "remoteconnection.h"
# define SOCKET_INITIALIZER_MIXIN : private WinsockInitializer
#else
# define SOCKET_INITIALIZER_MIXIN
#endif

#if defined __CYGWIN__ || defined __WIN32__
# include "safewindows.h" // Only for HANDLE!
#endif

#include <xapian/visibility.h>

#include <string>

/** TCP/IP socket based server for RemoteDatabase.
 *
 *  This class implements the server used by xapian-tcpsrv.
 */
class XAPIAN_VISIBILITY_DEFAULT TcpServer SOCKET_INITIALIZER_MIXIN {
    /// Don't allow assignment.
    void operator=(const TcpServer &);

    /// Don't allow copying.
    TcpServer(const TcpServer &);

#if defined __CYGWIN__ || defined __WIN32__
    /// Mutex to stop two TcpServers running on the same port.
    HANDLE mutex;
#endif

    /** The socket we're listening on. */
    int listen_socket;

    /** Create a listening socket ready to accept connections.
     *
     *  @param host	hostname or address to listen on or an empty string to
     *			accept connections on any interface.
     *  @param port	TCP port to listen on.
     *  @param tcp_nodelay	If true, enable TCP_NODELAY option.
     */
    static int get_listening_socket(const std::string & host, int port,
				    bool tcp_nodelay
#if defined __CYGWIN__ || defined __WIN32__
				    , HANDLE &mutex
#endif
	    );

  protected:
    /** Should we produce output when connections are made or lost? */
    bool verbose;

    /** Accept a connection and return the filedescriptor for it. */
    int accept_connection();

  public:
    /** Construct a TcpServer and start listening for connections.
     *
     *  @param host	The hostname or address for the interface to listen on
     *			(or "" to listen on all interfaces).
     *  @param port	The TCP port number to listen on.
     *  @param tcp_nodelay	If true, enable TCP_NODELAY option.
     *	@param verbose	Should we produce output when connections are
     *			made or lost?
     */
    TcpServer(const std::string &host, int port, bool tcp_nodelay,
	      bool verbose);

    /** Destructor. */
    virtual ~TcpServer();

    /** Accept connections and service requests indefinitely.
     *
     *  This method runs the TcpServer as a daemon which accepts a connection
     *  and forks itself (or creates a new thread under Windows) to serve the
     *  request while continuing to listen for more connections.
     */
    void run();

    /** Accept a single connection, service requests on it, then stop.  */
    void run_once();

    /// Handle a single connection on an already connected socket.
    virtual void handle_one_connection(int socket) = 0;
};

#endif  // XAPIAN_INCLUDED_TCPSERVER_H