Skip to content

Commit

Permalink
补全onlinelist和heartbeat
Browse files Browse the repository at this point in the history
Signed-off-by: liuyanghejerry <liuyanghejerry@126.com>
  • Loading branch information
liuyanghejerry committed Nov 22, 2014
1 parent 7074540 commit 329d428
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 44 deletions.
14 changes: 12 additions & 2 deletions src/common/network/clientsocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ ClientSocket::ClientSocket(QObject *parent) :

connect(hb_timer_, &QTimer::timeout,
this, &ClientSocket::sendHeartbeat);
connect(hb_timer_, &QTimer::timeout,
this, &ClientSocket::requestOnlinelist);
}

ClientSocket::State ClientSocket::currentState() const
Expand Down Expand Up @@ -121,6 +123,11 @@ void ClientSocket::setRoomCloseFlag()
remove_after_close_ = true;
}

int ClientSocket::getDelay() const
{
return roomDelay_.load();
}

QString ClientSocket::toUrl() const
{
return genRoomUrl(address().toString(),
Expand Down Expand Up @@ -184,7 +191,7 @@ void ClientSocket::onResponseRoomList(const QJsonObject &obj)
roomsInfo.insert(name, m);
}
// TODO: emit roomslist
emit roomlistFetched(roomsInfo);
emit roomListFetched(roomsInfo);
}

void ClientSocket::onResponseNewRoom(const QJsonObject &m)
Expand Down Expand Up @@ -364,6 +371,8 @@ void ClientSocket::onResponseArchive(const QJsonObject &o)
void ClientSocket::onCommandActionClose(const QJsonObject &)
{
emit roomAboutToClose();
cancelPendings();
stopHeartbeat();
setRoomCloseFlag();
}

Expand Down Expand Up @@ -467,7 +476,8 @@ void ClientSocket::onResponseHeartbeat(const QJsonObject &o)
int server_time = o.value("timestamp").toInt();
int now = QDateTime::currentMSecsSinceEpoch() / 1000;
int delta = now - server_time;
roomDelay_ = delta;
roomDelay_.store(delta);
emit delayGet(getDelay());
}


Expand Down
7 changes: 5 additions & 2 deletions src/common/network/clientsocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "../misc/router.h"
#include <QSize>
#include <QMutex>
#include <QAtomicInt>
class QTimer;
class ArchiveFile;

Expand Down Expand Up @@ -69,6 +70,7 @@ class ClientSocket : public Socket
quint64 archiveSize() const;
void setPoolEnabled(bool on);
void setRoomCloseFlag();
int getDelay() const;
QString roomKey() const;
QString toUrl() const;

Expand Down Expand Up @@ -122,7 +124,7 @@ class ClientSocket : public Socket
void clientSocketError(int);
void requestUnauthed();
void managerConnected();
void roomlistFetched(QHash<QString, QJsonObject>);
void roomListFetched(QHash<QString, QJsonObject>);
void roomCreated();
void roomJoined();

Expand All @@ -131,6 +133,7 @@ class ClientSocket : public Socket
void memberListFetched(const QHash<QString, QVariantList> &list);
void getNotified(const QString &content);
void getKicked();
void delayGet(int);

void dataPack(const QJsonObject&);
void msgPack(const QJsonObject&);
Expand Down Expand Up @@ -175,7 +178,7 @@ public slots:
const static int HEARTBEAT_RATE = 30; // sends 30 heartbeat packs per min
QTimer *hb_timer_;
State state_;
int roomDelay_;
QAtomicInt roomDelay_;
private slots:
void initRouter();
void setClientId(const QString &id);
Expand Down
65 changes: 26 additions & 39 deletions src/painttyDesktop/widgets/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -565,11 +565,8 @@ void MainWindow::socketInit()
this, &MainWindow::onNotify);
connect(&client_socket, &ClientSocket::getKicked,
this, &MainWindow::onKicked);

// QTimer *t = new QTimer(this);
// connect(t, &QTimer::timeout,
// this, &MainWindow::requestOnlinelist);
// t->start(5000);
connect(&client_socket, &ClientSocket::delayGet,
this, &MainWindow::onDelayGet);
}

void MainWindow::onServerDisconnected()
Expand Down Expand Up @@ -623,38 +620,30 @@ void MainWindow::onKicked()
GradualBox::showText(tr("You've been kicked by room owner."), true, 3000);
}

//void MainWindow::onResponseHeartbeat(const QJsonObject &o)
//{
// if(!o.contains("timestamp") || !networkIndicator_){
// return;
// }
// qDebug()<<o;
// int server_time = o.value("timestamp").toInt();
// int now = QDateTime::currentMSecsSinceEpoch() / 1000;
// int delta = now - server_time;
// typedef NetworkIndicator::LEVEL NL;
// if(delta < 0){
// qDebug()<<now<<server_time;
// networkIndicator_->setLevel(NL::UNKNOWN);
// return;
// }
// if(delta > 60){
// networkIndicator_->setLevel(NL::NONE);
// return;
// }
// if(delta > 20){
// networkIndicator_->setLevel(NL::LOW);
// return;
// }
// if(delta > 10){
// networkIndicator_->setLevel(NL::MEDIUM);
// return;
// }
// if(delta < 10){
// networkIndicator_->setLevel(NL::GOOD);
// return;
// }
//}
void MainWindow::onDelayGet(const int delay)
{
typedef NetworkIndicator::LEVEL NL;
if(delay < 0){
networkIndicator_->setLevel(NL::UNKNOWN);
return;
}
if(delay > 60){
networkIndicator_->setLevel(NL::NONE);
return;
}
if(delay > 20){
networkIndicator_->setLevel(NL::LOW);
return;
}
if(delay > 10){
networkIndicator_->setLevel(NL::MEDIUM);
return;
}
if(delay < 10){
networkIndicator_->setLevel(NL::GOOD);
return;
}
}

void MainWindow::onClientSocketError(const int code)
{
Expand Down Expand Up @@ -962,8 +951,6 @@ void MainWindow::deleteLayer(const QString &name)

void MainWindow::closeEvent( QCloseEvent * event )
{
client_socket.cancelPendings();
client_socket.stopHeartbeat();
ui->canvas->pause();

QProgressDialog dialog(tr("Waiting for sync, please do not close.\n"\
Expand Down
1 change: 1 addition & 0 deletions src/painttyDesktop/widgets/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ private slots:
void onMemberlistFetched(const QHash<QString, QVariantList> &list);
void onNotify(const QString &content);
void onKicked();
void onDelayGet(const int delay);
// void onResponseHeartbeat(const QJsonObject &o);
void onClientSocketError(const int code);
};
Expand Down
2 changes: 1 addition & 1 deletion src/painttyDesktop/widgets/roomlistdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ RoomListDialog::RoomListDialog(QWidget *parent) :
this, &RoomListDialog::requestRoomList);
connect(&client_socket, &ClientSocket::roomCreated,
this, &RoomListDialog::onNewRoomCreated);
connect(&client_socket, &ClientSocket::roomlistFetched,
connect(&client_socket, &ClientSocket::roomListFetched,
this, &RoomListDialog::onRoomlist);
connect(&client_socket, &ClientSocket::roomJoined,
this, &RoomListDialog::accept);
Expand Down

0 comments on commit 329d428

Please sign in to comment.