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 ***************************************************************************/
27 void server_connection::check_timeout()
29 if (timeout != -1 && last_action_time+timeout >= time(NULL))
33 void server_connection::reset_timeout()
35 last_action_time=time(NULL);
40 std::map<unsigned int, server_connection*>::iterator ie=connections.end();
41 for(std::map<unsigned int, server_connection*>::iterator i=connections.begin(); i != ie; i++)
45 int server::add_connection(server_connection* newconn)
47 unsigned int cid=next_id++;
49 newconn->set_server(this);
50 connections[cid]=newconn;
55 Gets a connection by id
57 \param conn_id Connection ID
59 \retval Pointer to connection object
61 server_connection* server::get_connection(unsigned int conn_id)
63 std::map<unsigned int, server_connection*>::iterator p=connections.find(conn_id);
64 if (p==connections.end())
70 void server::cleanup()
72 std::map<unsigned int, server_connection*>::iterator ie=connections.end();
73 for(std::map<unsigned int, server_connection*>::iterator i=connections.begin(); i != ie; i++)
74 i->second->check_timeout();
76 for(std::map<unsigned int, server_connection*>::iterator i=connections.begin(); i != ie;)
78 if (i->second->is_closed() && !i->second->packet_available())
80 // closed and no usable data in buffer -> remove
83 i=connections.begin();
91 bool server::get_packet(std::string& data, unsigned int& conn_id)
93 // todo: this is somehow unfair: the first connections in the map get checked more
94 // often than the others and can thus block them out
96 std::map<unsigned int, server_connection*>::iterator ie=connections.end();
97 for(std::map<unsigned int, server_connection*>::iterator i=connections.begin(); i != ie; i++)
98 if (i->second->get_packet(data))
107 void server::log(log_level_values level, const char* message)
109 if (logstream && level >= log_level)
110 (*logstream) << message << std::endl;