-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmainwindow.cpp
146 lines (131 loc) · 4.75 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
#include "mainwindow.h"
#include "accepter.h"
#include <QNetworkInterface>
#include <QtWidgets>
class MainWindowPrivate
{
public:
explicit MainWindowPrivate(QMainWindow* owner) : owner(owner){
ipBox = new QComboBox(owner);
portEdit = new QLineEdit(owner);
threadSpinBox = new QSpinBox(owner);
threadSpinBox->setRange(0, 2147483647);
threadSpinBox->setValue(QThreadPool::globalInstance()->maxThreadCount() * 2);
listenBtn = new QPushButton(QObject::tr("Listen"), owner);
listenBtn->setCheckable(true);
messageEdit = new QTextEdit(owner);
messageEdit->document()->setMaximumBlockCount(1000); //1000 max
currentConnections = new QLabel("0", owner);
historyMaxConnections = new QLabel("0", owner);
}
QMainWindow *owner;
QComboBox *ipBox;
QLineEdit *portEdit;
QSpinBox *threadSpinBox;
QPushButton *listenBtn;
QTextEdit *messageEdit;
QLabel *currentConnections;
QLabel *historyMaxConnections;
Accepter *accepter;
};
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, d_ptr(new MainWindowPrivate(this))
{
setupUI();
initParam();
buildConnect();
qDebug() << "MainWindows: " << QThread::currentThreadId();
}
MainWindow::~MainWindow()
{
delete d_ptr;
}
void MainWindow::onListen(bool state)
{
if(state){
d_ptr->listenBtn->setChecked(false);
QString port = d_ptr->portEdit->text();
if(port.isEmpty()){
QMessageBox::warning(this, tr("Warning!"),
tr("Port is empty!"), QMessageBox::Ok);
return;
}
int num = d_ptr->threadSpinBox->value();
if(num == 0)
d_ptr->messageEdit->append(tr("Single thread mode reactor!"));
else
d_ptr->messageEdit->append(tr("Multithreaded (master-slave) mode reactor!"));
d_ptr->accepter = new Accepter(quint16(port.toUInt()), num, this);
connect(d_ptr->accepter, &Accepter::message, d_ptr->messageEdit, &QTextEdit::append, Qt::UniqueConnection);
connect(d_ptr->accepter, &Accepter::maxCount, this, &MainWindow::onMaxCount, Qt::UniqueConnection);
connect(d_ptr->accepter, &Accepter::clientCount, this, &MainWindow::onCount, Qt::UniqueConnection);
d_ptr->accepter->start();
bool ok = d_ptr->accepter->isRunning();
d_ptr->ipBox->setEnabled(!ok);
d_ptr->portEdit->setEnabled(!ok);
d_ptr->threadSpinBox->setEnabled(!ok);
d_ptr->listenBtn->setChecked(ok);
QString text = ok? tr("Disconnect") : tr("Listen");
d_ptr->listenBtn->setText(text);
}
else if(d_ptr->accepter->isRunning()){
delete d_ptr->accepter;
d_ptr->accepter = nullptr;
d_ptr->ipBox->setEnabled(true);
d_ptr->portEdit->setEnabled(true);
d_ptr->threadSpinBox->setEnabled(true);
d_ptr->listenBtn->setChecked(false);
d_ptr->listenBtn->setText(tr("Listen"));
}
}
void MainWindow::onMaxCount(QAtomicInt count)
{
d_ptr->historyMaxConnections->setNum(count);
}
void MainWindow::onCount(QAtomicInt count)
{
d_ptr->currentConnections->setNum(count);
}
void MainWindow::setupUI()
{
QHBoxLayout *topLayout = new QHBoxLayout;
topLayout->addWidget(new QLabel(tr("Local IP: "), this));
topLayout->addWidget(d_ptr->ipBox);
topLayout->addWidget(new QLabel(tr("Local Port: "), this));
topLayout->addWidget(d_ptr->portEdit);
topLayout->addWidget(new QLabel(tr("SubReactor Nums: "), this));
topLayout->addWidget(d_ptr->threadSpinBox);
topLayout->addWidget(d_ptr->listenBtn);
QHBoxLayout *midLayout = new QHBoxLayout;
midLayout->addWidget(new QLabel(tr("Current Connections: "), this));
midLayout->addWidget(d_ptr->currentConnections);
midLayout->addStretch(1);
midLayout->addWidget(new QLabel(tr("History max Connections: "), this));
midLayout->addWidget(d_ptr->historyMaxConnections);
QVBoxLayout *layout = new QVBoxLayout;
layout->addLayout(topLayout);
layout->addLayout(midLayout);
layout->addWidget(d_ptr->messageEdit);
QFrame *frame = new QFrame(this);
frame->setLayout(layout);
setCentralWidget(frame);
}
void MainWindow::initParam()
{
QStringList ipListBox;
QList<QHostAddress> ipList = QNetworkInterface::allAddresses(); //获得IP
foreach(QHostAddress address, ipList){
if(address.protocol() == QAbstractSocket::IPv4Protocol){
ipListBox << address.toString();
}
}
d_ptr->ipBox->clear();
d_ptr->ipBox->addItems(ipListBox);
d_ptr->portEdit->setValidator(new QIntValidator(0, 65536, d_ptr->portEdit));
d_ptr->portEdit->setText("65533");
}
void MainWindow::buildConnect()
{
connect(d_ptr->listenBtn, &QPushButton::clicked, this, &MainWindow::onListen);
}