Commit | Line | Data |
---|---|---|
19facd85 TJ |
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 | */ | |
ac7fdc22 GE |
22 | #ifndef __LIBT2N_SOCKET_SERVER |
23 | #define __LIBT2N_SOCKET_SERVER | |
24 | ||
25 | #include <sys/types.h> | |
644c4d26 | 26 | #include <string> |
1fdd03d2 | 27 | #include <set> |
ac7fdc22 GE |
28 | |
29 | #include "server.hxx" | |
a11e19b7 GE |
30 | #include "socket_handler.hxx" |
31 | #include "types.hxx" | |
ac7fdc22 GE |
32 | |
33 | namespace libt2n | |
34 | { | |
35 | ||
94247295 GE |
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. | |
59adb9e2 | 44 | */ |
644c4d26 | 45 | class socket_server : public socket_handler, public server |
ac7fdc22 | 46 | { |
94247295 GE |
47 | friend class socket_server_connection; |
48 | ||
ac7fdc22 | 49 | private: |
ac7fdc22 | 50 | fd_set connection_set; |
0cf4dc9b | 51 | std::string unix_path; |
1fdd03d2 | 52 | std::set<int> sockets_set; |
0cf4dc9b | 53 | |
cc68aabb GE |
54 | void start_listening(); |
55 | ||
0cf4dc9b | 56 | void new_connection(); |
ac7fdc22 | 57 | |
94247295 GE |
58 | bool fill_connection_buffers(); |
59 | void remove_connection_socket(int sock); | |
60 | ||
a11e19b7 | 61 | protected: |
a7170401 GE |
62 | std::ostream* get_logstream(log_level_values level) |
63 | { return server::get_logstream(level); } | |
a11e19b7 | 64 | |
ac7fdc22 | 65 | public: |
644c4d26 GE |
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=""); | |
ac7fdc22 GE |
68 | |
69 | ~socket_server(); | |
70 | ||
45a2ebc9 | 71 | bool fill_buffer(long long usec_timeout=-1,long long* usec_timeout_remaining=NULL); |
1fdd03d2 CR |
72 | std::set<int> get_sockets_set() |
73 | { return sockets_set; }; | |
ac7fdc22 GE |
74 | }; |
75 | ||
94247295 GE |
76 | /** @brief Socket based connection |
77 | ||
78 | This class is used within a socket_server to represent the connection to each client. | |
59adb9e2 | 79 | */ |
644c4d26 | 80 | class socket_server_connection : public socket_handler, public server_connection |
ac7fdc22 | 81 | { |
04e6b271 GE |
82 | friend class socket_server; |
83 | ||
ac7fdc22 | 84 | private: |
a11e19b7 | 85 | socket_server_connection(int _sock, socket_type_value _stype, int _timeout) |
1eed3f4c | 86 | : socket_handler(_sock,_stype), server_connection(_timeout) |
a11e19b7 | 87 | { } |
aa499d20 | 88 | |
56f3994d TJ |
89 | ~socket_server_connection(); |
90 | ||
a7170401 GE |
91 | std::ostream* get_logstream(log_level_values level) |
92 | { return server_connection::get_logstream(level); } | |
ac7fdc22 | 93 | |
a11e19b7 | 94 | void real_write(const std::string& data) |
644c4d26 | 95 | { socket_write(data); } |
ac7fdc22 GE |
96 | |
97 | public: | |
ce44999e | 98 | bool fill_buffer(long long usec_timeout=-1,long long* usec_timeout_remaining=NULL); |
ac7fdc22 | 99 | |
56f3994d | 100 | virtual void close(); |
ac7fdc22 GE |
101 | }; |
102 | ||
103 | } | |
104 | ||
105 | #endif |