libt2n: (gerd) add ip communication
[libt2n] / src / server.cpp
... / ...
CommitLineData
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
24namespace libt2n
25{
26
27void server_connection::check_timeout()
28{
29 if (timeout != -1 && last_action_time+timeout >= time(NULL))
30 this->close();
31}
32
33void server_connection::reset_timeout()
34{
35 last_action_time=time(NULL);
36}
37
38server::~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
45int 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 @brief Gets a connection by id
56
57 @param conn_id Connection ID
58
59 @retval Pointer to connection object
60*/
61server_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/// check for timeouts, remove closed connections. don't forget to call this from time to time.
71void server::cleanup()
72{
73 std::map<unsigned int, server_connection*>::iterator ie=connections.end();
74 for(std::map<unsigned int, server_connection*>::iterator i=connections.begin(); i != ie; i++)
75 i->second->check_timeout();
76
77 for(std::map<unsigned int, server_connection*>::iterator i=connections.begin(); i != ie;)
78 {
79 if (i->second->is_closed() && !i->second->packet_available())
80 {
81 // closed and no usable data in buffer -> remove
82 delete i->second;
83 connections.erase(i);
84 i=connections.begin();
85 ie=connections.end();
86 }
87 else
88 i++;
89 }
90}
91
92bool server::get_packet(std::string& data, unsigned int& conn_id)
93{
94 // todo: this is somehow unfair: the first connections in the map get checked more
95 // often than the others and can thus block them out
96
97 std::map<unsigned int, server_connection*>::iterator ie=connections.end();
98 for(std::map<unsigned int, server_connection*>::iterator i=connections.begin(); i != ie; i++)
99 if (i->second->get_packet(data))
100 {
101 conn_id=i->first;
102 return true;
103 }
104
105 return false;
106}
107
108void server::log(log_level_values level, const char* message)
109{
110 if (logstream && level >= log_level)
111 (*logstream) << message << std::endl;
112}
113
114};