forked from teamnwah/openmw-tes3coop
[Browser] Add PingUpdater & PingHelper classes for detached ping
parent
42eaa7a8fd
commit
db16e45e76
@ -0,0 +1,58 @@
|
||||
//
|
||||
// Created by koncord on 03.05.17.
|
||||
//
|
||||
|
||||
#include "PingHelper.hpp"
|
||||
#include "ServerModel.hpp"
|
||||
#include <QDebug>
|
||||
#include "PingUpdater.hpp"
|
||||
|
||||
void PingHelper::Add(int row, AddrPair addrPair)
|
||||
{
|
||||
pingUpdater->addServer(row, addrPair);
|
||||
if(!pingThread->isRunning())
|
||||
pingThread->start();
|
||||
}
|
||||
|
||||
void PingHelper::Stop()
|
||||
{
|
||||
emit pingUpdater->stop();
|
||||
}
|
||||
|
||||
void PingHelper::SetModel(QAbstractTableModel *model)
|
||||
{
|
||||
this->model = model;
|
||||
}
|
||||
|
||||
void PingHelper::update(int row, unsigned ping)
|
||||
{
|
||||
model->setData(model->index(row, ServerData::PING), ping);
|
||||
}
|
||||
|
||||
PingHelper &PingHelper::Get()
|
||||
{
|
||||
static PingHelper helper;
|
||||
return helper;
|
||||
}
|
||||
|
||||
PingHelper::PingHelper()
|
||||
{
|
||||
QObject();
|
||||
pingThread = new QThread;
|
||||
pingUpdater = new PingUpdater;
|
||||
pingUpdater->moveToThread(pingThread);
|
||||
|
||||
connect(pingThread, SIGNAL(started()), pingUpdater, SLOT(process()));
|
||||
connect(pingUpdater, SIGNAL(start()), pingThread, SLOT(start()));
|
||||
connect(pingUpdater, SIGNAL(finished()), pingThread, SLOT(quit()));
|
||||
connect(this, SIGNAL(stop()), pingUpdater, SLOT(stop()));
|
||||
//connect(pingUpdater, SIGNAL(finished()), pingUpdater, SLOT(deleteLater()));
|
||||
connect(pingUpdater, SIGNAL(updateModel(int, unsigned)), this, SLOT(update(int, unsigned)));
|
||||
|
||||
|
||||
}
|
||||
|
||||
PingHelper::~PingHelper()
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
//
|
||||
// Created by koncord on 03.05.17.
|
||||
//
|
||||
|
||||
#ifndef OPENMW_PINGHELPER_HPP
|
||||
#define OPENMW_PINGHELPER_HPP
|
||||
|
||||
#include <QObject>
|
||||
#include <QAbstractTableModel>
|
||||
#include <QThread>
|
||||
#include "Types.hpp"
|
||||
|
||||
class PingUpdater;
|
||||
|
||||
class PingHelper : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
|
||||
void Add(int row, AddrPair addrPair);
|
||||
void Stop();
|
||||
void SetModel(QAbstractTableModel *model);
|
||||
//void UpdateImmedialy(PingUpdater::AddrPair addrPair);
|
||||
static PingHelper &Get();
|
||||
private:
|
||||
PingHelper();
|
||||
~PingHelper();
|
||||
|
||||
PingHelper(const PingHelper&) = delete;
|
||||
PingHelper& operator=(const PingHelper&) = delete;
|
||||
signals:
|
||||
void stop();
|
||||
public slots:
|
||||
void update(int row, unsigned ping);
|
||||
private:
|
||||
QThread *pingThread;
|
||||
PingUpdater *pingUpdater;
|
||||
QAbstractTableModel *model;
|
||||
};
|
||||
|
||||
|
||||
#endif //OPENMW_PINGHELPER_HPP
|
@ -0,0 +1,50 @@
|
||||
//
|
||||
// Created by koncord on 02.05.17.
|
||||
//
|
||||
|
||||
#include "PingUpdater.hpp"
|
||||
#include "netutils/Utils.hpp"
|
||||
#include <QDebug>
|
||||
#include <QModelIndex>
|
||||
#include <QThread>
|
||||
|
||||
void PingUpdater::stop()
|
||||
{
|
||||
servers.clear();
|
||||
run = false;
|
||||
}
|
||||
|
||||
void PingUpdater::addServer(int row, AddrPair addr)
|
||||
{
|
||||
servers.push_back({row, addr});
|
||||
run = true;
|
||||
emit start();
|
||||
}
|
||||
|
||||
void PingUpdater::process()
|
||||
{
|
||||
while(run)
|
||||
{
|
||||
if(servers.count() == 0)
|
||||
{
|
||||
QThread::msleep(1000);
|
||||
if(servers.count() == 0)
|
||||
{
|
||||
qDebug() << "PingUpdater stopped due to inactivity";
|
||||
run = false;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
ServerRow server = servers.back();
|
||||
servers.pop_back();
|
||||
|
||||
unsigned ping = PingRakNetServer(server.second.first.toLatin1(), server.second.second);
|
||||
|
||||
qDebug() << "Pong from" << server.second.first + "|" + QString::number(server.second.second)
|
||||
<< ":" << ping << "ms";
|
||||
|
||||
emit updateModel(server.first, ping);
|
||||
}
|
||||
emit finished();
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
//
|
||||
// Created by koncord on 02.05.17.
|
||||
//
|
||||
|
||||
#ifndef OPENMW_PINGUPDATER_HPP
|
||||
#define OPENMW_PINGUPDATER_HPP
|
||||
|
||||
#include <QObject>
|
||||
#include <QVector>
|
||||
|
||||
#include "Types.hpp"
|
||||
|
||||
class PingUpdater : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
void addServer(int row, AddrPair addrPair);
|
||||
public slots:
|
||||
void stop();
|
||||
void process();
|
||||
signals:
|
||||
void start();
|
||||
void updateModel(int row, unsigned ping);
|
||||
void finished();
|
||||
private:
|
||||
QVector<ServerRow> servers;
|
||||
bool run;
|
||||
};
|
||||
|
||||
|
||||
#endif //OPENMW_PINGUPDATER_HPP
|
@ -0,0 +1,15 @@
|
||||
//
|
||||
// Created by koncord on 07.05.17.
|
||||
//
|
||||
|
||||
#ifndef OPENMW_TYPES_HPP
|
||||
#define OPENMW_TYPES_HPP
|
||||
|
||||
#include <QPair>
|
||||
#include <QString>
|
||||
|
||||
typedef QPair <QString, unsigned short> AddrPair;
|
||||
typedef QPair <int, AddrPair> ServerRow;
|
||||
|
||||
|
||||
#endif //OPENMW_TYPES_HPP
|
Loading…
Reference in New Issue