Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added disconnected signals #1

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,14 @@ ADD_SUBDIRECTORY(example/client)
ADD_SUBDIRECTORY(example/server)
ADD_SUBDIRECTORY(test)

INSTALL(TARGETS ${lib_target} LIBRARY DESTINATION lib)
#INSTALL(TARGETS ${lib_target} LIBRARY DESTINATION lib)

if(WIN32)
INSTALL(TARGETS ${lib_target}
RUNTIME DESTINATION lib
)
else()
INSTALL(TARGETS ${lib_target}
LIBRARY DESTINATION lib
)
endif()
7 changes: 6 additions & 1 deletion include/CuteIPCInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CuteIPCInterface : public QObject
bool connectToServer(const QHostAddress& host, quint16 port);

void disconnectFromServer();

bool isConnected();
bool remoteConnect(const char* signal, QObject* object, const char* method);
bool remoteSlotConnect(QObject* localObject, const char* signal, const char* remoteSlot);

Expand Down Expand Up @@ -52,6 +52,11 @@ class CuteIPCInterface : public QObject

QString lastError() const;

signals:
void connected();
void disconnected();


protected:
CuteIPCInterfacePrivate* const d_ptr;
CuteIPCInterface(CuteIPCInterfacePrivate& dd, QObject* parent);
Expand Down
5 changes: 3 additions & 2 deletions include/CuteIPCService.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// Qt
#include <QObject>
#include <QtNetwork/QHostAddress>
#include <QLocalServer>

// Local
class CuteIPCServicePrivate;
Expand All @@ -16,8 +17,8 @@ class CuteIPCService : public QObject
explicit CuteIPCService(QObject* parent = 0);
~CuteIPCService();

bool listen(const QString& name = QString(), QObject* subject = 0);
bool listen(QObject* subject);
bool listen(const QString& name = QString(), QObject* subject = 0, QLocalServer::SocketOptions options=QLocalServer::NoOptions);
bool listen(QObject* subject, QLocalServer::SocketOptions options=QLocalServer::NoOptions);
QString serverName() const;

