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