-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
238 lines (218 loc) · 8.88 KB
/
mainwindow.cpp
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "client.h"
#include "server.h"
#include <QHostAddress>
#include <QJsonDocument>
#include <QJsonObject>
#include <QFile>
#include <QTimer>
#define storeCfgFilePath "config.json"
// 声明全局 QTextBrowser
static TextBrowserStreamBuf* globalBuffer = nullptr;
static std::thread* pClientThread = nullptr;
static Client* pClient = nullptr;
static std::thread* pServerThread = nullptr;
static Server* pServer = nullptr;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
loadConfigFromJson();
ui->textBrowser_log_display->setStyleSheet ("QTextBrowser {"
"background-color: lightgreen;"
// "background-image: url('path/to/your/image.jpg');"
// "background-repeat: no-repeat;"
// "background-position: center;"
// "background-attachment: scroll;"
"}");
ui->lcdNumber->setSegmentStyle(QLCDNumber::Flat);
ui->lcdNumber->setStyleSheet("QLCDNumber { background-color: white; color: black; border: 1px solid black; }");
ui->group_client->setStyleSheet("QGroupBox { background-color: lightgreen; border: 1px solid black;}");
ui->group_server->setStyleSheet("QGroupBox { background-color: lightgreen; border: 1px solid black;}");
// 重定向 stdout
globalBuffer = new TextBrowserStreamBuf(ui->textBrowser_log_display);
std::cerr.rdbuf(globalBuffer);
std::cout.rdbuf(globalBuffer);
connect(ui->pushButton_runClient, &QPushButton::clicked, this, &MainWindow::onPushButtonClientRunClicked);
connect(ui->pushButton_runServer, &QPushButton::clicked, this, &MainWindow::onPushButtonServerRunClicked);
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &MainWindow::periodTask);
timer->start(1000);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::onPushButtonClientRunClicked() {
ui->pushButton_runClient->setEnabled(false);
if (pClient == nullptr) {
// 校验 IPv6 地址
QHostAddress address;
if (!address.setAddress(ui->lineEdit_clientServerIPv6Address->text())) {
LogError("服务器IPV6地址无效");
ui->pushButton_runClient->setEnabled(true);
return;
}
// 校验端口号
bool validPort;
uint16_t serverPort = ui->lineEdit_clientServerPort->text().toUShort(&validPort);
if (!validPort || serverPort == 0 || serverPort > 65535) {
LogError("服务器端口号无效");
ui->pushButton_runClient->setEnabled(true);
return;
}
uint16_t remotePort = ui->lineEdit_clientRemotePort->text().toUShort(&validPort);
if (!validPort || remotePort == 0 || remotePort > 65535) {
LogError("远程端口号无效");
ui->pushButton_runClient->setEnabled(true);
return;
}
uint16_t localPort = ui->lineEdit_clientLocalPort->text().toUShort(&validPort);
if (!validPort || localPort == 0 || localPort > 65535) {
LogError("本地端口号无效");
ui->pushButton_runClient->setEnabled(true);
return;
}
pClient = new Client(ui->lineEdit_clientServerIPv6Address->text().toUtf8().constData(), serverPort, remotePort, localPort);
pClientThread = new std::thread(Client::start, std::ref(*pClient));
ui->pushButton_runClient->setText("停止客户端");
setClientConfigReadOnly(true);
saveClientConfigToJson();
} else {
pClient->stop();
if (pClientThread->joinable())
pClientThread->join();
delete pClientThread;
pClientThread = nullptr;
delete pClient;
pClient = nullptr;
ui->pushButton_runClient->setText("运行客户端");
setClientConfigReadOnly(false);
}
ui->pushButton_runClient->setEnabled(true);
}
void MainWindow::onPushButtonServerRunClicked() {
ui->pushButton_runServer->setEnabled(false);
if (pServer == nullptr) {
bool validPort;
uint16_t serverPort = ui->lineEdit_serverServerPort->text().toUShort(&validPort);
if (!validPort || serverPort == 0 || serverPort > 65535) {
LogError("服务器端口号无效");
ui->pushButton_runServer->setEnabled(true);
return;
}
uint16_t remotePort = ui->lineEdit_serverRemotePort->text().toUShort(&validPort);
if (!validPort || remotePort == 0 || remotePort > 65535) {
LogError("远程端口号无效");
ui->pushButton_runServer->setEnabled(true);
return;
}
pServer = new Server(serverPort, remotePort);
pServerThread = new std::thread(Server::start, std::ref(*pServer));
ui->pushButton_runServer->setText("停止服务端");
setServerConfigReadOnly(true);
saveServerConfigToJson();
} else {
pServer->stop();
if (pServerThread->joinable())
pServerThread->join();
delete pServerThread;
pServerThread = nullptr;
delete pServer;
pServer = nullptr;
ui->pushButton_runServer->setText("运行服务端");
setServerConfigReadOnly(false);
}
ui->pushButton_runServer->setEnabled(true);
}
void MainWindow::saveClientConfigToJson() {
QFile file(storeCfgFilePath);
QJsonObject json;
if (file.open(QFile::ReadOnly)) {
QByteArray data = file.readAll();
QJsonDocument doc(QJsonDocument::fromJson(data));
json = doc.object();
file.close();
}
json["clientServerIPv6Address"] = ui->lineEdit_clientServerIPv6Address->text();
json["clientServerPort"] = ui->lineEdit_clientServerPort->text();
json["clientRemotePort"] = ui->lineEdit_clientRemotePort->text();
json["clientLocalPort"] = ui->lineEdit_clientLocalPort->text();
if (file.open(QFile::WriteOnly)) {
QJsonDocument doc(json);
file.write(doc.toJson());
file.close();
LogDebug("Client config saved successfully!");
} else {
LogError("Client config saved failed.");
}
}
void MainWindow::saveServerConfigToJson() {
QFile file(storeCfgFilePath);
QJsonObject json;
if (file.open(QFile::ReadOnly)) {
QByteArray data = file.readAll();
QJsonDocument doc(QJsonDocument::fromJson(data));
json = doc.object();
file.close();
}
json["serverRemotePort"] = ui->lineEdit_serverRemotePort->text();
json["serverServerPort"] = ui->lineEdit_serverServerPort->text();
if (file.open(QFile::WriteOnly)) {
QJsonDocument doc(json);
file.write(doc.toJson());
file.close();
LogDebug("Server config saved successfully!");
} else {
LogError("Server config saved failed.");
}
}
void MainWindow::loadConfigFromJson() {
QFile file(storeCfgFilePath);
if (file.open(QFile::ReadOnly)) {
QByteArray data = file.readAll();
QJsonDocument doc(QJsonDocument::fromJson(data));
QJsonObject json = doc.object();
// 设置控件的值
if (json.contains("serverRemotePort")) {
ui->lineEdit_serverRemotePort->setText(json["serverRemotePort"].toString());
}
if (json.contains("serverServerPort")) {
ui->lineEdit_serverServerPort->setText(json["serverServerPort"].toString());
}
if (json.contains("clientServerIPv6Address")) {
ui->lineEdit_clientServerIPv6Address->setText(json["clientServerIPv6Address"].toString());
}
if (json.contains("clientServerPort")) {
ui->lineEdit_clientServerPort->setText(json["clientServerPort"].toString());
}
if (json.contains("clientRemotePort")) {
ui->lineEdit_clientRemotePort->setText(json["clientRemotePort"].toString());
}
if (json.contains("clientLocalPort")) {
ui->lineEdit_clientLocalPort->setText(json["clientLocalPort"].toString());
}
file.close();
}
}
void MainWindow::setClientConfigReadOnly(bool flag) {
ui->lineEdit_clientServerIPv6Address->setReadOnly(flag);
ui->lineEdit_clientServerPort->setReadOnly(flag);
ui->lineEdit_clientRemotePort->setReadOnly(flag);
ui->lineEdit_clientLocalPort->setReadOnly(flag);
}
void MainWindow::setServerConfigReadOnly(bool flag) {
ui->lineEdit_serverRemotePort->setReadOnly(flag);
ui->lineEdit_serverServerPort->setReadOnly(flag);
}
void MainWindow::periodTask() {
if (pServerThread != nullptr && pServer != nullptr) {
pServer->cleanUselessSockets();
int bufferNum = pServer->getUdpPacketQueueSize();
ui->lcdNumber->display(bufferNum);
}
std::cout << std::flush;
std::cerr << std::flush;
}