sendFileBlock_V3

This commit is contained in:
2026-02-18 17:20:20 +03:00
parent ed7de8af4e
commit 594aa8311e
9 changed files with 342 additions and 72 deletions

View File

@@ -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)

View File

@@ -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;

View File

@@ -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<FileData> fileSendList, QList<FileData> deleteList){
void SendSystem::updateFiles(QList<FileData> fileSendList, QList<FileData> deleteList)
{
//globalMutex->lock();
QMutexLocker locker(mutex);
QListIterator<FileData> clientIterator(deleteList);
while(clientIterator.hasNext())
@@ -283,6 +516,8 @@ void SendSystem::updateFiles(QList<FileData> fileSendList, QList<FileData> 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<FileData> fileSendList, QList<FileData> 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;
}

View File

@@ -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<FileData> fileSendList, QList<FileData> deleteList);
bool getIsSendStopped() const;
~SendSystem();
void updateFilesFULL(QList<FileData> fileSendList, QList<FileData> 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

View File

@@ -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

View File

@@ -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");
}

View File

@@ -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

View File

@@ -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)

View File

@@ -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<QString, QString> blockersMap;
void removeClient(int idSocket);
void addClient(qintptr descriptor, ClientHandler *client);
QMap<QString, QString> blockersMap;
};
#endif // MULTITHREADSERVER_H