raised server timeout, added ping widget
This commit is contained in:
parent
3c8f2b878b
commit
b8690627c8
4 changed files with 62 additions and 18 deletions
|
@ -74,12 +74,6 @@ void PendingCommand::responseReceived(ServerResponse resp)
|
||||||
deleteLater();
|
deleteLater();
|
||||||
}
|
}
|
||||||
|
|
||||||
void PendingCommand::checkTimeout()
|
|
||||||
{
|
|
||||||
if (++time > 5)
|
|
||||||
emit timeout();
|
|
||||||
}
|
|
||||||
|
|
||||||
void PendingCommand_ListPlayers::responseReceived(ServerResponse resp)
|
void PendingCommand_ListPlayers::responseReceived(ServerResponse resp)
|
||||||
{
|
{
|
||||||
if (resp == RespOk)
|
if (resp == RespOk)
|
||||||
|
@ -138,12 +132,6 @@ Client::~Client()
|
||||||
disconnectFromServer();
|
disconnectFromServer();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Client::timeout()
|
|
||||||
{
|
|
||||||
emit serverTimeout();
|
|
||||||
disconnectFromServer();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Client::slotSocketError(QAbstractSocket::SocketError /*error*/)
|
void Client::slotSocketError(QAbstractSocket::SocketError /*error*/)
|
||||||
{
|
{
|
||||||
emit logSocketError(socket->errorString());
|
emit logSocketError(socket->errorString());
|
||||||
|
@ -376,8 +364,6 @@ PendingCommand *Client::cmd(const QString &s, PendingCommand *_pc)
|
||||||
pc = new PendingCommand(MsgId);
|
pc = new PendingCommand(MsgId);
|
||||||
pendingCommands.insert(MsgId, pc);
|
pendingCommands.insert(MsgId, pc);
|
||||||
connect(pc, SIGNAL(finished(ServerResponse)), this, SLOT(removePendingCommand()));
|
connect(pc, SIGNAL(finished(ServerResponse)), this, SLOT(removePendingCommand()));
|
||||||
connect(pc, SIGNAL(timeout()), this, SLOT(timeout()));
|
|
||||||
connect(timer, SIGNAL(timeout()), pc, SLOT(checkTimeout()));
|
|
||||||
return pc;
|
return pc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -406,7 +392,19 @@ void Client::disconnectFromServer()
|
||||||
|
|
||||||
void Client::ping()
|
void Client::ping()
|
||||||
{
|
{
|
||||||
cmd("ping");
|
int maxTime = 0;
|
||||||
|
QMapIterator<int, PendingCommand *> i(pendingCommands);
|
||||||
|
while (i.hasNext()) {
|
||||||
|
int time = i.next().value()->tick();
|
||||||
|
if (time > maxTime)
|
||||||
|
maxTime = time;
|
||||||
|
}
|
||||||
|
emit maxPingTime(maxTime, maxTimeout);
|
||||||
|
if (maxTime >= maxTimeout) {
|
||||||
|
emit serverTimeout();
|
||||||
|
disconnectFromServer();
|
||||||
|
} else
|
||||||
|
cmd("ping");
|
||||||
}
|
}
|
||||||
|
|
||||||
PendingCommand *Client::chatListChannels()
|
PendingCommand *Client::chatListChannels()
|
||||||
|
|
|
@ -201,12 +201,11 @@ private:
|
||||||
int time;
|
int time;
|
||||||
signals:
|
signals:
|
||||||
void finished(ServerResponse resp);
|
void finished(ServerResponse resp);
|
||||||
void timeout();
|
|
||||||
public slots:
|
public slots:
|
||||||
virtual void responseReceived(ServerResponse resp);
|
virtual void responseReceived(ServerResponse resp);
|
||||||
void checkTimeout();
|
|
||||||
public:
|
public:
|
||||||
PendingCommand(int _msgid = -1);
|
PendingCommand(int _msgid = -1);
|
||||||
|
int tick() { return ++time; }
|
||||||
int getMsgId() const { return msgid; }
|
int getMsgId() const { return msgid; }
|
||||||
void setMsgId(int _msgId) { msgid = _msgId; }
|
void setMsgId(int _msgId) { msgid = _msgId; }
|
||||||
};
|
};
|
||||||
|
@ -310,6 +309,7 @@ signals:
|
||||||
void playerIdReceived(int id, QString name);
|
void playerIdReceived(int id, QString name);
|
||||||
void gameEvent(const ServerEventData &msg);
|
void gameEvent(const ServerEventData &msg);
|
||||||
void chatEvent(const ChatEventData &msg);
|
void chatEvent(const ChatEventData &msg);
|
||||||
|
void maxPingTime(int seconds, int maxSeconds);
|
||||||
void serverTimeout();
|
void serverTimeout();
|
||||||
void logSocketError(const QString &errorString);
|
void logSocketError(const QString &errorString);
|
||||||
void serverError(ServerResponse resp);
|
void serverError(ServerResponse resp);
|
||||||
|
@ -318,7 +318,6 @@ signals:
|
||||||
private slots:
|
private slots:
|
||||||
void slotConnected();
|
void slotConnected();
|
||||||
void readLine();
|
void readLine();
|
||||||
void timeout();
|
|
||||||
void slotSocketError(QAbstractSocket::SocketError error);
|
void slotSocketError(QAbstractSocket::SocketError error);
|
||||||
void ping();
|
void ping();
|
||||||
void removePendingCommand();
|
void removePendingCommand();
|
||||||
|
@ -327,6 +326,8 @@ private slots:
|
||||||
void leaveGameResponse(ServerResponse response);
|
void leaveGameResponse(ServerResponse response);
|
||||||
private:
|
private:
|
||||||
static const int protocolVersion = 1;
|
static const int protocolVersion = 1;
|
||||||
|
static const int maxTimeout = 10;
|
||||||
|
|
||||||
QTimer *timer;
|
QTimer *timer;
|
||||||
QMap<int, PendingCommand *> pendingCommands;
|
QMap<int, PendingCommand *> pendingCommands;
|
||||||
QTcpSocket *socket;
|
QTcpSocket *socket;
|
||||||
|
|
|
@ -38,6 +38,31 @@
|
||||||
#include "zoneviewlayout.h"
|
#include "zoneviewlayout.h"
|
||||||
#include "chatwidget.h"
|
#include "chatwidget.h"
|
||||||
|
|
||||||
|
PingWidget::PingWidget(QWidget *parent)
|
||||||
|
: QWidget(parent), color(Qt::black)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
QSize PingWidget::sizeHint() const
|
||||||
|
{
|
||||||
|
return QSize(10, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PingWidget::paintEvent(QPaintEvent */*event*/)
|
||||||
|
{
|
||||||
|
QPainter painter(this);
|
||||||
|
painter.fillRect(0, 0, width(), height(), color);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PingWidget::setPercentage(int value, int max)
|
||||||
|
{
|
||||||
|
if (max == -1)
|
||||||
|
color = Qt::black;
|
||||||
|
else
|
||||||
|
color.setHsv(120 * (1.0 - ((double) value / max)), 255, 255);
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
|
||||||
void MainWindow::playerAdded(Player *player)
|
void MainWindow::playerAdded(Player *player)
|
||||||
{
|
{
|
||||||
menuBar()->addMenu(player->getPlayerMenu());
|
menuBar()->addMenu(player->getPlayerMenu());
|
||||||
|
@ -57,6 +82,8 @@ void MainWindow::statusChanged(ProtocolStatus _status)
|
||||||
delete game;
|
delete game;
|
||||||
game = 0;
|
game = 0;
|
||||||
}
|
}
|
||||||
|
pingWidget->setPercentage(0, -1);
|
||||||
|
aConnect->setEnabled(true);
|
||||||
aDisconnect->setEnabled(false);
|
aDisconnect->setEnabled(false);
|
||||||
aRestartGame->setEnabled(false);
|
aRestartGame->setEnabled(false);
|
||||||
aLeaveGame->setEnabled(false);
|
aLeaveGame->setEnabled(false);
|
||||||
|
@ -67,6 +94,7 @@ void MainWindow::statusChanged(ProtocolStatus _status)
|
||||||
emit logDisconnected();
|
emit logDisconnected();
|
||||||
break;
|
break;
|
||||||
case StatusLoggingIn:
|
case StatusLoggingIn:
|
||||||
|
aConnect->setEnabled(false);
|
||||||
aDisconnect->setEnabled(true);
|
aDisconnect->setEnabled(true);
|
||||||
break;
|
break;
|
||||||
case StatusIdle: {
|
case StatusIdle: {
|
||||||
|
@ -267,6 +295,7 @@ MainWindow::MainWindow(QTranslator *_translator, QWidget *parent)
|
||||||
sayLabel = new QLabel;
|
sayLabel = new QLabel;
|
||||||
sayEdit = new QLineEdit;
|
sayEdit = new QLineEdit;
|
||||||
sayLabel->setBuddy(sayEdit);
|
sayLabel->setBuddy(sayEdit);
|
||||||
|
pingWidget = new PingWidget;
|
||||||
|
|
||||||
client = new Client(this);
|
client = new Client(this);
|
||||||
gameSelector = new GameSelector(client);
|
gameSelector = new GameSelector(client);
|
||||||
|
@ -277,6 +306,7 @@ MainWindow::MainWindow(QTranslator *_translator, QWidget *parent)
|
||||||
QHBoxLayout *hLayout = new QHBoxLayout;
|
QHBoxLayout *hLayout = new QHBoxLayout;
|
||||||
hLayout->addWidget(sayLabel);
|
hLayout->addWidget(sayLabel);
|
||||||
hLayout->addWidget(sayEdit);
|
hLayout->addWidget(sayEdit);
|
||||||
|
hLayout->addWidget(pingWidget);
|
||||||
|
|
||||||
QVBoxLayout *verticalLayout = new QVBoxLayout;
|
QVBoxLayout *verticalLayout = new QVBoxLayout;
|
||||||
verticalLayout->addWidget(cardInfo);
|
verticalLayout->addWidget(cardInfo);
|
||||||
|
@ -302,6 +332,7 @@ MainWindow::MainWindow(QTranslator *_translator, QWidget *parent)
|
||||||
|
|
||||||
connect(sayEdit, SIGNAL(returnPressed()), this, SLOT(actSay()));
|
connect(sayEdit, SIGNAL(returnPressed()), this, SLOT(actSay()));
|
||||||
|
|
||||||
|
connect(client, SIGNAL(maxPingTime(int, int)), pingWidget, SLOT(setPercentage(int, int)));
|
||||||
connect(client, SIGNAL(serverTimeout()), this, SLOT(serverTimeout()));
|
connect(client, SIGNAL(serverTimeout()), this, SLOT(serverTimeout()));
|
||||||
connect(client, SIGNAL(statusChanged(ProtocolStatus)), this, SLOT(statusChanged(ProtocolStatus)));
|
connect(client, SIGNAL(statusChanged(ProtocolStatus)), this, SLOT(statusChanged(ProtocolStatus)));
|
||||||
|
|
||||||
|
|
|
@ -43,6 +43,19 @@ class PhasesToolbar;
|
||||||
class GameSelector;
|
class GameSelector;
|
||||||
class ChatWidget;
|
class ChatWidget;
|
||||||
|
|
||||||
|
class PingWidget : public QWidget {
|
||||||
|
Q_OBJECT
|
||||||
|
private:
|
||||||
|
QColor color;
|
||||||
|
protected:
|
||||||
|
void paintEvent(QPaintEvent *event);
|
||||||
|
public:
|
||||||
|
PingWidget(QWidget *parent = 0);
|
||||||
|
QSize sizeHint() const;
|
||||||
|
public slots:
|
||||||
|
void setPercentage(int value, int max);
|
||||||
|
};
|
||||||
|
|
||||||
class MainWindow : public QMainWindow {
|
class MainWindow : public QMainWindow {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
private slots:
|
private slots:
|
||||||
|
@ -72,6 +85,7 @@ private:
|
||||||
QAction *aCloseMostRecentZoneView;
|
QAction *aCloseMostRecentZoneView;
|
||||||
QVBoxLayout *viewLayout;
|
QVBoxLayout *viewLayout;
|
||||||
|
|
||||||
|
PingWidget *pingWidget;
|
||||||
CardInfoWidget *cardInfo;
|
CardInfoWidget *cardInfo;
|
||||||
MessageLogWidget *messageLog;
|
MessageLogWidget *messageLog;
|
||||||
QLabel *sayLabel;
|
QLabel *sayLabel;
|
||||||
|
|
Loading…
Reference in a new issue