/*************************************************************************** * 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 "server.hxx" namespace libt2n { void connection::check_timeout() { if (timeout != -1 && last_action_time+timeout >= time(NULL)) this->close(); } void connection::reset_timeout() { last_action_time=time(NULL); } bool connection::get_packet(std::string& data, unsigned int& conn_id) { // max packet size is unsigned int // no size information -> no packet if (buffer.size() < sizeof(unsigned int)) return false; unsigned int psize=*((unsigned int*)(buffer.data())); // enough data for one packet in buffer? if (buffer.size() < sizeof(unsigned int)+psize) return false; data.assign(buffer,sizeof(unsigned int),psize); buffer.erase(0,sizeof(unsigned int)+psize); return true; } server::~server() { std::map::iterator ie=connections.end(); for(std::map::iterator i=connections.begin(); i != ie; i++) delete i->second; } 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::check_timeout() { std::map::iterator ie=connections.end(); for(std::map::iterator i=connections.begin(); i != ie; i++) i->second->check_timeout(); } 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)) return true; } void server::log(log_level_values level, const char* message) { if (logstream && level >= log_level) (*logstream) << message << std::endl; } };