From 0c458ec8c96d15f96947f1fe401654a903152a72 Mon Sep 17 00:00:00 2001 From: Nils Jeisecke Date: Mon, 31 May 2021 15:23:18 +0200 Subject: Add simple acknowledge protocol to ensure the server has received all data The server now acknowledges every received message by sending a \r. By waiting for the acknowledgement, clients should no longer terminate too early, causing bytes getting lost in Qt's internal socket handling. Message handling in the server is simplified because we can now rely on the readyRead signal being raised for every frame. This should finally solve #125 and #121. What remains is to correctly handle data sent using SingleApplication::sendMessage. --- singleapplication_p.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'singleapplication_p.h') diff --git a/singleapplication_p.h b/singleapplication_p.h index c49a46d..0777c33 100644 --- a/singleapplication_p.h +++ b/singleapplication_p.h @@ -81,6 +81,8 @@ public: QString primaryUser() const; void readInitMessageHeader(QLocalSocket *socket); void readInitMessageBody(QLocalSocket *socket); + bool writeConfirmedMessage(int msecs, const QByteArray &msg); + void writeAck(QLocalSocket *sock); static void randomSleep(); void addAppData(const QString &data); QStringList appData() const; -- cgit v1.2.1 From 25a7b609822f6e8ab9ef6a7850f60c33a8d996f3 Mon Sep 17 00:00:00 2001 From: Nils Jeisecke Date: Mon, 31 May 2021 16:14:39 +0200 Subject: Ensure data sent via SingleApplication::sendMessage is received completely Whilst the initial "connect" message is framed with a length header, this was missing for the user data. Thus there was no guarantee that the message frame was really received completely on emitting the "receivedMessage" signal. This change splits the previous "StageConnected" state into "StageConnectedHeader" and "StageConnectedBody" and does some refactoring to allow using the same write and read functions as the "init" messages. --- singleapplication_p.h | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'singleapplication_p.h') diff --git a/singleapplication_p.h b/singleapplication_p.h index 0777c33..58507cf 100644 --- a/singleapplication_p.h +++ b/singleapplication_p.h @@ -61,9 +61,10 @@ public: Reconnect = 3 }; enum ConnectionStage : quint8 { - StageHeader = 0, - StageBody = 1, - StageConnected = 2, + StageInitHeader = 0, + StageInitBody = 1, + StageConnectedHeader = 2, + StageConnectedBody = 3, }; Q_DECLARE_PUBLIC(SingleApplication) @@ -79,10 +80,12 @@ public: quint16 blockChecksum() const; qint64 primaryPid() const; QString primaryUser() const; - void readInitMessageHeader(QLocalSocket *socket); + bool isFrameComplete(QLocalSocket *sock); + void readMessageHeader(QLocalSocket *socket, ConnectionStage nextStage); void readInitMessageBody(QLocalSocket *socket); - bool writeConfirmedMessage(int msecs, const QByteArray &msg); void writeAck(QLocalSocket *sock); + bool writeConfirmedFrame(int msecs, const QByteArray &msg); + bool writeConfirmedMessage(int msecs, const QByteArray &msg); static void randomSleep(); void addAppData(const QString &data); QStringList appData() const; -- cgit v1.2.1