Files
RRJClient/Core/recognizesystem.cpp
2024-08-14 10:02:09 +03:00

244 lines
7.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "Core/recognizesystem.h"
RecognizeSystem::RecognizeSystem(QObject *parent):
QObject(parent)
{
packetType = PacketType::TYPE_NONE;
filePath.clear();
fileSize = 0;
message.clear();
sizeReceiveData = 0;
tmpBlock.clear();
countSend = 0;
}
RecognizeSystem::~RecognizeSystem()
{
}
void RecognizeSystem::Initialize(UpdateController *updateController)
{
this->updateController = updateController;
}
void RecognizeSystem::SetSocket(QTcpSocket *socket)
{
this->socket = socket;
}
void RecognizeSystem::Recognize()
{
QDataStream stream(socket);
stream.setVersion(QDataStream::Qt_DefaultCompiledVersion);
while(socket->bytesAvailable())
{
if(packetType == PacketType::TYPE_NONE){ //определение первичного пакета
stream.startTransaction();
stream >> packetType;
if(!stream.commitTransaction()){
emit onSendDebugLog(Tools::GetTime() + " CLIENT: packetType - FAIL commitTransaction");
if(socket->waitForReadyRead(TCP_READ_TIMEOUT)){
emit onSendDebugLog("ERROR: PACKET TYPE READ TIMEOUT");
return;
}
continue;
}
//qDebug() << Tools::GetTime() << "CLIENT: type: " << packetType;
}
if(packetType == PacketType::TYPE_MSG){ //поведение на тип пакета с сообщением
stream.startTransaction();
stream >> message;
if(!stream.commitTransaction()){
emit onSendDebugLog(Tools::GetTime() + "CLIENT: Message - FAIL commitTransaction");
if(socket->waitForReadyRead(TCP_READ_TIMEOUT)){
emit onSendDebugLog(Tools::GetTime() + "ERROR: MESSAGE READ TIMEOUT");
return;
}
continue;
}
emit onSendDebugLog(Tools::GetTime() + " CLIENT: Message from server " + message); //результат обрабатывать тут?
packetType = PacketType::TYPE_NONE;
continue;
}
if(packetType == PacketType::TYPE_FOLDER){ //создание папок
stream.startTransaction();
stream >> filePath;
if(!stream.commitTransaction()){
continue;
}
filePath = Tools::CreateFullPath(filePath);
QDir dir(filePath);
if(!dir.exists()){
if(dir.mkpath(filePath)){
qDebug() << "Dir Created";
}
}
packetType = PacketType::TYPE_NONE;
continue;
}
if(packetType == PacketType::TYPE_FILE) //загрузка файлов
{
//ПОЛУЧЕНИЕ ПУТИ
//ПОЛУЧЕНИЕ РАЗМЕРА ФАЙЛА
forever
{
stream.startTransaction();
stream >> filePath;
stream >> fileSize;
if(!stream.commitTransaction()){
emit onSendDebugLog(Tools::GetTime() + "CLIENT: filePath, fileSize - FAIL commitTransaction");
if (!socket->waitForReadyRead(TCP_READ_TIMEOUT)) {
emit onSendDebugLog(Tools::GetTime() + "CLIENT: ERROR! readyRead timeout - filePath, fileSize!!!");
return;
}
continue;
}
filePath = Tools::CreateFullPath(filePath);
emit onSendDebugLog("CLIENT: filesize: " + QString::number(fileSize));
emit onSendDebugLog("CLIENT: filePath: " + filePath);
socket->waitForReadyRead(100);
break;
}
//ПОЛУЧЕНИЕ САМОГО ФАЙЛА
emit onSendDebugLog(Tools::GetTime() + "AfterRead size and path BytesAvailable: " + socket->bytesAvailable());
//УКАЗАНИЕ ПУТИ ФАЙЛА
QFile file(filePath);
if (file.exists())
{
file.remove(); //удаление файла, если он уже есть, но необходимо обновить
emit onSendDebugLog(Tools::GetTime() + "Delete exist file: " + filePath);
}
file.open(QFile::Append);
forever
{
stream.startTransaction();
stream >> tmpBlock;
if(!stream.commitTransaction()){
//qDebug() << Tools::GetTime() << "CLIENT: tmpBlock - FAIL";
if(socket->waitForReadyRead(TCP_READ_TIMEOUT)){
continue;
}
continue;
}
quint64 toFile = file.write(tmpBlock);
emit onSendDebugLog(Tools::GetTime() + "CLIENT: toFile :" + toFile);
sizeReceiveData += toFile;
countSend++;
emit UpdateBytesAvailable(fileSize,sizeReceiveData);
tmpBlock.clear();
if(sizeReceiveData == fileSize){
emit onSendDebugLog(Tools::GetTime() + "FINAL Count send: " + QString::number(countSend));
emit onSendDebugLog(Tools::GetTime() + "FINAL Size received: " + QString::number(sizeReceiveData));
emit onSendDebugLog(Tools::GetTime() + "FINAL File size" + QString::number(fileSize));
break;
}
}
file.close();
emit onSendDebugLog(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::CreateFullPath(filePath);
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;
}
packetType = PacketType::TYPE_NONE;
continue;
}
if(packetType ==PacketType::TYPE_FINISH){ //для повторного создания хэша после загрузки
updateController->CalculateHash();
emit LoadComplete();
packetType = PacketType::TYPE_NONE;
}
if(packetType == PacketType::TYPE_NEEDUPDATE){
bool flag;
stream.startTransaction();
stream >> flag;
emit onNeedUpdate(flag);
packetType = PacketType::TYPE_NONE;
}
packetType = PacketType::TYPE_NONE;
}
}