Replace socket_handler::fill_buffer() recursion with loop (#8389)
[libt2n] / src / command_server.hxx
CommitLineData
19facd85
TJ
1/*
2Copyright (C) 2006 by Intra2net AG - Gerd v. Egidy
3
4The software in this package is distributed under the GNU General
5Public License version 2 (with a special exception described below).
6
7A copy of GNU General Public License (GPL) is included in this distribution,
8in the file COPYING.GPL.
9
10As a special exception, if other files instantiate templates or use macros
11or inline functions from this file, or you compile this file and link it
12with other works to produce a work based on this file, this file
13does not by itself cause the resulting work to be covered
14by the GNU General Public License.
15
16However the source code for this file must still be made available
17in accordance with section (3) of the GNU General Public License.
18
19This exception does not invalidate any other reasons why a work based
20on this file might be covered by the GNU General Public License.
21*/
7087e187
GE
22#ifndef __LIBT2N_COMMAND_SERVER
23#define __LIBT2N_COMMAND_SERVER
24
539b09c0 25#include "command.hxx"
7087e187
GE
26#include "server.hxx"
27
28namespace libt2n
29{
30
31/// a server handling incoming commands
32class command_server
33{
34 private:
35 server& s;
36
37 void handle_packet(const std::string& packet, server_connection* conn);
38
3b2543e7
GE
39 int guard_handle;
40
539b09c0
GE
41 protected:
42 virtual command* cast_command(command* input)
43 { return input; }
44
7087e187 45 public:
28cb45a5 46 command_server(server& _s);
56f3994d 47 ~command_server();
7087e187 48
45a2ebc9 49 void handle(long long usec_timeout=-1, long long* usec_timeout_remaining=NULL);
28cb45a5 50
8104c8f7 51 void send_hello(unsigned int conn_id);
c7857475
GE
52
53 std::ostream* get_logstream(log_level_values level)
54 { return s.get_logstream(level); }
7087e187
GE
55};
56
539b09c0
GE
57template<class T, class B> struct Derived_from {
58 static void constraints(T* p) { B* pb = p; }
1eed3f4c 59 Derived_from() { void(*/*p*/)(T*) = constraints; }
539b09c0
GE
60};
61
1e1f17bf
GE
62/** @brief server handling group of incoming commands
63
64 the template must be derived from libt2n::command.
65*/
539b09c0
GE
66template<class COMMAND_GROUP>
67class group_command_server : public command_server
68{
69 private:
70 virtual command* cast_command(command* input)
71 { return dynamic_cast<COMMAND_GROUP*>(input); }
7087e187 72
539b09c0
GE
73 public:
74 group_command_server(server& _s)
75 : command_server(_s)
76 { Derived_from<COMMAND_GROUP,command>(); }
77};
78
79}
7087e187
GE
80#endif
81