Make "Reconnect to Server" option work if initial socket conn failed

This commit is contained in:
Nick Renieris 2022-06-25 03:46:58 +03:00
parent 8f21d43bc3
commit 23cdf1442d
4 changed files with 38 additions and 28 deletions

View file

@ -252,6 +252,7 @@ class Client {
int mServerPort = 0; int mServerPort = 0;
bool waitForGameInit = true;
bool isFirstConnect = true; bool isFirstConnect = true;
// --- Game Layouts --- // --- Game Layouts ---

View file

@ -215,14 +215,16 @@ bool Client::startConnection() {
Client::openKeyboardPort(); Client::openKeyboardPort();
} }
bool result = mSocket->init(mServerIP.cstr(), mServerPort).isSuccess(); bool socketConnected = mSocket->isConnected();
if (!socketConnected) {
socketConnected = mSocket->init(mServerIP.cstr(), mServerPort).isSuccess();
}
if (result) { bool clientConnected = false;
if (socketConnected) {
// wait for client init packet // wait for client init packet
while (true) { while (true) {
if (mSocket->RECV()) { if (mSocket->RECV()) {
Packet* curPacket = mSocket->mPacketQueue.popFront(); Packet* curPacket = mSocket->mPacketQueue.popFront();
if (curPacket->mType == PacketType::CLIENTINIT) { if (curPacket->mType == PacketType::CLIENTINIT) {
@ -232,21 +234,20 @@ bool Client::startConnection() {
maxPuppets = initPacket->maxPlayers - 1; maxPuppets = initPacket->maxPlayers - 1;
}else { clientConnected = true;
} else {
Logger::log("First Packet was not Init!\n"); Logger::log("First Packet was not Init!\n");
result = false; clientConnected = false;
} }
free(curPacket); free(curPacket);
} }
break; break;
} }
} }
return clientConnected;
return result;
} }
/** /**
@ -317,9 +318,10 @@ void Client::openKeyboardPort() {
*/ */
void Client::readFunc() { void Client::readFunc() {
if (isFirstConnect) { if (waitForGameInit) {
nn::os::YieldThread(); // sleep the thread for the first thing we do so that game init can finish nn::os::YieldThread(); // sleep the thread for the first thing we do so that game init can finish
nn::os::SleepThread(nn::TimeSpan::FromSeconds(2)); nn::os::SleepThread(nn::TimeSpan::FromSeconds(2));
waitForGameInit = false;
} }
// we can use the start of readFunc to display an al::WindowConfirmWait while the server // we can use the start of readFunc to display an al::WindowConfirmWait while the server
@ -398,7 +400,8 @@ void Client::readFunc() {
mSocket->SEND(&initPacket); // re-send init packet as reconnect packet mSocket->SEND(&initPacket); // re-send init packet as reconnect packet
mConnectionWait->tryEnd(); mConnectionWait->tryEnd();
continue; continue;
} else {
Logger::log("%s: not reconnected\n", __func__);
} }
nn::os::YieldThread(); // if we're currently waiting on the socket to be initialized, wait until it is nn::os::YieldThread(); // if we're currently waiting on the socket to be initialized, wait until it is
@ -449,7 +452,7 @@ void Client::readFunc() {
updateShineInfo((ShineCollect*)curPacket); updateShineInfo((ShineCollect*)curPacket);
break; break;
case PacketType::PLAYERDC: case PacketType::PLAYERDC:
Logger::log("Recieved Player Disconnect!\n"); Logger::log("Received Player Disconnect!\n");
curPacket->mUserID.print(); curPacket->mUserID.print();
disconnectPlayer((PlayerDC*)curPacket); disconnectPlayer((PlayerDC*)curPacket);
break; break;

View file

@ -17,8 +17,7 @@ nn::Result SocketClient::init(const char* ip, u16 port) {
in_addr hostAddress = { 0 }; in_addr hostAddress = { 0 };
sockaddr serverAddress = { 0 }; sockaddr serverAddress = { 0 };
if (socket_log_state != SOCKET_LOG_UNINITIALIZED && socket_log_state != SOCKET_LOG_DISCONNECTED) Logger::log("SocketClient::init: %s:%d sock %s\n", ip, port, getStateChar());
return -1;
nn::nifm::Initialize(); nn::nifm::Initialize();
nn::nifm::SubmitNetworkRequest(); nn::nifm::SubmitNetworkRequest();

View file

@ -193,13 +193,20 @@ void StageSceneStateServerConfig::exeOpenKeyboardPort() {
void StageSceneStateServerConfig::exeRestartServer() { void StageSceneStateServerConfig::exeRestartServer() {
if (al::isFirstStep(this)) { if (al::isFirstStep(this)) {
mCurrentList->deactivate(); mCurrentList->deactivate();
Logger::log("Stopping connection\n");
Client::stopConnection(); Client::stopConnection();
} }
if (Client::isSocketActive()) { auto* client = Client::sInstance;
al::startHitReaction(mCurrentMenu, "リセット", 0);
al::setNerve(this, &nrvStageSceneStateServerConfigMainMenu); if (client) {
Logger::log("%s: Socket state: %s\n", __func__, client->mSocket->getStateChar());
client->StartThreads();
} }
al::startHitReaction(mCurrentMenu, "リセット", 0);
al::setNerve(this, &nrvStageSceneStateServerConfigMainMenu);
} }
void StageSceneStateServerConfig::exeGamemodeConfig() { void StageSceneStateServerConfig::exeGamemodeConfig() {