Repository navigation
Expand file tree
/
Copy pathbks_proxy_server.cpp
More file actions
187 lines (147 loc) · 5.12 KB
/
Copy pathbks_proxy_server.cpp
File metadata and controls
187 lines (147 loc) · 5.12 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "bks_proxy_server.h"
BksProxyServer::BksProxyServer(QString configPath, QString section, QObject *parent)
: QObject(parent),
configPath(configPath),
section(section) {
loadConfig();
transport = AbstractNetworkTransportFactory::getInstance(
QCoreApplication::applicationDirPath() +
"/transport/config.ini", "BksTransport");
locker = new NetworkTransportLocker(1000, 100);
server = new QTcpServer(this);
requester = new SerialCircularRequester(transport, locker);
device = new BksDevice(requester, QCoreApplication::applicationDirPath() + "/device/config.ini", "BksDevice");
if(server->listen(QHostAddress::Any, port)) {
qDebug() << name << "started on ip" << ServerAddress.toString() << ":" << port;
} else {
qDebug() << name << "start failed on ip" << ServerAddress.toString() << ":" << port;
}
connect(server, SIGNAL(newConnection()), this, SLOT(onNewConnection()));
}
void BksProxyServer::sendReply(QByteArray reply) {
QString clientIp = currentClient->peerAddress().toString();
if(currentClient->peerAddress().protocol() == QAbstractSocket::IPv6Protocol &&
clientIp.startsWith("::ffff:")) {
clientIp = clientIp.mid(7); // Deleting "::ffff:"
}
currentClient->write(reply);
}
void BksProxyServer::setBksDevice(BksDevice *implement) {
device = implement;
}
void BksProxyServer::loadConfig() {
QSettings settings(configPath, QSettings::IniFormat);
settings.beginGroup(section);
ServerAddress = QHostAddress(settings.value("HostIp", "127.0.0.1").toString());
ListenIp = QHostAddress(settings.value("ListenIp", QHostAddress::Any).toString());
port = settings.value("Port", 6666).toUInt();
name = settings.value("Name", "tcp_proxy").toString();
qDebug() << configPath;
qDebug() << "BksProxyServer on port:" << port;
settings.endGroup();
}
void BksProxyServer::ParseData(QByteArray data) {
buffer.append(data);
int headerIndex = buffer.indexOf(HEADER);
if(headerIndex < 0) {
return;
}
buffer.remove(0, headerIndex);
headerIndex = 0;
if(buffer.size() < 3) {
return;
}
quint8 packetLength = buffer[2];
if(buffer.size() < packetLength + 3) {
return;
}
QByteArray packet = buffer.mid(headerIndex, packetLength + 3);
buffer.remove(buffer.indexOf(packet), packet.size());
ProcessBksCmd(packet);
}
void BksProxyServer::ProcessBksCmd(QByteArray packet) {
QDataStream in(&packet, QIODevice::ReadOnly);
in.setByteOrder(QDataStream::LittleEndian);
in.setFloatingPointPrecision(QDataStream::SinglePrecision);
quint8 header;
quint8 length;
quint8 cmdId;
quint8 value;
in >> header
>> cmdId
>> length
>> value;
switch(cmdId) {
case BKS_CMD::FX1:
device->setFX1(value);
break;
case BKS_CMD::FX2:
device->setFX2(value);
break;
case BKS_CMD::FC1:
device->setFC1(value);
break;
case BKS_CMD::FC2:
device->setFC2(value);
break;
case BKS_CMD::BAUD:
device->setBaud(value);
break;
}
makeResponse(cmdId);
}
void BksProxyServer::makeResponse(quint8 cmdId) {
QByteArray reply;
QDataStream out(&reply, QIODevice::WriteOnly);
out.setByteOrder(QDataStream::LittleEndian);
out << HEADER
<< cmdId;
switch(cmdId) {
case BKS_CMD::FC1:
out << (quint8)1;
out << device->getFC1();
break;
case BKS_CMD::FC2:
out << (quint8)1;
out << device->getFC2();
break;
case BKS_CMD::FX1:
out << (quint8)1;
out << device->getFX1();
break;
case BKS_CMD::FX2:
out << (quint8)1;
out << device->getFX2();
break;
case BKS_CMD::STATUS:
out << (quint8)5;
out << device->getFC1()
<< device->getFC2()
<< device->getFX1()
<< device->getFX2()
<< static_cast<quint8>(device->getStatusOnline());
break;
default:
return;
}
sendReply(reply);
}
void BksProxyServer::onNewConnection() {
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()));
}
void BksProxyServer::onDataReady() {
QTcpSocket *clientSocket = qobject_cast<QTcpSocket *>(sender());
if(clientSocket) {
currentClient = clientSocket;
ParseData(currentClient->readAll());
}
}