From 4f54ad85a7fe02a4fd37123d39736a17bf037b2c Mon Sep 17 00:00:00 2001 From: liranbg Date: Fri, 11 Jul 2014 17:01:25 +0300 Subject: [PATCH] connection fix --- main/mainscreen.ui | 2 +- resources/connectionstatus.qrc | 16 +- src/connection/jcesslclient.cpp | 264 ++++++++++++++++---------------- src/connection/jcesslclient.h | 78 +++++----- 4 files changed, 180 insertions(+), 180 deletions(-) diff --git a/main/mainscreen.ui b/main/mainscreen.ui index 46b1ff9..8e3bcf1 100644 --- a/main/mainscreen.ui +++ b/main/mainscreen.ui @@ -21,7 +21,7 @@ - :/icons/icon.png:/icons/icon.png + :/icons/icon.ico:/icons/icon.ico #centralWidget diff --git a/resources/connectionstatus.qrc b/resources/connectionstatus.qrc index 10dd52c..f2b3bdf 100644 --- a/resources/connectionstatus.qrc +++ b/resources/connectionstatus.qrc @@ -1,8 +1,8 @@ - - - blueStatusIcon.png - greenStatusIcon.png - redStatusIcon.png - icon.png - - + + + blueStatusIcon.png + greenStatusIcon.png + redStatusIcon.png + icon.ico + + diff --git a/src/connection/jcesslclient.cpp b/src/connection/jcesslclient.cpp index 65ddb46..b1f2ca9 100644 --- a/src/connection/jcesslclient.cpp +++ b/src/connection/jcesslclient.cpp @@ -1,132 +1,132 @@ -#include "jcesslclient.h" - -jceSSLClient::jceSSLClient() : flag(false), packet("") -{ - connect(this,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(checkErrors(QAbstractSocket::SocketError))); - connect(this,SIGNAL(connected()),this,SLOT(setConnected())); - connect(this,SIGNAL(encrypted()),this,SLOT(setEncrypted())); - connect(this,SIGNAL(disconnected()),this,SLOT(setDisconnected())); - -} - -bool jceSSLClient::makeConnect(std::string server, int port) -{ - QEventLoop loop; - QObject::connect(this, SIGNAL(encrypted()), &loop, SLOT(quit())); - if (isConnected()) - abort(); - if (isOpen()) - abort(); - - connectToHostEncrypted(server.c_str(), port); - loop.exec(); - return isConnected(); - -} -bool jceSSLClient::makeDiconnect() -{ - - abort(); - if (state() == QAbstractSocket::SocketState::ConnectedState) - { - flag = false; - } - else - flag = true; - - return flag; -} - - -bool jceSSLClient::isConnected() -{ - - return flag; -} - -bool jceSSLClient::sendData(std::string str) -{ - if (isConnected()) //if connected - { - write(str.c_str(),str.length()); - while (waitForBytesWritten()); - return true; - } - return false; -} -/** - * @brief jceSSLClient::recieveData - * @param str this variable will store the recieved data - * @param fast true for LOGIN ONLY, false to retrieve all data - * @return true if recieved data bigger than zero - */ -bool jceSSLClient::recieveData(std::string &str, bool fast) -{ - packet = ""; - bool sflag = false; - - if (fast) //fast connection, good for login only!! - { - QEventLoop loop; - connect(this, SIGNAL(readyRead()), &loop, SLOT(quit())); - connect(this, SIGNAL(readyRead()), this, SLOT(readIt())); - loop.exec(); - disconnect(this, SIGNAL(readyRead()), &loop, SLOT(quit())); - disconnect(this, SIGNAL(readyRead()), this, SLOT(readIt())); - } - else - { - QString p; - while (waitForReadyRead(milisTimeOut)) - { - do - { - p = readAll(); - packet.append(p); - }while (p.size() > 0); - } - } - - str = packet.toStdString(); - if (str.size() > 0) - sflag = true; - - return sflag; - -} -void jceSSLClient::readIt() -{ - QString p; - do - { - p = readAll(); - packet.append(p); - }while (p.size() > 0); - - - -} -void jceSSLClient::setConnected() -{ - waitForEncrypted(); -} - -void jceSSLClient::setEncrypted() -{ - setReadBufferSize(10000); - setSocketOption(QAbstractSocket::KeepAliveOption,1); - flag = true; -} - -void jceSSLClient::setDisconnected() -{ - abort(); - flag = false; -} - - -void jceSSLClient::checkErrors(QAbstractSocket::SocketError) -{ - -} - +#include "jcesslclient.h" + +jceSSLClient::jceSSLClient() : flag(false), packet("") +{ + connect(this,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(checkErrors(QAbstractSocket::SocketError))); + connect(this,SIGNAL(connected()),this,SLOT(setConnected())); + connect(this,SIGNAL(encrypted()),this,SLOT(setEncrypted())); + connect(this,SIGNAL(disconnected()),this,SLOT(setDisconnected())); + +} + +bool jceSSLClient::makeConnect(std::string server, int port) +{ + QEventLoop loop; + QObject::connect(this, SIGNAL(encrypted()), &loop, SLOT(quit())); + if (isConnected()) + abort(); + if (isOpen()) + abort(); + + connectToHostEncrypted(server.c_str(), port); + loop.exec(); + return isConnected(); + +} +bool jceSSLClient::makeDiconnect() +{ + + abort(); + if (state() == QAbstractSocket::SocketState::ConnectedState) + { + flag = false; + } + else + flag = true; + + return flag; +} + + +bool jceSSLClient::isConnected() +{ + + return flag; +} + +bool jceSSLClient::sendData(std::string str) +{ + if (isConnected()) //if connected + { + write(str.c_str(),str.length()); + while (waitForBytesWritten()); + return true; + } + return false; +} +/** + * @brief jceSSLClient::recieveData + * @param str this variable will store the recieved data + * @param fast true for LOGIN ONLY, false to retrieve all data + * @return true if recieved data bigger than zero + */ +bool jceSSLClient::recieveData(std::string &str, bool fast) +{ + packet = ""; + bool sflag = false; + + if (fast) //fast connection, good for login only!! + { + QEventLoop loop; + connect(this, SIGNAL(readyRead()), &loop, SLOT(quit())); + connect(this, SIGNAL(readyRead()), this, SLOT(readIt())); + loop.exec(); + disconnect(this, SIGNAL(readyRead()), &loop, SLOT(quit())); + disconnect(this, SIGNAL(readyRead()), this, SLOT(readIt())); + } + else + { + QString p; + while (waitForReadyRead(milisTimeOut)) + { + do + { + p = readAll(); + packet.append(p); + }while (p.size() > 0); + } + } + + str = packet.toStdString(); + if (str.size() > 0) + sflag = true; + + return sflag; + +} +void jceSSLClient::readIt() +{ + QString p; + do + { + p = readAll(); + packet.append(p); + }while (p.size() > 0); + + + +} +void jceSSLClient::setConnected() +{ + waitForEncrypted(); +} + +void jceSSLClient::setEncrypted() +{ + setReadBufferSize(10000); + setSocketOption(QAbstractSocket::KeepAliveOption,1); + flag = true; +} + +void jceSSLClient::setDisconnected() +{ + abort(); + flag = false; +} + + +void jceSSLClient::checkErrors(QAbstractSocket::SocketError) +{ + +} + diff --git a/src/connection/jcesslclient.h b/src/connection/jcesslclient.h index 6e73658..b19ba10 100644 --- a/src/connection/jcesslclient.h +++ b/src/connection/jcesslclient.h @@ -1,39 +1,39 @@ -#ifndef JCESSLCLIENT_H -#define JCESSLCLIENT_H - -#include -#include -#include -#include - -#include -#include -#define milisTimeOut 3500 - -class jceSSLClient : QSslSocket -{ - Q_OBJECT -public: - jceSSLClient(); - - bool makeConnect(std::string server,int port); - bool isConnected(); - bool sendData(std::string str); - bool recieveData(std::string &str, bool fast); - bool makeDiconnect(); - -private slots: - void checkErrors(QAbstractSocket::SocketError); - void setConnected(); - void setEncrypted(); - void setDisconnected(); - void readIt(); - -private: - - bool flag; - QString packet; - -}; - -#endif // JCESSLCLIENT_H +#ifndef JCESSLCLIENT_H +#define JCESSLCLIENT_H + +#include +#include +#include +#include + +#include +#include +#define milisTimeOut 3500 + +class jceSSLClient : QSslSocket +{ + Q_OBJECT +public: + jceSSLClient(); + + bool makeConnect(std::string server,int port); + bool isConnected(); + bool sendData(std::string str); + bool recieveData(std::string &str, bool fast); + bool makeDiconnect(); + +private slots: + void checkErrors(QAbstractSocket::SocketError); + void setConnected(); + void setEncrypted(); + void setDisconnected(); + void readIt(); + +private: + + bool flag; + QString packet; + +}; + +#endif // JCESSLCLIENT_H