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 760b500..f7ecf3f 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); @@ -29,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_forQtClient,sendSystem,&SendSystem::slot_sendDocs_forQtClient,Qt::AutoConnection); } void RecognizeSystem::recognize() @@ -403,7 +405,7 @@ void RecognizeSystem::recognize() if (updateController->checkDuplicate(result[1])) { - sendSystem->sendNotify(commandDuplicateVerName); + sendSystem->slot_sendNotify(commandDuplicateVerName); packetType = PacketType::TYPE_NONE; break; } @@ -425,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 { @@ -452,7 +454,7 @@ void RecognizeSystem::recognize() if(packetType == PacketType::GET_DOCS) { - emit sigSendDocs(updateController->getPathAdditionalFile(tasksAMMfileName)); + emit sigSendDocs_forQtClient(updateController->getPathAdditionalFile(tasksAMMfileName)); } if (packetType == PacketType::SEND_HASH) diff --git a/LibServer/Systems/recognizesystem.h b/LibServer/Systems/recognizesystem.h index 5dda0e6..ba93219 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(); @@ -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(); @@ -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 03611f1..b7c66f9 100644 --- a/LibServer/Systems/sendsystem.cpp +++ b/LibServer/Systems/sendsystem.cpp @@ -1,88 +1,370 @@ #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) { - isSendStopped = false; + qDebug() << "SendSystem init thread ID " << QThread::currentThreadId(); + + buffer = new char[sendFileBlockSize]; } -void SendSystem::initialize(DataParser *dataParser,QMutex *globalMutex) +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) +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; - isSendStopped = false; + + this->type = client->getClientType(); + + flSendingStopped = false; } -void SendSystem::sendNotify(QString notify) +void SendSystem::updateFiles(QList fileSendList, QList deleteList) { - qDebug() << "SendSystem::sendNotify thread ID " << QThread::currentThreadId(); + QListIterator clientIterator(deleteList); + 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_forQtClient(data.path); + } + + if(slot_getIsSendingStopped()) + { + return; + } + } + + slot_sendPacketType(PacketType::UPDATE_FILES_COMPLETE); +} + +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); - sendXmlAnswer(answer); + slot_sendXmlAnswer(answer); } -void SendSystem::sendFileBlock(QString path) +void SendSystem::slot_sendPacketType(PacketType packetType) { - QFile file(path); - QFileInfo fileInfo(file); + 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; - if(isSendStopped) + 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_forQtClient(QString docsPath) +{ + slot_sendFileBlock_forQtClient(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_forGUI(QString path) +{ + //qDebug() << "SendSystem::sendFileBlock path: " << path; + + if(slot_getIsSendingStopped()) { //Поведение на случай отключения клиента - file.close(); - Logger::instance().log("UNLOCK STOP MUTEX : " + client->getLogin(),LogLevel::ERROR); + Logger::instance().log("Client: " + client->getLogin() + " isSendingStopped", 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; } - stream << PacketType::TYPE_FILE; //Отправляем тип блока + 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()) + { + if(socket->state() == QAbstractSocket::UnconnectedState) + { + Logger::instance().log("Client: " + client->getLogin() + " UnconnectedState", LogLevel::ERROR); + break; } - //emit sigSendToLogger(Tools::getTime() + " send file " + fileInfo.fileName()); + QByteArray data = file.read(sendFileBlockSize); + stream << data; + + waitWrittenData(QString("sendFileBlock:data (size %1) ").arg(data.size()) + fileInfo.fileName()); + + countSend++; } file.close(); countSend = 0; } -void SendSystem::sendFileBlockByteArray(QByteArray array, PacketType packetType) +void SendSystem::slot_sendFileBlock_forQtClient(QString path) { - if(client->getClientType() == TypeClientAutorization::TYPE_QT_CLIENT || - client->getClientType() == TypeClientAutorization::TYPE_GUI) + //qDebug() << "SendSystem::sendFileBlock path: " << path; + + if(slot_getIsSendingStopped()) + { //Поведение на случай отключения клиента + Logger::instance().log("Client: " + client->getLogin() + " isSendingStopped", 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, 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; +} + +void SendSystem::slot_sendFileBlockByteArray(QByteArray array, PacketType packetType) +{ + if(client->getClientType() == TypeClientAutorization::TYPE_GUI) { QDataStream stream(socket); stream.setVersion(QDataStream::Qt_DefaultCompiledVersion); @@ -99,17 +381,52 @@ void SendSystem::sendFileBlockByteArray(QByteArray array, PacketType packetType) stream << packetType; //Отправляем тип блока stream << size; + waitWrittenData("sendFileBlockByteArray"); + while (bytesSended < size) { - QByteArray chunk = array.mid(bytesSended,sendFileBlockSize); + 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 { - sendPacketType(packetType); + slot_sendPacketType(packetType); qint64 size = array.size(); qint64 bytesSended = 0; @@ -125,7 +442,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; @@ -134,211 +451,86 @@ void SendSystem::sendFileBlockByteArray(QByteArray array, PacketType packetType) } } -void SendSystem::sendVersion() +void SendSystem::slot_sendFileBlockWithRename_Hash_forQtClient(QString path, QString newName) { - QByteArray data = dataParser->ClientAnswer()->currentVersion(); - sendXmlAnswer(data); -} + //qDebug() << "SendSystem::sendFileBlockWithRename thread ID " << QThread::currentThreadId(); -void SendSystem::sendFileBlockWithRename(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; - stream << PacketType::TYPE_FILE; //Отправляем тип блока + countSend = 0; + QDataStream stream(socket); + stream.setVersion(QDataStream::Qt_DefaultCompiledVersion); + + stream << PacketType::TYPE_FILE; stream << pathForSend << fileSize; - if(isSendStopped) { //Поведение на случай отключения клиента + waitWrittenData("sendFileBlockWithRename"); - file.close(); - return; - } + connect(this->socket, &QTcpSocket::bytesWritten, this, &SendSystem::slot_BytesWrittenToSocket); - - socket->waitForBytesWritten(); - - if(file.open(QFile::ReadOnly)){ - while(!file.atEnd()){ - QByteArray data = file.read(sendFileBlockSize); - stream << data; - socket->waitForBytesWritten(); - 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; - socket->waitForBytesWritten(); - socket->waitForReadyRead(100); - - sendNotify(commandHashCompleteClient); -} - -void SendSystem::sendFolderBlock(QString path) -{ - QDataStream stream(socket); - stream.setVersion(QDataStream::Qt_DefaultCompiledVersion); - - stream << PacketType::TYPE_FOLDER; - stream << path; -} - -void SendSystem::sendDeleteBlock(QString path) -{ - QDataStream stream(socket); - stream.setVersion(QDataStream::Qt_DefaultCompiledVersion); - - stream << PacketType::TYPE_DELETE; - stream << path; - qDebug() << "DELETE: " + path; - socket->waitForReadyRead(10); -} - -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; - socket->waitForReadyRead(100); - } - 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; - socket->waitForBytesWritten(); - } - 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; -} - -void SendSystem::updateFiles(QList fileSendList, QList deleteList){ - - QMutexLocker locker(mutex); - QListIterator clientIterator(deleteList); - - while(clientIterator.hasNext()) - { - FileData data = clientIterator.next(); - - sendDeleteBlock(data.path); - if(getIsSendStopped()) - { - return; - } - } - - QListIterator serverIterator(fileSendList); - - while(serverIterator.hasNext()) - { - FileData data = serverIterator.next(); - - if (data.hash == "FOLDER") - { - sendFolderBlock(data.path); - socket->waitForBytesWritten(); - } - else - { - sendFileBlock(data.path); - socket->waitForBytesWritten(); - } - - if(isSendStopped) - { - return; - } - } - sendPacketType(PacketType::UPDATE_FILES_COMPLETE); -} - - -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; -} - -SendSystem::~SendSystem() -{ + //waitWrittenData("sendFileBlockWithRename"); + //socket->waitForReadyRead(100); + slot_sendNotify(commandHashCompleteClient); } diff --git a/LibServer/Systems/sendsystem.h b/LibServer/Systems/sendsystem.h index 7dd3066..9e04cb5 100644 --- a/LibServer/Systems/sendsystem.h +++ b/LibServer/Systems/sendsystem.h @@ -13,56 +13,69 @@ class DataParser; class FileData; + 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 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; +private: + bool waitWrittenData(QString marker, int msec = 500); - ~SendSystem(); +public slots: + void slot_BytesWrittenToSocket(qint64 bytes); + bool slot_getIsSendingStopped() const; + void slot_stopSending(); - void updateFilesFULL(QList fileSendList, QList deleteList); -public slots: - void socketClose(); - void sendVersion(); + 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_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_forGUI(QString path); + void slot_sendFileBlock_forQtClient(QString path); + void slot_sendFileBlockByteArray(QByteArray array, PacketType packetType); + void slot_sendFileBlockWithRename_Hash_forQtClient(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; - quint64 fileSize; - QMutex *mutex; - QWaitCondition *waitCondition; - int countSend; - bool isSendStopped; + + QMutex *globalMutex; //Не используется (но пока оставлен) + + char* buffer; + TypeClientAutorization type; + + bool flSendingStopped; + + bool flWaitWritenToSocket; + qint64 bytesWritedToSocket; + + qint64 fileSize; + int countSend; }; #endif // SENDSYSTEM_H diff --git a/LibServer/Systems/tools.h b/LibServer/Systems/tools.h index 2cbd5d6..04fbdfc 100644 --- a/LibServer/Systems/tools.h +++ b/LibServer/Systems/tools.h @@ -58,7 +58,8 @@ static const QString commandeGetOfflineMessages = "GETOFFLINEMESSAGE"; static const QString commandGetCFI = "GETCFI"; //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/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 4eeb7e1..d40e976 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; @@ -41,28 +42,29 @@ 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::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::sigSendXmlAnswer,sendSystem,&SendSystem::slot_sendXmlAnswer,Qt::AutoConnection); + //connect(this,&ClientHandler::sigInitSender,sendSystem,&SendSystem::initialize,Qt::AutoConnection/*Qt::DirectConnection*/); + 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_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); //не используется - 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(); - 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"); } @@ -99,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"); } @@ -133,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 f7c08f5..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); @@ -51,9 +51,9 @@ 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 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(); @@ -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