diff --git a/include/server/Client.hpp b/include/server/Client.hpp index 14a10e2..d3bf076 100644 --- a/include/server/Client.hpp +++ b/include/server/Client.hpp @@ -2,9 +2,9 @@ * @file server/Client.hpp * @author CraftyBoss (https://github.com/CraftyBoss) * @brief main class responsible for handing all client-server related communications, as well as any gamemodes. - * + * * @copyright Copyright (c) 2022 - * + * */ #pragma once @@ -92,7 +92,7 @@ class Client { bool isShineCollected(int shineId); static void initMode(GameModeInitInfo const &initInfo); - + static void sendHackCapInfPacket(const HackCap *hackCap); static void sendPlayerInfPacket(const PlayerActorHakoniwa *player); static void sendGameInfPacket(const PlayerActorHakoniwa *player, GameDataHolderAccessor holder); @@ -191,7 +191,7 @@ class Client { static void tryRestartCurrentMode(); static bool isModeActive() { return sInstance ? sInstance->mIsModeActive : false; } - + static bool isSelectedMode(GameMode mode) { return sInstance ? sInstance->mCurMode->getMode() == mode: false; } void resetCollectedShines(); @@ -224,7 +224,7 @@ class Client { // currently, only readThread is used to recieve and update PuppetInfo, while the main game thread is used to send packets without queueing them up first, which might cause performance issues al::AsyncFunctorThread *mReadThread = nullptr; // TODO: use this thread to send any queued packets // al::AsyncFunctorThread *mRecvThread; // TODO: use this thread to recieve packets and update PuppetInfo - + sead::SafeArray puppetPlayerID; int mConnectCount = 0; @@ -233,10 +233,10 @@ class Client { sead::FixedSafeString<0x20> mUsername; - // --- Server Syncing Members --- - + // --- Server Syncing Members --- + // array of shine IDs for checking if multiple shines have been collected in quick sucession, all moons within the players stage that match the ID will be deleted - sead::SafeArray curCollectedShines; + sead::SafeArray curCollectedShines; int collectedShineCount = 0; int lastCollectedShine = -1; @@ -252,6 +252,7 @@ class Client { int mServerPort = 0; + bool waitForGameInit = true; bool isFirstConnect = true; // --- Game Layouts --- diff --git a/source/server/Client.cpp b/source/server/Client.cpp index 010436b..2eff7b7 100644 --- a/source/server/Client.cpp +++ b/source/server/Client.cpp @@ -215,14 +215,16 @@ bool Client::startConnection() { 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 while (true) { - if (mSocket->RECV()) { - Packet* curPacket = mSocket->mPacketQueue.popFront(); if (curPacket->mType == PacketType::CLIENTINIT) { @@ -232,21 +234,20 @@ bool Client::startConnection() { maxPuppets = initPacket->maxPlayers - 1; - }else { + clientConnected = true; + } else { Logger::log("First Packet was not Init!\n"); - result = false; + clientConnected = false; } free(curPacket); - } break; } } - - return result; + return clientConnected; } /** @@ -317,9 +318,10 @@ void Client::openKeyboardPort() { */ 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::SleepThread(nn::TimeSpan::FromSeconds(2)); + waitForGameInit = false; } // 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 mConnectionWait->tryEnd(); 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 @@ -449,7 +452,7 @@ void Client::readFunc() { updateShineInfo((ShineCollect*)curPacket); break; case PacketType::PLAYERDC: - Logger::log("Recieved Player Disconnect!\n"); + Logger::log("Received Player Disconnect!\n"); curPacket->mUserID.print(); disconnectPlayer((PlayerDC*)curPacket); break; diff --git a/source/server/SocketClient.cpp b/source/server/SocketClient.cpp index 8dd50d8..bcf5a82 100644 --- a/source/server/SocketClient.cpp +++ b/source/server/SocketClient.cpp @@ -17,8 +17,7 @@ nn::Result SocketClient::init(const char* ip, u16 port) { in_addr hostAddress = { 0 }; sockaddr serverAddress = { 0 }; - if (socket_log_state != SOCKET_LOG_UNINITIALIZED && socket_log_state != SOCKET_LOG_DISCONNECTED) - return -1; + Logger::log("SocketClient::init: %s:%d sock %s\n", ip, port, getStateChar()); nn::nifm::Initialize(); nn::nifm::SubmitNetworkRequest(); diff --git a/source/states/StageSceneStateServerConfig.cpp b/source/states/StageSceneStateServerConfig.cpp index 230b55a..5dcb513 100644 --- a/source/states/StageSceneStateServerConfig.cpp +++ b/source/states/StageSceneStateServerConfig.cpp @@ -80,7 +80,7 @@ StageSceneStateServerConfig::StageSceneStateServerConfig(const char *name, al::S mCurrentMenu = mMainOptions; } -void StageSceneStateServerConfig::init() { +void StageSceneStateServerConfig::init() { initNerve(&nrvStageSceneStateServerConfigMainMenu, 0); } @@ -193,13 +193,20 @@ void StageSceneStateServerConfig::exeOpenKeyboardPort() { void StageSceneStateServerConfig::exeRestartServer() { if (al::isFirstStep(this)) { mCurrentList->deactivate(); + Logger::log("Stopping connection\n"); Client::stopConnection(); } - if (Client::isSocketActive()) { - al::startHitReaction(mCurrentMenu, "リセット", 0); - al::setNerve(this, &nrvStageSceneStateServerConfigMainMenu); + auto* client = Client::sInstance; + + 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() { @@ -210,7 +217,7 @@ void StageSceneStateServerConfig::exeGamemodeConfig() { } subMenuUpdate(); - + if (mIsDecideConfig && mCurrentList->isDecideEnd()) { if (mGamemodeConfigMenu->updateMenu(mCurrentList->mCurSelected)) { endSubMenu(); @@ -220,7 +227,7 @@ void StageSceneStateServerConfig::exeGamemodeConfig() { void StageSceneStateServerConfig::exeGamemodeSelect() { if (al::isFirstStep(this)) { - + mCurrentList = mModeSelectList; mCurrentMenu = mModeSelect;