Skip to content

Commit

Permalink
RMG: show ping in netplay dialogs
Browse files Browse the repository at this point in the history
  • Loading branch information
Rosalie241 committed Jan 29, 2025
1 parent 80d6d8a commit 9dc2506
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ CreateNetplaySessionDialog::CreateNetplaySessionDialog(QWidget *parent, QWebSock
// prepare web socket
this->webSocket = webSocket;
connect(this->webSocket, &QWebSocket::textMessageReceived, this, &CreateNetplaySessionDialog::on_webSocket_textMessageReceived);
connect(this->webSocket, &QWebSocket::pong, this, &CreateNetplaySessionDialog::on_webSocket_pong);

// prepare broadcast
broadcastSocket.bind(QHostAddress(QHostAddress::AnyIPv4), 0);
Expand Down Expand Up @@ -83,6 +84,8 @@ CreateNetplaySessionDialog::CreateNetplaySessionDialog(QWidget *parent, QWebSock
networkAccessManager->setTransferTimeout(15000);
networkAccessManager->get(QNetworkRequest(QUrl(serverUrl)));
}

this->pingTimerId = this->startTimer(2000);
}

CreateNetplaySessionDialog::~CreateNetplaySessionDialog(void)
Expand Down Expand Up @@ -153,6 +156,17 @@ void CreateNetplaySessionDialog::validateCreateButton(void)
createButton->setEnabled(this->validate());
}

void CreateNetplaySessionDialog::timerEvent(QTimerEvent* event)
{
if (event->timerId() == this->pingTimerId)
{
if (this->webSocket->isValid())
{
this->webSocket->ping();
}
}
}

void CreateNetplaySessionDialog::on_webSocket_textMessageReceived(QString message)
{
QJsonDocument jsonDocument = QJsonDocument::fromJson(message.toUtf8());
Expand All @@ -173,6 +187,11 @@ void CreateNetplaySessionDialog::on_webSocket_textMessageReceived(QString messag
}
}

void CreateNetplaySessionDialog::on_webSocket_pong(quint64 elapsedTime, const QByteArray&)
{
this->pingLineEdit->setText(QString::number(elapsedTime) + " ms");
}

void CreateNetplaySessionDialog::on_broadcastSocket_readyRead()
{
while (this->broadcastSocket.hasPendingDatagrams())
Expand Down Expand Up @@ -222,6 +241,8 @@ void CreateNetplaySessionDialog::on_serverComboBox_currentIndexChanged(int index
return;
}

this->pingLineEdit->setText("Calculating...");

QString address = this->serverComboBox->itemData(index).toString();
this->webSocket->open(QUrl(address));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include <QNetworkReply>
#include <QJsonObject>
#include <QTimerEvent>
#include <QWebSocket>
#include <QUdpSocket>
#include <QDialog>
Expand Down Expand Up @@ -40,6 +41,8 @@ class CreateNetplaySessionDialog : public QDialog, private Ui::CreateNetplaySess
QWebSocket* webSocket;
QUdpSocket broadcastSocket;

int pingTimerId = -1;

QJsonObject sessionJson;
QString sessionFile;

Expand All @@ -48,8 +51,12 @@ class CreateNetplaySessionDialog : public QDialog, private Ui::CreateNetplaySess
bool validate(void);
void validateCreateButton(void);

protected:
void timerEvent(QTimerEvent *event) Q_DECL_OVERRIDE;

private slots:
void on_webSocket_textMessageReceived(QString message);
void on_webSocket_pong(quint64 elapsedTime, const QByteArray&);
void on_broadcastSocket_readyRead(void);
void on_networkAccessManager_Finished(QNetworkReply* reply);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,30 @@
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_5">
<item>
<widget class="QLabel" name="label_5">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Server ping</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="pingLineEdit">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ NetplaySessionBrowserDialog::NetplaySessionBrowserDialog(QWidget *parent, QWebSo
connect(this->webSocket, &QWebSocket::connected, this, &NetplaySessionBrowserDialog::on_webSocket_connected);
connect(this->webSocket, &QWebSocket::disconnected, this, &NetplaySessionBrowserDialog::on_webSocket_disconnected);
connect(this->webSocket, &QWebSocket::textMessageReceived, this, &NetplaySessionBrowserDialog::on_webSocket_textMessageReceived);
connect(this->webSocket, &QWebSocket::pong, this, &NetplaySessionBrowserDialog::on_webSocket_pong);

// copy rom data for later
this->romData = modelData;
Expand Down Expand Up @@ -78,6 +79,8 @@ NetplaySessionBrowserDialog::NetplaySessionBrowserDialog(QWidget *parent, QWebSo
}

this->validateJoinButton();

this->pingTimerId = this->startTimer(2000);
}

NetplaySessionBrowserDialog::~NetplaySessionBrowserDialog(void)
Expand Down Expand Up @@ -158,6 +161,17 @@ void NetplaySessionBrowserDialog::validateJoinButton(void)
joinButton->setEnabled(this->validate());
}

void NetplaySessionBrowserDialog::timerEvent(QTimerEvent *event)
{
if (event->timerId() == this->pingTimerId)
{
if (this->webSocket->isValid())
{
this->webSocket->ping();
}
}
}

void NetplaySessionBrowserDialog::on_webSocket_connected(void)
{
if (!this->webSocket->isValid())
Expand Down Expand Up @@ -241,6 +255,11 @@ void NetplaySessionBrowserDialog::on_webSocket_textMessageReceived(QString messa
}
}

void NetplaySessionBrowserDialog::on_webSocket_pong(quint64 elapsedTime, const QByteArray&)
{
this->pingLineEdit->setText(QString::number(elapsedTime) + " ms");
}

void NetplaySessionBrowserDialog::on_webSocket_disconnected()
{
this->sessionBrowserWidget->Reset();
Expand Down Expand Up @@ -299,6 +318,8 @@ void NetplaySessionBrowserDialog::on_serverComboBox_currentIndexChanged(int inde
return;
}

this->pingLineEdit->setText("Calculating...");

this->sessionBrowserWidget->StartRefresh();

QString address = this->serverComboBox->itemData(index).toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include <QTableWidgetItem>
#include <QNetworkReply>
#include <QTimerEvent>
#include <QJsonObject>
#include <QWebSocket>
#include <QUdpSocket>
Expand Down Expand Up @@ -44,14 +45,20 @@ class NetplaySessionBrowserDialog : public QDialog, private Ui::NetplaySessionBr
QString sessionFile;
QMap<QString, CoreRomSettings> romData;

int pingTimerId = -1;

QString showROMDialog(QString name, QString md5);

bool validate(void);
void validateJoinButton(void);

protected:
void timerEvent(QTimerEvent *event) Q_DECL_OVERRIDE;

private slots:
void on_webSocket_connected(void);
void on_webSocket_textMessageReceived(QString message);
void on_webSocket_pong(quint64 elapsedTime, const QByteArray&);
void on_webSocket_disconnected(void);

void on_broadcastSocket_readyRead(void);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,30 @@
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLabel" name="label_2">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Server ping</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="pingLineEdit">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="UserInterface::Widget::NetplaySessionBrowserWidget" name="sessionBrowserWidget">
<property name="sizePolicy">
Expand Down

0 comments on commit 9dc2506

Please sign in to comment.