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> |
ac7fdc22 GE |
27 | |
28 | #include "server.hxx" | |
a11e19b7 GE |
29 | #include "socket_handler.hxx" |
30 | #include "types.hxx" | |
ac7fdc22 GE |
31 | |
32 | namespace libt2n | |
33 | { | |
34 | ||
94247295 GE |
35 | class socket_server_connection; |
36 | ||
37 | /** @brief Socket based server class | |
38 | ||
39 | Use this class to instantiate a server listening for client connections. | |
40 | Call fill_buffer() to read data from the network and get_packet() to retrieve | |
41 | this data. Don't forget to call cleanup() from time to time to remove closed | |
42 | connections and close idle ones. | |
59adb9e2 | 43 | */ |
644c4d26 | 44 | class socket_server : public socket_handler, public server |
ac7fdc22 | 45 | { |
94247295 GE |
46 | friend class socket_server_connection; |
47 | ||
ac7fdc22 | 48 | private: |
ac7fdc22 | 49 | fd_set connection_set; |
0cf4dc9b GE |
50 | std::string unix_path; |
51 | ||
cc68aabb GE |
52 | void start_listening(); |
53 | ||
0cf4dc9b | 54 | void new_connection(); |
ac7fdc22 | 55 | |
94247295 GE |
56 | bool fill_connection_buffers(); |
57 | void remove_connection_socket(int sock); | |
58 | ||
a11e19b7 | 59 | protected: |
a7170401 GE |
60 | std::ostream* get_logstream(log_level_values level) |
61 | { return server::get_logstream(level); } | |
a11e19b7 | 62 | |
ac7fdc22 | 63 | public: |
644c4d26 GE |
64 | socket_server(int port, const std::string& ip="0.0.0.0"); |
65 | socket_server(const std::string& path, mode_t filemode=00770, const std::string& user="", const std::string& group=""); | |
ac7fdc22 GE |
66 | |
67 | ~socket_server(); | |
68 | ||
45a2ebc9 | 69 | bool fill_buffer(long long usec_timeout=-1,long long* usec_timeout_remaining=NULL); |
ac7fdc22 GE |
70 | }; |
71 | ||
94247295 GE |
72 | /** @brief Socket based connection |
73 | ||
74 | This class is used within a socket_server to represent the connection to each client. | |
59adb9e2 | 75 | */ |
644c4d26 | 76 | class socket_server_connection : public socket_handler, public server_connection |
ac7fdc22 | 77 | { |
04e6b271 GE |
78 | friend class socket_server; |
79 | ||
ac7fdc22 | 80 | private: |
a11e19b7 | 81 | socket_server_connection(int _sock, socket_type_value _stype, int _timeout) |
1eed3f4c | 82 | : socket_handler(_sock,_stype), server_connection(_timeout) |
a11e19b7 | 83 | { } |
aa499d20 | 84 | |
56f3994d TJ |
85 | ~socket_server_connection(); |
86 | ||
a7170401 GE |
87 | std::ostream* get_logstream(log_level_values level) |
88 | { return server_connection::get_logstream(level); } | |
ac7fdc22 | 89 | |
a11e19b7 | 90 | void real_write(const std::string& data) |
644c4d26 | 91 | { socket_write(data); } |
ac7fdc22 GE |
92 | |
93 | public: | |
45a2ebc9 GE |
94 | bool fill_buffer(long long usec_timeout=-1,long long* usec_timeout_remaining=NULL) |
95 | { return socket_handler::fill_buffer(buffer,usec_timeout,usec_timeout_remaining); } | |
ac7fdc22 | 96 | |
56f3994d | 97 | virtual void close(); |
ac7fdc22 GE |
98 | }; |
99 | ||
100 | } | |
101 | ||
102 | #endif |