Address some review comments

This commit is contained in:
FearlessTobi 2022-09-12 22:39:18 +02:00
parent aa11d73bba
commit 4213f1c126
7 changed files with 38 additions and 52 deletions

View file

@ -35,12 +35,9 @@ void LanStation::OverrideInfo() {
LANDiscovery::LANDiscovery(Network::RoomNetwork& room_network_) LANDiscovery::LANDiscovery(Network::RoomNetwork& room_network_)
: stations({{{1, this}, {2, this}, {3, this}, {4, this}, {5, this}, {6, this}, {7, this}}}), : stations({{{1, this}, {2, this}, {3, this}, {4, this}, {5, this}, {6, this}, {7, this}}}),
room_network{room_network_} { room_network{room_network_} {}
LOG_INFO(Service_LDN, "LANDiscovery");
}
LANDiscovery::~LANDiscovery() { LANDiscovery::~LANDiscovery() {
LOG_INFO(Service_LDN, "~LANDiscovery");
if (inited) { if (inited) {
Result rc = Finalize(); Result rc = Finalize();
LOG_INFO(Service_LDN, "Finalize: {}", rc.raw); LOG_INFO(Service_LDN, "Finalize: {}", rc.raw);
@ -62,7 +59,7 @@ void LANDiscovery::InitNetworkInfo() {
} }
void LANDiscovery::InitNodeStateChange() { void LANDiscovery::InitNodeStateChange() {
for (auto& node_update : nodeChanges) { for (auto& node_update : node_changes) {
node_update.state_change = NodeStateChange::None; node_update.state_change = NodeStateChange::None;
} }
for (auto& node_state : node_last_states) { for (auto& node_state : node_last_states) {
@ -97,8 +94,8 @@ Result LANDiscovery::GetNetworkInfo(NetworkInfo& out_network,
if (state == State::AccessPointCreated || state == State::StationConnected) { if (state == State::AccessPointCreated || state == State::StationConnected) {
std::memcpy(&out_network, &network_info, sizeof(network_info)); std::memcpy(&out_network, &network_info, sizeof(network_info));
for (std::size_t i = 0; i < buffer_count; i++) { for (std::size_t i = 0; i < buffer_count; i++) {
out_updates[i].state_change = nodeChanges[i].state_change; out_updates[i].state_change = node_changes[i].state_change;
nodeChanges[i].state_change = NodeStateChange::None; node_changes[i].state_change = NodeStateChange::None;
} }
return ResultSuccess; return ResultSuccess;
} }
@ -168,9 +165,9 @@ Result LANDiscovery::Scan(std::vector<NetworkInfo>& networks, u16& count,
return ResultSuccess; return ResultSuccess;
} }
Result LANDiscovery::SetAdvertiseData(std::vector<u8>& data) { Result LANDiscovery::SetAdvertiseData(std::span<const u8> data) {
std::scoped_lock lock{packet_mutex}; std::scoped_lock lock{packet_mutex};
std::size_t size = data.size(); const std::size_t size = data.size();
if (size > AdvertiseDataSizeMax) { if (size > AdvertiseDataSizeMax) {
return ResultAdvertiseDataTooLarge; return ResultAdvertiseDataTooLarge;
} }
@ -288,7 +285,7 @@ Result LANDiscovery::DestroyNetwork() {
ResetStations(); ResetStations();
SetState(State::AccessPointOpened); SetState(State::AccessPointOpened);
LanEvent(); lan_event();
return ResultSuccess; return ResultSuccess;
} }
@ -323,12 +320,12 @@ Result LANDiscovery::Disconnect() {
} }
SetState(State::StationOpened); SetState(State::StationOpened);
LanEvent(); lan_event();
return ResultSuccess; return ResultSuccess;
} }
Result LANDiscovery::Initialize(LanEventFunc lan_event, bool listening) { Result LANDiscovery::Initialize(LanEventFunc lan_event_, bool listening) {
std::scoped_lock lock{packet_mutex}; std::scoped_lock lock{packet_mutex};
if (inited) { if (inited) {
return ResultSuccess; return ResultSuccess;
@ -341,7 +338,7 @@ Result LANDiscovery::Initialize(LanEventFunc lan_event, bool listening) {
} }
connected_clients.clear(); connected_clients.clear();
LanEvent = lan_event; lan_event = lan_event_;
SetState(State::Initialized); SetState(State::Initialized);
@ -408,13 +405,13 @@ void LANDiscovery::OnDisconnectFromHost() {
host_ip = std::nullopt; host_ip = std::nullopt;
if (state == State::StationConnected) { if (state == State::StationConnected) {
SetState(State::StationOpened); SetState(State::StationOpened);
LanEvent(); lan_event();
} }
} }
void LANDiscovery::OnNetworkInfoChanged() { void LANDiscovery::OnNetworkInfoChanged() {
if (IsNodeStateChanged()) { if (IsNodeStateChanged()) {
LanEvent(); lan_event();
} }
return; return;
} }
@ -439,7 +436,6 @@ void LANDiscovery::SendPacket(Network::LDNPacketType type, const Data& data,
packet.local_ip = GetLocalIp(); packet.local_ip = GetLocalIp();
packet.remote_ip = remote_ip; packet.remote_ip = remote_ip;
packet.data.clear();
packet.data.resize(sizeof(data)); packet.data.resize(sizeof(data));
std::memcpy(packet.data.data(), &data, sizeof(data)); std::memcpy(packet.data.data(), &data, sizeof(data));
SendPacket(packet); SendPacket(packet);
@ -453,7 +449,6 @@ void LANDiscovery::SendPacket(Network::LDNPacketType type, Ipv4Address remote_ip
packet.local_ip = GetLocalIp(); packet.local_ip = GetLocalIp();
packet.remote_ip = remote_ip; packet.remote_ip = remote_ip;
packet.data.clear();
SendPacket(packet); SendPacket(packet);
} }
@ -465,7 +460,6 @@ void LANDiscovery::SendBroadcast(Network::LDNPacketType type, const Data& data)
packet.broadcast = true; packet.broadcast = true;
packet.local_ip = GetLocalIp(); packet.local_ip = GetLocalIp();
packet.data.clear();
packet.data.resize(sizeof(data)); packet.data.resize(sizeof(data));
std::memcpy(packet.data.data(), &data, sizeof(data)); std::memcpy(packet.data.data(), &data, sizeof(data));
SendPacket(packet); SendPacket(packet);
@ -478,7 +472,6 @@ void LANDiscovery::SendBroadcast(Network::LDNPacketType type) {
packet.broadcast = true; packet.broadcast = true;
packet.local_ip = GetLocalIp(); packet.local_ip = GetLocalIp();
packet.data.clear();
SendPacket(packet); SendPacket(packet);
} }
@ -581,9 +574,9 @@ bool LANDiscovery::IsNodeStateChanged() {
for (int i = 0; i < NodeCountMax; i++) { for (int i = 0; i < NodeCountMax; i++) {
if (nodes[i].is_connected != node_last_states[i]) { if (nodes[i].is_connected != node_last_states[i]) {
if (nodes[i].is_connected) { if (nodes[i].is_connected) {
nodeChanges[i].state_change |= NodeStateChange::Connect; node_changes[i].state_change |= NodeStateChange::Connect;
} else { } else {
nodeChanges[i].state_change |= NodeStateChange::Disconnect; node_changes[i].state_change |= NodeStateChange::Disconnect;
} }
node_last_states[i] = nodes[i].is_connected; node_last_states[i] = nodes[i].is_connected;
changed = true; changed = true;
@ -598,15 +591,11 @@ bool LANDiscovery::IsFlagSet(ScanFilterFlag flag, ScanFilterFlag search_flag) co
return (flag_value & search_flag_value) == search_flag_value; return (flag_value & search_flag_value) == search_flag_value;
} }
int LANDiscovery::GetStationCount() { int LANDiscovery::GetStationCount() const {
int count = 0; return static_cast<int>(
for (const auto& station : stations) { std::count_if(stations.begin(), stations.end(), [](const auto& station) {
if (station.GetStatus() != NodeStatus::Disconnected) { return station.GetStatus() != NodeStatus::Disconnected;
count++; }));
}
}
return count;
} }
MacAddress LANDiscovery::GetFakeMac() const { MacAddress LANDiscovery::GetFakeMac() const {

View file

@ -10,6 +10,7 @@
#include <mutex> #include <mutex>
#include <optional> #include <optional>
#include <random> #include <random>
#include <span>
#include <thread> #include <thread>
#include <unordered_map> #include <unordered_map>
@ -44,7 +45,7 @@ protected:
class LANDiscovery { class LANDiscovery {
public: public:
typedef std::function<void()> LanEventFunc; using LanEventFunc = std::function<void()>;
LANDiscovery(Network::RoomNetwork& room_network_); LANDiscovery(Network::RoomNetwork& room_network_);
~LANDiscovery(); ~LANDiscovery();
@ -58,7 +59,7 @@ public:
DisconnectReason GetDisconnectReason() const; DisconnectReason GetDisconnectReason() const;
Result Scan(std::vector<NetworkInfo>& networks, u16& count, const ScanFilter& filter); Result Scan(std::vector<NetworkInfo>& networks, u16& count, const ScanFilter& filter);
Result SetAdvertiseData(std::vector<u8>& data); Result SetAdvertiseData(std::span<const u8> data);
Result OpenAccessPoint(); Result OpenAccessPoint();
Result CloseAccessPoint(); Result CloseAccessPoint();
@ -74,7 +75,7 @@ public:
u16 local_communication_version); u16 local_communication_version);
Result Disconnect(); Result Disconnect();
Result Initialize(LanEventFunc lan_event = empty_func, bool listening = true); Result Initialize(LanEventFunc lan_event_ = empty_func, bool listening = true);
Result Finalize(); Result Finalize();
void ReceivePacket(const Network::LDNPacket& packet); void ReceivePacket(const Network::LDNPacket& packet);
@ -94,7 +95,7 @@ protected:
bool IsNodeStateChanged(); bool IsNodeStateChanged();
bool IsFlagSet(ScanFilterFlag flag, ScanFilterFlag search_flag) const; bool IsFlagSet(ScanFilterFlag flag, ScanFilterFlag search_flag) const;
int GetStationCount(); int GetStationCount() const;
MacAddress GetFakeMac() const; MacAddress GetFakeMac() const;
Result GetNodeInfo(NodeInfo& node, const UserConfig& user_config, Result GetNodeInfo(NodeInfo& node, const UserConfig& user_config,
u16 local_communication_version); u16 local_communication_version);
@ -109,12 +110,12 @@ protected:
void SendPacket(const Network::LDNPacket& packet); void SendPacket(const Network::LDNPacket& packet);
static const LanEventFunc empty_func; static const LanEventFunc empty_func;
const Ssid fake_ssid{"YuzuFakeSsidForLdn"}; static constexpr Ssid fake_ssid{"YuzuFakeSsidForLdn"};
bool inited{}; bool inited{};
std::mutex packet_mutex; std::mutex packet_mutex;
std::array<LanStation, StationCountMax> stations; std::array<LanStation, StationCountMax> stations;
std::array<NodeLatestUpdate, NodeCountMax> nodeChanges{}; std::array<NodeLatestUpdate, NodeCountMax> node_changes{};
std::array<u8, NodeCountMax> node_last_states{}; std::array<u8, NodeCountMax> node_last_states{};
std::unordered_map<MacAddress, NetworkInfo, MACAddressHash> scan_results{}; std::unordered_map<MacAddress, NetworkInfo, MACAddressHash> scan_results{};
NodeInfo node_info{}; NodeInfo node_info{};
@ -124,9 +125,9 @@ protected:
// TODO (flTobi): Should this be an std::set? // TODO (flTobi): Should this be an std::set?
std::vector<Ipv4Address> connected_clients; std::vector<Ipv4Address> connected_clients;
std::optional<Ipv4Address> host_ip = std::nullopt; std::optional<Ipv4Address> host_ip;
LanEventFunc LanEvent; LanEventFunc lan_event;
Network::RoomNetwork& room_network; Network::RoomNetwork& room_network;
}; };

View file

@ -205,8 +205,6 @@ public:
} }
void GetIpv4Address(Kernel::HLERequestContext& ctx) { void GetIpv4Address(Kernel::HLERequestContext& ctx) {
LOG_CRITICAL(Service_LDN, "called");
const auto network_interface = Network::GetSelectedNetworkInterface(); const auto network_interface = Network::GetSelectedNetworkInterface();
if (!network_interface) { if (!network_interface) {

View file

@ -31,13 +31,7 @@ enum class NodeStateChange : u8 {
DisconnectAndConnect, DisconnectAndConnect,
}; };
inline NodeStateChange operator|(NodeStateChange a, NodeStateChange b) { DECLARE_ENUM_FLAG_OPERATORS(NodeStateChange)
return static_cast<NodeStateChange>(static_cast<u8>(a) | static_cast<u8>(b));
}
inline NodeStateChange operator|=(NodeStateChange& a, NodeStateChange b) {
return a = a | b;
}
enum class ScanFilterFlag : u32 { enum class ScanFilterFlag : u32 {
None = 0, None = 0,
@ -163,7 +157,7 @@ struct Ssid {
Ssid() = default; Ssid() = default;
explicit Ssid(std::string_view data) { constexpr explicit Ssid(std::string_view data) {
length = static_cast<u8>(std::min(data.size(), SsidLengthMax)); length = static_cast<u8>(std::min(data.size(), SsidLengthMax));
data.copy(raw.data(), length); data.copy(raw.data(), length);
raw[length] = 0; raw[length] = 0;
@ -176,6 +170,10 @@ struct Ssid {
bool operator==(const Ssid& b) const { bool operator==(const Ssid& b) const {
return (length == b.length) && (std::memcmp(raw.data(), b.raw.data(), length) == 0); return (length == b.length) && (std::memcmp(raw.data(), b.raw.data(), length) == 0);
} }
bool operator!=(const Ssid& b) const {
return !operator==(b);
}
}; };
static_assert(sizeof(Ssid) == 0x22, "Ssid is an invalid size"); static_assert(sizeof(Ssid) == 0x22, "Ssid is an invalid size");

View file

@ -188,7 +188,7 @@ std::vector<NetworkInterface> GetAvailableNetworkInterfaces() {
std::optional<NetworkInterface> GetSelectedNetworkInterface() { std::optional<NetworkInterface> GetSelectedNetworkInterface() {
const auto& selected_network_interface = Settings::values.network_interface.GetValue(); const auto& selected_network_interface = Settings::values.network_interface.GetValue();
const auto network_interfaces = Network::GetAvailableNetworkInterfaces(); const auto network_interfaces = Network::GetAvailableNetworkInterfaces();
if (network_interfaces.size() == 0) { if (network_interfaces.empty()) {
LOG_ERROR(Network, "GetAvailableNetworkInterfaces returned no interfaces"); LOG_ERROR(Network, "GetAvailableNetworkInterfaces returned no interfaces");
return std::nullopt; return std::nullopt;
} }
@ -209,7 +209,7 @@ std::optional<NetworkInterface> GetSelectedNetworkInterface() {
void SelectFirstNetworkInterface() { void SelectFirstNetworkInterface() {
const auto network_interfaces = Network::GetAvailableNetworkInterfaces(); const auto network_interfaces = Network::GetAvailableNetworkInterfaces();
if (network_interfaces.size() == 0) { if (network_interfaces.empty()) {
return; return;
} }

View file

@ -716,7 +716,7 @@ RoomMember::CallbackHandle<ProxyPacket> RoomMember::BindOnProxyPacketReceived(
RoomMember::CallbackHandle<LDNPacket> RoomMember::BindOnLdnPacketReceived( RoomMember::CallbackHandle<LDNPacket> RoomMember::BindOnLdnPacketReceived(
std::function<void(const LDNPacket&)> callback) { std::function<void(const LDNPacket&)> callback) {
return room_member_impl->Bind(callback); return room_member_impl->Bind(std::move(callback));
} }
RoomMember::CallbackHandle<RoomInformation> RoomMember::BindOnRoomInformationChanged( RoomMember::CallbackHandle<RoomInformation> RoomMember::BindOnRoomInformationChanged(

View file

@ -125,7 +125,7 @@
<bool>true</bool> <bool>true</bool>
</property> </property>
<property name="title"> <property name="title">
<string>Multiplayer</string> <string>&amp;Multiplayer</string>
</property> </property>
<addaction name="action_View_Lobby"/> <addaction name="action_View_Lobby"/>
<addaction name="action_Start_Room"/> <addaction name="action_Start_Room"/>