Switch time() calls to monotonic clock calls (#7597)
[libt2n] / src / server.hxx
CommitLineData
19facd85
TJ
1/*
2Copyright (C) 2006 by Intra2net AG - Gerd v. Egidy
3
4The software in this package is distributed under the GNU General
5Public License version 2 (with a special exception described below).
6
7A copy of GNU General Public License (GPL) is included in this distribution,
8in the file COPYING.GPL.
9
10As a special exception, if other files instantiate templates or use macros
11or inline functions from this file, or you compile this file and link it
12with other works to produce a work based on this file, this file
13does not by itself cause the resulting work to be covered
14by the GNU General Public License.
15
16However the source code for this file must still be made available
17in accordance with section (3) of the GNU General Public License.
18
19This exception does not invalidate any other reasons why a work based
20on this file might be covered by the GNU General Public License.
21*/
ac7fdc22
GE
22#ifndef __LIBT2N_SERVER
23#define __LIBT2N_SERVER
24
25#include <iostream>
26#include <string>
27#include <map>
28cb45a5
GE
28#include <vector>
29#include <list>
30
31#include <boost/function.hpp>
ac7fdc22 32
a11e19b7
GE
33#include "connection.hxx"
34#include "types.hxx"
35
0a531de6 36
ac7fdc22
GE
37namespace libt2n
38{
39
aa499d20
GE
40class server;
41
59adb9e2 42/**
94247295
GE
43 @brief connection on a server
44
45 on a server every connection to a client is represented as server_connection.
46 a server_connection is abstract, derived classes like socket_server_connection are used.
59adb9e2 47*/
a11e19b7 48class server_connection : public connection
ac7fdc22 49{
94247295
GE
50 friend class server;
51
ac7fdc22
GE
52 private:
53 int timeout;
54 int last_action_time;
aa499d20 55 unsigned int connection_id;
ac7fdc22 56
94247295
GE
57 void set_server(server* _my_server)
58 { my_server=_my_server; }
59
60 void set_id(unsigned int _connection_id)
61 { connection_id=_connection_id; }
62
ac7fdc22 63 protected:
aa499d20 64 server *my_server;
aa499d20 65
a7170401 66 server_connection(int _timeout);
56f3994d 67 virtual ~server_connection();
a7170401
GE
68
69 std::ostream* get_logstream(log_level_values level);
70
ac7fdc22 71 public:
ac7fdc22
GE
72 void check_timeout();
73 void reset_timeout();
74 void set_timeout(int _timeout)
75 { timeout=_timeout; }
76
94247295 77 /// get the id of this connection within the server object
aa499d20
GE
78 unsigned int get_id()
79 { return connection_id; }
6cda58a6 80
6cda58a6 81 void add_callback(callback_event_type event, const boost::function<void ()>& func);
ac7fdc22
GE
82};
83
59adb9e2 84/**
94247295
GE
85 @brief server base class
86
87 constitutes a server. is abstract, use derived classes like socket_server.
59adb9e2 88*/
ac7fdc22
GE
89class server
90{
ac7fdc22
GE
91 private:
92 int default_timeout;
93 log_level_values log_level;
94 std::ostream *logstream;
95
28cb45a5 96 /// vector initialized for all callback-types, all elements in each list will be called
6cda58a6 97 std::vector<std::list<boost::function<void (unsigned int)> > > callbacks;
28cb45a5 98
ac7fdc22 99 unsigned int next_id;
ac7fdc22
GE
100
101 protected:
a11e19b7 102 std::map<unsigned int, server_connection*> connections;
aa499d20 103
a7170401 104 server();
ac7fdc22 105
94247295
GE
106 virtual bool fill_connection_buffers(void)=0;
107
56f3994d 108 unsigned int add_connection(server_connection* newconn);
04e6b271 109
6cda58a6 110 void do_callbacks(callback_event_type event, unsigned int conn_id);
28cb45a5 111
ac7fdc22
GE
112 public:
113 virtual ~server();
114
94247295 115 /// set the default timeout for new client connections
ac7fdc22
GE
116 void set_default_timeout(int _default_timeout)
117 { default_timeout=_default_timeout; }
94247295
GE
118
119 /// get the current default timeout for client connections
04e6b271
GE
120 int get_default_timeout(void)
121 { return default_timeout; }
ac7fdc22 122
a7170401 123 void set_logging(std::ostream *_logstream, log_level_values _log_level);
ac7fdc22 124
a11e19b7 125 server_connection* get_connection(unsigned int conn_id);
ac7fdc22 126
6cda58a6 127 void add_callback(callback_event_type event, const boost::function<void (unsigned int)>& func);
28cb45a5 128
45a2ebc9 129 /** @brief look for new data and store it in the local buffer
94247295
GE
130 @param usec_timeout wait until new data is found, max timeout usecs.
131 -1: wait endless
45a2ebc9
GE
132 0: return instantly
133 @param usec_timeout_remaining if non-NULL the function will write the
134 not used time to the given target
94247295
GE
135 @retval true if new data was found (does not mean that the received data
136 is a complete packet though)
137 */
9a5d7790 138 virtual bool fill_buffer(long long usec_timeout=-1, long long* usec_timeout_remaining=NULL)=0;
94247295 139
56f3994d
TJ
140 void close();
141
a11e19b7 142 void cleanup();
ac7fdc22 143
94247295
GE
144 /** @brief get a complete data packet from any client. The packet is removed from the
145 connection buffer.
146 @param[out] data the data package
147 @retval true if packet found
148 */
a11e19b7
GE
149 bool get_packet(std::string& data)
150 { unsigned int x; return get_packet(data,x); }
ac7fdc22 151
94247295 152 bool get_packet(std::string& data, unsigned int& conn_id);
0cf4dc9b 153
a7170401 154 std::ostream* get_logstream(log_level_values level);
ac7fdc22
GE
155};
156
157}
158
159#endif