Commit | Line | Data |
---|---|---|
ac7fdc22 GE |
1 | /*************************************************************************** |
2 | * Copyright (C) 2006 by Gerd v. Egidy * | |
3 | * gve@intra2net.com * | |
4 | * * | |
5 | * This library is free software; you can redistribute it and/or modify * | |
6 | * it under the terms of the GNU Lesser General Public License version * | |
7 | * 2.1 as published by the Free Software Foundation. * | |
8 | * * | |
9 | * This library is distributed in the hope that it will be useful, * | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
12 | * GNU Lesser General Public License for more details. * | |
13 | * * | |
14 | * You should have received a copy of the GNU Lesser General Public * | |
15 | * License along with this program; if not, write to the * | |
16 | * Free Software Foundation, Inc., * | |
17 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * | |
18 | ***************************************************************************/ | |
19 | #ifndef __LIBT2N_SERVER | |
20 | #define __LIBT2N_SERVER | |
21 | ||
22 | #include <iostream> | |
23 | #include <string> | |
24 | #include <map> | |
25 | ||
a11e19b7 GE |
26 | #include "connection.hxx" |
27 | #include "types.hxx" | |
28 | ||
ac7fdc22 GE |
29 | namespace libt2n |
30 | { | |
31 | ||
aa499d20 GE |
32 | class server; |
33 | ||
59adb9e2 | 34 | /** |
94247295 GE |
35 | @brief connection on a server |
36 | ||
37 | on a server every connection to a client is represented as server_connection. | |
38 | a server_connection is abstract, derived classes like socket_server_connection are used. | |
59adb9e2 | 39 | */ |
a11e19b7 | 40 | class server_connection : public connection |
ac7fdc22 | 41 | { |
94247295 GE |
42 | friend class server; |
43 | ||
ac7fdc22 GE |
44 | private: |
45 | int timeout; | |
46 | int last_action_time; | |
aa499d20 | 47 | unsigned int connection_id; |
ac7fdc22 | 48 | |
94247295 GE |
49 | void set_server(server* _my_server) |
50 | { my_server=_my_server; } | |
51 | ||
52 | void set_id(unsigned int _connection_id) | |
53 | { connection_id=_connection_id; } | |
54 | ||
ac7fdc22 | 55 | protected: |
aa499d20 | 56 | server *my_server; |
aa499d20 | 57 | |
a7170401 GE |
58 | server_connection(int _timeout); |
59 | ||
60 | std::ostream* get_logstream(log_level_values level); | |
61 | ||
ac7fdc22 | 62 | public: |
ac7fdc22 GE |
63 | void check_timeout(); |
64 | void reset_timeout(); | |
65 | void set_timeout(int _timeout) | |
66 | { timeout=_timeout; } | |
67 | ||
94247295 | 68 | /// get the id of this connection within the server object |
aa499d20 GE |
69 | unsigned int get_id() |
70 | { return connection_id; } | |
ac7fdc22 GE |
71 | }; |
72 | ||
59adb9e2 | 73 | /** |
94247295 GE |
74 | @brief server base class |
75 | ||
76 | constitutes a server. is abstract, use derived classes like socket_server. | |
59adb9e2 | 77 | */ |
ac7fdc22 GE |
78 | class server |
79 | { | |
ac7fdc22 GE |
80 | private: |
81 | int default_timeout; | |
82 | log_level_values log_level; | |
83 | std::ostream *logstream; | |
84 | ||
85 | unsigned int next_id; | |
ac7fdc22 GE |
86 | |
87 | protected: | |
a11e19b7 | 88 | std::map<unsigned int, server_connection*> connections; |
aa499d20 | 89 | |
a7170401 | 90 | server(); |
ac7fdc22 | 91 | |
94247295 GE |
92 | virtual bool fill_connection_buffers(void)=0; |
93 | ||
a11e19b7 | 94 | int add_connection(server_connection* newconn); |
04e6b271 | 95 | |
ac7fdc22 GE |
96 | public: |
97 | virtual ~server(); | |
98 | ||
94247295 | 99 | /// set the default timeout for new client connections |
ac7fdc22 GE |
100 | void set_default_timeout(int _default_timeout) |
101 | { default_timeout=_default_timeout; } | |
94247295 GE |
102 | |
103 | /// get the current default timeout for client connections | |
04e6b271 GE |
104 | int get_default_timeout(void) |
105 | { return default_timeout; } | |
ac7fdc22 | 106 | |
a7170401 | 107 | void set_logging(std::ostream *_logstream, log_level_values _log_level); |
ac7fdc22 | 108 | |
a11e19b7 | 109 | server_connection* get_connection(unsigned int conn_id); |
ac7fdc22 | 110 | |
94247295 GE |
111 | /** @brief look for new data on all open connections, accept new connections |
112 | @param usec_timeout wait until new data is found, max timeout usecs. | |
113 | -1: wait endless | |
114 | NULL: no timeout | |
115 | @retval true if new data was found (does not mean that the received data | |
116 | is a complete packet though) | |
117 | */ | |
118 | virtual bool fill_buffer(long long usec_timeout=-1)=0; | |
119 | ||
a11e19b7 | 120 | void cleanup(); |
ac7fdc22 | 121 | |
94247295 GE |
122 | /** @brief get a complete data packet from any client. The packet is removed from the |
123 | connection buffer. | |
124 | @param[out] data the data package | |
125 | @retval true if packet found | |
126 | */ | |
a11e19b7 GE |
127 | bool get_packet(std::string& data) |
128 | { unsigned int x; return get_packet(data,x); } | |
ac7fdc22 | 129 | |
94247295 | 130 | bool get_packet(std::string& data, unsigned int& conn_id); |
0cf4dc9b | 131 | |
a7170401 | 132 | std::ostream* get_logstream(log_level_values level); |
ac7fdc22 GE |
133 | }; |
134 | ||
135 | } | |
136 | ||
137 | #endif |