Repository navigation
Expand file tree
/
Copy pathhtra_proxy_server.cpp
More file actions
112 lines (91 loc) · 3.84 KB
/
Copy pathhtra_proxy_server.cpp
File metadata and controls
112 lines (91 loc) · 3.84 KB
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
111
112
#include "htra_proxy_server.h"
HtraProxyServer::HtraProxyServer(QString configPath, QString section, IHtraDevice &device, QObject *parent)
: QObject(parent),
configPath(configPath),
section(section),
m_device(device) {
loadConfig();
server = new QTcpServer(this);
RFprocessor = new SwitcherProcessor(
QCoreApplication::applicationDirPath() + "/etc/transport/config.ini",
"ArduinoRFSwitcher",
this);
if(server->listen(ListenIp, port)) {
qDebug() << name << "started on port:" << port;
} else {
qDebug() << name << "start failed on port:" << port;
qDebug() << server->errorString();
}
connect(server, SIGNAL(newConnection()), this, SLOT(onNewConnection()));
}
void HtraProxyServer::onDataReady() {
QTcpSocket *clientSocket = qobject_cast<QTcpSocket *>(sender());
if(clientSocket) {
QByteArray &buffer = clientBuffers[clientSocket];
buffer.append(clientSocket->readAll());
QByteArray packet;
while(HtraProtocol::takePacket(buffer, packet)) {
emit translateLastPacket(packet.toHex());
processHtraCmd(packet, clientSocket);
}
}
}
void HtraProxyServer::sendReply(QTcpSocket *clientSocket, const QByteArray &reply) {
if(!clientSocket || reply.isEmpty()) {
return;
}
QString clientIp = clientSocket->peerAddress().toString();
if(clientSocket->peerAddress().protocol() == QAbstractSocket::IPv6Protocol &&
clientIp.startsWith("::ffff:")) {
clientIp = clientIp.mid(7); // Deleting "::ffff:"
}
clientSocket->write(reply);
}
void HtraProxyServer::loadConfig() {
QSettings settings(configPath, QSettings::IniFormat);
settings.beginGroup(section);
name = settings.value("Name", "tcp_proxy").toString();
ServerAddress = QHostAddress(settings.value("HostIp", QHostAddress::LocalHost).toString());
const QString listenIpValue = settings.value("ListenIp").toString().trimmed();
if(listenIpValue.isEmpty()) {
ListenIp = QHostAddress::Any;
} else if(!ListenIp.setAddress(listenIpValue)) {
qWarning() << name << "invalid ListenIp:" << listenIpValue
<< "- listening on all interfaces";
ListenIp = QHostAddress::Any;
}
port = settings.value("Port", 7777).toUInt();
settings.endGroup();
}
void HtraProxyServer::processHtraCmd(const QByteArray &packet, QTcpSocket *clientSocket) {
const QByteArray reply = HtraProtocol::processCommand(
packet,
m_device,
[this](quint8 cmdId) {
switch(cmdId) {
case HTRA_CMD::RF1: RFprocessor->setRF1(); break;
case HTRA_CMD::RF2: RFprocessor->setRF2(); break;
case HTRA_CMD::RF3: RFprocessor->setRF3(); break;
case HTRA_CMD::RF4: RFprocessor->setRF4(); break;
case HTRA_CMD::RFoff: RFprocessor->RFoff(); break;
}
});
sendReply(clientSocket, reply);
}
void HtraProxyServer::onNewConnection() {
while(server->hasPendingConnections()) {
QTcpSocket *clientSocket = server->nextPendingConnection();
QString clientIp = clientSocket->peerAddress().toString();
// Преобразование IPv4-mapped IPv6 в обычный IPv4
if(clientSocket->peerAddress().protocol() == QAbstractSocket::IPv6Protocol &&
clientIp.startsWith("::ffff:")) {
clientIp = clientIp.mid(7); // Deleting "::ffff:"
}
qDebug() << "New client connected from IP -> " + clientIp;
connect(clientSocket, SIGNAL(readyRead()), this, SLOT(onDataReady()));
connect(clientSocket, SIGNAL(disconnected()), clientSocket, SLOT(deleteLater()));
connect(clientSocket, &QTcpSocket::disconnected, this, [this, clientSocket]() {
clientBuffers.remove(clientSocket);
});
}
}