| 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2006 by Gerd v. Egidy * |
| 3 | * gve@intra2net.com * |
| 4 | * * |
| 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. * |
| 8 | * * |
| 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. * |
| 13 | * * |
| 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 | |
| 20 | #include <sstream> |
| 21 | |
| 22 | #include "server.hxx" |
| 23 | |
| 24 | namespace libt2n |
| 25 | { |
| 26 | |
| 27 | void server_connection::check_timeout() |
| 28 | { |
| 29 | if (timeout != -1 && last_action_time+timeout >= time(NULL)) |
| 30 | this->close(); |
| 31 | } |
| 32 | |
| 33 | void server_connection::reset_timeout() |
| 34 | { |
| 35 | last_action_time=time(NULL); |
| 36 | } |
| 37 | |
| 38 | server::~server() |
| 39 | { |
| 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++) |
| 42 | delete i->second; |
| 43 | } |
| 44 | |
| 45 | int server::add_connection(server_connection* newconn) |
| 46 | { |
| 47 | unsigned int cid=next_id++; |
| 48 | newconn->set_id(cid); |
| 49 | newconn->set_server(this); |
| 50 | connections[cid]=newconn; |
| 51 | return cid; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | Gets a connection by id |
| 56 | |
| 57 | \param conn_id Connection ID |
| 58 | |
| 59 | \retval Pointer to connection object |
| 60 | */ |
| 61 | server_connection* server::get_connection(unsigned int conn_id) |
| 62 | { |
| 63 | std::map<unsigned int, server_connection*>::iterator p=connections.find(conn_id); |
| 64 | if (p==connections.end()) |
| 65 | return NULL; |
| 66 | else |
| 67 | return p->second; |
| 68 | } |
| 69 | |
| 70 | void server::cleanup() |
| 71 | { |
| 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(); |
| 75 | |
| 76 | for(std::map<unsigned int, server_connection*>::iterator i=connections.begin(); i != ie;) |
| 77 | { |
| 78 | if (i->second->is_closed() && !i->second->packet_available()) |
| 79 | { |
| 80 | // closed and no usable data in buffer -> remove |
| 81 | delete i->second; |
| 82 | connections.erase(i); |
| 83 | i=connections.begin(); |
| 84 | ie=connections.end(); |
| 85 | } |
| 86 | else |
| 87 | i++; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | bool server::get_packet(std::string& data, unsigned int& conn_id) |
| 92 | { |
| 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 |
| 95 | |
| 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)) |
| 99 | { |
| 100 | conn_id=i->first; |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | void server::log(log_level_values level, const char* message) |
| 108 | { |
| 109 | if (logstream && level >= log_level) |
| 110 | (*logstream) << message << std::endl; |
| 111 | } |
| 112 | |
| 113 | }; |