mirror of
https://gitea.msk.dinamika-avia.ru/Constanta-Design/RRJServer.git
synced 2026-03-27 19:45:43 +03:00
ref: clientMap to MultithreadServer
This commit is contained in:
@@ -11,10 +11,7 @@
|
||||
ServerLMSWidget::ServerLMSWidget(QWidget *parent) :
|
||||
QWidget(parent),
|
||||
ui(new Ui::ServerLMSWidget),
|
||||
tcpServer(nullptr),
|
||||
hostPort(6000),
|
||||
stateServer(stoped),
|
||||
stateBlockAutorization(unblocked),
|
||||
server(nullptr),
|
||||
updateThread(nullptr),
|
||||
loggerThread(nullptr),
|
||||
dataParser(nullptr),
|
||||
@@ -63,13 +60,14 @@ ServerLMSWidget::ServerLMSWidget(QWidget *parent) :
|
||||
dataParser = new DataParser(assetsManager,processingSystem);
|
||||
|
||||
commonClientHandler = new CommonClientHandler;
|
||||
server = new MultiThreadServer(this,updateController,processingSystem,dataParser,logger,6000);
|
||||
|
||||
loggerThread->start();
|
||||
updateThread->start();
|
||||
|
||||
commonClientHandler->initialize(&clientsMap,processingSystem,dataParser,logger);
|
||||
processingSystem->initialize(this,dataParser,commonClientHandler,logger,updateController,chatSystem);
|
||||
chatSystem->initialize(commonClientHandler,dataParser,&clientsMap);
|
||||
commonClientHandler->initialize(server->getClientsMap(),processingSystem,dataParser,logger);
|
||||
processingSystem->initialize(server,dataParser,commonClientHandler,logger,updateController,chatSystem);
|
||||
chatSystem->initialize(commonClientHandler,dataParser,server->getClientsMap());
|
||||
|
||||
logger->setTypeLog("widget");
|
||||
connect(dataParser,&DataParser::sigLogMessage,logger,&Logger::addTextToLogger);
|
||||
@@ -93,127 +91,43 @@ void ServerLMSWidget::setError(int code)
|
||||
|
||||
ServerLMSWidget::~ServerLMSWidget()
|
||||
{
|
||||
stopServer();
|
||||
server->stopServer();
|
||||
updateThread->exit();
|
||||
loggerThread->exit();
|
||||
delete ui;
|
||||
}
|
||||
|
||||
bool ServerLMSWidget::startServer()
|
||||
{
|
||||
if(stateServer == stoped)
|
||||
{
|
||||
tcpServer = new MultiThreadServer(this,updateController,dataParser,logger);
|
||||
|
||||
//connect(tcpServer, &QTcpServer::newConnection, this, &ServerLMSWidget::slotNewConnection,Qt::AutoConnection);
|
||||
|
||||
if(!tcpServer->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 ServerLMSWidget::stopServer()
|
||||
{
|
||||
if(stateServer == started)
|
||||
{
|
||||
QByteArray arrayAnswer = dataParser->ClientAnswer()->notify(NOTIFY_SERVER_END);
|
||||
|
||||
//Закрываем все открытые сокеты
|
||||
foreach(int idSocket, clientsMap.keys())
|
||||
{
|
||||
ClientHandler* clientHandlerOpen = clientsMap[idSocket];
|
||||
|
||||
//Фиксируем время выхода Юнити-клиента
|
||||
if(clientHandlerOpen->getClient()->GETTYPE() == TypeClientAutorization::TYPE_UNITY_CLIENT)
|
||||
{
|
||||
processingSystem->processingExitUnityClient(clientHandlerOpen);
|
||||
}
|
||||
|
||||
clientHandlerOpen->sigSendXmlAnswer(arrayAnswer, PacketType::TYPE_XMLANSWER);
|
||||
//while (!clientsMap[idSocket]->sigSocketFlush()) {}
|
||||
|
||||
QString str = QString(arrayAnswer);
|
||||
emit sigLog("To Client: " + str);
|
||||
|
||||
//slotDisconnectClient(clientsMap[idSocket]->get, QString peerPort)
|
||||
processingSystem->processingClientDeAutorization(clientHandlerOpen->getClient()->getLogin());
|
||||
|
||||
clientHandlerOpen->sigSocketClose();
|
||||
//clientsMap.remove(idSocket);
|
||||
removeClient(idSocket);
|
||||
}
|
||||
|
||||
//Закрываем сервер
|
||||
tcpServer->close();
|
||||
stateServer = stoped;
|
||||
delete tcpServer;
|
||||
emit sigLog("Server is stopped");
|
||||
return true;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
Logger *ServerLMSWidget::getLogger() const
|
||||
{
|
||||
return logger;
|
||||
}
|
||||
|
||||
QMap<int, ClientHandler *> ServerLMSWidget::getClientsMap() const
|
||||
{
|
||||
return clientsMap;
|
||||
}
|
||||
|
||||
void ServerLMSWidget::slotUpdateListClients()
|
||||
{
|
||||
//Очищаем список
|
||||
ui->listWidget_Clients->clear();
|
||||
|
||||
//Проходим все открытые сокеты
|
||||
foreach(int idSocket, clientsMap.keys())
|
||||
for (const ClientHandler *handler : server->getClientsMap()->values())
|
||||
{
|
||||
//Добавляем в список Клиентов
|
||||
QString strClient = clientsMap[idSocket]->getClient()->getFullName();
|
||||
QString strClient = handler->getClient()->getFullName();
|
||||
|
||||
ui->listWidget_Clients->addItem(strClient);
|
||||
ui->listWidget_Clients->scrollToBottom();
|
||||
ui->listWidget_Clients->setCurrentRow(ui->listWidget_Clients->count() - 1);
|
||||
}
|
||||
|
||||
int countClients = clientsMap.count();
|
||||
int countClients = (*server->getClientsMap()).count();
|
||||
emit sigLog("SERVER: countClients = " + QString::number(countClients));
|
||||
}
|
||||
|
||||
void ServerLMSWidget::slot_BlockAutorization(bool block)
|
||||
{
|
||||
if(block)
|
||||
blockAutorization();
|
||||
server->blockAutorization();
|
||||
else
|
||||
unBlockAutorization();
|
||||
}
|
||||
|
||||
void ServerLMSWidget::removeClient(int idSocket)
|
||||
{
|
||||
clientsMap.remove(idSocket);
|
||||
slotUpdateListClients();
|
||||
}
|
||||
|
||||
void ServerLMSWidget::addClient(qintptr descriptor, ClientHandler *client)
|
||||
{
|
||||
clientsMap[descriptor] = client;
|
||||
slotUpdateListClients();
|
||||
server->unBlockAutorization();
|
||||
}
|
||||
|
||||
void ServerLMSWidget::slot_LanguageChanged(QString language)
|
||||
@@ -230,7 +144,7 @@ void ServerLMSWidget::slotAddToLog(QString msg)
|
||||
|
||||
void ServerLMSWidget::on_btnStartServer_clicked()
|
||||
{
|
||||
if(startServer())
|
||||
if(server->startServer())
|
||||
{
|
||||
QApplication::setOverrideCursor(Qt::WaitCursor);
|
||||
QApplication::restoreOverrideCursor();
|
||||
@@ -243,7 +157,7 @@ void ServerLMSWidget::on_btnStartServer_clicked()
|
||||
|
||||
void ServerLMSWidget::on_btnStopServer_clicked()
|
||||
{
|
||||
if(stopServer())
|
||||
if(server->stopServer())
|
||||
{
|
||||
ui->btnStopServer->setEnabled(false);
|
||||
ui->btnStartServer->setEnabled(true);
|
||||
|
||||
Reference in New Issue
Block a user