reconnected is working! just need to add the validations steps and we are good to go

This commit is contained in:
liranbg 2014-09-18 06:18:49 +03:00
parent 9be57bfecf
commit ac0973cf11
2 changed files with 37 additions and 34 deletions

View file

@ -3,7 +3,7 @@
/** /**
* @brief jceSSLClient::jceSSLClient Constructer, setting the signals * @brief jceSSLClient::jceSSLClient Constructer, setting the signals
*/ */
jceSSLClient::jceSSLClient() : flag(false), packet(""), networkConf() jceSSLClient::jceSSLClient() : flag(false), packet(""), networkConf(), reConnection(false)
{ {
//setting signals //setting signals
connect(this,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(checkErrors(QAbstractSocket::SocketError))); connect(this,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(checkErrors(QAbstractSocket::SocketError)));
@ -16,7 +16,6 @@ jceSSLClient::jceSSLClient() : flag(false), packet(""), networkConf()
connect(this, SIGNAL(encrypted()), &loop, SLOT(quit())); connect(this, SIGNAL(encrypted()), &loop, SLOT(quit()));
connect(this, SIGNAL(error(QAbstractSocket::SocketError)),&loop,SLOT(quit())); connect(this, SIGNAL(error(QAbstractSocket::SocketError)),&loop,SLOT(quit()));
} }
/** /**
@ -28,7 +27,7 @@ jceSSLClient::jceSSLClient() : flag(false), packet(""), networkConf()
bool jceSSLClient::makeConnect(QString server, int port) bool jceSSLClient::makeConnect(QString server, int port)
{ {
qDebug() << Q_FUNC_INFO << "Making connection"; qDebug() << Q_FUNC_INFO << "Making connection";
if (flag) if (isConnected())
{ {
qDebug() << Q_FUNC_INFO << "flag=true, calling makeDisconnect()"; qDebug() << Q_FUNC_INFO << "flag=true, calling makeDisconnect()";
makeDiconnect(); makeDiconnect();
@ -48,14 +47,14 @@ bool jceSSLClient::makeConnect(QString server, int port)
*/ */
bool jceSSLClient::makeDiconnect() bool jceSSLClient::makeDiconnect()
{ {
this->disconnectFromHost();
if (loop.isRunning()) if (loop.isRunning())
{ {
qWarning() << Q_FUNC_INFO << "Killing connection thread"; qWarning() << Q_FUNC_INFO << "Killing connection thread";
loop.exit(); loop.exit();
} }
qDebug() << Q_FUNC_INFO << "emitting disconnected()"; qDebug() << Q_FUNC_INFO << "disconnecting from host and emitting disconnected()";
emit disconnected(); this->disconnectFromHost(); //emits disconnected > setDisconnected
setSocketState(QAbstractSocket::SocketState::UnconnectedState);
return (!isConnected()); return (!isConnected());
} }
@ -66,29 +65,30 @@ bool jceSSLClient::makeDiconnect()
*/ */
bool jceSSLClient::isConnected() bool jceSSLClient::isConnected()
{ {
bool tempFlag;
//checking state before returning flag! //checking state before returning flag!
if (state() == QAbstractSocket::SocketState::UnconnectedState) if (state() == QAbstractSocket::SocketState::UnconnectedState)
{ {
flag = false; tempFlag = false;
} }
else if (state() == QAbstractSocket::SocketState::ClosingState) else if (state() == QAbstractSocket::SocketState::ClosingState)
{ {
flag = false; tempFlag = false;
} }
else if (state() == QAbstractSocket::SocketState::ConnectedState) else if (state() == QAbstractSocket::SocketState::ConnectedState)
{ {
if (this->networkConf.isOnline()) if (this->networkConf.isOnline())
flag = true; tempFlag = true;
else else
{ {
this->setSocketState(QAbstractSocket::SocketState::UnconnectedState); this->setSocketState(QAbstractSocket::SocketState::UnconnectedState);
flag = false; tempFlag = false;
} }
} }
if (!this->networkConf.isOnline()) //no link, ethernet\wifi if (!this->networkConf.isOnline()) //no link, ethernet\wifi
flag = false; tempFlag = false;
return flag; return ((flag) && (tempFlag));
} }
/** /**
* @brief jceSSLClient::sendData - given string, send it to server * @brief jceSSLClient::sendData - given string, send it to server
@ -167,16 +167,16 @@ void jceSSLClient::readIt()
void jceSSLClient::setOnlineState(bool isOnline) void jceSSLClient::setOnlineState(bool isOnline)
{ {
qWarning() << Q_FUNC_INFO << "isOnline status change: " << isOnline; qWarning() << Q_FUNC_INFO << "isOnline status change: " << isOnline;
if (isOnline) if (isOnline) //to be added later
{ {
if (this->makeConnect()) //we can add here auto reconnect if wifi\ethernet link has appear
qDebug() << Q_FUNC_INFO << "reconnected"; //will be added next version
} }
else else
{ {
setSocketState(QAbstractSocket::SocketState::UnconnectedState); //abort() ?
this->makeDiconnect(); this->makeDiconnect();
emit noInternetLink();
} }
} }
@ -194,21 +194,27 @@ void jceSSLClient::setDisconnected()
{ {
qDebug() << Q_FUNC_INFO << "DISCONNECTED"; qDebug() << Q_FUNC_INFO << "DISCONNECTED";
this->setSocketState(QAbstractSocket::SocketState::UnconnectedState); this->setSocketState(QAbstractSocket::SocketState::UnconnectedState);
packet.clear();
flag = false; flag = false;
if (reConnection)
makeConnect();
} }
/** /**
* @brief jceSSLClient::setEncrypted called when signaled with encrypted. setting the buffer size and keeping alive. * @brief jceSSLClient::setEncrypted called when signaled with encrypted. setting the buffer size and keeping alive.
*/ */
void jceSSLClient::setEncrypted() void jceSSLClient::setEncrypted()
{ {
qDebug() << Q_FUNC_INFO << "ENCRYPTED";
setReadBufferSize(10000); setReadBufferSize(10000);
setSocketOption(QAbstractSocket::KeepAliveOption,1); setSocketOption(QAbstractSocket::KeepAliveOption,true);
if (isConnected()); flag = true;
else if (!isConnected())
{ {
qWarning() << "jceSSLClient::setEncrypted(); Connection status didnt change!"; qWarning() << Q_FUNC_INFO << "Connection status didnt change! reseting flag to false";
flag = false; flag = false;
} }
} }
/** /**
* @brief jceSSLClient::showIfErrorMsg message box to show the error occured according to socket * @brief jceSSLClient::showIfErrorMsg message box to show the error occured according to socket
@ -231,15 +237,9 @@ void jceSSLClient::showIfErrorMsg()
case QAbstractSocket::SocketError::RemoteHostClosedError: /**/ case QAbstractSocket::SocketError::RemoteHostClosedError: /**/
errorString = QObject::tr("RemoteHostClosedError"); errorString = QObject::tr("RemoteHostClosedError");
//The remote host closed the connection //The remote host closed the connection
if (isConnected()) //we can reconnect if (networkConf.isOnline()) //we can reconnect
{ {
qDebug() << Q_FUNC_INFO << "trying to reconnect"; reConnection = true;
flag = false;
this->disconnectFromHost();
qDebug() << Q_FUNC_INFO << "we disconnected.";
if (makeConnect())
qDebug() << Q_FUNC_INFO << "RECONNECTED";
} }
else else
relevantError = true; relevantError = true;
@ -263,13 +263,11 @@ void jceSSLClient::showIfErrorMsg()
case QAbstractSocket::SocketError::NetworkError: /**/ case QAbstractSocket::SocketError::NetworkError: /**/
errorString = QObject::tr("NetworkError"); errorString = QObject::tr("NetworkError");
//An error occurred with the network (e.g., the network cable was accidentally plugged out). //An error occurred with the network (e.g., the network cable was accidentally plugged out).
if (isConnected()) //we can reconnect if (networkConf.isOnline()) //we can reconnect
{ {
makeConnect();
} }
else else
relevantError = true; relevantError = true;
relevantError = true;
break; break;
case QAbstractSocket::SocketError::SslHandshakeFailedError: /**/ case QAbstractSocket::SocketError::SslHandshakeFailedError: /**/
errorString = QObject::tr("SslHandshakeFailedError"); errorString = QObject::tr("SslHandshakeFailedError");

View file

@ -23,6 +23,11 @@ public:
bool recieveData(QString &str, bool fast); bool recieveData(QString &str, bool fast);
void showIfErrorMsg(); void showIfErrorMsg();
signals:
void serverDisconnectedbyRemote();
void noInternetLink();
void socketDisconnected();
private slots: private slots:
void checkErrors(QAbstractSocket::SocketError a); void checkErrors(QAbstractSocket::SocketError a);
void setConnected(); void setConnected();
@ -32,11 +37,11 @@ private slots:
void setOnlineState(bool isOnline); void setOnlineState(bool isOnline);
private: private:
bool flag; bool flag;
QString packet; QString packet;
QEventLoop loop; //handle the connection as thread QEventLoop loop; //handle the connection as thread
QNetworkConfigurationManager networkConf; //checking online QNetworkConfigurationManager networkConf; //checking if online
bool reConnection; //used for remote host disconnecting
}; };