Replace socket_handler::fill_buffer() recursion with loop (#8389)
[libt2n] / src / socket_server.hxx
1 /*
2 Copyright (C) 2006 by Intra2net AG - Gerd v. Egidy
3
4 The software in this package is distributed under the GNU General
5 Public License version 2 (with a special exception described below).
6
7 A copy of GNU General Public License (GPL) is included in this distribution,
8 in the file COPYING.GPL.
9
10 As a special exception, if other files instantiate templates or use macros
11 or inline functions from this file, or you compile this file and link it
12 with other works to produce a work based on this file, this file
13 does not by itself cause the resulting work to be covered
14 by the GNU General Public License.
15
16 However the source code for this file must still be made available
17 in accordance with section (3) of the GNU General Public License.
18
19 This exception does not invalidate any other reasons why a work based
20 on this file might be covered by the GNU General Public License.
21 */
22 #ifndef __LIBT2N_SOCKET_SERVER
23 #define __LIBT2N_SOCKET_SERVER
24
25 #include <sys/types.h>
26 #include <string>
27 #include <set>
28
29 #include "server.hxx"
30 #include "socket_handler.hxx"
31 #include "types.hxx"
32
33 namespace libt2n
34 {
35
36 class socket_server_connection;
37
38 /** @brief Socket based server class
39
40     Use this class to instantiate a server listening for client connections.
41     Call fill_buffer() to read data from the network and get_packet() to retrieve
42     this data. Don't forget to call cleanup() from time to time to remove closed
43     connections and close idle ones.
44 */
45 class socket_server : public socket_handler, public server
46 {
47     friend class socket_server_connection;
48
49     private:
50         fd_set connection_set;
51         std::string unix_path;
52         std::set<int> sockets_set;
53
54         void start_listening();
55
56         void new_connection();
57
58         bool fill_connection_buffers();
59         void remove_connection_socket(int sock);
60
61     protected:
62         std::ostream* get_logstream(log_level_values level)
63             { return server::get_logstream(level); }
64
65     public:
66         socket_server(int port, const std::string& ip="0.0.0.0");
67         socket_server(const std::string& path, mode_t filemode=00770, const std::string& user="", const std::string& group="");
68
69         ~socket_server();
70
71         bool fill_buffer(long long usec_timeout=-1,long long* usec_timeout_remaining=NULL);
72         std::set<int> get_sockets_set()
73             { return sockets_set; };
74 };
75
76 /** @brief Socket based connection
77
78     This class is used within a socket_server to represent the connection to each client.
79 */
80 class socket_server_connection : public socket_handler, public server_connection
81 {
82     friend class socket_server;
83
84     private:
85         socket_server_connection(int _sock, socket_type_value _stype, int _timeout)
86            : socket_handler(_sock,_stype), server_connection(_timeout)
87            { }
88
89         ~socket_server_connection();
90
91         std::ostream* get_logstream(log_level_values level)
92             { return server_connection::get_logstream(level); }
93
94         void real_write(const std::string& data)
95             { socket_write(data); }
96
97     public:
98         bool fill_buffer(long long usec_timeout=-1,long long* usec_timeout_remaining=NULL);
99
100         virtual void close();
101 };
102
103 }
104
105 #endif