aboutsummaryrefslogtreecommitdiffstats
path: root/SQLiteStudio3/coreSQLiteStudio/common/blockingsocket.cpp
diff options
context:
space:
mode:
authorLibravatarUnit 193 <unit193@ubuntu.com>2015-04-19 22:30:43 -0400
committerLibravatarUnit 193 <unit193@ubuntu.com>2015-04-19 22:30:43 -0400
commit094918f048c81474b22f9ba2940c96dc4033d753 (patch)
tree2b89c77ad7dc9c55e9ba383f23f9f25b82df358e /SQLiteStudio3/coreSQLiteStudio/common/blockingsocket.cpp
parent640fff60ceecde402131937dddb3458f7a003e9c (diff)
parenta308f430f694423064ebc86fd0506c8c6fdb3d93 (diff)
Merge tag 'upstream/3.0.5'
Upstream version 3.0.5 # gpg: Signature made Sun 19 Apr 2015 10:30:41 PM EDT using RSA key ID EBE9BD91 # gpg: Good signature from "Unit 193 <unit193@gmail.com>" # gpg: aka "Unit 193 <unit193@ninthfloor.org>" # gpg: aka "Unit 193 <unit193@ubuntu.com>" # gpg: aka "Unit 193 <unit193@ninthfloor.com>"
Diffstat (limited to 'SQLiteStudio3/coreSQLiteStudio/common/blockingsocket.cpp')
-rw-r--r--SQLiteStudio3/coreSQLiteStudio/common/blockingsocket.cpp97
1 files changed, 97 insertions, 0 deletions
diff --git a/SQLiteStudio3/coreSQLiteStudio/common/blockingsocket.cpp b/SQLiteStudio3/coreSQLiteStudio/common/blockingsocket.cpp
new file mode 100644
index 0000000..da725c8
--- /dev/null
+++ b/SQLiteStudio3/coreSQLiteStudio/common/blockingsocket.cpp
@@ -0,0 +1,97 @@
+#include "blockingsocket.h"
+#include "common/global.h"
+#include "common/threadwitheventloop.h"
+#include "common/private/blockingsocketprivate.h"
+#include <QMutexLocker>
+
+BlockingSocket::BlockingSocket(QObject* parent) :
+ QObject(parent)
+{
+ socketThread = new ThreadWithEventLoop;
+ socket = new BlockingSocketPrivate;
+ socket->moveToThread(socketThread);
+
+ connect(socketThread, &QThread::finished, socket, &QObject::deleteLater);
+ connect(socketThread, &QThread::finished, socketThread, &QObject::deleteLater);
+ connect(this, SIGNAL(callForSend(QByteArray,bool&)), socket, SLOT(handleSendCall(QByteArray,bool&)), Qt::BlockingQueuedConnection);
+ connect(this, SIGNAL(callForRead(qint64,int,QByteArray&,bool&)), socket, SLOT(handleReadCall(qint64,int,QByteArray&,bool&)), Qt::BlockingQueuedConnection);
+ connect(this, SIGNAL(callForConnect(QString,int,bool&)), socket, SLOT(handleConnectCall(QString,int,bool&)), Qt::BlockingQueuedConnection);
+ connect(this, SIGNAL(callForDisconnect()), socket, SLOT(handleDisconnectCall()), Qt::BlockingQueuedConnection);
+ connect(this, SIGNAL(callForIsConnected(bool&)), socket, SLOT(handleIsConnectedCall(bool&)), Qt::BlockingQueuedConnection);
+ connect(socket, SIGNAL(disconnected()), this, SIGNAL(disconnected()));
+
+ socketThread->start();
+}
+
+BlockingSocket::~BlockingSocket()
+{
+ QMutexLocker lock(&socketOperationMutex);
+ emit callForDisconnect();
+ socketThread->quit();
+}
+
+QAbstractSocket::SocketError BlockingSocket::getErrorCode()
+{
+ QMutexLocker lock(&socketOperationMutex);
+ return socket->getErrorCode();
+}
+
+QString BlockingSocket::getErrorText()
+{
+ QMutexLocker lock(&socketOperationMutex);
+ return socket->getErrorText();
+}
+
+bool BlockingSocket::connectToHost(const QString& host, int port)
+{
+ QMutexLocker lock(&socketOperationMutex);
+ bool res = false;
+ emit callForConnect(host, port, res);
+ return res;
+}
+
+void BlockingSocket::disconnectFromHost()
+{
+ QMutexLocker lock(&socketOperationMutex);
+ emit callForDisconnect();
+}
+
+bool BlockingSocket::isConnected()
+{
+ QMutexLocker lock(&socketOperationMutex);
+ bool res = false;
+ emit callForIsConnected(res);
+ return res;
+}
+
+bool BlockingSocket::send(const QByteArray& bytes)
+{
+ QMutexLocker lock(&socketOperationMutex);
+ bool res = false;
+ emit callForSend(bytes, res);
+ return res;
+}
+
+QByteArray BlockingSocket::read(qint64 count, int timeout, bool* ok)
+{
+ QMutexLocker lock(&socketOperationMutex);
+ bool res = false;
+ QByteArray bytes;
+ emit callForRead(count, timeout, bytes, res);
+ if (ok)
+ *ok = res;
+
+ return bytes;
+}
+
+void BlockingSocket::quit()
+{
+ QMutexLocker lock(&socketOperationMutex);
+ socketThread->quit();
+}
+
+void BlockingSocket::exit()
+{
+ QMutexLocker lock(&socketOperationMutex);
+ socketThread->quit();
+}