bool listenTcp(const QHostAddress& address = QHostAddress::Any, quint16 port = 0, QObject* subject = 0);
Expand Down
29 changes: 21 additions & 8 deletions src/CuteIPCInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ bool CuteIPCInterfacePrivate::sendSynchronousRequest(const QByteArray& request,
if (!connected)
{
socket.disconnectFromServer();
QString error("CuteIPC: Не удалось подключиться к серверу при вызове синхронного метода");
QString error("CuteIPC: Could not connect to the server when the synchronous method was called");
qWarning() << error;
_q_setLastError(error);
return false;
Expand All @@ -135,6 +135,8 @@ bool CuteIPCInterfacePrivate::sendSynchronousRequest(const QByteArray& request,

QEventLoop loop;
QObject::connect(&connection, SIGNAL(callFinished()), &loop, SLOT(quit()));

QObject::connect(&connection, SIGNAL(socketDisconnected()), q_ptr, SIGNAL(disconnected()));
QObject::connect(&connection, SIGNAL(socketDisconnected()), &loop, SLOT(quit()));
connection.sendCallRequest(request);
loop.exec();
Expand All @@ -149,7 +151,7 @@ bool CuteIPCInterfacePrivate::sendSynchronousRequest(const QByteArray& request,
if (!connected)
{
socket.disconnectFromHost();
QString error("CuteIPC: Не удалось подключиться к серверу при вызове синхронного метода");
QString error("CuteIPC: Could not connect to the server when the synchronous method was called");
qWarning() << error;
_q_setLastError(error);
return false;
Expand Down Expand Up @@ -296,6 +298,7 @@ CuteIPCInterface::CuteIPCInterface(QObject* parent)
Q_D(CuteIPCInterface);
d->q_ptr = this;

connect(d->m_worker, SIGNAL(disconnected()), SIGNAL(disconnected()));
connect(d->m_worker, SIGNAL(setLastError(QString)), SLOT(_q_setLastError(QString)));
connect(d->m_worker, SIGNAL(invokeRemoteSignal(QString, CuteIPCMessage::Arguments)),
SLOT(_q_invokeRemoteSignal(QString, CuteIPCMessage::Arguments)));
Expand Down Expand Up @@ -325,6 +328,11 @@ CuteIPCInterface::CuteIPCInterface(CuteIPCInterfacePrivate& dd, QObject* parent)
}


bool CuteIPCInterface::isConnected() {
Q_D(CuteIPCInterface);
return d->m_worker->isConnected();
}

/*!
Destroyes the object.
*/
Expand All @@ -344,16 +352,18 @@ CuteIPCInterface::~CuteIPCInterface()
bool CuteIPCInterface::connectToServer(const QString& name)
{
Q_D(CuteIPCInterface);
bool connected;
bool isConnected;

QEventLoop loop;
connect(d->m_worker, SIGNAL(connectToServerFinished()), &loop, SLOT(quit()));
QMetaObject::invokeMethod(d->m_worker, "connectToServer", Q_ARG(QString, name), Q_ARG(void*, &connected));
QMetaObject::invokeMethod(d->m_worker, "connectToServer", Q_ARG(QString, name), Q_ARG(void*, &isConnected));
loop.exec();

d->m_localServer = name;
if (isConnected)
emit connected();

return connected;
return isConnected;
}

/*!
Expand All @@ -367,15 +377,18 @@ bool CuteIPCInterface::connectToServer(const QString& name)
bool CuteIPCInterface::connectToServer(const QHostAddress& host, quint16 port)
{
Q_D(CuteIPCInterface);
bool connected;
bool isConnected;

QEventLoop loop;
connect(d->m_worker, SIGNAL(connectToServerFinished()), &loop, SLOT(quit()));
QMetaObject::invokeMethod(d->m_worker, "connectToTcpServer", Q_ARG(QHostAddress, host), Q_ARG(quint16, port), Q_ARG(void*, &connected));
QMetaObject::invokeMethod(d->m_worker, "connectToTcpServer", Q_ARG(QHostAddress, host), Q_ARG(quint16, port), Q_ARG(void*, &isConnected));
loop.exec();

d->m_tcpAddress = qMakePair(host, port);
return connected;
if (isConnected)
emit connected();

return isConnected;
}


Expand Down
5 changes: 4 additions & 1 deletion src/CuteIPCInterfaceConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ CuteIPCInterfaceConnection::CuteIPCInterfaceConnection(QTcpSocket* socket, QObje
connect(socket, SIGNAL(readyRead()), SLOT(readyRead()));
}

bool CuteIPCInterfaceConnection::isConnected() {
return m_socket && m_socket->isOpen();
}

void CuteIPCInterfaceConnection::sendCallRequest(const QByteArray& request)
{
Expand Down Expand Up @@ -108,7 +111,7 @@ bool CuteIPCInterfaceConnection::readMessageFromSocket()
}
case CuteIPCMessage::AboutToCloseSocket:
{
DEBUG << "Сервер сообщает о закрытии соединения";
DEBUG << "The server reports that the connection is closed";
CuteIPCMessage message = CuteIPCMarshaller::demarshallMessage(m_block);
CuteIPCMarshaller::freeArguments(message.arguments());
m_lastCallSuccessful = false;
Expand Down
1 change: 1 addition & 0 deletions src/CuteIPCInterfaceConnection_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class CuteIPCInterfaceConnection : public QObject
void sendCallRequest(const QByteArray& request);
void setReturnedObject(QGenericReturnArgument returnedObject);
bool lastCallSuccessful() const;
bool isConnected();

signals:
void callFinished();
Expand Down
7 changes: 7 additions & 0 deletions src/CuteIPCInterfaceWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ void CuteIPCInterfaceWorker::connectToServer(const QString& name, void* successf

QLocalSocket* socket = new QLocalSocket;
socket->connectToServer(name);

bool connected = socket->waitForConnected(5000);
if (!connected)
{
Expand All @@ -42,6 +43,7 @@ void CuteIPCInterfaceWorker::connectToServer(const QString& name, void* successf
this, SIGNAL(invokeRemoteSignal(QString, CuteIPCMessage::Arguments)));
connect(m_connection, SIGNAL(errorOccured(QString)), this, SIGNAL(setLastError(QString)));

connect(m_connection, SIGNAL(socketDisconnected()), SIGNAL(disconnected()));
connect(m_connection, SIGNAL(socketDisconnected()), m_connection, SLOT(deleteLater()));
connect(m_connection, SIGNAL(socketDisconnected()), socket, SLOT(deleteLater()));

Expand Down Expand Up @@ -88,6 +90,7 @@ void CuteIPCInterfaceWorker::connectToTcpServer(const QHostAddress& host, const
this, SIGNAL(invokeRemoteSignal(QString, CuteIPCMessage::Arguments)));
connect(m_connection, SIGNAL(errorOccured(QString)), this, SIGNAL(setLastError(QString)));

connect(m_connection, SIGNAL(socketDisconnected()), SIGNAL(disconnected()));
connect(m_connection, SIGNAL(socketDisconnected()), m_connection, SLOT(deleteLater()));
connect(m_connection, SIGNAL(socketDisconnected()), socket, SLOT(deleteLater()));

Expand Down Expand Up @@ -131,6 +134,10 @@ void CuteIPCInterfaceWorker::disconnectFromServer()
}


bool CuteIPCInterfaceWorker::isConnected() {
return m_connection->isConnected();
}

void CuteIPCInterfaceWorker::sendCallRequest(const QByteArray& request)
{
if (!m_connection)
Expand Down
2 changes: 2 additions & 0 deletions src/CuteIPCInterfaceWorker.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ class CuteIPCInterfaceWorker : public QObject
explicit CuteIPCInterfaceWorker(QObject* parent = 0);
~CuteIPCInterfaceWorker();

bool isConnected();
signals:
void setLastError(const QString& error);
void disconnected();

// slot finish signals
void connectToServerFinished();
Expand Down
20 changes: 12 additions & 8 deletions src/CuteIPCService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "CuteIPCSignalHandler_p.h"

// Qt
#include <QCoreApplication>
#include <QLocalServer>
#include <QLocalSocket>
#include <QTime>
Expand Down Expand Up @@ -62,11 +63,13 @@ CuteIPCServicePrivate::~CuteIPCServicePrivate()
}

// Clients reaction timeout
QEventLoop loop;
QTimer timer;
QObject::connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
timer.start(CLIENTS_DISCONNECT_TIMEOUT);
loop.exec();
if ( QCoreApplication::instance() ) {
QEventLoop loop;
QTimer timer;
QObject::connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
timer.start(CLIENTS_DISCONNECT_TIMEOUT);
loop.exec();
}
}


Expand Down Expand Up @@ -303,7 +306,7 @@ QHostAddress CuteIPCService::tcpAddress() const
object itself. Previous subject will be replaced.
Returns true on success, otherwise false.
*/
bool CuteIPCService::listen(const QString& serverName, QObject* subject)
bool CuteIPCService::listen(const QString& serverName, QObject* subject, QLocalServer::SocketOptions options)
{
Q_D(CuteIPCService);
QString name = serverName;
Expand All @@ -312,6 +315,7 @@ bool CuteIPCService::listen(const QString& serverName, QObject* subject)

DEBUG << "Trying to listen" << name;
d->registerLocalServer();
d->m_localServer->setSocketOptions(options);
bool ok = d->m_localServer->listen(name);

if (!ok)
Expand All @@ -335,9 +339,9 @@ bool CuteIPCService::listen(const QString& serverName, QObject* subject)
/*!
* This is an overloaded member function.
*/
bool CuteIPCService::listen(QObject* subject)
bool CuteIPCService::listen(QObject* subject, QLocalServer::SocketOptions options)
{
return listen(QString(), subject);
return listen(QString(), subject, options);
}


Expand Down