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 name="windowIcon">
<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 name="styleSheet">
<string notr="true">#centralWidget

View file

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

View file

@ -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)
{
}

View file

@ -1,39 +1,39 @@
#ifndef JCESSLCLIENT_H
#define JCESSLCLIENT_H
#include <QObject>
#include <QSslSocket>
#include <QThread>
#include <QEventLoop>
#include <iostream>
#include <string>
#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 <QObject>
#include <QSslSocket>
#include <QThread>
#include <QEventLoop>
#include <iostream>
#include <string>
#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