network: 能自动在30001/30002端口之间选择

This commit is contained in:
CalciteM Team 2019-07-28 19:45:49 +08:00
parent d10cc44d3d
commit 4e5170caf8
3 changed files with 24 additions and 16 deletions

View File

@ -41,15 +41,19 @@ AiThread::AiThread(int id, QObject *parent) :
connect(&timer, &QTimer::timeout, this, &AiThread::act, Qt::QueuedConnection); connect(&timer, &QTimer::timeout, this, &AiThread::act, Qt::QueuedConnection);
// 网络 // 网络
server = new Server(nullptr, 30000 + id); if (id == 1)
client = new Client(nullptr, id == 1? 30002 : 30001); {
server = new Server(nullptr, 30001);
uint16_t clientPort = server->getPort() == 30001 ? 30002 : 30001;
client = new Client(nullptr, clientPort);
}
} }
AiThread::~AiThread() AiThread::~AiThread()
{ {
// 网络相关 // 网络相关
delete server; //delete server;
delete client; //delete client;
stop(); stop();
quit(); quit();

View File

@ -902,10 +902,10 @@ bool GameController::command(const QString &cmd, bool update /* = true */)
if (isEngine1) if (isEngine1)
{ {
ai1.getServer()->setAction(cmd); ai1.getServer()->setAction(cmd);
qDebug() << "AI(1) set Action: " << cmd; qDebug() << "isEngine1: AI(1) set Action: " << cmd;
} else if (isEngine2) { } else if (isEngine2) {
ai2.getServer()->setAction(cmd); ai1.getServer()->setAction(cmd); // 注意: 同样是AI1
qDebug() << "AI(2) set Action: " << cmd; qDebug() << "isEngine2: AI(1) set Action: " << cmd;
} }
return true; return true;
@ -1080,7 +1080,4 @@ void GameController::showNetworkWindow()
{ {
ai1.getServer()->show(); ai1.getServer()->show();
ai1.getClient()->show(); ai1.getClient()->show();
ai2.getServer()->show();
ai2.getClient()->show();
} }

View File

@ -111,11 +111,19 @@ void Server::sessionOpened()
tcpServer = new QTcpServer(this); tcpServer = new QTcpServer(this);
if (!tcpServer->listen(QHostAddress::LocalHost, port)) { if (!tcpServer->listen(QHostAddress::LocalHost, port)) {
QMessageBox::critical(this, tr("Server"), port++;
tr("Unable to start the server: %1.") if (!tcpServer->listen(QHostAddress::LocalHost, port)) {
.arg(tcpServer->errorString())); QMessageBox::critical(this, tr("Server"),
close(); tr("Unable to start the server: %1.")
return; .arg(tcpServer->errorString()));
close();
return;
} else {
QMessageBox::information(this, tr("Server"), tr("server Started %1.").arg(port));
}
}
else {
QMessageBox::information(this, tr("Server"), tr("server Started %1.").arg(port));
} }
QString ipAddress; QString ipAddress;
@ -159,4 +167,3 @@ void Server::sendAction()
clientConnection->write(block); clientConnection->write(block);
clientConnection->disconnectFromHost(); clientConnection->disconnectFromHost();
} }