libt2n: (gerd) doxygenize
[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>
25
a11e19b7
GE
26#include "connection.hxx"
27#include "types.hxx"
28
ac7fdc22
GE
29namespace libt2n
30{
31
aa499d20
GE
32class 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 40class 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:
a11e19b7
GE
56 server_connection(int _timeout)
57 : connection()
ac7fdc22
GE
58 {
59 set_timeout(_timeout);
60 reset_timeout();
aa499d20
GE
61 connection_id=0;
62 my_server=0;
ac7fdc22
GE
63 }
64
aa499d20 65 server *my_server;
aa499d20 66
ac7fdc22 67 public:
94247295 68 /// check if timeout is expired, close connection if so
ac7fdc22 69 void check_timeout();
94247295
GE
70
71 /// reset the timeout, e.g. if something is received
ac7fdc22 72 void reset_timeout();
94247295 73
ac7fdc22
GE
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; }
ac7fdc22
GE
80};
81
59adb9e2 82/**
94247295
GE
83 @brief server base class
84
85 constitutes a server. is abstract, use derived classes like socket_server.
59adb9e2 86*/
ac7fdc22
GE
87class server
88{
ac7fdc22
GE
89 private:
90 int default_timeout;
91 log_level_values log_level;
92 std::ostream *logstream;
93
94 unsigned int next_id;
ac7fdc22
GE
95
96 protected:
a11e19b7 97 std::map<unsigned int, server_connection*> connections;
aa499d20 98
ac7fdc22
GE
99 server()
100 {
101 set_default_timeout(30);
102 set_logging(NULL,none);
aa499d20 103 next_id=1;
ac7fdc22
GE
104 }
105
94247295
GE
106 virtual bool fill_connection_buffers(void)=0;
107
a11e19b7 108 int add_connection(server_connection* newconn);
04e6b271 109
ac7fdc22
GE
110 public:
111 virtual ~server();
112
94247295 113 /// set the default timeout for new client connections
ac7fdc22
GE
114 void set_default_timeout(int _default_timeout)
115 { default_timeout=_default_timeout; }
94247295
GE
116
117 /// get the current default timeout for client connections
04e6b271
GE
118 int get_default_timeout(void)
119 { return default_timeout; }
ac7fdc22 120
94247295 121 /// activate logging to the given stream. everything above the given level is logged.
ac7fdc22
GE
122 void set_logging(std::ostream *_logstream, log_level_values _log_level)
123 {
124 log_level=_log_level;
125 logstream=_logstream;
126 }
127
a11e19b7 128 server_connection* get_connection(unsigned int conn_id);
ac7fdc22 129
94247295
GE
130 /** @brief look for new data on all open connections, accept new connections
131 @param usec_timeout wait until new data is found, max timeout usecs.
132 -1: wait endless
133 NULL: no timeout
134 @retval true if new data was found (does not mean that the received data
135 is a complete packet though)
136 */
137 virtual bool fill_buffer(long long usec_timeout=-1)=0;
138
a11e19b7 139 void cleanup();
ac7fdc22 140
94247295
GE
141 /** @brief get a complete data packet from any client. The packet is removed from the
142 connection buffer.
143 @param[out] data the data package
144 @retval true if packet found
145 */
a11e19b7
GE
146 bool get_packet(std::string& data)
147 { unsigned int x; return get_packet(data,x); }
ac7fdc22 148
94247295
GE
149 /** @brief get a complete data packet from any client. The packet is removed from the
150 connection buffer.
151 @param[out] data the data package
152 @param[out] conn_id the connection id we got this packet from
153 @retval true if packet found
154 */
155 bool get_packet(std::string& data, unsigned int& conn_id);
0cf4dc9b 156
94247295 157 /// write a message to the log if logging is enabled
0cf4dc9b
GE
158 void log(log_level_values level, const std::string& message)
159 { log(level,message.c_str()); }
94247295 160 /// write a message to the log if logging is enabled
ac7fdc22
GE
161 void log(log_level_values level, const char* message);
162};
163
164}
165
166#endif