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> | |
28cb45a5 GE |
25 | #include <vector> |
26 | #include <list> | |
27 | ||
28 | #include <boost/function.hpp> | |
ac7fdc22 | 29 | |
a11e19b7 GE |
30 | #include "connection.hxx" |
31 | #include "types.hxx" | |
32 | ||
ac7fdc22 GE |
33 | namespace libt2n |
34 | { | |
35 | ||
aa499d20 GE |
36 | class server; |
37 | ||
59adb9e2 | 38 | /** |
94247295 GE |
39 | @brief connection on a server |
40 | ||
41 | on a server every connection to a client is represented as server_connection. | |
42 | a server_connection is abstract, derived classes like socket_server_connection are used. | |
59adb9e2 | 43 | */ |
a11e19b7 | 44 | class server_connection : public connection |
ac7fdc22 | 45 | { |
94247295 GE |
46 | friend class server; |
47 | ||
ac7fdc22 GE |
48 | private: |
49 | int timeout; | |
50 | int last_action_time; | |
aa499d20 | 51 | unsigned int connection_id; |
ac7fdc22 | 52 | |
94247295 GE |
53 | void set_server(server* _my_server) |
54 | { my_server=_my_server; } | |
55 | ||
56 | void set_id(unsigned int _connection_id) | |
57 | { connection_id=_connection_id; } | |
58 | ||
ac7fdc22 | 59 | protected: |
aa499d20 | 60 | server *my_server; |
aa499d20 | 61 | |
a7170401 GE |
62 | server_connection(int _timeout); |
63 | ||
64 | std::ostream* get_logstream(log_level_values level); | |
65 | ||
ac7fdc22 | 66 | public: |
ac7fdc22 GE |
67 | void check_timeout(); |
68 | void reset_timeout(); | |
69 | void set_timeout(int _timeout) | |
70 | { timeout=_timeout; } | |
71 | ||
94247295 | 72 | /// get the id of this connection within the server object |
aa499d20 GE |
73 | unsigned int get_id() |
74 | { return connection_id; } | |
ac7fdc22 GE |
75 | }; |
76 | ||
59adb9e2 | 77 | /** |
94247295 GE |
78 | @brief server base class |
79 | ||
80 | constitutes a server. is abstract, use derived classes like socket_server. | |
59adb9e2 | 81 | */ |
ac7fdc22 GE |
82 | class server |
83 | { | |
ac7fdc22 GE |
84 | private: |
85 | int default_timeout; | |
86 | log_level_values log_level; | |
87 | std::ostream *logstream; | |
88 | ||
28cb45a5 GE |
89 | /// vector initialized for all callback-types, all elements in each list will be called |
90 | std::vector<std::list<boost::function<void (server_connection*)> > > callbacks; | |
91 | ||
ac7fdc22 | 92 | unsigned int next_id; |
ac7fdc22 GE |
93 | |
94 | protected: | |
a11e19b7 | 95 | std::map<unsigned int, server_connection*> connections; |
aa499d20 | 96 | |
a7170401 | 97 | server(); |
ac7fdc22 | 98 | |
94247295 GE |
99 | virtual bool fill_connection_buffers(void)=0; |
100 | ||
a11e19b7 | 101 | int add_connection(server_connection* newconn); |
04e6b271 | 102 | |
28cb45a5 GE |
103 | void do_callbacks(callback_event_type event, server_connection* conn); |
104 | ||
ac7fdc22 GE |
105 | public: |
106 | virtual ~server(); | |
107 | ||
94247295 | 108 | /// set the default timeout for new client connections |
ac7fdc22 GE |
109 | void set_default_timeout(int _default_timeout) |
110 | { default_timeout=_default_timeout; } | |
94247295 GE |
111 | |
112 | /// get the current default timeout for client connections | |
04e6b271 GE |
113 | int get_default_timeout(void) |
114 | { return default_timeout; } | |
ac7fdc22 | 115 | |
a7170401 | 116 | void set_logging(std::ostream *_logstream, log_level_values _log_level); |
ac7fdc22 | 117 | |
a11e19b7 | 118 | server_connection* get_connection(unsigned int conn_id); |
ac7fdc22 | 119 | |
28cb45a5 GE |
120 | void add_callback(callback_event_type event, const boost::function<void (server_connection*)>& func); |
121 | ||
94247295 GE |
122 | /** @brief look for new data on all open connections, accept new connections |
123 | @param usec_timeout wait until new data is found, max timeout usecs. | |
124 | -1: wait endless | |
125 | NULL: no timeout | |
126 | @retval true if new data was found (does not mean that the received data | |
127 | is a complete packet though) | |
128 | */ | |
129 | virtual bool fill_buffer(long long usec_timeout=-1)=0; | |
130 | ||
a11e19b7 | 131 | void cleanup(); |
ac7fdc22 | 132 | |
94247295 GE |
133 | /** @brief get a complete data packet from any client. The packet is removed from the |
134 | connection buffer. | |
135 | @param[out] data the data package | |
136 | @retval true if packet found | |
137 | */ | |
a11e19b7 GE |
138 | bool get_packet(std::string& data) |
139 | { unsigned int x; return get_packet(data,x); } | |
ac7fdc22 | 140 | |
94247295 | 141 | bool get_packet(std::string& data, unsigned int& conn_id); |
0cf4dc9b | 142 | |
a7170401 | 143 | std::ostream* get_logstream(log_level_values level); |
ac7fdc22 GE |
144 | }; |
145 | ||
146 | } | |
147 | ||
148 | #endif |