Commit | Line | Data |
---|---|---|
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 | ||
24 | namespace libt2n | |
25 | { | |
26 | ||
a11e19b7 | 27 | void server_connection::check_timeout() |
ac7fdc22 GE |
28 | { |
29 | if (timeout != -1 && last_action_time+timeout >= time(NULL)) | |
30 | this->close(); | |
31 | } | |
32 | ||
a11e19b7 | 33 | void server_connection::reset_timeout() |
ac7fdc22 GE |
34 | { |
35 | last_action_time=time(NULL); | |
36 | } | |
37 | ||
ac7fdc22 GE |
38 | server::~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 | 45 | int 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 TJ |
54 | /** |
55 | Gets a connection by id | |
56 | ||
57 | \param conn_id Connection ID | |
58 | ||
59 | \retval Pointer to connection object | |
60 | */ | |
a11e19b7 | 61 | server_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 | ||
a11e19b7 | 70 | void server::cleanup() |
ac7fdc22 | 71 | { |
a11e19b7 GE |
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++) | |
ac7fdc22 | 74 | i->second->check_timeout(); |
a11e19b7 GE |
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 | } | |
ac7fdc22 GE |
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 | ||
a11e19b7 GE |
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; | |
ac7fdc22 | 101 | return true; |
a11e19b7 GE |
102 | } |
103 | ||
104 | return false; | |
ac7fdc22 GE |
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 | }; |