-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
130 lines (112 loc) · 4.3 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
#include <QString>
#include <QRandomGenerator>
#include <QTcpServer>
#include <QTcpSocket>
#include <QHostAddress>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "barrage_item.h"
#include "barrage_screen.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle("Server");
initServer();
XYBarrageScreen::getScreen()->show();
XYBarrageScreen::getScreen()->setMaxBarrageNumber(100);
}
MainWindow::~MainWindow()
{
delete ui;
}
XYBarrageItem *MainWindow::getItem(QString drawText) {
XYContents *contents = new XYContents(drawText);
contents->next = new XYContents(XYContents::LF);
XYBarrageItem *item = new XYBarrageItem(contents);
QColor c(QRandomGenerator::global()->bounded(256),
QRandomGenerator::global()->bounded(256),
QRandomGenerator::global()->bounded(256));
QFont font("微软雅黑");
font.setPointSize(30);
item->setTextFont(font);
item->setTextColor(c);
item->setShowTimes(3500);
item->setStartPos(QPoint(1980, qMin(QRandomGenerator::global()->bounded(54) * 15, 800)));
item->setAnimation(QEasingCurve::Type(0));
return item;
}
void MainWindow::initServer() {
server = new QTcpServer(this);
connect(ui->startListen, &QPushButton::clicked, [this]{
if (server -> isListening()) {
closeServer();
qDebug() << QString("Stop listening successfully.");
ui->startListen->setText("启动监听");
} else {
const QString address_text = "127.0.0.1";
const QHostAddress address = (address_text == "Any") ? QHostAddress::Any : QHostAddress(address_text);
const unsigned short port = 8897;
if (server->listen(address, port)) {
qDebug() << QString("Listening %1:%2 successfully.").arg(address.toString()).arg(port);
ui->startListen->setText("停止监听");
} else {
qDebug() << QString("Failed to start listening: %1").arg(server->errorString());
}
}
updateState();
});
connect(server, &QTcpServer::newConnection, this, [this] {
while(server -> hasPendingConnections()) {
QTcpSocket *socket = server->nextPendingConnection();
clientList.append(socket);
qDebug() << QString("[%1:%2] socket connected!").arg(socket->peerAddress().toString()).arg(socket->peerPort());
updateState();
connect(socket, &QTcpSocket::readyRead, [this, socket] {
if (socket->bytesAvailable() <= 0) return;
const QString recv_text = QString::fromUtf8(socket->readAll());
qDebug() << recv_text;
XYBarrageItem* item = getItem(recv_text);
XYBarrageScreen::getScreen()->addItem(item);
});
connect(socket, &QAbstractSocket::errorOccurred, [socket](QAbstractSocket::SocketError){
qDebug() << QString("[%1:%2] Soket Error:%3")
.arg(socket->peerAddress().toString())
.arg(socket->peerPort())
.arg(socket->errorString());
});
connect(socket, &QTcpSocket::disconnected, [this, socket] {
socket -> deleteLater();
clientList.removeOne(socket);
qDebug() << QString("[%1:%2] socket disonnected")
.arg(socket->peerAddress().toString())
.arg(socket->peerPort());
updateState();
});
}
});
connect(server, &QTcpServer::acceptError, [this](QAbstractSocket::SocketError) {
qDebug() << "Server Error:" << server->errorString();
});
}
void MainWindow::updateState() {
if(server->isListening()){
setWindowTitle(QString("Server[%1:%2] connections:%3")
.arg(server->serverAddress().toString())
.arg(server->serverPort())
.arg(clientList.count()));
}else{
setWindowTitle("Server");
}
}
void MainWindow::closeServer() {
server->close();
for(QTcpSocket * socket:clientList)
{
socket->disconnectFromHost();
if(socket->state()!=QAbstractSocket::UnconnectedState){
socket->abort();
}
}
}