libt2n: (gerd) fixes & improvements
[libt2n] / src / server.hxx
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#ifndef __LIBT2N_SERVER
20#define __LIBT2N_SERVER
21
22#include <iostream>
23#include <string>
24#include <map>
25
26namespace libt2n
27{
28
29class connection
30{
31 private:
32 int timeout;
33 int last_action_time;
34 bool closed;
35 std::string buffer;
36
37 protected:
38 connection(int _timeout)
39 {
40 set_timeout(_timeout);
41 reset_timeout();
42 closed=false;
43 }
44
45 public:
46 ~connection()
47 { this->close(); }
48
49 void check_timeout();
50 void reset_timeout();
51 void set_timeout(int _timeout)
52 { timeout=_timeout; }
53
54 bool is_closed()
55 { return closed; }
56
57 virtual void close()
58 { closed=true; }
59
60 virtual void fill_buffer(void)=0;
61
62 bool get_packet(std::string& data, unsigned int& conn_id);
63 virtual void write(const std::string& data)=0;
64};
65
66class server
67{
68 public:
69 enum log_level_values { none=0, error=1, debug=2 };
70
71 private:
72 int default_timeout;
73 log_level_values log_level;
74 std::ostream *logstream;
75
76 unsigned int next_id;
77 std::map<unsigned int, connection*> connections;
78
79 protected:
80 server()
81 {
82 set_default_timeout(30);
83 set_logging(NULL,none);
84 next_id=0;
85 }
86
87 public:
88 virtual ~server();
89
90 void set_default_timeout(int _default_timeout)
91 { default_timeout=_default_timeout; }
92
93 void set_logging(std::ostream *_logstream, log_level_values _log_level)
94 {
95 log_level=_log_level;
96 logstream=_logstream;
97 }
98
99 connection* get_connection(unsigned int conn_id);
100
101 virtual void fill_buffer(long long usec_timeout=-1)=0;
102 void check_timeout();
103
104 bool get_packet(std::string& data, unsigned int& conn_id);
105
0cf4dc9b
GE
106 void fill_connection_buffers(void);
107
ac7fdc22 108 protected:
0cf4dc9b
GE
109 void log(log_level_values level, const std::string& message)
110 { log(level,message.c_str()); }
ac7fdc22
GE
111 void log(log_level_values level, const char* message);
112};
113
114}
115
116#endif