2017-11-18 01:29:14 +03:00
|
|
|
#include "ETcpSocket.h"
|
|
|
|
#include "exaptions.h"
|
2018-01-08 17:29:58 +03:00
|
|
|
#include <QTimer>
|
2017-11-18 01:29:14 +03:00
|
|
|
|
|
|
|
ETcpSocket::ETcpSocket()
|
|
|
|
{
|
|
|
|
source=new QTcpSocket();
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
ETcpSocket::ETcpSocket(QTcpSocket*ptr)
|
|
|
|
{
|
|
|
|
source=ptr;
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
ETcpSocket::ETcpSocket(const QString& address, int port){
|
|
|
|
source = new QTcpSocket();
|
2017-11-27 22:59:59 +03:00
|
|
|
source->connectToHost(address, port);
|
|
|
|
if(!source->waitForConnected(DEEP_SCANER_INTERVAL) || !source->open(QIODevice::ReadWrite)){
|
2017-11-24 21:17:41 +03:00
|
|
|
throw AddNodeExaption();
|
2017-11-18 01:29:14 +03:00
|
|
|
}
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ETcpSocket::init(){
|
2017-12-17 01:23:38 +03:00
|
|
|
array = new QByteArray;
|
|
|
|
|
2017-11-18 01:29:14 +03:00
|
|
|
connect(source,SIGNAL(connected()),this,SLOT(connected_()));
|
|
|
|
connect(source,SIGNAL(disconnected()),this,SLOT(disconnected_()));
|
|
|
|
connect(source,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(error_(QAbstractSocket::SocketError)));
|
|
|
|
connect(source,SIGNAL(hostFound()),this,SLOT(hostFound_()));
|
|
|
|
connect(source,SIGNAL(proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *)),this,SLOT(proxyAuthenticationRequired_(const QNetworkProxy &, QAuthenticator *)));
|
|
|
|
connect(source,SIGNAL(stateChanged(QAbstractSocket::SocketState)),this,SLOT(stateChanged_(QAbstractSocket::SocketState)));
|
|
|
|
connect(source,SIGNAL(readyRead()),this,SLOT(readReady_()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ETcpSocket::error_(QAbstractSocket::SocketError i){
|
|
|
|
emit Error(this,i);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ETcpSocket::connected_(){
|
|
|
|
emit Connected(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ETcpSocket::disconnected_(){
|
|
|
|
emit Disconnected(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ETcpSocket::hostFound_(){
|
|
|
|
emit HostFound(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ETcpSocket::proxyAuthenticationRequired_(const QNetworkProxy &proxy, QAuthenticator *authenticator){
|
2018-01-10 19:24:09 +03:00
|
|
|
emit ProxyAuthenticationRequired(this, proxy, authenticator);
|
2017-11-18 01:29:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ETcpSocket::stateChanged_(QAbstractSocket::SocketState socketState){
|
|
|
|
emit StateChanged(this,socketState);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ETcpSocket::readReady_(){
|
2018-01-12 11:16:06 +03:00
|
|
|
|
2018-01-13 18:38:26 +03:00
|
|
|
bool sizewrite = array->isEmpty();
|
2017-11-18 01:29:14 +03:00
|
|
|
array->append(source->readAll());
|
2018-01-12 11:16:06 +03:00
|
|
|
QDataStream stream(array, QIODevice::ReadOnly);
|
|
|
|
if(sizewrite)
|
2017-12-17 01:23:38 +03:00
|
|
|
stream >> size;
|
|
|
|
|
2017-11-18 01:29:14 +03:00
|
|
|
#ifdef QT_DEBUG
|
2018-01-13 18:38:26 +03:00
|
|
|
qDebug()<<"messae size:" << size;
|
|
|
|
qDebug()<<"message package size:" << array->size();
|
2017-11-18 01:29:14 +03:00
|
|
|
#endif
|
|
|
|
if(size==array->size())
|
|
|
|
{
|
2018-01-13 18:38:26 +03:00
|
|
|
array->remove(0, sizeof(qint32));
|
2017-11-18 01:29:14 +03:00
|
|
|
ReadyStack.push_back(array);
|
2017-11-20 00:37:12 +03:00
|
|
|
array=new QByteArray();
|
2017-11-18 01:29:14 +03:00
|
|
|
emit Message(this);
|
|
|
|
}else{
|
2018-01-14 13:17:38 +03:00
|
|
|
|
|
|
|
if(size < array->size()){
|
|
|
|
array->clear();
|
|
|
|
}
|
|
|
|
|
2018-01-13 18:38:26 +03:00
|
|
|
emit donwload(array->size(), size);
|
2017-11-18 01:29:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-11 21:48:01 +03:00
|
|
|
QString ETcpSocket::peerName() const{
|
|
|
|
return QString("%0:%1").arg(source->peerAddress().toString()).arg(source->peerPort());
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ETcpSocket::localName() const{
|
|
|
|
return QString("%0:%1").arg(source->localAddress().toString()).arg(source->localPort());
|
2017-11-18 01:29:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray* ETcpSocket::topStack(){
|
|
|
|
if(ReadyStack.size())
|
|
|
|
return ReadyStack.front();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTcpSocket* ETcpSocket::getSource()const{
|
|
|
|
return source;
|
|
|
|
}
|
|
|
|
|
2017-11-26 19:19:43 +03:00
|
|
|
void ETcpSocket::nextItem(bool free){
|
2017-11-20 00:37:12 +03:00
|
|
|
if( ReadyStack.size()){
|
2017-11-26 19:19:43 +03:00
|
|
|
if(free){
|
|
|
|
ReadyStack.front()->clear();
|
|
|
|
delete ReadyStack.front();
|
|
|
|
}
|
2017-11-18 01:29:14 +03:00
|
|
|
ReadyStack.pop_front();
|
2017-11-20 00:37:12 +03:00
|
|
|
}
|
2017-11-18 01:29:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int ETcpSocket::sizeDescriptPackege(){
|
|
|
|
return sizeof(qint32);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ETcpSocket::toStringTcp(){
|
|
|
|
return source->peerAddress().toString();
|
|
|
|
}
|
|
|
|
|
2018-01-14 13:17:38 +03:00
|
|
|
bool ETcpSocket::Write(const QByteArray&data){
|
2017-11-18 01:29:14 +03:00
|
|
|
if(source->state()==QTcpSocket::ConnectedState){
|
|
|
|
QByteArray array;
|
2018-01-13 18:38:26 +03:00
|
|
|
QDataStream stream(&array, QIODevice::ReadWrite);
|
2017-12-17 01:23:38 +03:00
|
|
|
|
|
|
|
stream << qint32(0);
|
2017-11-18 01:29:14 +03:00
|
|
|
array.append(data);
|
2018-01-14 13:17:38 +03:00
|
|
|
stream.device()->seek(0);
|
|
|
|
stream<<qint32(array.size());
|
|
|
|
|
2017-11-18 01:29:14 +03:00
|
|
|
#ifdef QT_DEBUG
|
2018-01-13 18:38:26 +03:00
|
|
|
qDebug() << "size :" << array.size();
|
|
|
|
qint64 temp = source->write(array);
|
|
|
|
qDebug() << "size write:" << temp << " size packege:" << array.size();
|
|
|
|
return temp == (array.size());
|
2017-11-18 01:29:14 +03:00
|
|
|
#else
|
2018-01-13 18:38:26 +03:00
|
|
|
return source->write(array) == (array.size());
|
2017-11-18 01:29:14 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ETcpSocket::~ETcpSocket()
|
|
|
|
{
|
|
|
|
for(QByteArray*i:ReadyStack){
|
|
|
|
i->clear();
|
|
|
|
delete i;
|
|
|
|
}
|
|
|
|
disconnect(source,SIGNAL(connected()),this,SLOT(connected_()));
|
|
|
|
disconnect(source,SIGNAL(disconnected()),this,SLOT(disconnected_()));
|
|
|
|
disconnect(source,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(error_(QAbstractSocket::SocketError)));
|
|
|
|
disconnect(source,SIGNAL(hostFound()),this,SLOT(hostFound_()));
|
|
|
|
disconnect(source,SIGNAL(proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *)),this,SLOT(proxyAuthenticationRequired_(const QNetworkProxy &, QAuthenticator *)));
|
|
|
|
disconnect(source,SIGNAL(stateChanged(QAbstractSocket::SocketState)),this,SLOT(stateChanged_(QAbstractSocket::SocketState)));
|
|
|
|
disconnect(source,SIGNAL(readyRead()),this,SLOT(readReady_()));
|
|
|
|
source->deleteLater();
|
|
|
|
}
|