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 | #ifndef __LIBT2N_SERVER | |
20 | #define __LIBT2N_SERVER | |
21 | ||
22 | #include <iostream> | |
23 | #include <string> | |
24 | #include <map> | |
25 | ||
26 | namespace libt2n | |
27 | { | |
28 | ||
aa499d20 GE |
29 | class server; |
30 | ||
59adb9e2 TJ |
31 | /** |
32 | Basic connection class | |
33 | */ | |
ac7fdc22 GE |
34 | class connection |
35 | { | |
36 | private: | |
37 | int timeout; | |
38 | int last_action_time; | |
39 | bool closed; | |
aa499d20 | 40 | unsigned int connection_id; |
ac7fdc22 GE |
41 | |
42 | protected: | |
43 | connection(int _timeout) | |
44 | { | |
45 | set_timeout(_timeout); | |
46 | reset_timeout(); | |
47 | closed=false; | |
aa499d20 GE |
48 | connection_id=0; |
49 | my_server=0; | |
ac7fdc22 GE |
50 | } |
51 | ||
aa499d20 GE |
52 | server *my_server; |
53 | std::string buffer; | |
54 | ||
55 | typedef unsigned int packet_size_indicator; | |
56 | ||
ac7fdc22 GE |
57 | public: |
58 | ~connection() | |
59 | { this->close(); } | |
60 | ||
61 | void check_timeout(); | |
62 | void reset_timeout(); | |
63 | void set_timeout(int _timeout) | |
64 | { timeout=_timeout; } | |
65 | ||
66 | bool is_closed() | |
67 | { return closed; } | |
68 | ||
aa499d20 GE |
69 | void set_server(server* _my_server) |
70 | { my_server=_my_server; } | |
71 | ||
72 | void set_id(unsigned int _connection_id) | |
73 | { connection_id=_connection_id; } | |
74 | unsigned int get_id() | |
75 | { return connection_id; } | |
76 | std::string get_id_string(); | |
77 | ||
ac7fdc22 GE |
78 | virtual void close() |
79 | { closed=true; } | |
80 | ||
ac7fdc22 GE |
81 | bool get_packet(std::string& data, unsigned int& conn_id); |
82 | virtual void write(const std::string& data)=0; | |
83 | }; | |
84 | ||
59adb9e2 TJ |
85 | /** |
86 | Basic server class | |
87 | */ | |
ac7fdc22 GE |
88 | class server |
89 | { | |
90 | public: | |
91 | enum log_level_values { none=0, error=1, debug=2 }; | |
92 | ||
93 | private: | |
94 | int default_timeout; | |
95 | log_level_values log_level; | |
96 | std::ostream *logstream; | |
97 | ||
98 | unsigned int next_id; | |
ac7fdc22 GE |
99 | |
100 | protected: | |
aa499d20 GE |
101 | std::map<unsigned int, connection*> connections; |
102 | ||
ac7fdc22 GE |
103 | server() |
104 | { | |
105 | set_default_timeout(30); | |
106 | set_logging(NULL,none); | |
aa499d20 | 107 | next_id=1; |
ac7fdc22 GE |
108 | } |
109 | ||
04e6b271 GE |
110 | int add_connection(connection* newconn); |
111 | ||
ac7fdc22 GE |
112 | public: |
113 | virtual ~server(); | |
114 | ||
115 | void set_default_timeout(int _default_timeout) | |
116 | { default_timeout=_default_timeout; } | |
04e6b271 GE |
117 | int get_default_timeout(void) |
118 | { return default_timeout; } | |
ac7fdc22 GE |
119 | |
120 | void set_logging(std::ostream *_logstream, log_level_values _log_level) | |
121 | { | |
122 | log_level=_log_level; | |
123 | logstream=_logstream; | |
124 | } | |
125 | ||
126 | connection* get_connection(unsigned int conn_id); | |
127 | ||
128 | virtual void fill_buffer(long long usec_timeout=-1)=0; | |
129 | void check_timeout(); | |
130 | ||
131 | bool get_packet(std::string& data, unsigned int& conn_id); | |
132 | ||
aa499d20 | 133 | virtual void fill_connection_buffers(void)=0; |
0cf4dc9b | 134 | |
0cf4dc9b GE |
135 | void log(log_level_values level, const std::string& message) |
136 | { log(level,message.c_str()); } | |
ac7fdc22 GE |
137 | void log(log_level_values level, const char* message); |
138 | }; | |
139 | ||
140 | } | |
141 | ||
142 | #endif |