summaryrefslogtreecommitdiffstats
path: root/client
diff options
context:
space:
mode:
Diffstat (limited to 'client')
-rw-r--r--client/resourceaccess.cpp45
-rw-r--r--client/resourceaccess.h4
2 files changed, 49 insertions, 0 deletions
diff --git a/client/resourceaccess.cpp b/client/resourceaccess.cpp
index 4042219..e73c773 100644
--- a/client/resourceaccess.cpp
+++ b/client/resourceaccess.cpp
@@ -3,6 +3,7 @@
3#include "common/console.h" 3#include "common/console.h"
4#include "common/commands.h" 4#include "common/commands.h"
5#include "common/handshake_generated.h" 5#include "common/handshake_generated.h"
6#include "common/revisionupdate_generated.h"
6 7
7#include <QDebug> 8#include <QDebug>
8#include <QProcess> 9#include <QProcess>
@@ -26,6 +27,9 @@ ResourceAccess::ResourceAccess(const QString &resourceName, QObject *parent)
26 this, &ResourceAccess::disconnected); 27 this, &ResourceAccess::disconnected);
27 connect(m_socket, SIGNAL(error(QLocalSocket::LocalSocketError)), 28 connect(m_socket, SIGNAL(error(QLocalSocket::LocalSocketError)),
28 this, SLOT(connectionError(QLocalSocket::LocalSocketError))); 29 this, SLOT(connectionError(QLocalSocket::LocalSocketError)));
30 connect(m_socket, &QIODevice::readyRead,
31 this, &ResourceAccess::readResourceMessage);
32
29} 33}
30 34
31ResourceAccess::~ResourceAccess() 35ResourceAccess::~ResourceAccess()
@@ -109,3 +113,44 @@ void ResourceAccess::connectionError(QLocalSocket::LocalSocketError error)
109 } 113 }
110} 114}
111 115
116void ResourceAccess::readResourceMessage()
117{
118 if (!m_socket->isValid()) {
119 return;
120 }
121
122 m_partialMessageBuffer += m_socket->readAll();
123
124 // should be scheduled rather than processed all at once
125 while (processMessageBuffer()) {}
126}
127
128bool ResourceAccess::processMessageBuffer()
129{
130 static const int headerSize = (sizeof(int) * 2);
131 Console::main()->log(QString("processing %1").arg(m_partialMessageBuffer.size()));
132 if (m_partialMessageBuffer.size() < headerSize) {
133 return false;
134 }
135
136 const int commandId = *(int*)m_partialMessageBuffer.constData();
137 const int size = *(int*)(m_partialMessageBuffer.constData() + sizeof(int));
138
139 if (size > m_partialMessageBuffer.size() - headerSize) {
140 return false;
141 }
142
143 switch (commandId) {
144 case Commands::RevisionUpdateCommand: {
145 auto buffer = Toynadi::GetRevisionUpdate(m_partialMessageBuffer.constData() + headerSize);
146 Console::main()->log(QString(" Revision updated to: %1").arg(buffer->revision()));
147 emit revisionChanged(buffer->revision());
148 break;
149 }
150 default:
151 break;
152 }
153
154 m_partialMessageBuffer.remove(0, headerSize + size);
155 return m_partialMessageBuffer.size() >= headerSize;
156}
diff --git a/client/resourceaccess.h b/client/resourceaccess.h
index 53b46c5..09df614 100644
--- a/client/resourceaccess.h
+++ b/client/resourceaccess.h
@@ -21,15 +21,19 @@ public Q_SLOTS:
21 21
22Q_SIGNALS: 22Q_SIGNALS:
23 void ready(bool isReady); 23 void ready(bool isReady);
24 void revisionChanged(unsigned long long revision);
24 25
25private Q_SLOTS: 26private Q_SLOTS:
26 void connected(); 27 void connected();
27 void disconnected(); 28 void disconnected();
28 void connectionError(QLocalSocket::LocalSocketError error); 29 void connectionError(QLocalSocket::LocalSocketError error);
30 void readResourceMessage();
31 bool processMessageBuffer();
29 32
30private: 33private:
31 QString m_resourceName; 34 QString m_resourceName;
32 QLocalSocket *m_socket; 35 QLocalSocket *m_socket;
33 QTimer *m_tryOpenTimer; 36 QTimer *m_tryOpenTimer;
34 bool m_startingProcess; 37 bool m_startingProcess;
38 QByteArray m_partialMessageBuffer;
35}; 39};