libt2n: (gerd) small improvements, resolve doxygen conflicts
[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
27void connection::check_timeout()
28{
29 if (timeout != -1 && last_action_time+timeout >= time(NULL))
30 this->close();
31}
32
33void connection::reset_timeout()
34{
35 last_action_time=time(NULL);
36}
37
aa499d20
GE
38std::string connection::get_id_string()
39{
40 std::ostringstream os;
41 os << get_id();
42 return os.str();
43}
44
ac7fdc22
GE
45bool connection::get_packet(std::string& data, unsigned int& conn_id)
46{
47 // max packet size is unsigned int
48
49 // no size information -> no packet
50 if (buffer.size() < sizeof(unsigned int))
51 return false;
52
53 unsigned int psize=*((unsigned int*)(buffer.data()));
54
55 // enough data for one packet in buffer?
56 if (buffer.size() < sizeof(unsigned int)+psize)
57 return false;
58
59 data.assign(buffer,sizeof(unsigned int),psize);
60 buffer.erase(0,sizeof(unsigned int)+psize);
61
62 return true;
63}
64
65server::~server()
66{
67 std::map<unsigned int, connection*>::iterator ie=connections.end();
68 for(std::map<unsigned int, connection*>::iterator i=connections.begin(); i != ie; i++)
69 delete i->second;
70}
71
04e6b271
GE
72int server::add_connection(connection* newconn)
73{
aa499d20
GE
74 unsigned int cid=next_id++;
75 newconn->set_id(cid);
76 newconn->set_server(this);
77 connections[cid]=newconn;
78 return cid;
04e6b271
GE
79}
80
59adb9e2
TJ
81/**
82 Gets a connection by id
83
84 \param conn_id Connection ID
85
86 \retval Pointer to connection object
87*/
ac7fdc22
GE
88connection* server::get_connection(unsigned int conn_id)
89{
90 std::map<unsigned int, connection*>::iterator p=connections.find(conn_id);
91 if (p==connections.end())
92 return NULL;
93 else
94 return p->second;
95}
96
97void server::check_timeout()
98{
99 std::map<unsigned int, connection*>::iterator ie=connections.end();
100 for(std::map<unsigned int, connection*>::iterator i=connections.begin(); i != ie; i++)
101 i->second->check_timeout();
102}
103
104bool server::get_packet(std::string& data, unsigned int& conn_id)
105{
106 // todo: this is somehow unfair: the first connections in the map get checked more
107 // often than the others and can thus block them out
108
109 std::map<unsigned int, connection*>::iterator ie=connections.end();
110 for(std::map<unsigned int, connection*>::iterator i=connections.begin(); i != ie; i++)
111 if (i->second->get_packet(data,conn_id))
112 return true;
113}
114
115void server::log(log_level_values level, const char* message)
116{
117 if (logstream && level >= log_level)
118 (*logstream) << message << std::endl;
119}
120
121};