mirror of
https://gitea.msk.dinamika-avia.ru/Constanta-Design/RRJServer.git
synced 2026-03-28 19:55:48 +03:00
ref: clientMap to MultithreadServer
This commit is contained in:
@@ -1,14 +1,19 @@
|
||||
#include "multithreadserver.h"
|
||||
|
||||
MultiThreadServer::MultiThreadServer(ServerLMSWidget *widget,UpdateController *updateController,
|
||||
DataParser *dataParser,Logger *logger,QObject *parent):
|
||||
MultiThreadServer::MultiThreadServer(ServerLMSWidget *widget,UpdateController *updateController,ProcessingSystem *processingSystem,
|
||||
DataParser *dataParser,Logger *logger,qint16 hostPort, QObject *parent ):
|
||||
QTcpServer(parent),
|
||||
serverLmsWidget(widget),
|
||||
hostPort(hostPort),
|
||||
processingSystem(processingSystem),
|
||||
updateController(updateController),
|
||||
dataParser(dataParser),
|
||||
logger(logger)
|
||||
logger(logger),
|
||||
stateServer(stoped),
|
||||
stateBlockAutorization(unblocked)
|
||||
{
|
||||
connect(this,&MultiThreadServer::sigSendToLogger,logger,&Logger::addTextToLogger);
|
||||
clientsMap = new QMap<int,ClientHandler*>;
|
||||
}
|
||||
|
||||
void MultiThreadServer::incomingConnection(qintptr socketDesriptor)
|
||||
@@ -21,21 +26,94 @@ void MultiThreadServer::incomingConnection(qintptr socketDesriptor)
|
||||
emit sigInitClient(socketDesriptor,serverLmsWidget,updateController,dataParser,logger);
|
||||
disconnect(this,&MultiThreadServer::sigInitClient,newClient,&ClientHandler::initialize);
|
||||
|
||||
serverLmsWidget->addClient(socketDesriptor,newClient);
|
||||
addClient(socketDesriptor,newClient);
|
||||
|
||||
emit sigSendToLogger("To Client: " + QString(SERVER_HELLO));
|
||||
}
|
||||
|
||||
bool MultiThreadServer::startServer()
|
||||
{
|
||||
if(stateServer == stoped)
|
||||
{
|
||||
//connect(tcpServer, &QTcpServer::newConnection, this, &ServerLMSWidget::slotNewConnection,Qt::AutoConnection);
|
||||
|
||||
if(!listen(QHostAddress::Any, hostPort))
|
||||
{
|
||||
logger->addTextToLogger("SERVER: start ERROR");
|
||||
stateServer = stoped;
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
logger->addTextToLogger("SERVER: start OK");
|
||||
stateServer = started;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
bool MultiThreadServer::stopServer()
|
||||
{
|
||||
if(stateServer == started)
|
||||
{
|
||||
disableClients();
|
||||
|
||||
//Закрываем сервер
|
||||
close();
|
||||
stateServer = stoped;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
QMap<int, ClientHandler *> *MultiThreadServer::getClientsMap() const
|
||||
{
|
||||
return clientsMap;
|
||||
}
|
||||
|
||||
void MultiThreadServer::updateClientList()
|
||||
{
|
||||
serverLmsWidget->slotUpdateListClients();
|
||||
}
|
||||
|
||||
void MultiThreadServer::disableClients()
|
||||
{
|
||||
QByteArray arrayAnswer = dataParser->ClientAnswer()->notify(NOTIFY_SERVER_END);
|
||||
|
||||
//Закрываем все открытые сокеты
|
||||
foreach(int idSocket, clientsMap->keys())
|
||||
{
|
||||
ClientHandler* handler = (*clientsMap)[idSocket];
|
||||
|
||||
//Фиксируем время выхода Юнити-клиента
|
||||
if(handler->getClient()->GETTYPE() == TypeClientAutorization::TYPE_UNITY_CLIENT)
|
||||
{
|
||||
processingSystem->processingExitUnityClient(handler);
|
||||
}
|
||||
|
||||
handler->sigSendXmlAnswer(arrayAnswer, PacketType::TYPE_XMLANSWER);
|
||||
QString str = QString(arrayAnswer);
|
||||
processingSystem->processingClientDeAutorization(handler->getClient()->getLogin());
|
||||
|
||||
handler->sigSocketClose();
|
||||
//clientsMap.remove(idSocket);
|
||||
removeClient(idSocket);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void MultiThreadServer::slotDisconnectClient(QString peerAddress, QString peerPort)
|
||||
{
|
||||
auto clientsMap = serverLmsWidget->getClientsMap();
|
||||
QString login = "";
|
||||
qDebug() << peerAddress << " " << peerPort << " " << "disconnect";
|
||||
|
||||
//Проходим все открытые сокеты, ищем нужный
|
||||
foreach(int idSocket, clientsMap.keys())
|
||||
foreach(int idSocket, clientsMap->keys())
|
||||
{
|
||||
ClientHandler *client = clientsMap[idSocket];
|
||||
ClientHandler *client = (*clientsMap)[idSocket];
|
||||
|
||||
if(client->getClient()->getAddress() == peerAddress && client->getClient()->getPort() == peerPort)
|
||||
{
|
||||
@@ -43,9 +121,9 @@ void MultiThreadServer::slotDisconnectClient(QString peerAddress, QString peerPo
|
||||
|
||||
ClientDeAutorization clientDeAutorization;
|
||||
clientDeAutorization.Login = login;
|
||||
serverLmsWidget->getProcessingSystem()->processingClientDeAutorization(client, clientDeAutorization);
|
||||
processingSystem->processingClientDeAutorization(client, clientDeAutorization);
|
||||
|
||||
serverLmsWidget->removeClient(idSocket);
|
||||
removeClient(idSocket);
|
||||
delete client;
|
||||
continue;
|
||||
}
|
||||
@@ -59,3 +137,15 @@ void MultiThreadServer::slotDisconnectClient(QString peerAddress, QString peerPo
|
||||
serverLmsWidget->getProcessingSystem()->processingClientDeAutorization(login);
|
||||
}
|
||||
|
||||
void MultiThreadServer::removeClient(int idSocket)
|
||||
{
|
||||
clientsMap->remove(idSocket);
|
||||
serverLmsWidget->slotUpdateListClients();
|
||||
}
|
||||
|
||||
void MultiThreadServer::addClient(qintptr descriptor, ClientHandler *client)
|
||||
{
|
||||
(*clientsMap)[descriptor] = client;
|
||||
serverLmsWidget->slotUpdateListClients();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user