network: 将 Server/Client 实例改放到 AI 线程类
This commit is contained in:
parent
6b8a22e528
commit
0b2b043f96
|
@ -39,10 +39,18 @@ AiThread::AiThread(int id, QObject *parent) :
|
|||
|
||||
// 连接定时器处理函数
|
||||
connect(&timer, &QTimer::timeout, this, &AiThread::act, Qt::QueuedConnection);
|
||||
|
||||
// 网络
|
||||
server = new Server(nullptr, 30000 + id);
|
||||
client = new Client(nullptr, id == 1? 30002 : 30001);
|
||||
}
|
||||
|
||||
AiThread::~AiThread()
|
||||
{
|
||||
// 网络相关
|
||||
delete server;
|
||||
delete client;
|
||||
|
||||
stop();
|
||||
quit();
|
||||
wait();
|
||||
|
|
|
@ -28,6 +28,8 @@
|
|||
#include <QTimer>
|
||||
#include "ninechess.h"
|
||||
#include "ninechessai_ab.h"
|
||||
#include "server.h"
|
||||
#include "client.h"
|
||||
|
||||
class AiThread : public QThread
|
||||
{
|
||||
|
@ -55,6 +57,17 @@ public:
|
|||
void setAi(const NineChess &chess);
|
||||
void setAi(const NineChess &chess, int depth, int time);
|
||||
|
||||
// 网络
|
||||
Server *getServer()
|
||||
{
|
||||
return server;
|
||||
}
|
||||
|
||||
Client *getClient()
|
||||
{
|
||||
return client;
|
||||
}
|
||||
|
||||
// 深度和限时
|
||||
void getDepthTime(int &depth, int &time)
|
||||
{
|
||||
|
@ -102,6 +115,10 @@ private:
|
|||
|
||||
// 定时器
|
||||
QTimer timer;
|
||||
|
||||
// 网络
|
||||
Server *server;
|
||||
Client *client;
|
||||
};
|
||||
|
||||
#endif // AITHREAD_H
|
||||
|
|
|
@ -23,7 +23,7 @@
|
|||
|
||||
#include "client.h"
|
||||
|
||||
Client::Client(QWidget *parent)
|
||||
Client::Client(QWidget *parent, uint16_t port)
|
||||
: QDialog(parent)
|
||||
, hostCombo(new QComboBox)
|
||||
, portLineEdit(new QLineEdit)
|
||||
|
@ -92,7 +92,7 @@ Client::Client(QWidget *parent)
|
|||
|
||||
setWindowTitle(QGuiApplication::applicationDisplayName());
|
||||
portLineEdit->setFocus();
|
||||
portLineEdit->setText("33333");
|
||||
portLineEdit->setText(QString::number(port));
|
||||
|
||||
QNetworkConfigurationManager manager;
|
||||
|
||||
|
|
|
@ -39,7 +39,7 @@ class Client : public QDialog
|
|||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit Client(QWidget *parent = nullptr);
|
||||
explicit Client(QWidget *parent = nullptr, uint16_t port = 33333);
|
||||
|
||||
private slots:
|
||||
void requestNewAction();
|
||||
|
@ -47,6 +47,14 @@ private slots:
|
|||
void displayError(QAbstractSocket::SocketError socketError);
|
||||
void enableGetActionButton();
|
||||
void sessionOpened();
|
||||
void setPort(uint16_t port)
|
||||
{
|
||||
this->port = port;
|
||||
}
|
||||
uint16_t getPort()
|
||||
{
|
||||
return port;
|
||||
}
|
||||
|
||||
private:
|
||||
QComboBox *hostCombo = nullptr;
|
||||
|
@ -59,6 +67,8 @@ private:
|
|||
QString currentAction;
|
||||
|
||||
QNetworkSession *networkSession = nullptr;
|
||||
|
||||
uint16_t port;
|
||||
};
|
||||
|
||||
#endif // CLIENT_H
|
||||
|
|
|
@ -75,11 +75,7 @@ GameController::GameController(GameScene & scene, QObject * parent) :
|
|||
|
||||
// 安装事件过滤器监视scene的各个事件,
|
||||
// 由于我重载了QGraphicsScene,相关事件在重载函数中已设定,不必安装监视器。
|
||||
//scene.installEventFilter(this);
|
||||
|
||||
// 网络
|
||||
server = new Server();
|
||||
client = new Client();
|
||||
//scene.installEventFilter(this);
|
||||
}
|
||||
|
||||
GameController::~GameController()
|
||||
|
@ -94,10 +90,6 @@ GameController::~GameController()
|
|||
ai1.wait();
|
||||
ai2.wait();
|
||||
|
||||
// 网络相关
|
||||
delete server;
|
||||
delete client;
|
||||
|
||||
#ifdef BOOK_LEARNING
|
||||
NineChessAi_ab::recordOpeningBookHashMapToFile();
|
||||
#endif /* BOOK_LEARNING */
|
||||
|
@ -907,7 +899,12 @@ bool GameController::command(const QString &cmd, bool update /* = true */)
|
|||
}
|
||||
|
||||
// 网络: 将着法放到服务器的发送列表中
|
||||
this->server->setAction(cmd);
|
||||
if (isEngine1)
|
||||
{
|
||||
ai1.getServer()->setAction(cmd);
|
||||
} else if (isEngine2) {
|
||||
ai1.getServer()->setAction(cmd);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -1079,6 +1076,9 @@ bool GameController::updateScence(NineChess &chess)
|
|||
|
||||
void GameController::showNetworkWindow()
|
||||
{
|
||||
server->show();
|
||||
client->show();
|
||||
ai1.getServer()->show();
|
||||
ai1.getClient()->show();
|
||||
|
||||
ai2.getServer()->show();
|
||||
ai2.getClient()->show();
|
||||
}
|
||||
|
|
|
@ -247,10 +247,6 @@ private:
|
|||
|
||||
// 棋谱字符串列表模型
|
||||
QStringListModel manualListModel;
|
||||
|
||||
// 网络
|
||||
Server *server;
|
||||
Client *client;
|
||||
};
|
||||
|
||||
#endif // GAMECONTROLLER_H
|
||||
|
|
|
@ -24,13 +24,15 @@
|
|||
|
||||
#include "server.h"
|
||||
|
||||
Server::Server(QWidget *parent)
|
||||
Server::Server(QWidget *parent, uint16_t port)
|
||||
: QDialog(parent)
|
||||
, statusLabel(new QLabel)
|
||||
{
|
||||
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
|
||||
statusLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
|
||||
|
||||
this->port = port;
|
||||
|
||||
QNetworkConfigurationManager manager;
|
||||
|
||||
if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) {
|
||||
|
@ -108,7 +110,7 @@ void Server::sessionOpened()
|
|||
|
||||
tcpServer = new QTcpServer(this);
|
||||
|
||||
if (!tcpServer->listen(QHostAddress::LocalHost, 33333)) {
|
||||
if (!tcpServer->listen(QHostAddress::LocalHost, port)) {
|
||||
QMessageBox::critical(this, tr("Server"),
|
||||
tr("Unable to start the server: %1.")
|
||||
.arg(tcpServer->errorString()));
|
||||
|
|
|
@ -36,8 +36,16 @@ class Server : public QDialog
|
|||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit Server(QWidget *parent = nullptr);
|
||||
explicit Server(QWidget *parent = nullptr, uint16_t port = 33333);
|
||||
void setAction(const QString &action);
|
||||
void setPort(uint16_t port)
|
||||
{
|
||||
this->port = port;
|
||||
}
|
||||
uint16_t getPort()
|
||||
{
|
||||
return port;
|
||||
}
|
||||
|
||||
private slots:
|
||||
void sessionOpened();
|
||||
|
@ -49,6 +57,7 @@ private:
|
|||
QVector<QString> actions;
|
||||
QString action;
|
||||
QNetworkSession *networkSession = nullptr;
|
||||
uint16_t port;
|
||||
};
|
||||
|
||||
#endif // SERVER_H
|
||||
|
|
Loading…
Reference in New Issue