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);
// 网络
server = new Server(nullptr, 30000 + id);
client = new Client(nullptr, id == 1? 30002 : 30001);
if (id == 1)
{
server = new Server(nullptr, 30001);
uint16_t clientPort = server->getPort() == 30001 ? 30002 : 30001;
client = new Client(nullptr, clientPort);
}
}
AiThread::~AiThread()
{
// 网络相关
delete server;
delete client;
//delete server;
//delete client;
stop();
quit();

View File

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

View File

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