From 0db27016b0470b989f4fa114ae9dde5aa2a325fa Mon Sep 17 00:00:00 2001 From: Francis Giraldeau Date: Mon, 10 Sep 2018 17:35:38 -0400 Subject: Async socket processing (#49) * Process socket events asynchronously Avoid blocking the event loop using waitForReadyRead(). Instead, process the initialization in two phases. It was necessary to add a map to keep track of the state of the initial message processing Signed-off-by: Francis Giraldeau * Fix undefined variable on Windows The timout variable does not exists in this scope, we can safely remove the Q_UNUSED. Signed-off-by: Francis Giraldeau * Fix validation logic of initial message I modified the logic to a positive value, but the modification was incomplete and caused the initial message to be incorrectly considered as invalid. Signed-off-by: Francis Giraldeau * Use the proper socket when receiving message The socket variable is a class member, but we really want to use nextConnSocket when receiving the message in the lambda. Signed-off-by: Francis Giraldeau --- singleapplication_p.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'singleapplication_p.h') diff --git a/singleapplication_p.h b/singleapplication_p.h index bbe6751..e2c361f 100644 --- a/singleapplication_p.h +++ b/singleapplication_p.h @@ -44,6 +44,14 @@ struct InstancesInfo { quint16 checksum; }; +struct ConnectionInfo { + explicit ConnectionInfo() : + msgLen(0), instanceId(0), stage(0) {} + qint64 msgLen; + quint32 instanceId; + quint8 stage; +}; + class SingleApplicationPrivate : public QObject { Q_OBJECT public: @@ -53,6 +61,11 @@ public: SecondaryInstance = 2, Reconnect = 3 }; + enum ConnectionStage : quint8 { + StageHeader = 0, + StageBody = 1, + StageConnected = 2, + }; Q_DECLARE_PUBLIC(SingleApplication) SingleApplicationPrivate( SingleApplication *q_ptr ); @@ -65,6 +78,8 @@ public: void connectToPrimary(int msecs, ConnectionType connectionType ); quint16 blockChecksum(); qint64 primaryPid(); + void readInitMessageHeader(QLocalSocket *socket); + void readInitMessageBody(QLocalSocket *socket); SingleApplication *q_ptr; QSharedMemory *memory; @@ -73,6 +88,7 @@ public: quint32 instanceNumber; QString blockServerName; SingleApplication::Options options; + QMap connectionMap; public Q_SLOTS: void slotConnectionEstablished(); -- cgit v1.2.1