From ed50b4fcabcb617406ebafce4150da16aaa65dc6 Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Fri, 6 Nov 2026 13:50:23 -0600 Subject: Core/NetPlayProto: Add sf::Packet replacement, NetPacket. --- Source/Core/Core/NetPlayProto.h | 128 ++++++++++++++++++++++++++++++++++ Source/UnitTests/Core/CMakeLists.txt | 1 + Source/UnitTests/Core/NetPlayTest.cpp | 119 +++++++++++++++++++++++++++++++ 3 files changed, 248 insertions(+) create mode 100644 Source/UnitTests/Core/NetPlayTest.cpp diff --git a/Source/Core/Core/NetPlayProto.h b/Source/Core/Core/NetPlayProto.h index 0458e32237..7b35360c87 100644 --- a/Source/Core/Core/NetPlayProto.h +++ b/Source/Core/Core/NetPlayProto.h @@ -4,10 +4,14 @@ #pragma once #include +#include #include +#include "Common/BitUtils.h" #include "Common/CommonTypes.h" #include "Common/EnumMap.h" +#include "Common/Swap.h" +#include "Common/TypeUtils.h" #include "Core/Config/SYSCONFSettings.h" #include "Core/HW/EXI/EXI.h" #include "Core/HW/EXI/EXI_Device.h" @@ -272,4 +276,128 @@ void SendPowerButtonEvent(); std::string GetGBASavePath(int pad_num); PadDetails GetPadDetails(int pad_num); int NumLocalWiimotes(); + +class NetPacket +{ +public: + // Write string. + NetPacket& operator<<(std::string_view str) + { + *this << u32(str.size()); + return append(Common::AsU8Span(str)); + } + + // Read string. + NetPacket& operator>>(std::string& str) + { + u32 length{}; + if (*this >> length) + { + if (length > RemainingBytes()) + { + m_good = false; + } + else + { + str.resize(length); + read(Common::AsWritableU8Span(str)); + } + } + return *this; + } + + // Write enum. + NetPacket& operator<<(Common::Enum auto value) + { + return *this << Common::BigEndianValue{std::to_underlying(value)}; + } + + // Read enum. + template + NetPacket& operator>>(T& value) + { + Common::BigEndianValue> big_endian_value{}; + if (*this >> big_endian_value) + value = T(std::underlying_type_t(big_endian_value)); + return *this; + } + + // Write bool (avoiding implicit conversions). + NetPacket& operator<<(std::same_as auto value) { return append(std::array{value}); } + + // Read bool. + NetPacket& operator>>(bool& value) + { + std::array data{}; + if (read(data)) + value = data.front() != 0; + return *this; + } + + // Write arithmetic. + template + NetPacket& operator<<(const T& value) + requires(std::is_arithmetic_v && !std::is_same_v) + { + return *this << Common::BigEndianValue{value}; + } + + // Read arithmetic. + template + NetPacket& operator>>(T& value) requires(std::is_arithmetic_v && !std::is_same_v) + { + Common::BigEndianValue big_endian_value{}; + if (*this >> big_endian_value) + value = T(big_endian_value); + return *this; + } + + // Write BigEndianValue. + template + NetPacket& operator<<(Common::BigEndianValue value) + { + return append(Common::AsU8Span(value)); + } + + // Read BigEndianValue. + template + NetPacket& operator>>(Common::BigEndianValue& value) + { + return read(Common::AsWritableU8Span(value)); + } + + NetPacket& append(std::span bytes) + { + m_data.insert(m_data.end(), bytes.begin(), bytes.end()); + return *this; + } + + NetPacket& read(std::span bytes) + { + if (bytes.size() > RemainingBytes()) + { + m_good = false; + return *this; + } + + std::copy_n(data() + m_read_pos, bytes.size(), bytes.data()); + m_read_pos += bytes.size(); + return *this; + } + + explicit operator bool() const { return m_good; } + + const u8* data() const { return m_data.data(); } + std::size_t size() const { return m_data.size(); } + const u8* begin() const { return m_data.data(); } + const u8* end() const { return m_data.data() + m_data.size(); } + +private: + std::size_t RemainingBytes() const { return m_good ? (size() - m_read_pos) : 0; } + + bool m_good{true}; + std::size_t m_read_pos{}; + std::vector m_data; +}; + } // namespace NetPlay diff --git a/Source/UnitTests/Core/CMakeLists.txt b/Source/UnitTests/Core/CMakeLists.txt index 30aeae4477..f97e42c1d0 100644 --- a/Source/UnitTests/Core/CMakeLists.txt +++ b/Source/UnitTests/Core/CMakeLists.txt @@ -1,6 +1,7 @@ add_dolphin_test(MMIOTest MMIOTest.cpp) add_dolphin_test(PageFaultTest PageFaultTest.cpp) add_dolphin_test(CoreTimingTest CoreTimingTest.cpp) +add_dolphin_test(NetPlayTest NetPlayTest.cpp) add_dolphin_test(PatchAllowlistTest PatchAllowlistTest.cpp) add_dolphin_test(DSPAcceleratorTest DSP/DSPAcceleratorTest.cpp) diff --git a/Source/UnitTests/Core/NetPlayTest.cpp b/Source/UnitTests/Core/NetPlayTest.cpp new file mode 100644 index 0000000000..ee37e80db7 --- /dev/null +++ b/Source/UnitTests/Core/NetPlayTest.cpp @@ -0,0 +1,119 @@ +// Copyright 2026 Dolphin Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include + +#include + +#include "Core/NetPlayProto.h" + +using NetPlay::NetPacket; + +static constexpr u16 test_u16_value = 0x42; + +TEST(NetPacket, Integer) +{ + using Common::BigEndianValue; + + NetPacket pkt; + EXPECT_TRUE(pkt); + + enum class MyEnum : u16 + { + Zero, + Value = test_u16_value, + }; + + // Integers are written in network byte order. + pkt << u16(test_u16_value) << Common::BigEndianValue{test_u16_value} << MyEnum::Value; + EXPECT_EQ(pkt.size(), 6); + EXPECT_EQ(pkt.data()[0], 0x00); + EXPECT_EQ(pkt.data()[1], test_u16_value); + EXPECT_EQ(pkt.data()[2], 0x00); + EXPECT_EQ(pkt.data()[3], test_u16_value); + EXPECT_EQ(pkt.data()[4], 0x00); + EXPECT_EQ(pkt.data()[5], test_u16_value); + + // Attempting to read beyond the end fails. + { + auto pkt2 = pkt; + EXPECT_TRUE(pkt2); + u64 x{}; + EXPECT_FALSE(pkt2 >> x); + + // A failed read puts the packet into a "bad" state that stops reading. + u16 x16{}; + EXPECT_FALSE(pkt2 >> x16); + } + + // Reading integer types works. + { + u16 x16{}; + Common::BigEndianValue be16{}; + MyEnum my_enum{}; + + EXPECT_TRUE(pkt >> x16 >> be16 >> my_enum); + EXPECT_EQ(x16, test_u16_value); + EXPECT_EQ(be16, test_u16_value); + EXPECT_EQ(my_enum, MyEnum::Value); + } +} + +TEST(NetPacket, Float) +{ + NetPacket pkt; + + pkt << std::numbers::pi_v << std::numbers::pi_v; + + float flt{}; + double dbl{}; + EXPECT_TRUE(pkt >> flt >> dbl); + + EXPECT_EQ(flt, std::numbers::pi_v); + EXPECT_EQ(dbl, std::numbers::pi_v); +} + +TEST(NetPacket, String) +{ + NetPacket pkt; + + constexpr auto test_string = "hello there."; + + pkt << test_string << test_string << test_u16_value; + + std::string str1; + std::string str2; + u16 x{}; + EXPECT_TRUE(pkt >> str1 >> str2 >> x); + + // Reading beyond the end fails, and doesn't change the referenced values. + EXPECT_FALSE(pkt >> str1 >> str2 >> x); + + EXPECT_EQ(str1, test_string); + EXPECT_EQ(str2, test_string); + EXPECT_EQ(x, test_u16_value); + + // Falsifying a string longer than the packet fails to read. + pkt = {}; + pkt << u32(-1) << u64(-1); + + std::string str; + EXPECT_FALSE(pkt >> str); +} + +TEST(NetPacket, Bool) +{ + NetPacket pkt; + pkt << true << false; + + // Each bool is 1 byte. + EXPECT_EQ(2, pkt.size()); + + bool b{}; + EXPECT_TRUE(pkt >> b); + EXPECT_TRUE(b); + EXPECT_TRUE(pkt >> b); + EXPECT_FALSE(b); + + EXPECT_FALSE(pkt >> b); +} -- cgit v1.2.3