2 Copyright (C) 2006 by Intra2net AG - Gerd v. Egidy
4 The software in this package is distributed under the GNU General
5 Public License version 2 (with a special exception described below).
7 A copy of GNU General Public License (GPL) is included in this distribution,
8 in the file COPYING.GPL.
10 As a special exception, if other files instantiate templates or use macros
11 or inline functions from this file, or you compile this file and link it
12 with other works to produce a work based on this file, this file
13 does not by itself cause the resulting work to be covered
14 by the GNU General Public License.
16 However the source code for this file must still be made available
17 in accordance with section (3) of the GNU General Public License.
19 This exception does not invalidate any other reasons why a work based
20 on this file might be covered by the GNU General Public License.
22 #ifndef __LIBT2N_SERVER
23 #define __LIBT2N_SERVER
31 #include <boost/function.hpp>
33 #include "connection.hxx"
43 @brief connection on a server
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.
48 class server_connection : public connection
55 unsigned int connection_id;
57 void set_server(server* _my_server)
58 { my_server=_my_server; }
60 void set_id(unsigned int _connection_id)
61 { connection_id=_connection_id; }
66 server_connection(int _timeout);
67 virtual ~server_connection();
69 std::ostream* get_logstream(log_level_values level);
74 void set_timeout(int _timeout)
77 /// get the id of this connection within the server object
79 { return connection_id; }
81 void add_callback(callback_event_type event, const boost::function<void ()>& func);
85 @brief server base class
87 constitutes a server. is abstract, use derived classes like socket_server.
93 log_level_values log_level;
94 std::ostream *logstream;
96 /// vector initialized for all callback-types, all elements in each list will be called
97 std::vector<std::list<boost::function<void (unsigned int)> > > callbacks;
102 std::map<unsigned int, server_connection*> connections;
106 virtual bool fill_connection_buffers(void)=0;
108 unsigned int add_connection(server_connection* newconn);
110 void do_callbacks(callback_event_type event, unsigned int conn_id);
115 /// set the default timeout for new client connections
116 void set_default_timeout(int _default_timeout)
117 { default_timeout=_default_timeout; }
119 /// get the current default timeout for client connections
120 int get_default_timeout(void)
121 { return default_timeout; }
123 void set_logging(std::ostream *_logstream, log_level_values _log_level);
125 server_connection* get_connection(unsigned int conn_id);
127 void add_callback(callback_event_type event, const boost::function<void (unsigned int)>& func);
129 /** @brief look for new data and store it in the local buffer
130 @param usec_timeout wait until new data is found, max timeout usecs.
133 @param usec_timeout_remaining if non-NULL the function will write the
134 not used time to the given target
135 @retval true if new data was found (does not mean that the received data
136 is a complete packet though)
138 virtual bool fill_buffer(long long usec_timeout=-1, long long* usec_timeout_remaining=NULL)=0;
144 /** @brief get a complete data packet from any client. The packet is removed from the
146 @param[out] data the data package
147 @retval true if packet found
149 bool get_packet(std::string& data)
150 { unsigned int x; return get_packet(data,x); }
152 bool get_packet(std::string& data, unsigned int& conn_id);
154 std::ostream* get_logstream(log_level_values level);