Skip to content

Commit

Permalink
Cleanup: replace obsoleted QTime::elapsed() with QElapsedTimer::elaps…
Browse files Browse the repository at this point in the history
…ed() (#4400)

The latter has been present since Qt 4.7 and even on some Windows OSes
where it may be a 32-Bit value that can overflow after nearly 50 days
when the lower quality TickCounter clock is used as a fallback instead of
the PerformanceCounter one this is a better bet than the QTime based one
that will overflow (wrap) after 24 hours and will be affected by
Summer-Time changes and user/system adjustment of the system clock.

Also:
* rename three elements related to timing the network latency, their
  original names were somewhat ambiguous:
  * (QLineEdit*) TConsole::networkLatency
                                  ==> TConsole::mpLineEdit_networkLatency
  * (double) cTelnet::networkLatency
                                          ==> cTelnet::networkLatencyTime
  * (QTime) cTelnet::networkLatencyTime
                         ==> (QElapsedTimer) cTelnet::networkLatencyTimer
* rename some other elements in a comparable way:
  * (QTime) cTelnet::timeOffset
                        ==> (QElapsedTimer) cTelnet::mRecordingChunkTimer
  * (QTime) cTelnet::mConnectionTime
                            ==> (QElapsedTimer) cTelnet::mConnectionTimer
  * (int) cTelnet::lastTimeOffset
                        ==> (int) cTelnet::mRecordLastChunkMSecTimeOffset
* simplify a C string array access - there is no need to use the address
operator AND an index when referring to the start of a C array, i.e. for
char buffer[datalen]: '&buffer[0]' is simply 'buffer' !
* the display of the network latency (if available) and the system
processing time is a UI feature but it was not being put through the
translation system, this commit now allows for that to happen.

This should remove 12 warnings (on my Linux Qt 5.14.2 system).

Signed-off-by: Stephen Lyons <slysven@virginmedia.com>
  • Loading branch information
SlySven committed Nov 26, 2020
1 parent a447607 commit 3b33bfb
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 47 deletions.
6 changes: 3 additions & 3 deletions src/LuaInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -781,8 +781,8 @@ void LuaInterface::iterateTable(lua_State* L, int index, TVar* tVar, bool hide)
void LuaInterface::getVars(bool hide)
{
//returns the base item
QTime t;
t.start();
// QElapsedTimer t;
// t.start();
L = interpreter->pGlobalLua;
lua_pushnil(L);
depth = 0;
Expand All @@ -798,5 +798,5 @@ void LuaInterface::getVars(bool hide)
varUnit->setBase(g);
varUnit->addVariable(g);
iterateTable(L, LUA_GLOBALSINDEX, g, hide);
//FIXME: possible to keep and report? qDebug()<<"took"<<t.elapsed()<<"to get variables in";
// FIXME: possible to keep and report? qDebug()<<"took"<<t.elapsed()<<"to get variables in";
}
50 changes: 30 additions & 20 deletions src/TConsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ TConsole::TConsole(Host* pH, ConsoleType type, QWidget* parent)
, mSystemMessageFgColor(QColor(Qt::red))
, mTriggerEngineMode(false)
, mWrapAt(100)
, networkLatency(new QLineEdit)
, mpLineEdit_networkLatency(new QLineEdit)
, mProfileName(mpHost ? mpHost->getName() : QStringLiteral("debug console"))
, mIsPromptLine(false)
, mUserAgreedToCloseConsole(false)
Expand Down Expand Up @@ -403,37 +403,37 @@ TConsole::TConsole(Host* pH, ConsoleType type, QWidget* parent)
logButton->setIcon(logIcon);
connect(logButton, &QAbstractButton::pressed, this, &TConsole::slot_toggleLogging);

networkLatency->setReadOnly(true);
networkLatency->setSizePolicy(sizePolicy4);
networkLatency->setFocusPolicy(Qt::NoFocus);
networkLatency->setToolTip(QStringLiteral("<html><head/><body><p>%1</p></body></html>").arg(
mpLineEdit_networkLatency->setReadOnly(true);
mpLineEdit_networkLatency->setSizePolicy(sizePolicy4);
mpLineEdit_networkLatency->setFocusPolicy(Qt::NoFocus);
mpLineEdit_networkLatency->setToolTip(QStringLiteral("<html><head/><body><p>%1</p></body></html>").arg(
tr("<i>N:</i> is the latency of the game server and network (aka ping, in seconds), <br>"
"<i>S:</i> is the system processing time - how long your triggers took to process the last line(s).")));
networkLatency->setMaximumSize(120, 30);
networkLatency->setMinimumSize(120, 30);
networkLatency->setAutoFillBackground(true);
networkLatency->setContentsMargins(0, 0, 0, 0);
mpLineEdit_networkLatency->setMaximumSize(120, 30);
mpLineEdit_networkLatency->setMinimumSize(120, 30);
mpLineEdit_networkLatency->setAutoFillBackground(true);
mpLineEdit_networkLatency->setContentsMargins(0, 0, 0, 0);
QPalette basePalette;
basePalette.setColor(QPalette::Text, QColor(Qt::black));
basePalette.setColor(QPalette::Base, QColor(Qt::white));
networkLatency->setPalette(basePalette);
networkLatency->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
mpLineEdit_networkLatency->setPalette(basePalette);
mpLineEdit_networkLatency->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);

QFont latencyFont = QFont("Bitstream Vera Sans Mono", 10, QFont::Normal);
int width;
int maxWidth = 120;
width = QFontMetrics(latencyFont).boundingRect(QString("N:0.000 S:0.000")).width();
if (width < maxWidth) {
networkLatency->setFont(latencyFont);
mpLineEdit_networkLatency->setFont(latencyFont);
} else {
QFont latencyFont2 = QFont("Bitstream Vera Sans Mono", 9, QFont::Normal);
width = QFontMetrics(latencyFont2).boundingRect(QString("N:0.000 S:0.000")).width();
if (width < maxWidth) {
networkLatency->setFont(latencyFont2);
mpLineEdit_networkLatency->setFont(latencyFont2);
} else {
QFont latencyFont3 = QFont("Bitstream Vera Sans Mono", 8, QFont::Normal);
width = QFontMetrics(latencyFont3).boundingRect(QString("N:0.000 S:0.000")).width();
networkLatency->setFont(latencyFont3);
mpLineEdit_networkLatency->setFont(latencyFont3);
}
}

Expand Down Expand Up @@ -497,10 +497,10 @@ TConsole::TConsole(Host* pH, ConsoleType type, QWidget* parent)
layoutButtonLayer->addWidget(replayButton, 0, 8);
layoutButtonLayer->addWidget(logButton, 0, 9);
layoutButtonLayer->addWidget(emergencyStop, 0, 10);
layoutButtonLayer->addWidget(networkLatency, 0, 11);
layoutButtonLayer->addWidget(mpLineEdit_networkLatency, 0, 11);
layoutLayer2->setContentsMargins(0, 0, 0, 0);
layout->addWidget(layer);
networkLatency->setFrame(false);
mpLineEdit_networkLatency->setFrame(false);
//QPalette whitePalette;
//whitePalette.setColor( QPalette::Window, baseColor);//,255) );
layerCommandLine->setPalette(basePalette);
Expand Down Expand Up @@ -1053,7 +1053,7 @@ void TConsole::setConsoleBgColor(int r, int g, int b, int a)

void TConsole::printOnDisplay(std::string& incomingSocketData, const bool isFromServer)
{
mProcessingTime.restart();
mProcessingTimer.restart();
mTriggerEngineMode = true;
buffer.translateToPlainText(incomingSocketData, isFromServer);
mTriggerEngineMode = false;
Expand All @@ -1066,11 +1066,21 @@ void TConsole::printOnDisplay(std::string& incomingSocketData, const bool isFrom
mpHost->mLuaInterpreter.signalMXPEvent(event.name, event.attrs, event.actions);
}

double processT = mProcessingTime.elapsed();
double processT = mProcessingTimer.elapsed() / 1000.0;
if (mpHost->mTelnet.mGA_Driver) {
networkLatency->setText(QString("N:%1 S:%2").arg(mpHost->mTelnet.networkLatency, 0, 'f', 3).arg(processT / 1000, 0, 'f', 3));
mpLineEdit_networkLatency->setText(tr("N:%1 S:%2",
// intentional comment to separate arguments
"The first argument 'N' represents the 'N'etwork latency; the second 'S' the "
"'S'ystem (processing) time")
.arg(mpHost->mTelnet.networkLatencyTime, 0, 'f', 3)
.arg(processT, 0, 'f', 3));
} else {
networkLatency->setText(QString("<no GA> S:%1").arg(processT / 1000, 0, 'f', 3));
mpLineEdit_networkLatency->setText(tr("<no GA> S:%1",
// intentional comment to separate arguments
"The argument 'S' represents the 'S'ystem (processing) time, in this situation "
"the Game Server is not sending \"GoAhead\" signals so we cannot deduce the "
"network latency...")
.arg(processT, 0, 'f', 3));
}
// Modify the tab text if this is not the currently active host - this
// method is only used on the "main" console so no need to filter depending
Expand Down
5 changes: 3 additions & 2 deletions src/TConsole.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

#include "pre_guard.h"
#include <QDataStream>
#include <QElapsedTimer>
#include <QHBoxLayout>
#include <QFile>
#include <QLabel>
Expand Down Expand Up @@ -244,7 +245,7 @@ class TConsole : public QWidget
QScrollBar* mpHScrollBar;


QTime mProcessingTime;
QElapsedTimer mProcessingTimer;
bool mRecordReplay;
QFile mReplayFile;
QDataStream mReplayStream;
Expand All @@ -257,7 +258,7 @@ class TConsole : public QWidget
QPoint mUserCursor;
bool mWindowIsHidden;
int mWrapAt;
QLineEdit* networkLatency;
QLineEdit* mpLineEdit_networkLatency;
QPoint P_begin;
QPoint P_end;
QString mProfileName;
Expand Down
4 changes: 1 addition & 3 deletions src/TLuaInterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6843,9 +6843,7 @@ int TLuaInterpreter::getButtonState(lua_State* L)
int TLuaInterpreter::getNetworkLatency(lua_State* L)
{
Host& host = getHostFromLua(L);
double number;
number = host.mTelnet.networkLatency;
lua_pushnumber(L, number);
lua_pushnumber(L, host.mTelnet.networkLatencyTime);
return 1;
}

Expand Down
33 changes: 19 additions & 14 deletions src/ctelnet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ QFile replayFile;

cTelnet::cTelnet(Host* pH, const QString& profileName)
: mResponseProcessed(true)
, networkLatency()
, networkLatencyTime(0.0)
, mAlertOnNewData(true)
, mGA_Driver(false)
, mFORCE_GA_OFF(false)
Expand All @@ -93,7 +93,7 @@ cTelnet::cTelnet(Host* pH, const QString& profileName)
, mMCCP_version_1(false)
, mMCCP_version_2(false)
, mIsTimerPosting()
, lastTimeOffset()
, mRecordLastChunkMSecTimeOffset()
, enableCHARSET(false)
, enableATCP(false)
, enableGMCP(false)
Expand Down Expand Up @@ -479,7 +479,7 @@ void cTelnet::handle_socket_signal_connected()
QString func = "onConnect";
QString nothing = "";
mpHost->mLuaInterpreter.call(func, nothing);
mConnectionTime.start();
mConnectionTimer.start();
mTimerLogin->start(2000);
mTimerPass->start(3000);

Expand Down Expand Up @@ -508,7 +508,13 @@ void cTelnet::handle_socket_signal_disconnected()
mpHost->raiseEvent(event);

QTime timeDiff(0, 0, 0, 0);
msg = QString(tr("[ INFO ] - Connection time: %1\n ")).arg(timeDiff.addMSecs(mConnectionTime.elapsed()).toString("hh:mm:ss.zzz"));
msg = tr("[ INFO ] - Connection time: %1\n ")
.arg(timeDiff.addMSecs(mConnectionTimer.elapsed())
.toString(tr("hh:mm:ss.zzz",
// Intentional comment to separate arguments
"This is the format to be used to show the profile connection time, it follows "
"the rules of the \"QDateTime::toString(...)\" function and may need "
"modification for some locales, e.g. France, Spain.")));
mNeedDecompression = false;
reset();

Expand Down Expand Up @@ -732,7 +738,7 @@ bool cTelnet::socketOutRaw(std::string& data)
++mCommands;
if (mCommands == 1) {
mWaitingForResponse = true;
networkLatencyTime.restart();
networkLatencyTimer.restart();
}
}

Expand Down Expand Up @@ -2539,8 +2545,8 @@ int cTelnet::decompressBuffer(char*& in_buffer, int& length, char* out_buffer)

void cTelnet::recordReplay()
{
lastTimeOffset = 0;
timeOffset.start();
mRecordLastChunkMSecTimeOffset = 0;
mRecordingChunkTimer.start();
}

bool cTelnet::loadReplay(const QString& name, QString* pErrMsg)
Expand Down Expand Up @@ -2686,7 +2692,7 @@ void cTelnet::slot_processReplayChunk()
mGA_Driver = true;
if (mCommands > 0) {
mCommands--;
if (networkLatencyTime.elapsed() > 2000) {
if (networkLatencyTimer.elapsed() > 2000) {
mCommands = 0;
}
}
Expand All @@ -2711,8 +2717,7 @@ void cTelnet::slot_processReplayChunk()
void cTelnet::handle_socket_signal_readyRead()
{
if (mWaitingForResponse) {
double time = networkLatencyTime.elapsed();
networkLatency = time / 1000;
networkLatencyTime = networkLatencyTimer.elapsed() / 1000.0;
mWaitingForResponse = false;
}

Expand Down Expand Up @@ -2746,9 +2751,9 @@ void cTelnet::processSocketData(char* in_buffer, int amount)
}
buffer[datalen] = '\0';
if (mpHost->mpConsole->mRecordReplay) {
mpHost->mpConsole->mReplayStream << timeOffset.elapsed() - lastTimeOffset;
mpHost->mpConsole->mReplayStream << mRecordingChunkTimer.elapsed() - mRecordLastChunkMSecTimeOffset;
mpHost->mpConsole->mReplayStream << datalen;
mpHost->mpConsole->mReplayStream.writeRawData(&buffer[0], datalen);
mpHost->mpConsole->mReplayStream.writeRawData(buffer, datalen);
}

recvdGA = false;
Expand Down Expand Up @@ -2866,7 +2871,7 @@ void cTelnet::processSocketData(char* in_buffer, int amount)
mGA_Driver = true;
if (mCommands > 0) {
mCommands--;
if (networkLatencyTime.elapsed() > 2000) {
if (networkLatencyTimer.elapsed() > 2000) {
mCommands = 0;
}
}
Expand All @@ -2885,7 +2890,7 @@ void cTelnet::processSocketData(char* in_buffer, int amount)
gotRest(cleandata);
}
mpHost->mpConsole->finalize();
lastTimeOffset = timeOffset.elapsed();
mRecordLastChunkMSecTimeOffset = mRecordingChunkTimer.elapsed();
}

void cTelnet::raiseProtocolEvent(const QString& name, const QString& protocol)
Expand Down
11 changes: 6 additions & 5 deletions src/ctelnet.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@


#include "pre_guard.h"
#include <QElapsedTimer>
#include <QHostAddress>
#include <QHostInfo>
#include <QPointer>
Expand Down Expand Up @@ -190,8 +191,8 @@ class cTelnet : public QObject

QMap<int, bool> supportedTelnetOptions;
bool mResponseProcessed;
double networkLatency;
QTime networkLatencyTime;
double networkLatencyTime;
QElapsedTimer networkLatencyTimer;
bool mAlertOnNewData;
bool mGA_Driver;
bool mFORCE_GA_OFF;
Expand Down Expand Up @@ -291,9 +292,9 @@ public slots:
bool mIsTimerPosting;
QTimer* mTimerLogin;
QTimer* mTimerPass;
QTime timeOffset;
QTime mConnectionTime;
int lastTimeOffset;
QElapsedTimer mRecordingChunkTimer;
QElapsedTimer mConnectionTimer;
int mRecordLastChunkMSecTimeOffset;
bool enableCHARSET;
bool enableATCP;
bool enableGMCP;
Expand Down

0 comments on commit 3b33bfb

Please sign in to comment.