[rkward/frameworks] rkward/rbackend: Unify the "wait for security token" code a bit, also hoping it will fix the sporadic problems
Thomas Friedrichsmeier
null at kde.org
Sat May 27 07:21:32 UTC 2017
Git commit 72af88de1bf0dda52f781275c84928c9368c29bc by Thomas Friedrichsmeier.
Committed on 27/05/2017 at 07:20.
Pushed by tfry into branch 'frameworks'.
Unify the "wait for security token" code a bit, also hoping it will fix the sporadic problems
when connecting on Windows.
M +14 -16 rkward/rbackend/rkfrontendtransmitter.cpp
M +4 -0 rkward/rbackend/rkfrontendtransmitter.h
M +2 -3 rkward/rbackend/rkwarddevice/rkgraphicsdevice_frontendtransmitter.cpp
https://commits.kde.org/rkward/72af88de1bf0dda52f781275c84928c9368c29bc
diff --git a/rkward/rbackend/rkfrontendtransmitter.cpp b/rkward/rbackend/rkfrontendtransmitter.cpp
index 3ebdfc23..ffde284f 100644
--- a/rkward/rbackend/rkfrontendtransmitter.cpp
+++ b/rkward/rbackend/rkfrontendtransmitter.cpp
@@ -128,14 +128,7 @@ void RKFrontendTransmitter::run () {
handleTransmissionError (i18n ("The backend executable could not be started. Error message was: %1", backend->errorString ()));
}
- // fetch security token
- {
- // NOTE: On Qt5+Windows, readyReady may actually come in char by char, so calling waitForReadyRead() does not guaranteed we will
- // see the full line, at all. But also, of course, we want to put some cap on trying. Using a time threshold for this.
- QTime time;
- time.start ();
- while ((!backend->canReadLine ()) && (time.elapsed () < 3000)) backend->waitForReadyRead ();
- }
+ waitForCanReadLine (backend, 3000);
token = QString::fromLocal8Bit (backend->readLine ()).trimmed ();
backend->closeReadChannel (QProcess::StandardError);
backend->closeReadChannel (QProcess::StandardOutput);
@@ -150,6 +143,16 @@ void RKFrontendTransmitter::run () {
}
}
+void RKFrontendTransmitter::waitForCanReadLine (QIODevice* con, int msecs) {
+ RK_TRACE (RBACKEND);
+
+ // NOTE: On Qt5+Windows, readyReady may actually come in char by char, so calling waitForReadyRead() does not guarantee we will
+ // see the full line, at all. But also, of course, we want to put some cap on trying. Using a time threshold for this.
+ QTime time;
+ time.start ();
+ while ((!con->canReadLine ()) && (time.elapsed () < msecs)) con->waitForReadyRead (500);
+}
+
void RKFrontendTransmitter::connectAndEnterLoop () {
RK_TRACE (RBACKEND);
RK_ASSERT (server->hasPendingConnections ());
@@ -158,16 +161,11 @@ void RKFrontendTransmitter::connectAndEnterLoop () {
server->close ();
// handshake
- if (!con->canReadLine ()) con->waitForReadyRead (1000);
- QString token_c = QString::fromLocal8Bit (con->readLine ());
- token_c = token_c.trimmed ();
+ waitForCanReadLine (con, 1000);
+ QString token_c = QString::fromLocal8Bit (con->readLine ()). trimmed ();
if (token_c != token) handleTransmissionError (i18n ("Error during handshake with backend process. Expected token '%1', received token '%2'", token, token_c));
- if (!con->canReadLine ()) con->waitForReadyRead (1000);
+ waitForCanReadLine (con, 1000);
QString version_c = QString::fromLocal8Bit (con->readLine ().trimmed ());
- if (version_c.isEmpty ()) { // may happend on Windows, due to \r\n lines ends
- if (!con->canReadLine ()) con->waitForReadyRead (1000);
- version_c = QString::fromLocal8Bit (con->readLine ().trimmed ());
- }
if (version_c != RKWARD_VERSION) handleTransmissionError (i18n ("Version mismatch during handshake with backend process. Frontend is version '%1' while backend is '%2'.\nPlease fix your installation.", QString (RKWARD_VERSION), version_c));
setConnection (con);
diff --git a/rkward/rbackend/rkfrontendtransmitter.h b/rkward/rbackend/rkfrontendtransmitter.h
index 6b4b263b..1e1a95af 100644
--- a/rkward/rbackend/rkfrontendtransmitter.h
+++ b/rkward/rbackend/rkfrontendtransmitter.h
@@ -21,6 +21,7 @@
#include "rktransmitter.h"
class QProcess;
+class QIODevice;
class QLocalServer;
class RKGraphicsDeviceFrontendTransmitter;
@@ -38,6 +39,9 @@ public:
};
void writeRequest (RBackendRequest *request) override;
void requestReceived (RBackendRequest *request) override;
+ /** Simple convenience function similar to QIODevice::waitForReadyRead(), but waiting for a full line to be available.
+ In particular on Windows, we often receive _less_ than a full line per chunk. */
+ static void waitForCanReadLine (QIODevice *con, int msecs);
private slots:
void connectAndEnterLoop ();
void backendExit (int exitcode);
diff --git a/rkward/rbackend/rkwarddevice/rkgraphicsdevice_frontendtransmitter.cpp b/rkward/rbackend/rkwarddevice/rkgraphicsdevice_frontendtransmitter.cpp
index f5c1f477..3de7c7b3 100644
--- a/rkward/rbackend/rkwarddevice/rkgraphicsdevice_frontendtransmitter.cpp
+++ b/rkward/rbackend/rkwarddevice/rkgraphicsdevice_frontendtransmitter.cpp
@@ -71,9 +71,8 @@ void RKGraphicsDeviceFrontendTransmitter::newConnection () {
// handshake
QString token = RKFrontendTransmitter::instance ()->connectionToken ();
- if (!con->canReadLine ()) con->waitForReadyRead (1000);
- QString token_c = QString::fromLocal8Bit (con->readLine ());
- token_c.chop (1);
+ RKFrontendTransmitter::waitForCanReadLine (con, 2000);
+ QString token_c = QString::fromLocal8Bit (con->readLine ()).trimmed ();
if (token_c != token) {
KMessageBox::detailedError (0, QString ("<p>%1</p>").arg (i18n ("There has been an error while trying to connect the on-screen graphics backend. This means, on-screen graphics using the RKWard device will not work in this session.")), i18n ("Expected connection token %1, but read connection token %2", token, token_c), i18n ("Error while connection graphics backend"));
con->close ();
More information about the rkward-tracker
mailing list