-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection_manager.h
110 lines (82 loc) · 3.09 KB
/
connection_manager.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* This file is part of the ct-Bot remote viewer tool.
* Copyright (c) 2020-2022 Timo Sandmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file connection_manager.h
* @brief Connection and ct-Bot command management
* @author Timo Sandmann
* @date 11.04.2020
*/
#pragma once
#include <QTcpSocket>
#include <QByteArray>
#include <map>
#include <vector>
#include <string>
#include <functional>
#include <regex>
#include "command.h"
#include "connect_button.h"
class QQmlApplicationEngine;
class ConnectionManagerBase {
ConnectButton* p_connect_button_;
protected:
static constexpr bool DEBUG_ { false };
QQmlApplicationEngine* p_engine_;
QTcpSocket socket_;
QByteArray in_buffer_;
bool connected_;
ConnectButton* p_shutdown_button_;
virtual bool process_incoming() = 0;
virtual void register_buttons();
virtual void connected_hook() {}
virtual void disconnected_hook() {}
public:
ConnectionManagerBase(QQmlApplicationEngine* p_engine);
virtual ~ConnectionManagerBase();
virtual int get_version() const = 0;
int version_active() const;
auto get_socket() {
return &socket_;
}
};
class ConnectionManagerV1 : public ConnectionManagerBase {
std::map<ctbot::CommandCodes /*cmd*/, std::vector<std::function<bool(const ctbot::CommandBase&)>> /*functions*/> commands_;
protected:
virtual bool process_incoming() override;
bool evaluate_cmd(const ctbot::CommandNoCRC* p_cmd) const;
public:
ConnectionManagerV1(QQmlApplicationEngine* p_engine);
virtual ~ConnectionManagerV1();
virtual int get_version() const override;
virtual void register_buttons() override;
void register_cmd(const ctbot::CommandCodes& cmd, std::function<bool(const ctbot::CommandBase&)>&& func);
};
class ConnectionManagerV2 : public ConnectionManagerBase {
std::map<std::string /*cmd*/, std::vector<std::function<bool(const std::string_view&)>> /*functions*/> commands_;
protected:
static inline const std::regex cmd_regex_ { R"(<(\w+)>((?:.|\r|\n)+?)<(/\1)>\r\n)" };
virtual bool process_incoming() override;
virtual void connected_hook() override;
virtual void disconnected_hook() override;
bool evaluate_cmd(const std::string_view& cmd, const std::string_view& data) const;
public:
ConnectionManagerV2(QQmlApplicationEngine* p_engine);
virtual ~ConnectionManagerV2();
virtual int get_version() const override;
virtual void register_buttons() override;
void register_cmd(const std::string_view& cmd, std::function<bool(const std::string_view&)>&& func);
};