libt2n: (gerd) add ip communication
[libt2n] / src / server.cpp
CommitLineData
ac7fdc22
GE
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
aa499d20
GE
20#include <sstream>
21
ac7fdc22
GE
22#include "server.hxx"
23
24namespace libt2n
25{
26
a11e19b7 27void server_connection::check_timeout()
ac7fdc22
GE
28{
29 if (timeout != -1 && last_action_time+timeout >= time(NULL))
30 this->close();
31}
32
a11e19b7 33void server_connection::reset_timeout()
ac7fdc22
GE
34{
35 last_action_time=time(NULL);
36}
37
ac7fdc22
GE
38server::~server()
39{
a11e19b7
GE
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++)
ac7fdc22
GE
42 delete i->second;
43}
44
a11e19b7 45int server::add_connection(server_connection* newconn)
04e6b271 46{
aa499d20
GE
47 unsigned int cid=next_id++;
48 newconn->set_id(cid);
49 newconn->set_server(this);
50 connections[cid]=newconn;
51 return cid;
04e6b271
GE
52}
53
59adb9e2 54/**
94247295 55 @brief Gets a connection by id
59adb9e2 56
94247295 57 @param conn_id Connection ID
59adb9e2 58
94247295 59 @retval Pointer to connection object
59adb9e2 60*/
a11e19b7 61server_connection* server::get_connection(unsigned int conn_id)
ac7fdc22 62{
a11e19b7 63 std::map<unsigned int, server_connection*>::iterator p=connections.find(conn_id);
ac7fdc22
GE
64 if (p==connections.end())
65 return NULL;
66 else
67 return p->second;
68}
69
94247295 70/// check for timeouts, remove closed connections. don't forget to call this from time to time.
a11e19b7 71void server::cleanup()
ac7fdc22 72{
a11e19b7
GE
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++)
ac7fdc22 75 i->second->check_timeout();
a11e19b7
GE
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 }
ac7fdc22
GE
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
a11e19b7
GE
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;
ac7fdc22 102 return true;
a11e19b7
GE
103 }
104
105 return false;
ac7fdc22
GE
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};