From 594aa8311e5313db4625a70e6802b3d149067d94 Mon Sep 17 00:00:00 2001 From: krivoshein Date: Wed, 18 Feb 2026 17:20:20 +0300 Subject: [PATCH 1/4] sendFileBlock_V3 --- LibServer/Systems/recognizesystem.cpp | 7 +- LibServer/Systems/recognizesystem.h | 3 +- LibServer/Systems/sendsystem.cpp | 333 +++++++++++++++--- LibServer/Systems/sendsystem.h | 28 +- LibServer/Systems/tools.h | 3 +- LibServer/clienthandler/clienthandler.cpp | 16 +- LibServer/clienthandler/clienthandler.h | 12 +- .../multithreadserver/multithreadserver.cpp | 2 + .../multithreadserver/multithreadserver.h | 10 +- 9 files changed, 342 insertions(+), 72 deletions(-) diff --git a/LibServer/Systems/recognizesystem.cpp b/LibServer/Systems/recognizesystem.cpp index 2f1ee30..67d7a26 100644 --- a/LibServer/Systems/recognizesystem.cpp +++ b/LibServer/Systems/recognizesystem.cpp @@ -1,7 +1,8 @@ #include "recognizesystem.h" RecognizeSystem::RecognizeSystem(QObject *parent): -QObject(parent) +QObject(parent), +globalMutex(nullptr) { packetType = PacketType::TYPE_NONE; filePath.clear(); @@ -13,7 +14,7 @@ QObject(parent) } -void RecognizeSystem::initialize(UpdateController *updateController,DataParser* dataParser,SendSystem *sendSystem, ClientHandler *handler) +void RecognizeSystem::initialize(UpdateController *updateController,DataParser* dataParser, QMutex *globalMutex,SendSystem *sendSystem, ClientHandler *handler) { qDebug() << "RecognizeSystem::initialize thread ID " << QThread::currentThreadId(); @@ -22,6 +23,7 @@ void RecognizeSystem::initialize(UpdateController *updateController,DataParser* this->clientHandler = handler; this->sendSystem = sendSystem; socket = handler->getSocket(); + this->globalMutex = globalMutex; connect(this,&RecognizeSystem::sigCalculateHash,updateController,&UpdateController::calculateFullHashWithSetup,Qt::AutoConnection); connect(this,&RecognizeSystem::sigChangeVersion,updateController,&UpdateController::changeAssetVersion,Qt::AutoConnection); @@ -453,6 +455,7 @@ void RecognizeSystem::recognize() if(packetType == PacketType::GET_DOCS) { emit sigSendDocs(updateController->getPathAdditionalFile(tasksAMMfileName)); + //globalMutex->unlock(); } if (packetType == PacketType::SEND_HASH) diff --git a/LibServer/Systems/recognizesystem.h b/LibServer/Systems/recognizesystem.h index 5dda0e6..f450e99 100644 --- a/LibServer/Systems/recognizesystem.h +++ b/LibServer/Systems/recognizesystem.h @@ -22,7 +22,7 @@ class RecognizeSystem : public QObject public: RecognizeSystem(QObject *parent = nullptr); - void initialize(UpdateController *updateController,DataParser *dataParser, SendSystem *sendSystem, ClientHandler *handler); + void initialize(UpdateController *updateController,DataParser *dataParser, QMutex *globalMutex, SendSystem *sendSystem, ClientHandler *handler); void recognize(); ~RecognizeSystem(); @@ -47,6 +47,7 @@ private: QString filePath; ClientHandler *clientHandler; QMutex *mutex; + QMutex *globalMutex; QTcpSocket *socket; diff --git a/LibServer/Systems/sendsystem.cpp b/LibServer/Systems/sendsystem.cpp index 86027d9..a464be1 100644 --- a/LibServer/Systems/sendsystem.cpp +++ b/LibServer/Systems/sendsystem.cpp @@ -2,26 +2,53 @@ SendSystem::SendSystem(QObject *parent) : QObject(parent) { - isSendStopped = false; + qDebug() << "SendSystem init thread ID " << QThread::currentThreadId(); + + client = nullptr; + socket = nullptr; + dataParser = nullptr; + + globalMutex = nullptr; + waitCondition = nullptr; + + buffer = nullptr; + + type = TypeClientAutorization::TYPE_GUI; + + isSendStopped = false; + flWaitWriten = false; + + fileSize = 0; + countSend = 0; + + buffer = new char[sendFileBlockSize]; } +SendSystem::~SendSystem() +{ + delete buffer; +} + + void SendSystem::initialize(DataParser *dataParser,QMutex *globalMutex) { qDebug() << "SendSystem::initialize thread ID " << QThread::currentThreadId(); this->dataParser = dataParser; - //connect(this,&SendSystem::sigSendXMLmessage,dataParser->ClientAnswer(),&ClientAnswerParser::message,Qt::AutoConnection); //сигнал не используется - connect(this,&SendSystem::sigSendNotify,dataParser->ClientAnswer(),&ClientAnswerParser::notify,Qt::DirectConnection); //потому что возвращает значение - //connect(this,&SendSystem::sigSendVersion,dataParser->ClientAnswer(),&ClientAnswerParser::currentVersion,Qt::AutoConnection); //сигнал не используется + this->globalMutex = globalMutex; - mutex = globalMutex; + connect(this,&SendSystem::sigSendNotify,dataParser->ClientAnswer(),&ClientAnswerParser::notify,Qt::DirectConnection); //потому что возвращает значение } void SendSystem::setClient(Client *client,QTcpSocket *socket) { - this->socket = socket; - this->type = client->getClientType(); + qDebug() << "SendSystem::setClient thread ID " << QThread::currentThreadId(); + + this->socket = socket; this->client = client; + + this->type = client->getClientType(); + isSendStopped = false; } @@ -35,50 +62,239 @@ void SendSystem::sendNotify(QString notify) void SendSystem::sendFileBlock(QString path) { - QFile file(path); - QFileInfo fileInfo(file); + //qDebug() << "SendSystem::sendFileBlock path: " << path; - if(isSendStopped) + if(getIsSendStopped()) { //Поведение на случай отключения клиента - file.close(); - Logger::instance().log("UNLOCK STOP MUTEX : " + client->getLogin(),LogLevel::ERROR); + Logger::instance().log("Client: " + client->getLogin() + " isSendStopped", LogLevel::ERROR); return; } - QDataStream stream(socket); - stream.setVersion(QDataStream::Qt_DefaultCompiledVersion); + QFile file(path); + QFileInfo fileInfo(file); fileSize = file.size(); if (fileSize == 0) { - Logger::instance().log("Client: " + client->getLogin() + " WARNING! Zero size " + fileInfo.fileName(),LogLevel::ERROR); - Logger::instance().log(path,LogLevel::ERROR); + Logger::instance().log("Client: " + client->getLogin() + " ERROR! File zero size " + fileInfo.fileName(), LogLevel::ERROR); + Logger::instance().log(path, LogLevel::ERROR); return; } + if(!file.open(QFile::ReadOnly)) + { + Logger::instance().log("Client: " + client->getLogin() + " ERROR! File not open: " + fileInfo.fileName(), LogLevel::ERROR); + Logger::instance().log(path, LogLevel::ERROR); + return; + } + + countSend = 0; + + QDataStream stream(socket); + stream.setVersion(QDataStream::Qt_DefaultCompiledVersion); + stream << PacketType::TYPE_FILE; //Отправляем тип блока stream << path << fileSize; - if(file.open(QFile::ReadOnly)) - { - while(!file.atEnd()) - { - QByteArray data = file.read(sendFileBlockSize); - stream << data; - //socket->waitForBytesWritten(10); + waitWrittenData("sendFileBlock:header " + fileInfo.fileName()); - if(socket->state() == QAbstractSocket::UnconnectedState) break; - countSend++; + while(!file.atEnd()) + { + QByteArray data = file.read(sendFileBlockSize); + stream << data; + + waitWrittenData(QString("sendFileBlock:data (size %1) ").arg(data.size()) + fileInfo.fileName()); + + if(socket->state() == QAbstractSocket::UnconnectedState) + { + Logger::instance().log("Client: " + client->getLogin() + " UnconnectedState", LogLevel::ERROR); + break; } - //emit sigSendToLogger(Tools::getTime() + " send file " + fileInfo.fileName()); + countSend++; } file.close(); countSend = 0; } +void SendSystem::sendFileBlock_V2(QString path) +{ + QFile file(path); + if(!file.open(QIODevice::ReadOnly)) + { + Logger::instance().log("ERROR open file: " + path, LogLevel::ERROR); + return; + } + + countSend = 0; + fileSize = file.size(); + + if (fileSize == 0) + { + Logger::instance().log("Client: " + client->getLogin() + " WARNING! Zero size " +file.fileName(), LogLevel::ERROR); + Logger::instance().log(path, LogLevel::ERROR); + return; + } + + QString type = QString::number(PacketType::TYPE_FILE); + QString fileSizeStr = QString::number(fileSize); + + qDebug() << "SendSystem::sendFileBlock_V2 file " << file.fileName() << " size " << fileSizeStr; + + QDataStream stream(socket); + stream.setVersion(QDataStream::Qt_DefaultCompiledVersion); + + + + //mutexWriten.lock(); + //flWaitWriten = true; + + stream << PacketType::TYPE_FILE; //Отправляем тип блока + stream << path << fileSize; + + socket->waitForBytesWritten(10); + + /* + while(flWaitWriten) + { + QCoreApplication::processEvents(); // Обеспечиваем обработку сообщений + } + */ + //socket->waitForBytesWritten(10); + + //socket->write(type.toUtf8()); + //socket->write(fileSizeStr.toUtf8()); + + connect(this->socket, &QTcpSocket::bytesWritten, this, &SendSystem::slot_BytesWrittenToSocket); + + + while(true) + { + if(socket->state() == QAbstractSocket::UnconnectedState) + break; + + countSend++; + + qint64 readBytes = file.read(buffer, /*sizeof(buffer)*/sendFileBlockSize); + if(readBytes <= 0) + break; + + flWaitWriten = true; + while(!socket->write(buffer, readBytes)) + { + int i = 0; + i++; + } + while(flWaitWriten) + { + QCoreApplication::processEvents(); // Обеспечиваем обработку сообщений + } + /* + while(!socket->flush()) + { + int i = 0; + i++; + } + */ + // Завершаем отправку + //socket->flush(); + //socket->waitForBytesWritten(10); + } + + disconnect(this->socket, &QTcpSocket::bytesWritten, this, &SendSystem::slot_BytesWrittenToSocket); + + //socket->flush(); + /* + while(!socket->flush()) + { + int i = 0; + i++; + }*/ + + + file.close(); +} + +void SendSystem::sendFileBlock_V3(QString path) +{ + //qDebug() << "SendSystem::sendFileBlock path: " << path; + + if(getIsSendStopped()) + { //Поведение на случай отключения клиента + Logger::instance().log("Client: " + client->getLogin() + " isSendStopped", LogLevel::ERROR); + return; + } + + QFile file(path); + QFileInfo fileInfo(file); + + fileSize = file.size(); + + if (fileSize == 0) + { + Logger::instance().log("Client: " + client->getLogin() + " ERROR! File zero size " + fileInfo.fileName(), LogLevel::ERROR); + Logger::instance().log(path, LogLevel::ERROR); + return; + } + + if(!file.open(QFile::ReadOnly)) + { + Logger::instance().log("Client: " + client->getLogin() + " ERROR! File not open: " + fileInfo.fileName(), LogLevel::ERROR); + Logger::instance().log(path, LogLevel::ERROR); + return; + } + + countSend = 0; + + QDataStream stream(socket); + stream.setVersion(QDataStream::Qt_DefaultCompiledVersion); + + stream << PacketType::TYPE_FILE; //Отправляем тип блока + stream << path << fileSize; + + waitWrittenData("sendFileBlock:header " + fileInfo.fileName()); + + connect(this->socket, &QTcpSocket::bytesWritten, this, &SendSystem::slot_BytesWrittenToSocket); + + while(!file.atEnd()) + { + if(socket->state() == QAbstractSocket::UnconnectedState) + { + Logger::instance().log("Client: " + client->getLogin() + " UnconnectedState", LogLevel::ERROR); + break; + } + + qint64 readBytes = file.read(buffer, /*sizeof(buffer)*/sendFileBlockSize); + if(readBytes <= 0) + break; + + flWaitWriten = true; + while(!socket->write(buffer, readBytes)) + { + int i = 0; + i++; + qCritical() << "socket->write ERROR. size " + QString::number(readBytes); + } + while(flWaitWriten) + { + QCoreApplication::processEvents(); // Обеспечиваем обработку сообщений + int i = 0; + i++; + } + + waitWrittenData(QString("sendFileBlock:data (readBytes %1, num %2) ").arg(QString::number(readBytes), QString::number(countSend)) + fileInfo.fileName()); + + countSend++; + } + + disconnect(this->socket, &QTcpSocket::bytesWritten, this, &SendSystem::slot_BytesWrittenToSocket); + + file.close(); + countSend = 0; +} + void SendSystem::sendFileBlockByteArray(QByteArray array, PacketType packetType) { if(client->getClientType() == TypeClientAutorization::TYPE_QT_CLIENT || @@ -99,11 +315,15 @@ void SendSystem::sendFileBlockByteArray(QByteArray array, PacketType packetType) stream << packetType; //Отправляем тип блока stream << size; + socket->waitForBytesWritten(10); + while (bytesSended < size) { QByteArray chunk = array.mid(bytesSended,sendFileBlockSize); stream << chunk; + socket->waitForBytesWritten(10); + bytesSended += chunk.length(); } } @@ -140,6 +360,11 @@ void SendSystem::sendVersion() sendXmlAnswer(data); } +void SendSystem::slot_BytesWrittenToSocket(qint64 bytes) +{ + flWaitWriten = false; +} + void SendSystem::sendFileBlockWithRename(QString path, QString newName) { qDebug() << "SendSystem::sendFileBlockWithRename thread ID " << QThread::currentThreadId(); @@ -162,6 +387,8 @@ void SendSystem::sendFileBlockWithRename(QString path, QString newName) stream << pathForSend << fileSize; + socket->waitForBytesWritten(10); + if(isSendStopped) { //Поведение на случай отключения клиента file.close(); @@ -169,13 +396,13 @@ void SendSystem::sendFileBlockWithRename(QString path, QString newName) } - socket->waitForBytesWritten(); + //socket->waitForBytesWritten(); if(file.open(QFile::ReadOnly)){ while(!file.atEnd()){ QByteArray data = file.read(sendFileBlockSize); stream << data; - socket->waitForBytesWritten(); + socket->waitForBytesWritten(10); countSend++; } @@ -184,8 +411,8 @@ void SendSystem::sendFileBlockWithRename(QString path, QString newName) file.close(); countSend = 0; - socket->waitForBytesWritten(); - socket->waitForReadyRead(100); + socket->waitForBytesWritten(10); + //socket->waitForReadyRead(100); sendNotify(commandHashCompleteClient); } @@ -197,6 +424,8 @@ void SendSystem::sendFolderBlock(QString path) stream << PacketType::TYPE_FOLDER; stream << path; + + waitWrittenData("sendFolderBlock"); } void SendSystem::sendDeleteBlock(QString path) @@ -206,8 +435,8 @@ void SendSystem::sendDeleteBlock(QString path) stream << PacketType::TYPE_DELETE; stream << path; - qDebug() << "DELETE: " + path; - socket->waitForReadyRead(10); + + waitWrittenData("sendDeleteBlock"); } void SendSystem::sendPacketType(PacketType packetType) @@ -216,11 +445,11 @@ void SendSystem::sendPacketType(PacketType packetType) if (client->getClientType() == TypeClientAutorization::TYPE_QT_CLIENT || client->getClientType() == TypeClientAutorization::TYPE_GUI) { - QDataStream stream(socket); stream.setVersion(QDataStream::Qt_DefaultCompiledVersion); stream << packetType; - socket->waitForReadyRead(100); + + waitWrittenData("sendPacketType"); } else { @@ -247,7 +476,8 @@ void SendSystem::sendXmlAnswer(QByteArray array, PacketType packetType) stream << packetType; stream << array; - socket->waitForBytesWritten(); + + waitWrittenData("sendXmlAnswer"); } else { @@ -269,11 +499,14 @@ void SendSystem::sendNeedUpdate(bool flag,quint64 size,quint64 fileCount,quint64 stream << size; stream << fileCount; stream << deleteCount; + + waitWrittenData("sendNeedUpdate"); } -void SendSystem::updateFiles(QList fileSendList, QList deleteList){ +void SendSystem::updateFiles(QList fileSendList, QList deleteList) +{ + //globalMutex->lock(); - QMutexLocker locker(mutex); QListIterator clientIterator(deleteList); while(clientIterator.hasNext()) @@ -283,6 +516,8 @@ void SendSystem::updateFiles(QList fileSendList, QList delet sendDeleteBlock(data.path); if(getIsSendStopped()) { + Logger::instance().log("Client: " + client->getLogin() + " isSendStopped", LogLevel::ERROR); + //globalMutex->unlock(); return; } } @@ -296,20 +531,22 @@ void SendSystem::updateFiles(QList fileSendList, QList delet if (data.hash == "FOLDER") { sendFolderBlock(data.path); - socket->waitForBytesWritten(); } else { - sendFileBlock(data.path); - socket->waitForBytesWritten(); + sendFileBlock_V3(data.path); } - if(isSendStopped) + if(getIsSendStopped()) { + //globalMutex->unlock(); return; } } + sendPacketType(PacketType::UPDATE_FILES_COMPLETE); + + //globalMutex->unlock(); } @@ -333,7 +570,17 @@ bool SendSystem::getIsSendStopped() const return isSendStopped; } -SendSystem::~SendSystem() +bool SendSystem::waitWrittenData(QString marker, int msec) { - + bool success = socket->waitForBytesWritten(msec); + if(success) + { + //qInfo() << "Data sended OK. <" + marker + ">"; + } + else + { + qCritical() << "Data sended ERROR. <" + marker + ">"; + } + return success; } + diff --git a/LibServer/Systems/sendsystem.h b/LibServer/Systems/sendsystem.h index 30d79e5..5dad19a 100644 --- a/LibServer/Systems/sendsystem.h +++ b/LibServer/Systems/sendsystem.h @@ -19,11 +19,15 @@ class SendSystem : public QObject Q_OBJECT public: explicit SendSystem(QObject* parent = nullptr); + ~SendSystem(); void initialize(DataParser* dataParser,QMutex *globalMutex); void setClient(Client* client,QTcpSocket *socket); + void sendMessageBlock(QString message); void sendFileBlock(QString path); + void sendFileBlock_V2(QString path); + void sendFileBlock_V3(QString path); void sendFileBlockByteArray(QByteArray array, PacketType packetType); void sendFileBlockWithRename(QString path,QString newName); void sendFolderBlock(QString path); @@ -34,18 +38,21 @@ public: void sendDocs(QString docPath); void sendXmlAnswer(QByteArray array, PacketType packetType = PacketType::TYPE_XMLANSWER); void sendNeedUpdate(bool flag,quint64 size,quint64 fileCount,quint64 deleteCount); + void updateFiles(QList fileSendList, QList deleteList); bool getIsSendStopped() const; - ~SendSystem(); - - void updateFilesFULL(QList fileSendList, QList deleteList); + + bool waitWrittenData(QString marker, int msec = 500); + public slots: void socketClose(); void sendVersion(); + void slot_BytesWrittenToSocket(qint64 bytes); + signals: void sigLoadHash(); //QByteArray sigSendXMLmessage(QString loginFrom, QString loginTo, QString text, QString userType); //сигнал не используется @@ -56,12 +63,19 @@ private: Client *client; QTcpSocket* socket; DataParser* dataParser; - quint64 fileSize; - QMutex *mutex; + + QMutex *globalMutex; QWaitCondition *waitCondition; - int countSend; - bool isSendStopped; + + char* buffer; + TypeClientAutorization type; + + bool isSendStopped; + bool flWaitWriten; + + quint64 fileSize; + int countSend; }; #endif // SENDSYSTEM_H diff --git a/LibServer/Systems/tools.h b/LibServer/Systems/tools.h index faf611b..b264f10 100644 --- a/LibServer/Systems/tools.h +++ b/LibServer/Systems/tools.h @@ -57,7 +57,8 @@ static const QString commandGetTasks = "GETTASKS"; static const QString commandeGetOfflineMessages = "GETOFFLINEMESSAGE"; //static quint64 fileBlockSize = 1460; -static quint64 sendFileBlockSize = 256000; +const int BLOCK_SIZE = 1024 * 1024; // Размер блока +static quint64 sendFileBlockSize = /*256000*/BLOCK_SIZE; static quint64 readFileBlockSize = 65535; //1025*250 diff --git a/LibServer/clienthandler/clienthandler.cpp b/LibServer/clienthandler/clienthandler.cpp index 4eeb7e1..7f4eb4c 100644 --- a/LibServer/clienthandler/clienthandler.cpp +++ b/LibServer/clienthandler/clienthandler.cpp @@ -14,14 +14,15 @@ ClientHandler::ClientHandler( QObject *parent): void ClientHandler::initialize(int descriptor, UpdateController *updateController,DataParser *dataParser, QMutex *mutex) { - this->socket = new QTcpSocket; - this->thread = new QThread; + qDebug() << "ClientHandler::initialize thread ID " << QThread::currentThreadId(); + socket = new QTcpSocket; + thread = new QThread; + + QIODevice::OpenMode openMode = socket->openMode(); socket->setParent(nullptr); socket->setSocketDescriptor(descriptor); - qDebug() << "ClientHandler::initialize thread ID " << QThread::currentThreadId(); - sendSystem = new SendSystem; recognizeSystem = new RecognizeSystem; @@ -42,7 +43,7 @@ void ClientHandler::initialize(int descriptor, connect(recognizeSystem,&RecognizeSystem::signal_updateDocsXML,this,&ClientHandler::signal_updateDocsXML); connect(this,&ClientHandler::sigSendXmlAnswer,sendSystem,&SendSystem::sendXmlAnswer,Qt::AutoConnection); - connect(this,&ClientHandler::sigInitSender,sendSystem,&SendSystem::initialize,Qt::AutoConnection/*Qt::DirectConnection*/); + //connect(this,&ClientHandler::sigInitSender,sendSystem,&SendSystem::initialize,Qt::AutoConnection/*Qt::DirectConnection*/); connect(this,&ClientHandler::sigFileBlock,sendSystem,&SendSystem::sendFileBlock,Qt::AutoConnection); connect(this,&ClientHandler::sigFileBlockByteArray,sendSystem,&SendSystem::sendFileBlockByteArray,Qt::AutoConnection); connect(this,&ClientHandler::sigFolderBlock,sendSystem,&SendSystem::sendFolderBlock,Qt::AutoConnection); @@ -60,9 +61,10 @@ void ClientHandler::initialize(int descriptor, connect(socket,&QTcpSocket::readyRead,this,&ClientHandler::initClientType,Qt::AutoConnection); initClientType(); - recognizeSystem->initialize(updateController,dataParser,sendSystem, this); + recognizeSystem->initialize(updateController,dataParser, mutex,sendSystem, this); sendSystem->setClient(client,socket); - emit sigInitSender(dataParser, mutex); + sendSystem->initialize(dataParser, mutex); + //emit sigInitSender(dataParser, mutex); Logger::instance().log("SERVER: Client connected"); } diff --git a/LibServer/clienthandler/clienthandler.h b/LibServer/clienthandler/clienthandler.h index f7c08f5..491a311 100644 --- a/LibServer/clienthandler/clienthandler.h +++ b/LibServer/clienthandler/clienthandler.h @@ -51,7 +51,7 @@ public: signals: void sigSendXmlAnswer(QByteArray array, PacketType packetType); - void sigInitSender (DataParser *dataParse, QMutex *mutex); + //void sigInitSender (DataParser *dataParse, QMutex *mutex); void sigFolderBlock(QString path); void sigFileBlock(QString path); void sigFileBlockByteArray(QByteArray array, PacketType packetType); @@ -71,6 +71,10 @@ signals: void signal_updateDocsXML(); +private: + void initClientType(); + void packetTypeInit(PacketType packet, Client *client); + public : QThread *thread; QTcpSocket *socket; @@ -82,11 +86,7 @@ private: UpdateController *updateController; RecognizeSystem *recognizeSystem; Client *client; - - SendSystem *sendSystem; - - void initClientType(); - void packetTypeInit(PacketType packet, Client *client); + SendSystem *sendSystem; }; #endif // CLIENTHANDLER_H diff --git a/LibServer/multithreadserver/multithreadserver.cpp b/LibServer/multithreadserver/multithreadserver.cpp index 84d1541..dccca63 100644 --- a/LibServer/multithreadserver/multithreadserver.cpp +++ b/LibServer/multithreadserver/multithreadserver.cpp @@ -4,6 +4,7 @@ MultiThreadServer::MultiThreadServer(UpdateController *updateController, DocsUpd DataParser *dataParser,qint16 hostPort, QObject *parent ): QTcpServer(parent), mutex(nullptr), + clientsMap(nullptr), hostPort(hostPort), processingSystem(processingSystem), updateController(updateController), @@ -23,6 +24,7 @@ MultiThreadServer::MultiThreadServer(UpdateController *updateController, DocsUpd MultiThreadServer::~MultiThreadServer() { delete mutex; + delete clientsMap; } void MultiThreadServer::incomingConnection(qintptr socketDesriptor) diff --git a/LibServer/multithreadserver/multithreadserver.h b/LibServer/multithreadserver/multithreadserver.h index 8bf4771..766e8ac 100644 --- a/LibServer/multithreadserver/multithreadserver.h +++ b/LibServer/multithreadserver/multithreadserver.h @@ -33,7 +33,7 @@ public: { return stateBlockAutorization; } - EStateServer getStateServer() + EStateServer getStateServer() const { return stateServer; } @@ -58,6 +58,9 @@ private: emit signal_StateServer(stateServer, stateBlockAutorization); } + void removeClient(int idSocket); + void addClient(qintptr descriptor, ClientHandler *client); + signals: //void sigInitClient(int descriptor, ServerLMSWidget *serverWidget, // UpdateController *updateController, DataParser *dataParser, QMutex *mutex); @@ -95,10 +98,7 @@ private: EStateServer stateServer; EStateBlockAutorization stateBlockAutorization; - QMap blockersMap; - - void removeClient(int idSocket); - void addClient(qintptr descriptor, ClientHandler *client); + QMap blockersMap; }; #endif // MULTITHREADSERVER_H From a1f3e04fad8e3575687c7eb412d697b7ea114a32 Mon Sep 17 00:00:00 2001 From: krivoshein Date: Thu, 19 Feb 2026 12:26:46 +0300 Subject: [PATCH 2/4] refact --- LibServer/Systems/recognizesystem.cpp | 8 +- LibServer/Systems/sendsystem.cpp | 569 +++++++++------------- LibServer/Systems/sendsystem.h | 65 ++- LibServer/clienthandler/clienthandler.cpp | 26 +- 4 files changed, 274 insertions(+), 394 deletions(-) diff --git a/LibServer/Systems/recognizesystem.cpp b/LibServer/Systems/recognizesystem.cpp index 478d108..56671cf 100644 --- a/LibServer/Systems/recognizesystem.cpp +++ b/LibServer/Systems/recognizesystem.cpp @@ -31,7 +31,7 @@ void RecognizeSystem::initialize(UpdateController *updateController,DataParser* connect(this,&RecognizeSystem::sigCopyVersion,updateController,&UpdateController::createCopyVersion,Qt::AutoConnection); connect(this,&RecognizeSystem::sigXmlParser,dataParser->getProcessParser(),&ProcessParser::slot_read,Qt::AutoConnection); //connect(this,&RecognizeSystem::sigRecalculateDocs,server,&ServerLMSWidget::slot_UpdateDocs,Qt::AutoConnection); - connect(this,&RecognizeSystem::sigSendDocs,sendSystem,&SendSystem::sendDocs,Qt::AutoConnection); + connect(this,&RecognizeSystem::sigSendDocs,sendSystem,&SendSystem::slot_sendDocs,Qt::AutoConnection); } void RecognizeSystem::recognize() @@ -405,7 +405,7 @@ void RecognizeSystem::recognize() if (updateController->checkDuplicate(result[1])) { - sendSystem->sendNotify(commandDuplicateVerName); + sendSystem->slot_sendNotify(commandDuplicateVerName); packetType = PacketType::TYPE_NONE; break; } @@ -427,11 +427,11 @@ void RecognizeSystem::recognize() if (versionName == baseNameVersion) { - sendSystem->sendNotify(commandTryBaseDelete); + sendSystem->slot_sendNotify(commandTryBaseDelete); } else if (versionName == updateController->getCurrentVersionName()) { - sendSystem->sendNotify(commandTryActiveDelete); + sendSystem->slot_sendNotify(commandTryActiveDelete); } else { diff --git a/LibServer/Systems/sendsystem.cpp b/LibServer/Systems/sendsystem.cpp index 3434b7b..84fca20 100644 --- a/LibServer/Systems/sendsystem.cpp +++ b/LibServer/Systems/sendsystem.cpp @@ -1,25 +1,20 @@ #include "sendsystem.h" -SendSystem::SendSystem(QObject *parent) : QObject(parent) +SendSystem::SendSystem(QObject *parent) : + QObject(parent), + client(nullptr), + socket(nullptr), + dataParser(nullptr), + globalMutex(nullptr), + buffer(nullptr), + type(TypeClientAutorization::TYPE_GUI), + flSendingStopped(false), + flWaitWritenToSocket(false), + bytesWritedToSocket(0), + fileSize(0), + countSend(0) { - qDebug() << "SendSystem init thread ID " << QThread::currentThreadId(); - - client = nullptr; - socket = nullptr; - dataParser = nullptr; - - globalMutex = nullptr; - waitCondition = nullptr; - - buffer = nullptr; - - type = TypeClientAutorization::TYPE_GUI; - - isSendStopped = false; - flWaitWriten = false; - - fileSize = 0; - countSend = 0; + qDebug() << "SendSystem init thread ID " << QThread::currentThreadId(); buffer = new char[sendFileBlockSize]; } @@ -30,17 +25,17 @@ SendSystem::~SendSystem() } -void SendSystem::initialize(DataParser *dataParser,QMutex *globalMutex) +void SendSystem::initialize(DataParser *dataParser, QMutex *globalMutex) { qDebug() << "SendSystem::initialize thread ID " << QThread::currentThreadId(); this->dataParser = dataParser; this->globalMutex = globalMutex; - connect(this,&SendSystem::sigSendNotify,dataParser->ClientAnswer(),&ClientAnswerParser::notify,Qt::DirectConnection); //потому что возвращает значение + connect(this, &SendSystem::sigSendNotify, dataParser->ClientAnswer(), &ClientAnswerParser::notify, Qt::DirectConnection); //потому что возвращает значение } -void SendSystem::setClient(Client *client,QTcpSocket *socket) +void SendSystem::setClient(Client *client, QTcpSocket *socket) { qDebug() << "SendSystem::setClient thread ID " << QThread::currentThreadId(); @@ -49,24 +44,193 @@ void SendSystem::setClient(Client *client,QTcpSocket *socket) this->type = client->getClientType(); - isSendStopped = false; + flSendingStopped = false; } -void SendSystem::sendNotify(QString notify) +void SendSystem::updateFiles(QList fileSendList, QList deleteList) { - qDebug() << "SendSystem::sendNotify thread ID " << QThread::currentThreadId(); + QListIterator clientIterator(deleteList); - auto answer = emit sigSendNotify(notify); - sendXmlAnswer(answer); + while(clientIterator.hasNext()) + { + FileData data = clientIterator.next(); + + slot_sendDeleteBlock(data.path); + if(slot_getIsSendingStopped()) + { + Logger::instance().log("Client: " + client->getLogin() + " isSendStopped", LogLevel::ERROR); + return; + } + } + + QListIterator serverIterator(fileSendList); + + while(serverIterator.hasNext()) + { + FileData data = serverIterator.next(); + + if (data.hash == "FOLDER") + { + slot_sendFolderBlock(data.path); + } + else + { + slot_sendFileBlock_V3(data.path); + } + + if(slot_getIsSendingStopped()) + { + return; + } + } + + slot_sendPacketType(PacketType::UPDATE_FILES_COMPLETE); } -void SendSystem::sendFileBlock(QString path) +bool SendSystem::waitWrittenData(QString marker, int msec) +{ + bool success = socket->waitForBytesWritten(msec); + if(success) + { + //qInfo() << "Data sended OK. <" + marker + ">"; + } + else + { + qDebug() << "WaitForBytesWritten timeout. <" + marker + ">"; + } + return success; +} + + +void SendSystem::slot_BytesWrittenToSocket(qint64 bytes) +{ + flWaitWritenToSocket = false; + bytesWritedToSocket = bytes; +} + +bool SendSystem::slot_getIsSendingStopped() const +{ + return flSendingStopped; +} + +void SendSystem::slot_stopSending() +{ + flSendingStopped = true; +} + +void SendSystem::slot_socketClose() +{ + socket->close(); +} + + +void SendSystem::slot_sendVersion() +{ + QByteArray data = dataParser->ClientAnswer()->currentVersion(); + slot_sendXmlAnswer(data); +} + +void SendSystem::slot_sendNotify(QString notify) +{ + auto answer = emit sigSendNotify(notify); + slot_sendXmlAnswer(answer); +} + +void SendSystem::slot_sendPacketType(PacketType packetType) +{ + Logger::instance().log(" SEND TO: " + client->getLogin() + " " + enumToString(packetType), LogLevel::DEBUG); + if (client->getClientType() == TypeClientAutorization::TYPE_QT_CLIENT || + client->getClientType() == TypeClientAutorization::TYPE_GUI) + { + QDataStream stream(socket); + stream.setVersion(QDataStream::Qt_DefaultCompiledVersion); + stream << packetType; + + waitWrittenData("sendPacketType"); + } + else + { + QByteArray message; + int type = (int)packetType; + message.append(reinterpret_cast(&type), sizeof(int)); + socket->write(message); + } +} + +void SendSystem::slot_sendXmlAnswer(QByteArray array, PacketType packetType) +{ + Logger::instance().log("SEND TO: "+ client->getLogin() + " " + enumToString(packetType) + "\n Text: " + + QString(array), LogLevel::DEBUG); + + if (client->getClientType() == TypeClientAutorization::TYPE_QT_CLIENT || + client->getClientType() == TypeClientAutorization::TYPE_GUI) + { + QDataStream stream(socket); + stream.setVersion(QDataStream::Qt_DefaultCompiledVersion); + + stream << packetType; + stream << array; + + waitWrittenData("sendXmlAnswer"); + } + else + { + slot_sendPacketType(packetType); + QByteArray message; + int size = array.length(); + message.append(reinterpret_cast(&size), sizeof(int)); + socket->write(message); + socket->write(array); + } +} + +void SendSystem::slot_sendDocs(QString docsPath) +{ + slot_sendFileBlock(docsPath); +} + +void SendSystem::slot_sendNeedUpdate(bool flag, quint64 size, quint64 fileCount, quint64 deleteCount) +{ + QDataStream stream(socket); + stream.setVersion(QDataStream::Qt_DefaultCompiledVersion); + stream << PacketType::TYPE_NEEDUPDATE; + stream << flag; + stream << size; + stream << fileCount; + stream << deleteCount; + + waitWrittenData("sendNeedUpdate"); +} + +void SendSystem::slot_sendFolderBlock(QString path) +{ + QDataStream stream(socket); + stream.setVersion(QDataStream::Qt_DefaultCompiledVersion); + + stream << PacketType::TYPE_FOLDER; + stream << path; + + waitWrittenData("sendFolderBlock"); +} + +void SendSystem::slot_sendDeleteBlock(QString path) +{ + QDataStream stream(socket); + stream.setVersion(QDataStream::Qt_DefaultCompiledVersion); + + stream << PacketType::TYPE_DELETE; + stream << path; + + waitWrittenData("sendDeleteBlock"); +} + +void SendSystem::slot_sendFileBlock(QString path) { //qDebug() << "SendSystem::sendFileBlock path: " << path; - if(getIsSendStopped()) + if(slot_getIsSendingStopped()) { //Поведение на случай отключения клиента - Logger::instance().log("Client: " + client->getLogin() + " isSendStopped", LogLevel::ERROR); + Logger::instance().log("Client: " + client->getLogin() + " isSendingStopped", LogLevel::ERROR); return; } @@ -94,24 +258,24 @@ void SendSystem::sendFileBlock(QString path) QDataStream stream(socket); stream.setVersion(QDataStream::Qt_DefaultCompiledVersion); - stream << PacketType::TYPE_FILE; //Отправляем тип блока + stream << PacketType::TYPE_FILE; stream << path << fileSize; waitWrittenData("sendFileBlock:header " + fileInfo.fileName()); while(!file.atEnd()) { + if(socket->state() == QAbstractSocket::UnconnectedState) + { + Logger::instance().log("Client: " + client->getLogin() + " UnconnectedState", LogLevel::ERROR); + break; + } + QByteArray data = file.read(sendFileBlockSize); stream << data; waitWrittenData(QString("sendFileBlock:data (size %1) ").arg(data.size()) + fileInfo.fileName()); - if(socket->state() == QAbstractSocket::UnconnectedState) - { - Logger::instance().log("Client: " + client->getLogin() + " UnconnectedState", LogLevel::ERROR); - break; - } - countSend++; } @@ -119,111 +283,13 @@ void SendSystem::sendFileBlock(QString path) countSend = 0; } -void SendSystem::sendFileBlock_V2(QString path) -{ - QFile file(path); - if(!file.open(QIODevice::ReadOnly)) - { - Logger::instance().log("ERROR open file: " + path, LogLevel::ERROR); - return; - } - - countSend = 0; - fileSize = file.size(); - - if (fileSize == 0) - { - Logger::instance().log("Client: " + client->getLogin() + " WARNING! Zero size " +file.fileName(), LogLevel::ERROR); - Logger::instance().log(path, LogLevel::ERROR); - return; - } - - QString type = QString::number(PacketType::TYPE_FILE); - QString fileSizeStr = QString::number(fileSize); - - qDebug() << "SendSystem::sendFileBlock_V2 file " << file.fileName() << " size " << fileSizeStr; - - QDataStream stream(socket); - stream.setVersion(QDataStream::Qt_DefaultCompiledVersion); - - - - //mutexWriten.lock(); - //flWaitWriten = true; - - stream << PacketType::TYPE_FILE; //Отправляем тип блока - stream << path << fileSize; - - socket->waitForBytesWritten(10); - - /* - while(flWaitWriten) - { - QCoreApplication::processEvents(); // Обеспечиваем обработку сообщений - } - */ - //socket->waitForBytesWritten(10); - - //socket->write(type.toUtf8()); - //socket->write(fileSizeStr.toUtf8()); - - connect(this->socket, &QTcpSocket::bytesWritten, this, &SendSystem::slot_BytesWrittenToSocket); - - - while(true) - { - if(socket->state() == QAbstractSocket::UnconnectedState) - break; - - countSend++; - - qint64 readBytes = file.read(buffer, /*sizeof(buffer)*/sendFileBlockSize); - if(readBytes <= 0) - break; - - flWaitWriten = true; - while(!socket->write(buffer, readBytes)) - { - int i = 0; - i++; - } - while(flWaitWriten) - { - QCoreApplication::processEvents(); // Обеспечиваем обработку сообщений - } - /* - while(!socket->flush()) - { - int i = 0; - i++; - } - */ - // Завершаем отправку - //socket->flush(); - //socket->waitForBytesWritten(10); - } - - disconnect(this->socket, &QTcpSocket::bytesWritten, this, &SendSystem::slot_BytesWrittenToSocket); - - //socket->flush(); - /* - while(!socket->flush()) - { - int i = 0; - i++; - }*/ - - - file.close(); -} - -void SendSystem::sendFileBlock_V3(QString path) +void SendSystem::slot_sendFileBlock_V3(QString path) { //qDebug() << "SendSystem::sendFileBlock path: " << path; - if(getIsSendStopped()) + if(slot_getIsSendingStopped()) { //Поведение на случай отключения клиента - Logger::instance().log("Client: " + client->getLogin() + " isSendStopped", LogLevel::ERROR); + Logger::instance().log("Client: " + client->getLogin() + " isSendingStopped", LogLevel::ERROR); return; } @@ -251,7 +317,7 @@ void SendSystem::sendFileBlock_V3(QString path) QDataStream stream(socket); stream.setVersion(QDataStream::Qt_DefaultCompiledVersion); - stream << PacketType::TYPE_FILE; //Отправляем тип блока + stream << PacketType::TYPE_FILE; stream << path << fileSize; waitWrittenData("sendFileBlock:header " + fileInfo.fileName()); @@ -266,18 +332,19 @@ void SendSystem::sendFileBlock_V3(QString path) break; } - qint64 readBytes = file.read(buffer, /*sizeof(buffer)*/sendFileBlockSize); + qint64 readBytes = file.read(buffer, sendFileBlockSize); if(readBytes <= 0) break; - flWaitWriten = true; + flWaitWritenToSocket = true; + while(!socket->write(buffer, readBytes)) - { + { + qCritical() << "socket->write ERROR. size " + QString::number(readBytes); int i = 0; i++; - qCritical() << "socket->write ERROR. size " + QString::number(readBytes); } - while(flWaitWriten) + while(flWaitWritenToSocket) { QCoreApplication::processEvents(); // Обеспечиваем обработку сообщений int i = 0; @@ -295,7 +362,7 @@ void SendSystem::sendFileBlock_V3(QString path) countSend = 0; } -void SendSystem::sendFileBlockByteArray(QByteArray array, PacketType packetType) +void SendSystem::slot_sendFileBlockByteArray(QByteArray array, PacketType packetType) { if(client->getClientType() == TypeClientAutorization::TYPE_QT_CLIENT || client->getClientType() == TypeClientAutorization::TYPE_GUI) @@ -315,21 +382,21 @@ void SendSystem::sendFileBlockByteArray(QByteArray array, PacketType packetType) stream << packetType; //Отправляем тип блока stream << size; - socket->waitForBytesWritten(10); + waitWrittenData("sendFileBlockByteArray"); while (bytesSended < size) { - QByteArray chunk = array.mid(bytesSended,sendFileBlockSize); + QByteArray chunk = array.mid(bytesSended, sendFileBlockSize); stream << chunk; - socket->waitForBytesWritten(10); + waitWrittenData("sendFileBlockByteArray"); bytesSended += chunk.length(); } } else { - sendPacketType(packetType); + slot_sendPacketType(packetType); qint64 size = array.size(); qint64 bytesSended = 0; @@ -345,7 +412,7 @@ void SendSystem::sendFileBlockByteArray(QByteArray array, PacketType packetType) while (size > 0) { - QByteArray chunk = array.mid(bytesSended,sendFileBlockSize); + QByteArray chunk = array.mid(bytesSended, sendFileBlockSize); bytesSended = socket->write(chunk); size -= bytesSended; @@ -354,20 +421,9 @@ void SendSystem::sendFileBlockByteArray(QByteArray array, PacketType packetType) } } -void SendSystem::sendVersion() +void SendSystem::slot_sendFileBlockWithRename(QString path, QString newName) { - QByteArray data = dataParser->ClientAnswer()->currentVersion(); - sendXmlAnswer(data); -} - -void SendSystem::slot_BytesWrittenToSocket(qint64 bytes) -{ - flWaitWriten = false; -} - -void SendSystem::sendFileBlockWithRename(QString path, QString newName) -{ - qDebug() << "SendSystem::sendFileBlockWithRename thread ID " << QThread::currentThreadId(); + //qDebug() << "SendSystem::sendFileBlockWithRename thread ID " << QThread::currentThreadId(); QDataStream stream(socket); stream.setVersion(QDataStream::Qt_DefaultCompiledVersion); @@ -383,26 +439,24 @@ void SendSystem::sendFileBlockWithRename(QString path, QString newName) QString pathForSend = Tools::createFolderPath(path) + "/" + staticDataFolderName + newName; - stream << PacketType::TYPE_FILE; //Отправляем тип блока - + stream << PacketType::TYPE_FILE; stream << pathForSend << fileSize; - socket->waitForBytesWritten(10); + waitWrittenData("sendFileBlockWithRename"); - if(isSendStopped) { //Поведение на случай отключения клиента + if(slot_getIsSendingStopped()) { //Поведение на случай отключения клиента file.close(); return; } - //socket->waitForBytesWritten(); if(file.open(QFile::ReadOnly)){ while(!file.atEnd()){ QByteArray data = file.read(sendFileBlockSize); stream << data; - socket->waitForBytesWritten(10); + waitWrittenData("sendFileBlockWithRename"); countSend++; } @@ -411,181 +465,8 @@ void SendSystem::sendFileBlockWithRename(QString path, QString newName) file.close(); countSend = 0; - socket->waitForBytesWritten(10); + waitWrittenData("sendFileBlockWithRename"); //socket->waitForReadyRead(100); - sendNotify(commandHashCompleteClient); + slot_sendNotify(commandHashCompleteClient); } - -void SendSystem::sendFolderBlock(QString path) -{ - QDataStream stream(socket); - stream.setVersion(QDataStream::Qt_DefaultCompiledVersion); - - stream << PacketType::TYPE_FOLDER; - stream << path; - - waitWrittenData("sendFolderBlock"); -} - -void SendSystem::sendDeleteBlock(QString path) -{ - QDataStream stream(socket); - stream.setVersion(QDataStream::Qt_DefaultCompiledVersion); - - stream << PacketType::TYPE_DELETE; - stream << path; - - waitWrittenData("sendDeleteBlock"); -} - -void SendSystem::sendPacketType(PacketType packetType) -{ - Logger::instance().log(" SEND TO: " + client->getLogin() + " " + enumToString(packetType), LogLevel::DEBUG); - if (client->getClientType() == TypeClientAutorization::TYPE_QT_CLIENT || - client->getClientType() == TypeClientAutorization::TYPE_GUI) - { - QDataStream stream(socket); - stream.setVersion(QDataStream::Qt_DefaultCompiledVersion); - stream << packetType; - - waitWrittenData("sendPacketType"); - } - else - { - QByteArray message; - int type = (int)packetType; - message.append(reinterpret_cast(&type), sizeof(int)); - socket->write(message); - } - -} - -void SendSystem::sendXmlAnswer(QByteArray array, PacketType packetType) -{ - qDebug() << "SendSystem::sendXmlAnswer thread ID " << QThread::currentThreadId(); - - Logger::instance().log("SEND TO: "+ client->getLogin() + " " + enumToString(packetType) + "\n Text: " + - QString(array),LogLevel::DEBUG); - - if (client->getClientType() == TypeClientAutorization::TYPE_QT_CLIENT || - client->getClientType() == TypeClientAutorization::TYPE_GUI) - { - QDataStream stream(socket); - stream.setVersion(QDataStream::Qt_DefaultCompiledVersion); - - stream << packetType; - stream << array; - - waitWrittenData("sendXmlAnswer"); - } - else - { - sendPacketType(packetType); - QByteArray message; - int size = array.length(); - message.append(reinterpret_cast(&size), sizeof(int)); - socket->write(message); - socket->write(array); - } -} - -void SendSystem::sendNeedUpdate(bool flag,quint64 size,quint64 fileCount,quint64 deleteCount) -{ - QDataStream stream(socket); - stream.setVersion(QDataStream::Qt_DefaultCompiledVersion); - stream << PacketType::TYPE_NEEDUPDATE; - stream << flag; - stream << size; - stream << fileCount; - stream << deleteCount; - - waitWrittenData("sendNeedUpdate"); -} - -void SendSystem::updateFiles(QList fileSendList, QList deleteList) -{ - //globalMutex->lock(); - - QListIterator clientIterator(deleteList); - - while(clientIterator.hasNext()) - { - FileData data = clientIterator.next(); - - sendDeleteBlock(data.path); - if(getIsSendStopped()) - { - Logger::instance().log("Client: " + client->getLogin() + " isSendStopped", LogLevel::ERROR); - //globalMutex->unlock(); - return; - } - } - - QListIterator serverIterator(fileSendList); - - while(serverIterator.hasNext()) - { - FileData data = serverIterator.next(); - - if (data.hash == "FOLDER") - { - sendFolderBlock(data.path); - } - else - { - sendFileBlock_V3(data.path); - } - - if(getIsSendStopped()) - { - //globalMutex->unlock(); - return; - } - } - - sendPacketType(PacketType::UPDATE_FILES_COMPLETE); - - //globalMutex->unlock(); -} - - -void SendSystem::socketClose() -{ - socket->close(); -} - -void SendSystem::sendStop() -{ - isSendStopped = true; -} - -void SendSystem::sendCFI(QByteArray array) -{ - sendXmlAnswer(array,PacketType::TYPE_UPDATEDCFI); -} - -void SendSystem::sendDocs(QString docsPath) -{ - sendFileBlock(docsPath); -} - -bool SendSystem::getIsSendStopped() const -{ - return isSendStopped; -} - -bool SendSystem::waitWrittenData(QString marker, int msec) -{ - bool success = socket->waitForBytesWritten(msec); - if(success) - { - //qInfo() << "Data sended OK. <" + marker + ">"; - } - else - { - qCritical() << "Data sended ERROR. <" + marker + ">"; - } - return success; -} - diff --git a/LibServer/Systems/sendsystem.h b/LibServer/Systems/sendsystem.h index 140f9c9..121afd9 100644 --- a/LibServer/Systems/sendsystem.h +++ b/LibServer/Systems/sendsystem.h @@ -13,9 +13,9 @@ class DataParser; class FileData; + class SendSystem : public QObject { - Q_OBJECT public: explicit SendSystem(QObject* parent = nullptr); @@ -24,58 +24,57 @@ public: void initialize(DataParser* dataParser,QMutex *globalMutex); void setClient(Client* client,QTcpSocket *socket); - void sendMessageBlock(QString message); - void sendFileBlock(QString path); - void sendFileBlock_V2(QString path); - void sendFileBlock_V3(QString path); - void sendFileBlockByteArray(QByteArray array, PacketType packetType); - void sendFileBlockWithRename(QString path,QString newName); - void sendFolderBlock(QString path); - void sendDeleteBlock(QString path); - void sendPacketType(PacketType packet); - void sendNotify(QString notify); - void sendStop(); - void sendCFI(QByteArray array); - void sendDocs(QString docPath); - void sendXmlAnswer(QByteArray array, PacketType packetType = PacketType::TYPE_XMLANSWER); - void sendNeedUpdate(bool flag,quint64 size,quint64 fileCount,quint64 deleteCount); - void updateFiles(QList fileSendList, QList deleteList); - bool getIsSendStopped() const; - - void updateFilesFULL(QList fileSendList, QList deleteList); - +private: bool waitWrittenData(QString marker, int msec = 500); -public slots: - void socketClose(); - void sendVersion(); - +public slots: void slot_BytesWrittenToSocket(qint64 bytes); + bool slot_getIsSendingStopped() const; + void slot_stopSending(); + + void slot_socketClose(); + + void slot_sendVersion(); + + void slot_sendNotify(QString notify); + void slot_sendPacketType(PacketType packet); + void slot_sendXmlAnswer(QByteArray array, PacketType packetType = PacketType::TYPE_XMLANSWER); + + void slot_sendDocs(QString docPath); + + void slot_sendNeedUpdate(bool flag,quint64 size,quint64 fileCount,quint64 deleteCount); + + void slot_sendFolderBlock(QString path); + void slot_sendDeleteBlock(QString path); + + void slot_sendFileBlock(QString path); + void slot_sendFileBlock_V3(QString path); + void slot_sendFileBlockByteArray(QByteArray array, PacketType packetType); + void slot_sendFileBlockWithRename(QString path,QString newName); + signals: - void sigLoadHash(); - //QByteArray sigSendXMLmessage(QString loginFrom, QString loginTo, QString text, QString userType); //сигнал не используется QByteArray sigSendNotify(QString message); - //QByteArray sigSendVersion(); //сигнал не используется private: Client *client; QTcpSocket* socket; DataParser* dataParser; - QMutex *globalMutex; - QWaitCondition *waitCondition; + QMutex *globalMutex; //Не используется (но пока оставлен) char* buffer; TypeClientAutorization type; - bool isSendStopped; - bool flWaitWriten; + bool flSendingStopped; - quint64 fileSize; + bool flWaitWritenToSocket; + qint64 bytesWritedToSocket; + + qint64 fileSize; int countSend; }; diff --git a/LibServer/clienthandler/clienthandler.cpp b/LibServer/clienthandler/clienthandler.cpp index 7f4eb4c..8e0b7bb 100644 --- a/LibServer/clienthandler/clienthandler.cpp +++ b/LibServer/clienthandler/clienthandler.cpp @@ -42,21 +42,21 @@ void ClientHandler::initialize(int descriptor, connect(recognizeSystem,&RecognizeSystem::signal_updateDocsXML,this,&ClientHandler::signal_updateDocsXML); - connect(this,&ClientHandler::sigSendXmlAnswer,sendSystem,&SendSystem::sendXmlAnswer,Qt::AutoConnection); + connect(this,&ClientHandler::sigSendXmlAnswer,sendSystem,&SendSystem::slot_sendXmlAnswer,Qt::AutoConnection); //connect(this,&ClientHandler::sigInitSender,sendSystem,&SendSystem::initialize,Qt::AutoConnection/*Qt::DirectConnection*/); - connect(this,&ClientHandler::sigFileBlock,sendSystem,&SendSystem::sendFileBlock,Qt::AutoConnection); - connect(this,&ClientHandler::sigFileBlockByteArray,sendSystem,&SendSystem::sendFileBlockByteArray,Qt::AutoConnection); - connect(this,&ClientHandler::sigFolderBlock,sendSystem,&SendSystem::sendFolderBlock,Qt::AutoConnection); - connect(this,&ClientHandler::sigGetIsSendStopped,sendSystem,&SendSystem::getIsSendStopped,/*Qt::AutoConnection*/Qt::DirectConnection); //Возвращает значение - connect(this,&ClientHandler::sigSendDeleteBlock,sendSystem,&SendSystem::sendDeleteBlock,Qt::AutoConnection); - connect(this,&ClientHandler::sigNeedUpdate,sendSystem,&SendSystem::sendNeedUpdate,Qt::AutoConnection); - connect(this,&ClientHandler::sigSendNotify,sendSystem,&SendSystem::sendNotify,Qt::AutoConnection); - connect(this,&ClientHandler::sigSendFileBlockWithRename,sendSystem,&SendSystem::sendFileBlockWithRename,Qt::AutoConnection); - connect(this,&ClientHandler::sigSendVersion,sendSystem,&SendSystem::sendVersion,Qt::AutoConnection); - connect(this,&ClientHandler::sigSocketClose,sendSystem,&SendSystem::socketClose,Qt::AutoConnection); + connect(this,&ClientHandler::sigFileBlock,sendSystem,&SendSystem::slot_sendFileBlock,Qt::AutoConnection); + connect(this,&ClientHandler::sigFileBlockByteArray,sendSystem,&SendSystem::slot_sendFileBlockByteArray,Qt::AutoConnection); + connect(this,&ClientHandler::sigFolderBlock,sendSystem,&SendSystem::slot_sendFolderBlock,Qt::AutoConnection); + connect(this,&ClientHandler::sigGetIsSendStopped,sendSystem,&SendSystem::slot_getIsSendingStopped,/*Qt::AutoConnection*/Qt::DirectConnection); //Возвращает значение + connect(this,&ClientHandler::sigSendDeleteBlock,sendSystem,&SendSystem::slot_sendDeleteBlock,Qt::AutoConnection); + connect(this,&ClientHandler::sigNeedUpdate,sendSystem,&SendSystem::slot_sendNeedUpdate,Qt::AutoConnection); + connect(this,&ClientHandler::sigSendNotify,sendSystem,&SendSystem::slot_sendNotify,Qt::AutoConnection); + connect(this,&ClientHandler::sigSendFileBlockWithRename,sendSystem,&SendSystem::slot_sendFileBlockWithRename,Qt::AutoConnection); + connect(this,&ClientHandler::sigSendVersion,sendSystem,&SendSystem::slot_sendVersion,Qt::AutoConnection); + connect(this,&ClientHandler::sigSocketClose,sendSystem,&SendSystem::slot_socketClose,Qt::AutoConnection); //connect(this,&ClientHandler::sigSocketFlush,sendSystem,&SendSystem::socketFlush,Qt::AutoConnection); //не используется - connect(this,&ClientHandler::sigSendPacketType,sendSystem,&SendSystem::sendPacketType,Qt::AutoConnection); - connect(this,&ClientHandler::sigSendStop,sendSystem,&SendSystem::sendStop,Qt::DirectConnection); + connect(this,&ClientHandler::sigSendPacketType,sendSystem,&SendSystem::slot_sendPacketType,Qt::AutoConnection); + connect(this,&ClientHandler::sigSendStop,sendSystem,&SendSystem::slot_stopSending,Qt::DirectConnection); connect(socket,&QTcpSocket::readyRead,this,&ClientHandler::initClientType,Qt::AutoConnection); initClientType(); From a0e54c0e1836b6d3ccc4ced0a8177a8690f089fe Mon Sep 17 00:00:00 2001 From: krivoshein Date: Tue, 24 Feb 2026 11:17:48 +0300 Subject: [PATCH 3/4] refact2 --- LibServer/Systems/processingsystem.cpp | 4 +- LibServer/Systems/recognizesystem.cpp | 5 +- LibServer/Systems/recognizesystem.h | 2 +- LibServer/Systems/sendsystem.cpp | 84 ++++++++++++++++------- LibServer/Systems/sendsystem.h | 8 +-- LibServer/Systems/updatecontroller.cpp | 4 +- LibServer/clienthandler/clienthandler.cpp | 10 +-- LibServer/clienthandler/clienthandler.h | 6 +- 8 files changed, 79 insertions(+), 44 deletions(-) diff --git a/LibServer/Systems/processingsystem.cpp b/LibServer/Systems/processingsystem.cpp index 7e6f7ad..0f101e2 100644 --- a/LibServer/Systems/processingsystem.cpp +++ b/LibServer/Systems/processingsystem.cpp @@ -652,7 +652,7 @@ void ProcessingSystem::processingClientQueryTasksXML(ClientHandler *client, Clie nameFile = tasksFIMfileName; pathFile = updateController->getPathAdditionalFile(nameFile); Logger::instance().log(pathFile); - client->sendFileBlock(pathFile); + client->sendFileBlock_forGUI(pathFile); client->sendPacketType(PacketType::TYPE_XMLANSWER_QUERY_TASKS_XML_FIM); } else if(clientQueryTasksXML.Type == "amm") @@ -660,7 +660,7 @@ void ProcessingSystem::processingClientQueryTasksXML(ClientHandler *client, Clie nameFile = tasksAMMfileName; pathFile = updateController->getPathAdditionalFile(nameFile); docsUpdater->lockAccessToDocsXML(); - client->sendFileBlock(pathFile); + client->sendFileBlock_forGUI(pathFile); client->sendPacketType(PacketType::TYPE_XMLANSWER_QUERY_TASKS_XML_AMM); docsUpdater->unLockAccessToDocsXML(); } diff --git a/LibServer/Systems/recognizesystem.cpp b/LibServer/Systems/recognizesystem.cpp index 56671cf..f7ecf3f 100644 --- a/LibServer/Systems/recognizesystem.cpp +++ b/LibServer/Systems/recognizesystem.cpp @@ -31,7 +31,7 @@ void RecognizeSystem::initialize(UpdateController *updateController,DataParser* connect(this,&RecognizeSystem::sigCopyVersion,updateController,&UpdateController::createCopyVersion,Qt::AutoConnection); connect(this,&RecognizeSystem::sigXmlParser,dataParser->getProcessParser(),&ProcessParser::slot_read,Qt::AutoConnection); //connect(this,&RecognizeSystem::sigRecalculateDocs,server,&ServerLMSWidget::slot_UpdateDocs,Qt::AutoConnection); - connect(this,&RecognizeSystem::sigSendDocs,sendSystem,&SendSystem::slot_sendDocs,Qt::AutoConnection); + connect(this,&RecognizeSystem::sigSendDocs_forQtClient,sendSystem,&SendSystem::slot_sendDocs_forQtClient,Qt::AutoConnection); } void RecognizeSystem::recognize() @@ -454,8 +454,7 @@ void RecognizeSystem::recognize() if(packetType == PacketType::GET_DOCS) { - emit sigSendDocs(updateController->getPathAdditionalFile(tasksAMMfileName)); - //globalMutex->unlock(); + emit sigSendDocs_forQtClient(updateController->getPathAdditionalFile(tasksAMMfileName)); } if (packetType == PacketType::SEND_HASH) diff --git a/LibServer/Systems/recognizesystem.h b/LibServer/Systems/recognizesystem.h index f450e99..ba93219 100644 --- a/LibServer/Systems/recognizesystem.h +++ b/LibServer/Systems/recognizesystem.h @@ -34,7 +34,7 @@ signals: void sigDeleteVersion(QString versionName); void sigCopyVersion(QString versionName,QString newVersionName,QString author); //void sigRecalculateDocs(); - void sigSendDocs(QString docsPath); + void sigSendDocs_forQtClient(QString docsPath); void signal_updateDocsXML(); diff --git a/LibServer/Systems/sendsystem.cpp b/LibServer/Systems/sendsystem.cpp index 84fca20..e67fd94 100644 --- a/LibServer/Systems/sendsystem.cpp +++ b/LibServer/Systems/sendsystem.cpp @@ -75,7 +75,7 @@ void SendSystem::updateFiles(QList fileSendList, QList delet } else { - slot_sendFileBlock_V3(data.path); + slot_sendFileBlock_forQtClient(data.path); } if(slot_getIsSendingStopped()) @@ -184,9 +184,9 @@ void SendSystem::slot_sendXmlAnswer(QByteArray array, PacketType packetType) } } -void SendSystem::slot_sendDocs(QString docsPath) +void SendSystem::slot_sendDocs_forQtClient(QString docsPath) { - slot_sendFileBlock(docsPath); + slot_sendFileBlock_forQtClient(docsPath); } void SendSystem::slot_sendNeedUpdate(bool flag, quint64 size, quint64 fileCount, quint64 deleteCount) @@ -224,7 +224,7 @@ void SendSystem::slot_sendDeleteBlock(QString path) waitWrittenData("sendDeleteBlock"); } -void SendSystem::slot_sendFileBlock(QString path) +void SendSystem::slot_sendFileBlock_forGUI(QString path) { //qDebug() << "SendSystem::sendFileBlock path: " << path; @@ -283,7 +283,7 @@ void SendSystem::slot_sendFileBlock(QString path) countSend = 0; } -void SendSystem::slot_sendFileBlock_V3(QString path) +void SendSystem::slot_sendFileBlock_forQtClient(QString path) { //qDebug() << "SendSystem::sendFileBlock path: " << path; @@ -421,51 +421,85 @@ void SendSystem::slot_sendFileBlockByteArray(QByteArray array, PacketType packet } } -void SendSystem::slot_sendFileBlockWithRename(QString path, QString newName) +void SendSystem::slot_sendFileBlockWithRename_Hash_forQtClient(QString path, QString newName) { //qDebug() << "SendSystem::sendFileBlockWithRename thread ID " << QThread::currentThreadId(); - QDataStream stream(socket); - stream.setVersion(QDataStream::Qt_DefaultCompiledVersion); + if(slot_getIsSendingStopped()) + { //Поведение на случай отключения клиента + Logger::instance().log("Client: " + client->getLogin() + " isSendingStopped", LogLevel::ERROR); + return; + } QFile file(Tools::createRootPath(path)); QFileInfo fileInfo(file); fileSize = fileInfo.size(); - if(fileSize == 0){ - Logger::instance().log("Client: " + client->getLogin() + " WARNING! Zero size " + fileInfo.fileName(),LogLevel::ERROR); + if (fileSize == 0) + { + Logger::instance().log("Client: " + client->getLogin() + " ERROR! File zero size " + fileInfo.fileName(), LogLevel::ERROR); + Logger::instance().log(path, LogLevel::ERROR); + return; + } + + if(!file.open(QFile::ReadOnly)) + { + Logger::instance().log("Client: " + client->getLogin() + " ERROR! File not open: " + fileInfo.fileName(), LogLevel::ERROR); + Logger::instance().log(path, LogLevel::ERROR); return; } QString pathForSend = Tools::createFolderPath(path) + "/" + staticDataFolderName + newName; + countSend = 0; + + QDataStream stream(socket); + stream.setVersion(QDataStream::Qt_DefaultCompiledVersion); + stream << PacketType::TYPE_FILE; stream << pathForSend << fileSize; waitWrittenData("sendFileBlockWithRename"); - if(slot_getIsSendingStopped()) { //Поведение на случай отключения клиента + connect(this->socket, &QTcpSocket::bytesWritten, this, &SendSystem::slot_BytesWrittenToSocket); - file.close(); - return; - } - - //socket->waitForBytesWritten(); - - if(file.open(QFile::ReadOnly)){ - while(!file.atEnd()){ - QByteArray data = file.read(sendFileBlockSize); - stream << data; - waitWrittenData("sendFileBlockWithRename"); - countSend++; + while(!file.atEnd()) + { + if(socket->state() == QAbstractSocket::UnconnectedState) + { + Logger::instance().log("Client: " + client->getLogin() + " UnconnectedState", LogLevel::ERROR); + break; } - Logger::instance().log("Send file " + fileInfo.fileName()); + qint64 readBytes = file.read(buffer, sendFileBlockSize); + if(readBytes <= 0) + break; + + flWaitWritenToSocket = true; + + while(!socket->write(buffer, readBytes)) + { + qCritical() << "socket->write ERROR. size " + QString::number(readBytes); + int i = 0; + i++; + } + while(flWaitWritenToSocket) + { + QCoreApplication::processEvents(); // Обеспечиваем обработку сообщений + int i = 0; + i++; + } + + waitWrittenData(QString("sendFileBlock:data (readBytes %1, num %2) ").arg(QString::number(readBytes), QString::number(countSend)) + fileInfo.fileName()); + + countSend++; } + disconnect(this->socket, &QTcpSocket::bytesWritten, this, &SendSystem::slot_BytesWrittenToSocket); + file.close(); countSend = 0; - waitWrittenData("sendFileBlockWithRename"); + //waitWrittenData("sendFileBlockWithRename"); //socket->waitForReadyRead(100); slot_sendNotify(commandHashCompleteClient); diff --git a/LibServer/Systems/sendsystem.h b/LibServer/Systems/sendsystem.h index 121afd9..9e04cb5 100644 --- a/LibServer/Systems/sendsystem.h +++ b/LibServer/Systems/sendsystem.h @@ -43,17 +43,17 @@ public slots: void slot_sendPacketType(PacketType packet); void slot_sendXmlAnswer(QByteArray array, PacketType packetType = PacketType::TYPE_XMLANSWER); - void slot_sendDocs(QString docPath); + void slot_sendDocs_forQtClient(QString docPath); void slot_sendNeedUpdate(bool flag,quint64 size,quint64 fileCount,quint64 deleteCount); void slot_sendFolderBlock(QString path); void slot_sendDeleteBlock(QString path); - void slot_sendFileBlock(QString path); - void slot_sendFileBlock_V3(QString path); + void slot_sendFileBlock_forGUI(QString path); + void slot_sendFileBlock_forQtClient(QString path); void slot_sendFileBlockByteArray(QByteArray array, PacketType packetType); - void slot_sendFileBlockWithRename(QString path,QString newName); + void slot_sendFileBlockWithRename_Hash_forQtClient(QString path,QString newName); signals: QByteArray sigSendNotify(QString message); diff --git a/LibServer/Systems/updatecontroller.cpp b/LibServer/Systems/updatecontroller.cpp index 1f34f20..3b3eae7 100644 --- a/LibServer/Systems/updatecontroller.cpp +++ b/LibServer/Systems/updatecontroller.cpp @@ -54,10 +54,12 @@ void UpdateController::changeAssetVersion(QString versionName) currentStreamingPath = assetManager->setVersion(versionName); setUpCurrentServerHash(); - emit sigUpdateDocsXML(); + commonClientHandler->slot_sendPacketToAllClients(PacketType::HASH_READY, false); commonClientHandler->sendCurrentVersionToAllClient(); + emit sigUpdateDocsXML(); + //commonClientHandler->slot_sendPacketToAllClients(PacketType::FREE); res = emit signal_BlockAutorization(false, "SERVER", "ChangeAssetVersion"); } diff --git a/LibServer/clienthandler/clienthandler.cpp b/LibServer/clienthandler/clienthandler.cpp index 8e0b7bb..d40e976 100644 --- a/LibServer/clienthandler/clienthandler.cpp +++ b/LibServer/clienthandler/clienthandler.cpp @@ -44,14 +44,14 @@ void ClientHandler::initialize(int descriptor, connect(this,&ClientHandler::sigSendXmlAnswer,sendSystem,&SendSystem::slot_sendXmlAnswer,Qt::AutoConnection); //connect(this,&ClientHandler::sigInitSender,sendSystem,&SendSystem::initialize,Qt::AutoConnection/*Qt::DirectConnection*/); - connect(this,&ClientHandler::sigFileBlock,sendSystem,&SendSystem::slot_sendFileBlock,Qt::AutoConnection); + connect(this,&ClientHandler::sigFileBlock_forGUI,sendSystem,&SendSystem::slot_sendFileBlock_forGUI,Qt::AutoConnection); connect(this,&ClientHandler::sigFileBlockByteArray,sendSystem,&SendSystem::slot_sendFileBlockByteArray,Qt::AutoConnection); connect(this,&ClientHandler::sigFolderBlock,sendSystem,&SendSystem::slot_sendFolderBlock,Qt::AutoConnection); connect(this,&ClientHandler::sigGetIsSendStopped,sendSystem,&SendSystem::slot_getIsSendingStopped,/*Qt::AutoConnection*/Qt::DirectConnection); //Возвращает значение connect(this,&ClientHandler::sigSendDeleteBlock,sendSystem,&SendSystem::slot_sendDeleteBlock,Qt::AutoConnection); connect(this,&ClientHandler::sigNeedUpdate,sendSystem,&SendSystem::slot_sendNeedUpdate,Qt::AutoConnection); connect(this,&ClientHandler::sigSendNotify,sendSystem,&SendSystem::slot_sendNotify,Qt::AutoConnection); - connect(this,&ClientHandler::sigSendFileBlockWithRename,sendSystem,&SendSystem::slot_sendFileBlockWithRename,Qt::AutoConnection); + connect(this,&ClientHandler::sigSendFileBlockWithRename_Hash_forQtClient,sendSystem,&SendSystem::slot_sendFileBlockWithRename_Hash_forQtClient,Qt::AutoConnection); connect(this,&ClientHandler::sigSendVersion,sendSystem,&SendSystem::slot_sendVersion,Qt::AutoConnection); connect(this,&ClientHandler::sigSocketClose,sendSystem,&SendSystem::slot_socketClose,Qt::AutoConnection); //connect(this,&ClientHandler::sigSocketFlush,sendSystem,&SendSystem::socketFlush,Qt::AutoConnection); //не используется @@ -101,7 +101,7 @@ void ClientHandler::initClientType() void ClientHandler::sendHash() { QString path = "\\" + hashFileName; - emit sigSendFileBlockWithRename(path,"/serverHash.xml"); + emit sigSendFileBlockWithRename_Hash_forQtClient(path,"/serverHash.xml"); //emit sigSendNotify("HASHSENDCOMPLETE"); } @@ -135,9 +135,9 @@ void ClientHandler::sendXmlAnswer(QByteArray array, PacketType packetType) emit sigSendXmlAnswer(array, packetType); } -void ClientHandler::sendFileBlock(QString path) +void ClientHandler::sendFileBlock_forGUI(QString path) { - emit sigFileBlock(path); + emit sigFileBlock_forGUI(path); } void ClientHandler::sendFileBlockByteArray(QByteArray array, PacketType packetType) diff --git a/LibServer/clienthandler/clienthandler.h b/LibServer/clienthandler/clienthandler.h index 491a311..2c32f06 100644 --- a/LibServer/clienthandler/clienthandler.h +++ b/LibServer/clienthandler/clienthandler.h @@ -27,7 +27,7 @@ public: void initSender(DataParser *dataParser); void sendXmlAnswer(QByteArray array, PacketType packetType = PacketType::TYPE_XMLANSWER); void sendFolderBlock(QString path); - void sendFileBlock(QString path); + void sendFileBlock_forGUI(QString path); void sendFileBlockByteArray(QByteArray array, PacketType packetType); bool getIsSendStopped(); void sendDeleteBlock(QString path); @@ -53,7 +53,7 @@ signals: void sigSendXmlAnswer(QByteArray array, PacketType packetType); //void sigInitSender (DataParser *dataParse, QMutex *mutex); void sigFolderBlock(QString path); - void sigFileBlock(QString path); + void sigFileBlock_forGUI(QString path); void sigFileBlockByteArray(QByteArray array, PacketType packetType); bool sigGetIsSendStopped(); void sigSendDeleteBlock(QString path); @@ -62,7 +62,7 @@ signals: void sigSendHash(); void sigRecognize(ClientHandler *handler); void sigSendNotify(QString notify); - void sigSendFileBlockWithRename (QString path,QString newName); + void sigSendFileBlockWithRename_Hash_forQtClient (QString path,QString newName); void sigSocketClose(); //bool sigSocketFlush(); //не используется void sigSendVersion(); From 2b656c29597e548894a5c94c145027f5e772b2f5 Mon Sep 17 00:00:00 2001 From: krivoshein Date: Thu, 26 Feb 2026 17:59:17 +0300 Subject: [PATCH 4/4] =?UTF-8?q?=D0=A0=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5?= =?UTF-8?q?=D1=82=20=D1=81=D0=BE=D0=BA=D0=B5=D1=82=20=D1=81=D1=82=D1=80?= =?UTF-8?q?=D0=B8=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LibServer/Systems/sendsystem.cpp | 34 ++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/LibServer/Systems/sendsystem.cpp b/LibServer/Systems/sendsystem.cpp index e67fd94..b7c66f9 100644 --- a/LibServer/Systems/sendsystem.cpp +++ b/LibServer/Systems/sendsystem.cpp @@ -364,8 +364,7 @@ void SendSystem::slot_sendFileBlock_forQtClient(QString path) void SendSystem::slot_sendFileBlockByteArray(QByteArray array, PacketType packetType) { - if(client->getClientType() == TypeClientAutorization::TYPE_QT_CLIENT || - client->getClientType() == TypeClientAutorization::TYPE_GUI) + if(client->getClientType() == TypeClientAutorization::TYPE_GUI) { QDataStream stream(socket); stream.setVersion(QDataStream::Qt_DefaultCompiledVersion); @@ -388,12 +387,43 @@ void SendSystem::slot_sendFileBlockByteArray(QByteArray array, PacketType packet { QByteArray chunk = array.mid(bytesSended, sendFileBlockSize); stream << chunk; + //bytesSended = socket->write(chunk); waitWrittenData("sendFileBlockByteArray"); bytesSended += chunk.length(); } } + else if(client->getClientType() == TypeClientAutorization::TYPE_QT_CLIENT) + { + QDataStream stream(socket); + stream.setVersion(QDataStream::Qt_DefaultCompiledVersion); + + qint64 size = array.size(); + qint64 bytesSended = 0; + + if (size == 0) + { + Logger::instance().log(" WARNING! Zero size ",LogLevel::ERROR); + return; + } + + stream << packetType; //Отправляем тип блока + stream << size; + + waitWrittenData("sendFileBlockByteArray"); + + while (bytesSended < size) + { + QByteArray chunk = array.mid(bytesSended, sendFileBlockSize); + //stream << chunk; + bytesSended = socket->write(chunk); + + waitWrittenData("sendFileBlockByteArray"); + + bytesSended += chunk.length(); + } + } else { slot_sendPacketType(packetType);