libt2n: (gerd) add client timeouts & tests, hello peek missing
[libt2n] / src / server.hxx
CommitLineData
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
33namespace libt2n
34{
35
aa499d20
GE
36class 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 44class 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
6cda58a6
GE
59 /// vector initialized for all callback-types, all elements in each list will be called
60 std::vector<std::list<boost::function<void ()> > > callbacks;
61
ac7fdc22 62 protected:
aa499d20 63 server *my_server;
aa499d20 64
a7170401
GE
65 server_connection(int _timeout);
66
67 std::ostream* get_logstream(log_level_values level);
68
6cda58a6
GE
69 void do_callbacks(callback_event_type event);
70
ac7fdc22 71 public:
6cda58a6
GE
72 virtual ~server_connection();
73
ac7fdc22
GE
74 void check_timeout();
75 void reset_timeout();
76 void set_timeout(int _timeout)
77 { timeout=_timeout; }
78
94247295 79 /// get the id of this connection within the server object
aa499d20
GE
80 unsigned int get_id()
81 { return connection_id; }
6cda58a6
GE
82
83 void close();
84
85 void add_callback(callback_event_type event, const boost::function<void ()>& func);
ac7fdc22
GE
86};
87
59adb9e2 88/**
94247295
GE
89 @brief server base class
90
91 constitutes a server. is abstract, use derived classes like socket_server.
59adb9e2 92*/
ac7fdc22
GE
93class server
94{
ac7fdc22
GE
95 private:
96 int default_timeout;
97 log_level_values log_level;
98 std::ostream *logstream;
99
28cb45a5 100 /// vector initialized for all callback-types, all elements in each list will be called
6cda58a6 101 std::vector<std::list<boost::function<void (unsigned int)> > > callbacks;
28cb45a5 102
ac7fdc22 103 unsigned int next_id;
ac7fdc22
GE
104
105 protected:
a11e19b7 106 std::map<unsigned int, server_connection*> connections;
aa499d20 107
a7170401 108 server();
ac7fdc22 109
94247295
GE
110 virtual bool fill_connection_buffers(void)=0;
111
a11e19b7 112 int add_connection(server_connection* newconn);
04e6b271 113
6cda58a6 114 void do_callbacks(callback_event_type event, unsigned int conn_id);
28cb45a5 115
ac7fdc22
GE
116 public:
117 virtual ~server();
118
94247295 119 /// set the default timeout for new client connections
ac7fdc22
GE
120 void set_default_timeout(int _default_timeout)
121 { default_timeout=_default_timeout; }
94247295
GE
122
123 /// get the current default timeout for client connections
04e6b271
GE
124 int get_default_timeout(void)
125 { return default_timeout; }
ac7fdc22 126
a7170401 127 void set_logging(std::ostream *_logstream, log_level_values _log_level);
ac7fdc22 128
a11e19b7 129 server_connection* get_connection(unsigned int conn_id);
ac7fdc22 130
6cda58a6 131 void add_callback(callback_event_type event, const boost::function<void (unsigned int)>& func);
28cb45a5 132
45a2ebc9 133 /** @brief look for new data and store it in the local buffer
94247295
GE
134 @param usec_timeout wait until new data is found, max timeout usecs.
135 -1: wait endless
45a2ebc9
GE
136 0: return instantly
137 @param usec_timeout_remaining if non-NULL the function will write the
138 not used time to the given target
94247295
GE
139 @retval true if new data was found (does not mean that the received data
140 is a complete packet though)
141 */
45a2ebc9 142 virtual bool fill_buffer(long long usec_timeout=-1, long long* timeout_remaining=NULL)=0;
94247295 143
a11e19b7 144 void cleanup();
ac7fdc22 145
94247295
GE
146 /** @brief get a complete data packet from any client. The packet is removed from the
147 connection buffer.
148 @param[out] data the data package
149 @retval true if packet found
150 */
a11e19b7
GE
151 bool get_packet(std::string& data)
152 { unsigned int x; return get_packet(data,x); }
ac7fdc22 153
94247295 154 bool get_packet(std::string& data, unsigned int& conn_id);
0cf4dc9b 155
a7170401 156 std::ostream* get_logstream(log_level_values level);
ac7fdc22
GE
157};
158
159}
160
161#endif