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 ***************************************************************************/
25 void connection::check_timeout()
27 if (timeout != -1 && last_action_time+timeout >= time(NULL))
31 void connection::reset_timeout()
33 last_action_time=time(NULL);
36 bool connection::get_packet(std::string& data, unsigned int& conn_id)
38 // max packet size is unsigned int
40 // no size information -> no packet
41 if (buffer.size() < sizeof(unsigned int))
44 unsigned int psize=*((unsigned int*)(buffer.data()));
46 // enough data for one packet in buffer?
47 if (buffer.size() < sizeof(unsigned int)+psize)
50 data.assign(buffer,sizeof(unsigned int),psize);
51 buffer.erase(0,sizeof(unsigned int)+psize);
58 std::map<unsigned int, connection*>::iterator ie=connections.end();
59 for(std::map<unsigned int, connection*>::iterator i=connections.begin(); i != ie; i++)
63 connection* server::get_connection(unsigned int conn_id)
65 std::map<unsigned int, connection*>::iterator p=connections.find(conn_id);
66 if (p==connections.end())
72 void server::check_timeout()
74 std::map<unsigned int, connection*>::iterator ie=connections.end();
75 for(std::map<unsigned int, connection*>::iterator i=connections.begin(); i != ie; i++)
76 i->second->check_timeout();
79 bool server::get_packet(std::string& data, unsigned int& conn_id)
81 // todo: this is somehow unfair: the first connections in the map get checked more
82 // often than the others and can thus block them out
84 std::map<unsigned int, connection*>::iterator ie=connections.end();
85 for(std::map<unsigned int, connection*>::iterator i=connections.begin(); i != ie; i++)
86 if (i->second->get_packet(data,conn_id))
90 void server::log(log_level_values level, const char* message)
92 if (logstream && level >= log_level)
93 (*logstream) << message << std::endl;