connection fix

This commit is contained in:
liranbg 2014-07-11 17:01:25 +03:00
parent 5825c6623a
commit 4f54ad85a7
4 changed files with 180 additions and 180 deletions

View file

@ -21,7 +21,7 @@
</property> </property>
<property name="windowIcon"> <property name="windowIcon">
<iconset resource="../resources/connectionstatus.qrc"> <iconset resource="../resources/connectionstatus.qrc">
<normaloff>:/icons/icon.png</normaloff>:/icons/icon.png</iconset> <normaloff>:/icons/icon.ico</normaloff>:/icons/icon.ico</iconset>
</property> </property>
<property name="styleSheet"> <property name="styleSheet">
<string notr="true">#centralWidget <string notr="true">#centralWidget

View file

@ -1,8 +1,8 @@
<RCC> <RCC>
<qresource prefix="/icons"> <qresource prefix="/icons">
<file>blueStatusIcon.png</file> <file>blueStatusIcon.png</file>
<file>greenStatusIcon.png</file> <file>greenStatusIcon.png</file>
<file>redStatusIcon.png</file> <file>redStatusIcon.png</file>
<file>icon.png</file> <file>icon.ico</file>
</qresource> </qresource>
</RCC> </RCC>

View file

@ -1,132 +1,132 @@
#include "jcesslclient.h" #include "jcesslclient.h"
jceSSLClient::jceSSLClient() : flag(false), packet("") jceSSLClient::jceSSLClient() : flag(false), packet("")
{ {
connect(this,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(checkErrors(QAbstractSocket::SocketError))); connect(this,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(checkErrors(QAbstractSocket::SocketError)));
connect(this,SIGNAL(connected()),this,SLOT(setConnected())); connect(this,SIGNAL(connected()),this,SLOT(setConnected()));
connect(this,SIGNAL(encrypted()),this,SLOT(setEncrypted())); connect(this,SIGNAL(encrypted()),this,SLOT(setEncrypted()));
connect(this,SIGNAL(disconnected()),this,SLOT(setDisconnected())); connect(this,SIGNAL(disconnected()),this,SLOT(setDisconnected()));
} }
bool jceSSLClient::makeConnect(std::string server, int port) bool jceSSLClient::makeConnect(std::string server, int port)
{ {
QEventLoop loop; QEventLoop loop;
QObject::connect(this, SIGNAL(encrypted()), &loop, SLOT(quit())); QObject::connect(this, SIGNAL(encrypted()), &loop, SLOT(quit()));
if (isConnected()) if (isConnected())
abort(); abort();
if (isOpen()) if (isOpen())
abort(); abort();
connectToHostEncrypted(server.c_str(), port); connectToHostEncrypted(server.c_str(), port);
loop.exec(); loop.exec();
return isConnected(); return isConnected();
} }
bool jceSSLClient::makeDiconnect() bool jceSSLClient::makeDiconnect()
{ {
abort(); abort();
if (state() == QAbstractSocket::SocketState::ConnectedState) if (state() == QAbstractSocket::SocketState::ConnectedState)
{ {
flag = false; flag = false;
} }
else else
flag = true; flag = true;
return flag; return flag;
} }
bool jceSSLClient::isConnected() bool jceSSLClient::isConnected()
{ {
return flag; return flag;
} }
bool jceSSLClient::sendData(std::string str) bool jceSSLClient::sendData(std::string str)
{ {
if (isConnected()) //if connected if (isConnected()) //if connected
{ {
write(str.c_str(),str.length()); write(str.c_str(),str.length());
while (waitForBytesWritten()); while (waitForBytesWritten());
return true; return true;
} }
return false; return false;
} }
/** /**
* @brief jceSSLClient::recieveData * @brief jceSSLClient::recieveData
* @param str this variable will store the recieved data * @param str this variable will store the recieved data
* @param fast true for LOGIN ONLY, false to retrieve all data * @param fast true for LOGIN ONLY, false to retrieve all data
* @return true if recieved data bigger than zero * @return true if recieved data bigger than zero
*/ */
bool jceSSLClient::recieveData(std::string &str, bool fast) bool jceSSLClient::recieveData(std::string &str, bool fast)
{ {
packet = ""; packet = "";
bool sflag = false; bool sflag = false;
if (fast) //fast connection, good for login only!! if (fast) //fast connection, good for login only!!
{ {
QEventLoop loop; QEventLoop loop;
connect(this, SIGNAL(readyRead()), &loop, SLOT(quit())); connect(this, SIGNAL(readyRead()), &loop, SLOT(quit()));
connect(this, SIGNAL(readyRead()), this, SLOT(readIt())); connect(this, SIGNAL(readyRead()), this, SLOT(readIt()));
loop.exec(); loop.exec();
disconnect(this, SIGNAL(readyRead()), &loop, SLOT(quit())); disconnect(this, SIGNAL(readyRead()), &loop, SLOT(quit()));
disconnect(this, SIGNAL(readyRead()), this, SLOT(readIt())); disconnect(this, SIGNAL(readyRead()), this, SLOT(readIt()));
} }
else else
{ {
QString p; QString p;
while (waitForReadyRead(milisTimeOut)) while (waitForReadyRead(milisTimeOut))
{ {
do do
{ {
p = readAll(); p = readAll();
packet.append(p); packet.append(p);
}while (p.size() > 0); }while (p.size() > 0);
} }
} }
str = packet.toStdString(); str = packet.toStdString();
if (str.size() > 0) if (str.size() > 0)
sflag = true; sflag = true;
return sflag; return sflag;
} }
void jceSSLClient::readIt() void jceSSLClient::readIt()
{ {
QString p; QString p;
do do
{ {
p = readAll(); p = readAll();
packet.append(p); packet.append(p);
}while (p.size() > 0); }while (p.size() > 0);
} }
void jceSSLClient::setConnected() void jceSSLClient::setConnected()
{ {
waitForEncrypted(); waitForEncrypted();
} }
void jceSSLClient::setEncrypted() void jceSSLClient::setEncrypted()
{ {
setReadBufferSize(10000); setReadBufferSize(10000);
setSocketOption(QAbstractSocket::KeepAliveOption,1); setSocketOption(QAbstractSocket::KeepAliveOption,1);
flag = true; flag = true;
} }
void jceSSLClient::setDisconnected() void jceSSLClient::setDisconnected()
{ {
abort(); abort();
flag = false; flag = false;
} }
void jceSSLClient::checkErrors(QAbstractSocket::SocketError) void jceSSLClient::checkErrors(QAbstractSocket::SocketError)
{ {
} }

