/*************************************************************************** * Copyright (C) 2006 by Gerd v. Egidy * * gve@intra2net.com * * * * This library is free software; you can redistribute it and/or modify * * it under the terms of the GNU Lesser General Public License version * * 2.1 as published by the Free Software Foundation. * * * * This library is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU Lesser General Public License for more details. * * * * You should have received a copy of the GNU Lesser General Public * * License along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include #include "server.hxx" namespace libt2n { void server_connection::check_timeout() { if (timeout != -1 && last_action_time+timeout >= time(NULL)) this->close(); } void server_connection::reset_timeout() { last_action_time=time(NULL); } server::~server() { std::map::iterator ie=connections.end(); for(std::map::iterator i=connections.begin(); i != ie; i++) delete i->second; } int server::add_connection(server_connection* newconn) { unsigned int cid=next_id++; newconn->set_id(cid); newconn->set_server(this); connections[cid]=newconn; return cid; } /** Gets a connection by id \param conn_id Connection ID \retval Pointer to connection object */ server_connection* server::get_connection(unsigned int conn_id) { std::map::iterator p=connections.find(conn_id); if (p==connections.end()) return NULL; else return p->second; } void server::cleanup() { std::map::iterator ie=connections.end(); for(std::map::iterator i=connections.begin(); i != ie; i++) i->second->check_timeout(); for(std::map::iterator i=connections.begin(); i != ie;) { if (i->second->is_closed() && !i->second->packet_available()) { // closed and no usable data in buffer -> remove delete i->second; connections.erase(i); i=connections.begin(); ie=connections.end(); } else i++; } } bool server::get_packet(std::string& data, unsigned int& conn_id) { // todo: this is somehow unfair: the first connections in the map get checked more // often than the others and can thus block them out std::map::iterator ie=connections.end(); for(std::map::iterator i=connections.begin(); i != ie; i++) if (i->second->get_packet(data)) { conn_id=i->first; return true; } return false; } void server::log(log_level_values level, const char* message) { if (logstream && level >= log_level) (*logstream) << message << std::endl; } };