Merge pull request #8822 from FearlessTobi/multiplayer-fixes

network: Fixes and improvements to the room feature
This commit is contained in:
bunnei 2022-09-02 10:24:32 -07:00 committed by GitHub
commit 5addff8d59
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
28 changed files with 182 additions and 49 deletions

View file

@ -2,8 +2,6 @@
# SPDX-License-Identifier: GPL-2.0-or-later
add_library(core STATIC
announce_multiplayer_session.cpp
announce_multiplayer_session.h
arm/arm_interface.h
arm/arm_interface.cpp
arm/dynarmic/arm_dynarmic_32.cpp

View file

@ -1,164 +0,0 @@
// SPDX-FileCopyrightText: Copyright 2017 Citra Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include <chrono>
#include <future>
#include <vector>
#include "announce_multiplayer_session.h"
#include "common/announce_multiplayer_room.h"
#include "common/assert.h"
#include "common/settings.h"
#include "network/network.h"
#ifdef ENABLE_WEB_SERVICE
#include "web_service/announce_room_json.h"
#endif
namespace Core {
// Time between room is announced to web_service
static constexpr std::chrono::seconds announce_time_interval(15);
AnnounceMultiplayerSession::AnnounceMultiplayerSession(Network::RoomNetwork& room_network_)
: room_network{room_network_} {
#ifdef ENABLE_WEB_SERVICE
backend = std::make_unique<WebService::RoomJson>(Settings::values.web_api_url.GetValue(),
Settings::values.yuzu_username.GetValue(),
Settings::values.yuzu_token.GetValue());
#else
backend = std::make_unique<AnnounceMultiplayerRoom::NullBackend>();
#endif
}
WebService::WebResult AnnounceMultiplayerSession::Register() {
auto room = room_network.GetRoom().lock();
if (!room) {
return WebService::WebResult{WebService::WebResult::Code::LibError,
"Network is not initialized", ""};
}
if (room->GetState() != Network::Room::State::Open) {
return WebService::WebResult{WebService::WebResult::Code::LibError, "Room is not open", ""};
}
UpdateBackendData(room);
WebService::WebResult result = backend->Register();
if (result.result_code != WebService::WebResult::Code::Success) {
return result;
}
LOG_INFO(WebService, "Room has been registered");
room->SetVerifyUID(result.returned_data);
registered = true;
return WebService::WebResult{WebService::WebResult::Code::Success, "", ""};
}
void AnnounceMultiplayerSession::Start() {
if (announce_multiplayer_thread) {
Stop();
}
shutdown_event.Reset();
announce_multiplayer_thread =
std::make_unique<std::thread>(&AnnounceMultiplayerSession::AnnounceMultiplayerLoop, this);
}
void AnnounceMultiplayerSession::Stop() {
if (announce_multiplayer_thread) {
shutdown_event.Set();
announce_multiplayer_thread->join();
announce_multiplayer_thread.reset();
backend->Delete();
registered = false;
}
}
AnnounceMultiplayerSession::CallbackHandle AnnounceMultiplayerSession::BindErrorCallback(
std::function<void(const WebService::WebResult&)> function) {
std::lock_guard lock(callback_mutex);
auto handle = std::make_shared<std::function<void(const WebService::WebResult&)>>(function);
error_callbacks.insert(handle);
return handle;
}
void AnnounceMultiplayerSession::UnbindErrorCallback(CallbackHandle handle) {
std::lock_guard lock(callback_mutex);
error_callbacks.erase(handle);
}
AnnounceMultiplayerSession::~AnnounceMultiplayerSession() {
Stop();
}
void AnnounceMultiplayerSession::UpdateBackendData(std::shared_ptr<Network::Room> room) {
Network::RoomInformation room_information = room->GetRoomInformation();
std::vector<AnnounceMultiplayerRoom::Member> memberlist = room->GetRoomMemberList();
backend->SetRoomInformation(room_information.name, room_information.description,
room_information.port, room_information.member_slots,
Network::network_version, room->HasPassword(),
room_information.preferred_game);
backend->ClearPlayers();
for (const auto& member : memberlist) {
backend->AddPlayer(member);
}
}
void AnnounceMultiplayerSession::AnnounceMultiplayerLoop() {
// Invokes all current bound error callbacks.
const auto ErrorCallback = [this](WebService::WebResult result) {
std::lock_guard lock(callback_mutex);
for (auto callback : error_callbacks) {
(*callback)(result);
}
};
if (!registered) {
WebService::WebResult result = Register();
if (result.result_code != WebService::WebResult::Code::Success) {
ErrorCallback(result);
return;
}
}
auto update_time = std::chrono::steady_clock::now();
std::future<WebService::WebResult> future;
while (!shutdown_event.WaitUntil(update_time)) {
update_time += announce_time_interval;
auto room = room_network.GetRoom().lock();
if (!room) {
break;
}
if (room->GetState() != Network::Room::State::Open) {
break;
}
UpdateBackendData(room);
WebService::WebResult result = backend->Update();
if (result.result_code != WebService::WebResult::Code::Success) {
ErrorCallback(result);
}
if (result.result_string == "404") {
registered = false;
// Needs to register the room again
WebService::WebResult register_result = Register();
if (register_result.result_code != WebService::WebResult::Code::Success) {
ErrorCallback(register_result);
}
}
}
}
AnnounceMultiplayerRoom::RoomList AnnounceMultiplayerSession::GetRoomList() {
return backend->GetRoomList();
}
bool AnnounceMultiplayerSession::IsRunning() const {
return announce_multiplayer_thread != nullptr;
}
void AnnounceMultiplayerSession::UpdateCredentials() {
ASSERT_MSG(!IsRunning(), "Credentials can only be updated when session is not running");
#ifdef ENABLE_WEB_SERVICE
backend = std::make_unique<WebService::RoomJson>(Settings::values.web_api_url.GetValue(),
Settings::values.yuzu_username.GetValue(),
Settings::values.yuzu_token.GetValue());
#endif
}
} // namespace Core

View file

@ -1,98 +0,0 @@
// SPDX-FileCopyrightText: Copyright 2017 Citra Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <atomic>
#include <functional>
#include <memory>
#include <mutex>
#include <set>
#include <thread>
#include "common/announce_multiplayer_room.h"
#include "common/common_types.h"
#include "common/thread.h"
namespace Network {
class Room;
class RoomNetwork;
} // namespace Network
namespace Core {
/**
* Instruments AnnounceMultiplayerRoom::Backend.
* Creates a thread that regularly updates the room information and submits them
* An async get of room information is also possible
*/
class AnnounceMultiplayerSession {
public:
using CallbackHandle = std::shared_ptr<std::function<void(const WebService::WebResult&)>>;
AnnounceMultiplayerSession(Network::RoomNetwork& room_network_);
~AnnounceMultiplayerSession();
/**
* Allows to bind a function that will get called if the announce encounters an error
* @param function The function that gets called
* @return A handle that can be used the unbind the function
*/
CallbackHandle BindErrorCallback(std::function<void(const WebService::WebResult&)> function);
/**
* Unbind a function from the error callbacks
* @param handle The handle for the function that should get unbind
*/
void UnbindErrorCallback(CallbackHandle handle);
/**
* Registers a room to web services
* @return The result of the registration attempt.
*/
WebService::WebResult Register();
/**
* Starts the announce of a room to web services
*/
void Start();
/**
* Stops the announce to web services
*/
void Stop();
/**
* Returns a list of all room information the backend got
* @param func A function that gets executed when the async get finished, e.g. a signal
* @return a list of rooms received from the web service
*/
AnnounceMultiplayerRoom::RoomList GetRoomList();
/**
* Whether the announce session is still running
*/
bool IsRunning() const;
/**
* Recreates the backend, updating the credentials.
* This can only be used when the announce session is not running.
*/
void UpdateCredentials();
private:
void UpdateBackendData(std::shared_ptr<Network::Room> room);
void AnnounceMultiplayerLoop();
Common::Event shutdown_event;
std::mutex callback_mutex;
std::set<CallbackHandle> error_callbacks;
std::unique_ptr<std::thread> announce_multiplayer_thread;
/// Backend interface that logs fields
std::unique_ptr<AnnounceMultiplayerRoom::Backend> backend;
std::atomic_bool registered = false; ///< Whether the room has been registered
Network::RoomNetwork& room_network;
};
} // namespace Core

View file

@ -319,10 +319,19 @@ struct System::Impl {
if (app_loader->ReadTitle(name) != Loader::ResultStatus::Success) {
LOG_ERROR(Core, "Failed to read title for ROM (Error {})", load_result);
}
std::string title_version;
const FileSys::PatchManager pm(program_id, system.GetFileSystemController(),
system.GetContentProvider());
const auto metadata = pm.GetControlMetadata();
if (metadata.first != nullptr) {
title_version = metadata.first->GetVersionString();
}
if (auto room_member = room_network.GetRoomMember().lock()) {
Network::GameInfo game_info;
game_info.name = name;
game_info.id = program_id;
game_info.version = title_version;
room_member->SendGameInfo(game_info);
}

View file

@ -534,7 +534,7 @@ public:
private:
void CheckAvailability(Kernel::HLERequestContext& ctx) {
LOG_WARNING(Service_ACC, "(STUBBED) called");
LOG_DEBUG(Service_ACC, "(STUBBED) called");
IPC::ResponseBuilder rb{ctx, 3};
rb.Push(ResultSuccess);
rb.Push(false); // TODO: Check when this is supposed to return true and when not

View file

@ -113,7 +113,7 @@ enum class LinkLevel : s8 {
Bad,
Low,
Good,
Excelent,
Excellent,
};
struct NodeLatestUpdate {
@ -145,11 +145,19 @@ struct NetworkId {
static_assert(sizeof(NetworkId) == 0x20, "NetworkId is an invalid size");
struct Ssid {
u8 length;
std::array<char, SsidLengthMax + 1> raw;
u8 length{};
std::array<char, SsidLengthMax + 1> raw{};
Ssid() = default;
explicit Ssid(std::string_view data) {
length = static_cast<u8>(std::min(data.size(), SsidLengthMax));
data.copy(raw.data(), length);
raw[length] = 0;
}
std::string GetStringValue() const {
return std::string(raw.data(), length);
return std::string(raw.data());
}
};
static_assert(sizeof(Ssid) == 0x22, "Ssid is an invalid size");

View file

@ -933,7 +933,11 @@ BSD::BSD(Core::System& system_, const char* name)
}
}
BSD::~BSD() = default;
BSD::~BSD() {
if (auto room_member = room_network.GetRoomMember().lock()) {
room_member->Unbind(proxy_packet_received);
}
}
BSDCFG::BSDCFG(Core::System& system_) : ServiceFramework{system_, "bsdcfg"} {
// clang-format off

View file

@ -26,6 +26,12 @@ void ProxySocket::HandleProxyPacket(const ProxyPacket& packet) {
closed) {
return;
}
if (!broadcast && packet.broadcast) {
LOG_INFO(Network, "Received broadcast packet, but not configured for broadcast mode");
return;
}
std::lock_guard guard(packets_mutex);
received_packets.push(packet);
}
@ -203,7 +209,7 @@ std::pair<s32, Errno> ProxySocket::SendTo(u32 flags, const std::vector<u8>& mess
packet.local_endpoint = local_endpoint;
packet.remote_endpoint = *addr;
packet.protocol = protocol;
packet.broadcast = broadcast;
packet.broadcast = broadcast && packet.remote_endpoint.ip[3] == 255;
auto& ip = local_endpoint.ip;
auto ipv4 = Network::GetHostIPv4Address();