View file

@ -1,39 +1,39 @@
#ifndef JCESSLCLIENT_H #ifndef JCESSLCLIENT_H
#define JCESSLCLIENT_H #define JCESSLCLIENT_H
#include <QObject> #include <QObject>
#include <QSslSocket> #include <QSslSocket>
#include <QThread> #include <QThread>
#include <QEventLoop> #include <QEventLoop>
#include <iostream> #include <iostream>
#include <string> #include <string>
#define milisTimeOut 3500 #define milisTimeOut 3500
class jceSSLClient : QSslSocket class jceSSLClient : QSslSocket
{ {
Q_OBJECT Q_OBJECT
public: public:
jceSSLClient(); jceSSLClient();
bool makeConnect(std::string server,int port); bool makeConnect(std::string server,int port);
bool isConnected(); bool isConnected();
bool sendData(std::string str); bool sendData(std::string str);
bool recieveData(std::string &str, bool fast); bool recieveData(std::string &str, bool fast);
bool makeDiconnect(); bool makeDiconnect();
private slots: private slots:
void checkErrors(QAbstractSocket::SocketError); void checkErrors(QAbstractSocket::SocketError);
void setConnected(); void setConnected();
void setEncrypted(); void setEncrypted();
void setDisconnected(); void setDisconnected();
void readIt(); void readIt();
private: private:
bool flag; bool flag;
QString packet; QString packet;
}; };
#endif // JCESSLCLIENT_H #endif // JCESSLCLIENT_H