From a308f430f694423064ebc86fd0506c8c6fdb3d93 Mon Sep 17 00:00:00 2001 From: Unit 193 Date: Sun, 19 Apr 2015 22:30:21 -0400 Subject: Imported Upstream version 3.0.5 --- .../coreSQLiteStudio/common/blockingsocket.cpp | 97 ++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 SQLiteStudio3/coreSQLiteStudio/common/blockingsocket.cpp (limited to 'SQLiteStudio3/coreSQLiteStudio/common/blockingsocket.cpp') 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 + +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(); +} -- cgit v1.2.3