1 /***************************************************************************
2 * Copyright (C) 2006 by Gerd v. Egidy *
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. *
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. *
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
28 #include <boost/function.hpp>
30 #include "connection.hxx"
39 @brief connection on a server
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.
44 class server_connection : public connection
51 unsigned int connection_id;
53 void set_server(server* _my_server)
54 { my_server=_my_server; }
56 void set_id(unsigned int _connection_id)
57 { connection_id=_connection_id; }
59 /// vector initialized for all callback-types, all elements in each list will be called
60 std::vector<std::list<boost::function<void ()> > > callbacks;
65 server_connection(int _timeout);
67 std::ostream* get_logstream(log_level_values level);
69 void do_callbacks(callback_event_type event);
72 virtual ~server_connection();
76 void set_timeout(int _timeout)
79 /// get the id of this connection within the server object
81 { return connection_id; }
85 void add_callback(callback_event_type event, const boost::function<void ()>& func);
89 @brief server base class
91 constitutes a server. is abstract, use derived classes like socket_server.
97 log_level_values log_level;
98 std::ostream *logstream;
100 /// vector initialized for all callback-types, all elements in each list will be called
101 std::vector<std::list<boost::function<void (unsigned int)> > > callbacks;
103 unsigned int next_id;
106 std::map<unsigned int, server_connection*> connections;
110 virtual bool fill_connection_buffers(void)=0;
112 int add_connection(server_connection* newconn);
114 void do_callbacks(callback_event_type event, unsigned int conn_id);
119 /// set the default timeout for new client connections
120 void set_default_timeout(int _default_timeout)
121 { default_timeout=_default_timeout; }
123 /// get the current default timeout for client connections
124 int get_default_timeout(void)
125 { return default_timeout; }
127 void set_logging(std::ostream *_logstream, log_level_values _log_level);
129 server_connection* get_connection(unsigned int conn_id);
131 void add_callback(callback_event_type event, const boost::function<void (unsigned int)>& func);
133 /** @brief look for new data on all open connections, accept new connections
134 @param usec_timeout wait until new data is found, max timeout usecs.
137 @retval true if new data was found (does not mean that the received data
138 is a complete packet though)
140 virtual bool fill_buffer(long long usec_timeout=-1)=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);