Revert "android: Fix multiplayer implementation issues"

This reverts commit 02035b5a08.
This commit is contained in:
Zephyron 2025-01-31 16:38:46 +10:00
parent 4c8033d156
commit 81448f4c31
No known key found for this signature in database
GPG key ID: 2177ADED8AC966AF
3 changed files with 35 additions and 50 deletions

View file

@ -1,6 +1,6 @@
// SPDX-FileCopyrightText: Copyright 2024 Mandarine Project
// SPDX-FileCopyrightText: Copyright 2025 citron Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright 2024 Mandarine Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
package org.citron.citron_emu.utils
@ -96,21 +96,15 @@ object NetPlayManager {
when (type) {
NetPlayStatus.CHAT_MESSAGE -> {
try {
val parts = msg.split(":", limit = 2)
if (parts.size == 2) {
val nickname = parts[0].trim()
val chatMessage = parts[1].trim()
addChatMessage(ChatMessage(
nickname = nickname,
username = "",
message = chatMessage
))
} else {
Log.e("NetPlayManager", "Invalid chat message format: $msg")
}
} catch (e: Exception) {
Log.e("NetPlayManager", "Error processing chat message", e)
val parts = msg.split(":", limit = 2)
if (parts.size == 2) {
val nickname = parts[0].trim()
val chatMessage = parts[1].trim()
addChatMessage(ChatMessage(
nickname = nickname,
username = "",
message = chatMessage
))
}
}
NetPlayStatus.MEMBER_JOIN,
@ -125,12 +119,13 @@ object NetPlayManager {
}
}
// Show toast on UI thread if chat isn't open
Handler(Looper.getMainLooper()).post {
if (!isChatOpen) {
Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
Handler(Looper.getMainLooper()).post {
if (!isChatOpen) {
Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
}
}
}
messageListener?.invoke(type, msg)
adapterRefreshListener?.invoke(type, msg)

View file

@ -1,7 +1,6 @@
// SPDX-FileCopyrightText: Copyright 2025 citra Emulator Project
// SPDX-FileCopyrightText: Copyright 2025 mandarine Emulator Project
// SPDX-FileCopyrightText: Copyright 2025 citron Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright 2025 Citra Project / Mandarine Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include "multiplayer.h"
#include "core/core.h"
#include "network/network.h"
@ -140,11 +139,6 @@ const AnnounceMultiplayerRoom::GameInfo game{
NetPlayStatus NetPlayCreateRoom(const std::string& ipaddress, int port, const std::string& username,
const std::string& password, const std::string& room_name,
int max_players) {
// Validate input parameters
if (ipaddress.empty() || port <= 0 || port > 65535 || username.empty() ||
room_name.length() < 3 || room_name.length() > 20 || max_players < 2 || max_players > 16) {
return NetPlayStatus::CREATE_ROOM_ERROR;
}
auto member = Network::RoomNetwork().GetRoomMember().lock();
if (!member) {
@ -169,9 +163,13 @@ NetPlayStatus NetPlayCreateRoom(const std::string& ipaddress, int port, const st
return NetPlayStatus::CREATE_ROOM_ERROR;
}
// Add timeout for room creation
const int MAX_CREATION_ATTEMPTS = 10;
for (int i = 0; i < MAX_CREATION_ATTEMPTS; i++) {
// Failsafe timer to avoid joining before creation
std::this_thread::sleep_for(std::chrono::milliseconds(100));
member->Join(username, ipaddress.c_str(), port, 0, Network::NoPreferredIP, password);
// Failsafe timer to avoid joining before creation
for (int i = 0; i < 5; i++) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
if (member->GetState() == Network::RoomMember::State::Joined ||
member->GetState() == Network::RoomMember::State::Moderator) {
@ -179,7 +177,7 @@ NetPlayStatus NetPlayCreateRoom(const std::string& ipaddress, int port, const st
}
}
// Cleanup on failure
// If join failed while room is created, clean up the room
room->Destroy();
return NetPlayStatus::CREATE_ROOM_ERROR;
}
@ -291,18 +289,14 @@ bool NetPlayIsHostedRoom() {
void NetPlayLeaveRoom() {
if (auto room = Network::RoomNetwork().GetRoom().lock()) {
// Clear chat before leaving to prevent memory leaks
ClearChat();
// Leave room first if connected
// if you are in a room, leave it
if (auto member = Network::RoomNetwork().GetRoomMember().lock()) {
member->Leave();
// Wait for leave to complete
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
// Then destroy room if hosting
ClearChat();
// if you are hosting a room, also stop hosting
if (room->GetState() == Network::Room::State::Open) {
room->Destroy();
}

View file

@ -1,5 +1,4 @@
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-FileCopyrightText: Copyright 2025 citron Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include <jni.h>
@ -570,11 +569,8 @@ jint JNI_OnLoad(JavaVM* vm, void* reserved) {
// Initialize applets
Common::Android::SoftwareKeyboard::InitJNI(env);
// Initialize network for multiplayer - add error handling
if (!NetworkInit()) {
LOG_ERROR(Network, "Failed to initialize network for multiplayer");
// Don't fail JNI_OnLoad completely, just log the error
}
// Init network for multiplayer
NetworkInit();
return JNI_VERSION;
}