mirror of
https://gitea.msk.dinamika-avia.ru/Constanta-Design/RRJServer.git
synced 2026-03-28 19:55:48 +03:00
refact struct projects
This commit is contained in:
205
ServerLMS/clienthandler/clienthandler.cpp
Normal file
205
ServerLMS/clienthandler/clienthandler.cpp
Normal file
@@ -0,0 +1,205 @@
|
||||
#include "clienthandler.h"
|
||||
|
||||
#include <QThread>
|
||||
|
||||
#include <Systems/sendsystem.h>
|
||||
|
||||
ClientHandler::ClientHandler( QObject *parent):
|
||||
QObject(parent)
|
||||
{
|
||||
}
|
||||
|
||||
void ClientHandler::initialize(int descriptor,ServerLMSWidget *serverWidget,
|
||||
UpdateController *updateController,DataParser *dataParser)
|
||||
{
|
||||
this->socket = new QTcpSocket;
|
||||
this->thread = new QThread;
|
||||
|
||||
socket->setParent(nullptr);
|
||||
socket->setSocketDescriptor(descriptor);
|
||||
|
||||
qDebug() << "Client thread: " << QThread::currentThreadId();
|
||||
|
||||
sendSystem = new SendSystem;
|
||||
recognizeSystem = new RecognizeSystem;
|
||||
|
||||
thread->start();
|
||||
|
||||
socket->moveToThread(thread);
|
||||
sendSystem->moveToThread(thread);
|
||||
recognizeSystem->moveToThread(thread);
|
||||
|
||||
this->updateController = updateController;
|
||||
this->server = serverWidget;
|
||||
|
||||
QString peerName = socket->peerName();
|
||||
QString peerAddress = socket->peerAddress().toString();
|
||||
QString peerPort = QString::number(socket->peerPort());
|
||||
|
||||
client = new Client(peerName,peerAddress,peerPort,socket);
|
||||
|
||||
connect(this,&ClientHandler::sigSendXmlAnswer,sendSystem,&SendSystem::sendXmlAnswer,Qt::AutoConnection);
|
||||
connect(this,&ClientHandler::sigInitSender,sendSystem,&SendSystem::initialize,Qt::AutoConnection);
|
||||
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);
|
||||
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::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(socket,&QTcpSocket::readyRead,this,&ClientHandler::initClientType,Qt::AutoConnection);
|
||||
initClientType();
|
||||
|
||||
recognizeSystem->initialize(updateController,dataParser,serverWidget,sendSystem, this);
|
||||
sendSystem->setClient(client,socket);
|
||||
emit sigInitSender(dataParser,serverWidget->getMutex());
|
||||
|
||||
}
|
||||
|
||||
void ClientHandler::setClient(Client *value)
|
||||
{
|
||||
client = value;
|
||||
}
|
||||
|
||||
void ClientHandler::initClientType()
|
||||
{
|
||||
QDataStream stream(socket);
|
||||
stream.setVersion(QDataStream::Qt_DefaultCompiledVersion);
|
||||
|
||||
while (socket->bytesAvailable() > 0)
|
||||
{
|
||||
stream.startTransaction();
|
||||
char *read = new char[4];
|
||||
stream.readRawData(read,4);
|
||||
|
||||
int numPackage = *((int*)read);
|
||||
PacketType packetType = static_cast<PacketType>(numPackage);
|
||||
|
||||
if(!stream.commitTransaction()) continue;
|
||||
|
||||
packetTypeInit(packetType,client);
|
||||
|
||||
disconnect(socket,&QTcpSocket::readyRead,this,&ClientHandler::initClientType);
|
||||
connect(socket,&QTcpSocket::readyRead,recognizeSystem,&RecognizeSystem::recognize,Qt::AutoConnection);
|
||||
connect(socket, &QTcpSocket::disconnected, this, &ClientHandler::sendDisable,Qt::AutoConnection);
|
||||
}
|
||||
}
|
||||
|
||||
void ClientHandler::sendHash()
|
||||
{
|
||||
QString path = "\\" + hashFileName;
|
||||
emit sigSendFileBlockWithRename(path,"/serverHash.xml");
|
||||
//emit sigSendNotify("HASHSENDCOMPLETE");
|
||||
}
|
||||
|
||||
void ClientHandler::sendVersion()
|
||||
{
|
||||
emit sigSendVersion();
|
||||
}
|
||||
|
||||
void ClientHandler::setReady(bool flag)
|
||||
{
|
||||
getClient()->setReady(flag);
|
||||
}
|
||||
|
||||
void ClientHandler::sendVersionList()
|
||||
{
|
||||
QFile file(versionListFile);
|
||||
file.open(QFile::ReadOnly);
|
||||
|
||||
QByteArray array = file.readAll();
|
||||
file.close();
|
||||
emit sendXmlAnswer(array);
|
||||
}
|
||||
|
||||
void ClientHandler::sendPacketType(PacketType packetType)
|
||||
{
|
||||
emit sigSendPacketType(packetType);
|
||||
}
|
||||
|
||||
void ClientHandler::sendXmlAnswer(QByteArray array, PacketType packetType)
|
||||
{
|
||||
emit sigSendXmlAnswer(array, packetType);
|
||||
}
|
||||
|
||||
void ClientHandler::sendFileBlock(QString path)
|
||||
{
|
||||
emit sigFileBlock(path);
|
||||
}
|
||||
|
||||
void ClientHandler::sendFileBlockByteArray(QByteArray array, PacketType packetType)
|
||||
{
|
||||
emit sigFileBlockByteArray(array, packetType);
|
||||
}
|
||||
|
||||
bool ClientHandler::getIsSendStopped()
|
||||
{
|
||||
return emit sigGetIsSendStopped();
|
||||
}
|
||||
|
||||
void ClientHandler::sendDeleteBlock(QString path)
|
||||
{
|
||||
emit sigSendDeleteBlock(path);
|
||||
}
|
||||
|
||||
void ClientHandler::sendNotify(QString text)
|
||||
{
|
||||
emit sigSendNotify(text);
|
||||
}
|
||||
|
||||
void ClientHandler::sendNeedUpdate(bool flag, quint64 size, quint64 fileCount,quint64 deleteCount)
|
||||
{
|
||||
emit sigNeedUpdate(flag,size,fileCount,deleteCount);
|
||||
}
|
||||
|
||||
void ClientHandler::sendDisable()
|
||||
{
|
||||
// thread->exit();
|
||||
// thread->wait();
|
||||
thread->quit();
|
||||
emit sigSendStop();
|
||||
emit sigClientDisconnected(client->getAddress(),client->getPort());
|
||||
}
|
||||
|
||||
void ClientHandler::packetTypeInit(PacketType packet,Client *client)
|
||||
{
|
||||
if(packet == PacketType::TYPE_QT)
|
||||
{
|
||||
client->setUnity(TypeClientAutorization::TYPE_QT_CLIENT);
|
||||
}
|
||||
else if (packet == PacketType::TYPE_UNITY)
|
||||
{
|
||||
client->setUnity(TypeClientAutorization::TYPE_UNITY_CLIENT);
|
||||
}
|
||||
|
||||
Logger::instance().log("C: " + client->getLogin() + " send pack " + enumToString(packet));
|
||||
}
|
||||
|
||||
|
||||
Client *ClientHandler::getClient() const
|
||||
{
|
||||
return client;
|
||||
}
|
||||
|
||||
SendSystem *ClientHandler::getSendSystem() const
|
||||
{
|
||||
return sendSystem;
|
||||
}
|
||||
|
||||
QTcpSocket *ClientHandler::getSocket() const
|
||||
{
|
||||
return socket;
|
||||
}
|
||||
|
||||
ClientHandler::~ClientHandler()
|
||||
{
|
||||
thread->quit();
|
||||
thread->wait();
|
||||
}
|
||||
91
ServerLMS/clienthandler/clienthandler.h
Normal file
91
ServerLMS/clienthandler/clienthandler.h
Normal file
@@ -0,0 +1,91 @@
|
||||
#ifndef CLIENTHANDLER_H
|
||||
#define CLIENTHANDLER_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QTcpSocket>
|
||||
#include <QThread>
|
||||
|
||||
#include <Systems/sendsystem.h>
|
||||
#include <Systems/tools.h>
|
||||
#include <Data/Client.h>
|
||||
#include <Data/PacketType.h>
|
||||
|
||||
class SendSystem;
|
||||
class DataParser;
|
||||
class Logger;
|
||||
class ServerLMSWidget;
|
||||
class UpdateController;
|
||||
class RecognizeSystem;
|
||||
class ClientAnswerParser;
|
||||
|
||||
class ClientHandler : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
ClientHandler(QObject* parent = nullptr);
|
||||
void initSender(DataParser *dataParser);
|
||||
void sendXmlAnswer(QByteArray array, PacketType packetType = PacketType::TYPE_XMLANSWER);
|
||||
void sendFolderBlock(QString path);
|
||||
void sendFileBlock(QString path);
|
||||
void sendFileBlockByteArray(QByteArray array, PacketType packetType);
|
||||
bool getIsSendStopped();
|
||||
void sendDeleteBlock(QString path);
|
||||
void sendFinish();
|
||||
void sendNotify(QString text);
|
||||
void sendNeedUpdate(bool flag, quint64 size,quint64 fileCount,quint64 deleteCount);
|
||||
void sendDisable();
|
||||
void recognize();
|
||||
void sendHash();
|
||||
void sendVersionList();
|
||||
void sendPacketType(PacketType packetType);
|
||||
void sendVersion();
|
||||
void setReady(bool flag);
|
||||
|
||||
~ClientHandler();
|
||||
|
||||
Client *getClient() const;
|
||||
SendSystem *getSendSystem() const;
|
||||
|
||||
QTcpSocket *getSocket() const;
|
||||
|
||||
signals:
|
||||
void sigSendXmlAnswer(QByteArray array, PacketType packetType);
|
||||
void sigInitSender (DataParser *dataParse, QMutex *mutex);
|
||||
void sigFolderBlock(QString path);
|
||||
void sigFileBlock(QString path);
|
||||
void sigFileBlockByteArray(QByteArray array, PacketType packetType);
|
||||
bool sigGetIsSendStopped();
|
||||
void sigSendDeleteBlock(QString path);
|
||||
void sigNeedUpdate(bool flag,quint64 size,quint64 fileCount,quint64 deleteCount);
|
||||
void sigClientDisconnected(QString address,QString port);
|
||||
void sigSendHash();
|
||||
void sigRecognize(ClientHandler *handler);
|
||||
void sigSendNotify(QString notify);
|
||||
void sigSendFileBlockWithRename (QString path,QString newName);
|
||||
void sigSocketClose();
|
||||
bool sigSocketFlush();
|
||||
void sigSendVersion();
|
||||
void sigSendPacketType(PacketType packetType);
|
||||
void sigSendStop();
|
||||
|
||||
public :
|
||||
QThread *thread;
|
||||
QTcpSocket *socket;
|
||||
|
||||
void initialize(int descriptor, ServerLMSWidget *serverWidget,
|
||||
UpdateController *updateController, DataParser *dataParser);
|
||||
void setClient(Client *value);
|
||||
private:
|
||||
UpdateController *updateController;
|
||||
RecognizeSystem *recognizeSystem;
|
||||
Client *client;
|
||||
|
||||
SendSystem *sendSystem;
|
||||
ServerLMSWidget *server;
|
||||
|
||||
void initClientType();
|
||||
void packetTypeInit(PacketType packet, Client *client);
|
||||
};
|
||||
|
||||
#endif // CLIENTHANDLER_H
|
||||
Reference in New Issue
Block a user