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