mirror of
https://gitea.msk.dinamika-avia.ru/Constanta-Design/RRJClient.git
synced 2026-03-28 05:25:39 +03:00
Попытка оптимизации
This commit is contained in:
@@ -5,10 +5,11 @@ RecognizeSystem::RecognizeSystem(QObject *parent):
|
||||
QObject(parent),
|
||||
versionContainer(nullptr),
|
||||
dataParser(nullptr),
|
||||
postProcessorSystem(nullptr)
|
||||
postProcessorSystem(nullptr),
|
||||
client(nullptr)
|
||||
{
|
||||
packetType = PacketType::TYPE_NONE;
|
||||
filePath.clear();
|
||||
filePath = "";
|
||||
fileSize = 0;
|
||||
sizeReceiveData = 0;
|
||||
tmpBlock.clear();
|
||||
@@ -17,292 +18,301 @@ RecognizeSystem::RecognizeSystem(QObject *parent):
|
||||
|
||||
void RecognizeSystem::initialize(DataParser *dataParser, VersionContainer *versionContainer,PostProcessorSystem *postProcessorSystem,Client *client)
|
||||
{
|
||||
this->versionContainer = versionContainer;
|
||||
this->postProcessorSystem = postProcessorSystem;
|
||||
this->versionContainer = versionContainer;
|
||||
this->dataParser = dataParser;
|
||||
this->postProcessorSystem = postProcessorSystem;
|
||||
this->client = client;
|
||||
}
|
||||
|
||||
const int BLOCK_SIZE = 1024 * 1024; // Размер блока
|
||||
|
||||
void RecognizeSystem::recognize(QTcpSocket *socket)
|
||||
{
|
||||
qDebug() << "RecognizeThreadId " << QThread::currentThreadId();
|
||||
//qDebug() << "RecognizeThreadId " << QThread::currentThreadId();
|
||||
|
||||
QDataStream stream(socket);
|
||||
stream.setVersion(QDataStream::Qt_DefaultCompiledVersion);
|
||||
|
||||
while(socket->bytesAvailable())
|
||||
{
|
||||
|
||||
if (socket->state() != QTcpSocket::ConnectedState) return;
|
||||
|
||||
if(packetType == PacketType::TYPE_NONE){ //определение первичного пакета
|
||||
|
||||
stream.startTransaction();
|
||||
stream >> packetType;
|
||||
|
||||
if(!stream.commitTransaction())
|
||||
{
|
||||
emit sigSendDebugLog(Tools::getTime() + " CLIENT: packetType - FAIL commitTransaction");
|
||||
|
||||
if(socket->waitForReadyRead(TCP_READ_TIMEOUT)){
|
||||
emit sigSendDebugLog("ERROR: PACKET TYPE READ TIMEOUT");
|
||||
return;
|
||||
}
|
||||
continue;
|
||||
|
||||
}
|
||||
|
||||
//qDebug() << Tools::GetTime() << "CLIENT: type: " << packetType;
|
||||
if (socket->state() != QTcpSocket::ConnectedState)
|
||||
{
|
||||
qDebug() << "RecognizeSystem::recognize socket->state() != QTcpSocket::ConnectedState";
|
||||
return;
|
||||
}
|
||||
|
||||
if(packetType == PacketType::TYPE_FOLDER){ //создание папок
|
||||
stream.startTransaction();
|
||||
stream >> filePath;
|
||||
|
||||
if(!stream.commitTransaction()){
|
||||
continue;
|
||||
}
|
||||
|
||||
filePath = Tools::createReceiveFullPath(filePath,versionContainer->getServerVersionData());
|
||||
|
||||
QDir dir(filePath);
|
||||
if(!dir.exists()){
|
||||
if(dir.mkpath(filePath)){
|
||||
qDebug() << "Dir Created";
|
||||
}
|
||||
}
|
||||
|
||||
packetType = PacketType::TYPE_NONE;
|
||||
emit sigUpdateBytesAvailable();
|
||||
/*
|
||||
if(!socket->waitForReadyRead(TCP_READ_TIMEOUT))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
*/
|
||||
|
||||
if(packetType == PacketType::TYPE_FILE) //загрузка файлов
|
||||
switch ((int)packetType)
|
||||
{
|
||||
//ПОЛУЧЕНИЕ ПУТИ
|
||||
//ПОЛУЧЕНИЕ РАЗМЕРА ФАЙЛА
|
||||
forever
|
||||
case PacketType::TYPE_NONE: //определение первичного пакета
|
||||
{
|
||||
stream.startTransaction();
|
||||
stream >> packetType;
|
||||
|
||||
if(!stream.commitTransaction())
|
||||
{
|
||||
socket->waitForReadyRead(TCP_READ_TIMEOUT);
|
||||
continue;
|
||||
}
|
||||
//qDebug() << Tools::GetTime() << "CLIENT: type: " << packetType;
|
||||
}
|
||||
continue;
|
||||
|
||||
case PacketType::TYPE_FOLDER: //создание папок
|
||||
{
|
||||
stream.startTransaction();
|
||||
stream >> filePath;
|
||||
stream >> fileSize;
|
||||
|
||||
if(!stream.commitTransaction()){
|
||||
emit sigSendDebugLog(Tools::getTime() + "CLIENT: filePath, fileSize - FAIL commitTransaction");
|
||||
|
||||
if (!socket->waitForReadyRead(TCP_READ_TIMEOUT)) {
|
||||
emit sigSendDebugLog(Tools::getTime() + "CLIENT: ERROR! readyRead timeout - filePath, fileSize!!!");
|
||||
return;
|
||||
}
|
||||
if(!stream.commitTransaction())
|
||||
{
|
||||
continue;
|
||||
|
||||
}
|
||||
|
||||
filePath = Tools::createReceiveFullPath(filePath,versionContainer->getServerVersionData());
|
||||
|
||||
emit sigSendDebugLog("CLIENT: filesize: " + QString::number(fileSize));
|
||||
emit sigSendDebugLog("CLIENT: filePath: " + filePath);
|
||||
|
||||
socket->waitForReadyRead(100);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
//ПОЛУЧЕНИЕ САМОГО ФАЙЛА
|
||||
emit sigSendDebugLog(Tools::getTime() + "AfterRead size and path BytesAvailable: " + socket->bytesAvailable());
|
||||
|
||||
//УКАЗАНИЕ ПУТИ ФАЙЛА
|
||||
QFile file(filePath);
|
||||
|
||||
if (file.exists())
|
||||
{
|
||||
file.remove(); //удаление файла, если он уже есть, но необходимо обновить
|
||||
emit sigSendDebugLog(Tools::getTime() + "Delete exist file: " + filePath);
|
||||
socket->waitForReadyRead(100);
|
||||
}
|
||||
|
||||
tmpBlock.clear();
|
||||
sizeReceiveData = 0;
|
||||
countSend = 0;
|
||||
|
||||
file.open(QFile::WriteOnly);
|
||||
|
||||
if(! file.isOpen())
|
||||
{
|
||||
int marker = 0;
|
||||
QMessageBox::critical(nullptr, "P31", "POINT 31");
|
||||
}
|
||||
if(! file.isWritable())
|
||||
{
|
||||
int marker = 0;
|
||||
QMessageBox::critical(nullptr, "P32", "POINT 32");
|
||||
}
|
||||
|
||||
while(true)
|
||||
{
|
||||
socket->waitForReadyRead(10);
|
||||
|
||||
if(socket->bytesAvailable() <= 0)
|
||||
continue;
|
||||
|
||||
if(fileSize - sizeReceiveData >= BLOCK_SIZE)
|
||||
tmpBlock = socket->read(BLOCK_SIZE);
|
||||
else
|
||||
tmpBlock = socket->read(fileSize - sizeReceiveData);
|
||||
|
||||
qint64 bytesReceived = tmpBlock.length();
|
||||
|
||||
if(bytesReceived <= 0)
|
||||
continue;
|
||||
|
||||
sizeReceiveData += bytesReceived;
|
||||
countSend++;
|
||||
|
||||
qint64 toFile = file.write(tmpBlock);
|
||||
|
||||
if(toFile <= 0)
|
||||
QDir dir(filePath);
|
||||
if(!dir.exists())
|
||||
{
|
||||
//emit sigUpdateBytesAvailable();
|
||||
QMessageBox::critical(nullptr, "P2", "POINT 33");
|
||||
if(dir.mkpath(filePath))
|
||||
{
|
||||
qDebug() << "Dir Created: " << filePath;
|
||||
}
|
||||
}
|
||||
|
||||
emit sigUpdateBytesAvailable();
|
||||
}
|
||||
break;
|
||||
|
||||
case PacketType::TYPE_FILE: //загрузка файлов
|
||||
{
|
||||
//ПОЛУЧЕНИЕ ПУТИ
|
||||
//ПОЛУЧЕНИЕ РАЗМЕРА ФАЙЛА
|
||||
forever
|
||||
{
|
||||
stream.startTransaction();
|
||||
stream >> filePath;
|
||||
stream >> fileSize;
|
||||
|
||||
if(!stream.commitTransaction())
|
||||
{
|
||||
emit sigSendDebugLog(Tools::getTime() + "CLIENT: filePath, fileSize - FAIL commitTransaction");
|
||||
continue;
|
||||
}
|
||||
|
||||
filePath = Tools::createReceiveFullPath(filePath,versionContainer->getServerVersionData());
|
||||
|
||||
emit sigSendDebugLog("CLIENT: filesize: " + QString::number(fileSize));
|
||||
emit sigSendDebugLog("CLIENT: filePath: " + filePath);
|
||||
|
||||
//socket->waitForReadyRead(100);
|
||||
break;
|
||||
}
|
||||
|
||||
//ПОЛУЧЕНИЕ САМОГО ФАЙЛА
|
||||
emit sigSendDebugLog(Tools::getTime() + "AfterRead size and path BytesAvailable: " + socket->bytesAvailable());
|
||||
|
||||
//УКАЗАНИЕ ПУТИ ФАЙЛА
|
||||
QFile file(filePath);
|
||||
|
||||
if (file.exists())
|
||||
{
|
||||
file.remove(); //удаление файла, если он уже есть, но необходимо обновить
|
||||
emit sigSendDebugLog(Tools::getTime() + "Delete exist file: " + filePath);
|
||||
|
||||
//socket->waitForReadyRead(100);
|
||||
}
|
||||
|
||||
tmpBlock.clear();
|
||||
sizeReceiveData = 0;
|
||||
countSend = 0;
|
||||
|
||||
if(sizeReceiveData == fileSize)
|
||||
file.open(QFile::WriteOnly);
|
||||
|
||||
if(! file.isOpen())
|
||||
QMessageBox::critical(nullptr, "P1", "isOpen false. File: " + file.fileName());
|
||||
|
||||
if(! file.isWritable())
|
||||
QMessageBox::critical(nullptr, "P2", "isWritable false. File: " + file.fileName());
|
||||
|
||||
while(true)
|
||||
{
|
||||
emit sigSendDebugLog(Tools::getTime() + "FINAL Count send: " + QString::number(countSend));
|
||||
emit sigSendDebugLog(Tools::getTime() + "FINAL Size received: " + QString::number(sizeReceiveData));
|
||||
emit sigSendDebugLog(Tools::getTime() + "FINAL File size" + QString::number(fileSize));
|
||||
emit sigUpdateBytesAvailable();
|
||||
break;
|
||||
}
|
||||
else if(sizeReceiveData > fileSize)
|
||||
{
|
||||
int marker = 0;
|
||||
QMessageBox::critical(nullptr, "P40", "POINT 34");
|
||||
if(socket->bytesAvailable() <= 0)
|
||||
{
|
||||
socket->waitForReadyRead(TCP_READ_TIMEOUT);
|
||||
continue;
|
||||
}
|
||||
|
||||
if(fileSize - sizeReceiveData >= BLOCK_SIZE)
|
||||
tmpBlock = socket->read(BLOCK_SIZE);
|
||||
else
|
||||
tmpBlock = socket->read(fileSize - sizeReceiveData);
|
||||
|
||||
qint64 bytesReceived = tmpBlock.length();
|
||||
|
||||
if(bytesReceived <= 0)
|
||||
{
|
||||
QMessageBox::critical(nullptr, "P3", "bytesReceived <= 0. File: " + file.fileName());
|
||||
continue;
|
||||
}
|
||||
|
||||
sizeReceiveData += bytesReceived;
|
||||
countSend++;
|
||||
|
||||
qint64 toFile = file.write(tmpBlock);
|
||||
|
||||
if(toFile <= 0)
|
||||
{
|
||||
//emit sigUpdateBytesAvailable();
|
||||
QMessageBox::critical(nullptr, "P4", "write toFile <= 0. File: " + file.fileName());
|
||||
continue;
|
||||
}
|
||||
|
||||
tmpBlock.clear();
|
||||
|
||||
if(sizeReceiveData == fileSize)
|
||||
{
|
||||
emit sigSendDebugLog(Tools::getTime() + "FINAL Count send: " + QString::number(countSend));
|
||||
emit sigSendDebugLog(Tools::getTime() + "FINAL Size received: " + QString::number(sizeReceiveData));
|
||||
emit sigSendDebugLog(Tools::getTime() + "FINAL File size" + QString::number(fileSize));
|
||||
emit sigUpdateBytesAvailable();
|
||||
break;
|
||||
}
|
||||
else if(sizeReceiveData > fileSize)
|
||||
{
|
||||
QMessageBox::critical(nullptr, "P5", "sizeReceiveData > fileSize");
|
||||
}
|
||||
}
|
||||
|
||||
file.close();
|
||||
emit sigSendDebugLog(Tools::getTime() + "File loaded");
|
||||
|
||||
//ОЧИСТКА ПОСЛЕ ПЕРЕДАЧИ
|
||||
|
||||
filePath.clear();
|
||||
fileSize = 0;
|
||||
tmpBlock.clear();
|
||||
sizeReceiveData = 0;
|
||||
countSend = 0;
|
||||
}
|
||||
break;
|
||||
|
||||
file.close();
|
||||
emit sigSendDebugLog(Tools::getTime() + "File loaded");
|
||||
|
||||
//ОЧИСТКА ПОСЛЕ ПЕРЕДАЧИ
|
||||
|
||||
filePath.clear();
|
||||
fileSize = 0;
|
||||
tmpBlock.clear();
|
||||
sizeReceiveData = 0;
|
||||
countSend = 0;
|
||||
}
|
||||
|
||||
if(packetType == PacketType::TYPE_DELETE) //удаление лишних файлов (рекурсивно удаляет все содежимое)
|
||||
{
|
||||
stream.startTransaction();
|
||||
stream >> filePath;
|
||||
|
||||
if(!stream.commitTransaction()){
|
||||
continue;
|
||||
}
|
||||
|
||||
filePath = Tools::createReceiveFullPath(filePath,versionContainer->getServerVersionData());
|
||||
|
||||
QFileInfo fileInfo(filePath);
|
||||
|
||||
|
||||
if(fileInfo.exists())
|
||||
case PacketType::TYPE_DELETE: //удаление лишних файлов (рекурсивно удаляет все содежимое)
|
||||
{
|
||||
if(fileInfo.isFile())
|
||||
stream.startTransaction();
|
||||
stream >> filePath;
|
||||
|
||||
if(!stream.commitTransaction())
|
||||
{
|
||||
QFile file(filePath);
|
||||
file.remove();
|
||||
continue;
|
||||
}
|
||||
|
||||
if(fileInfo.isDir()){
|
||||
QDir dir(filePath);
|
||||
dir.removeRecursively();
|
||||
filePath = Tools::createReceiveFullPath(filePath,versionContainer->getServerVersionData());
|
||||
|
||||
QFileInfo fileInfo(filePath);
|
||||
|
||||
if(fileInfo.exists())
|
||||
{
|
||||
if(fileInfo.isFile())
|
||||
{
|
||||
QFile file(filePath);
|
||||
file.remove();
|
||||
}
|
||||
|
||||
if(fileInfo.isDir())
|
||||
{
|
||||
QDir dir(filePath);
|
||||
dir.removeRecursively();
|
||||
}
|
||||
|
||||
qDebug() << Tools::getTime() << "Deleted: " << filePath;
|
||||
}
|
||||
|
||||
qDebug() << Tools::getTime() << "Deleted: " << filePath;
|
||||
}
|
||||
break;
|
||||
|
||||
packetType = PacketType::TYPE_NONE;
|
||||
continue;
|
||||
|
||||
}
|
||||
|
||||
if(packetType == PacketType::TYPE_NEEDUPDATE)
|
||||
{ //нужно обновление
|
||||
|
||||
bool flag = false;
|
||||
quint64 size = 0;
|
||||
quint64 fileCount = 0;
|
||||
quint64 fileDelete = 0;
|
||||
|
||||
stream.startTransaction();
|
||||
stream >> flag;
|
||||
stream >> size;
|
||||
stream >> fileCount;
|
||||
stream >> fileDelete;
|
||||
|
||||
if(!stream.commitTransaction()){
|
||||
continue;
|
||||
}
|
||||
|
||||
emit sigNeedUpdate(flag,size,fileCount,fileDelete);
|
||||
packetType = PacketType::TYPE_NONE;
|
||||
}
|
||||
|
||||
if(packetType == PacketType::TYPE_XMLANSWER){ //ответы формата XML
|
||||
QByteArray array;
|
||||
stream.startTransaction();
|
||||
stream >> array;
|
||||
|
||||
if(!stream.commitTransaction()){
|
||||
continue;
|
||||
}
|
||||
|
||||
dataParser->xmlParser(array);
|
||||
|
||||
packetType = PacketType::TYPE_NONE;
|
||||
}
|
||||
|
||||
if(packetType == PacketType::HASH_READY)
|
||||
{
|
||||
emit sigCheckUpdate();
|
||||
}
|
||||
|
||||
if(packetType == PacketType::RECALCULATE_HASH)
|
||||
{
|
||||
emit sigdRecalculateHashOnServerState();
|
||||
}
|
||||
|
||||
if(packetType == PacketType::BUSY)
|
||||
{
|
||||
emit sigAnimationActivated(true);
|
||||
}
|
||||
|
||||
if(packetType == PacketType::FREE)
|
||||
{
|
||||
emit sigAnimationActivated(false);
|
||||
}
|
||||
|
||||
if (packetType == PacketType::TYPE_XMLANSWER_DOCS_CHANGED) //на случай общего обновления
|
||||
{
|
||||
if (client->getIsLoggedIn())
|
||||
case PacketType::TYPE_NEEDUPDATE: //нужно обновление
|
||||
{
|
||||
bool flag = false;
|
||||
quint64 size = 0;
|
||||
quint64 fileCount = 0;
|
||||
quint64 fileDelete = 0;
|
||||
|
||||
stream.startTransaction();
|
||||
stream >> flag;
|
||||
stream >> size;
|
||||
stream >> fileCount;
|
||||
stream >> fileDelete;
|
||||
|
||||
if(!stream.commitTransaction())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
emit sigNeedUpdate(flag,size,fileCount,fileDelete);
|
||||
}
|
||||
break;
|
||||
|
||||
case PacketType::TYPE_XMLANSWER: //ответы формата XML
|
||||
{
|
||||
QByteArray array;
|
||||
stream.startTransaction();
|
||||
stream >> array;
|
||||
|
||||
if(!stream.commitTransaction())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
dataParser->xmlParser(array);
|
||||
}
|
||||
break;
|
||||
|
||||
case PacketType::HASH_READY:
|
||||
{
|
||||
emit sigCheckUpdate();
|
||||
}
|
||||
break;
|
||||
|
||||
case PacketType::RECALCULATE_HASH:
|
||||
{
|
||||
emit sigdRecalculateHashOnServerState();
|
||||
}
|
||||
break;
|
||||
|
||||
case PacketType::BUSY:
|
||||
{
|
||||
emit sigAnimationActivated(true);
|
||||
}
|
||||
break;
|
||||
|
||||
case PacketType::FREE:
|
||||
{
|
||||
emit sigAnimationActivated(false);
|
||||
}
|
||||
break;
|
||||
|
||||
case PacketType::TYPE_XMLANSWER_DOCS_CHANGED: //на случай общего обновления
|
||||
{
|
||||
if (client->getIsLoggedIn())
|
||||
{
|
||||
emit sigSendPacketType(PacketType::GET_DOCS);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case PacketType::UPDATE_FILE_COMPLETE:
|
||||
{
|
||||
postProcessorSystem->calculateCommonHash();
|
||||
emit sigLoadComplete();
|
||||
emit sigSendPacketType(PacketType::GET_DOCS);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
if (packetType == PacketType::UPDATE_FILE_COMPLETE)
|
||||
{
|
||||
postProcessorSystem->calculateCommonHash();
|
||||
emit sigLoadComplete();
|
||||
emit sigSendPacketType(PacketType::GET_DOCS);
|
||||
packetType = PacketType::TYPE_NONE;
|
||||
default:
|
||||
qCritical() << "RecognizeSystem::recognize packetType unknown!";
|
||||
}
|
||||
|
||||
packetType = PacketType::TYPE_NONE;
|
||||
|
||||
@@ -14,6 +14,8 @@
|
||||
#include <Data\streamingversiondata.h>
|
||||
#include <Core\hashcomparer.h>
|
||||
|
||||
const int BLOCK_SIZE = 1024 * 1024; // Размер блока
|
||||
|
||||
class RecognizeSystem : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
@@ -21,8 +23,10 @@ class RecognizeSystem : public QObject
|
||||
public:
|
||||
explicit RecognizeSystem(QObject *parent = 0);
|
||||
~RecognizeSystem(){};
|
||||
|
||||
void initialize(DataParser *dataParser, VersionContainer* versionContainer,PostProcessorSystem *postProcessorSystem,Client *client);
|
||||
void recognize(QTcpSocket *socket);
|
||||
|
||||
signals:
|
||||
void sigUpdateBytesAvailable();
|
||||
void sigLoadComplete();
|
||||
@@ -40,13 +44,13 @@ private:
|
||||
VersionContainer *versionContainer;
|
||||
DataParser *dataParser;
|
||||
PostProcessorSystem *postProcessorSystem;
|
||||
PacketType packetType;
|
||||
QString filePath;
|
||||
QByteArray tmpBlock;
|
||||
Client *client;
|
||||
|
||||
qint64 sizeReceiveData;
|
||||
PacketType packetType;
|
||||
QString filePath;
|
||||
qint64 fileSize;
|
||||
qint64 sizeReceiveData;
|
||||
QByteArray tmpBlock;
|
||||
int countSend;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user