mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2026-12-06 12:43:35 +01:00
Core/NetPlayProto: Add sf::Packet replacement, NetPacket.
This commit is contained in:
1 parent
e3ffa6ba49
commit
ed50b4fcab
3 files changed
+248
No files matched your search
@@ -4,10 +4,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <concepts>
|
||||
#include <string>
|
||||
|
||||
#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 <Common::Enum T>
|
||||
NetPacket& operator>>(T& value)
|
||||
{
|
||||
Common::BigEndianValue<std::underlying_type_t<T>> big_endian_value{};
|
||||
if (*this >> big_endian_value)
|
||||
value = T(std::underlying_type_t<T>(big_endian_value));
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Write bool (avoiding implicit conversions).
|
||||
NetPacket& operator<<(std::same_as<bool> auto value) { return append(std::array<u8, 1>{value}); }
|
||||
|
||||
// Read bool.
|
||||
NetPacket& operator>>(bool& value)
|
||||
{
|
||||
std::array<u8, 1> data{};
|
||||
if (read(data))
|
||||
value = data.front() != 0;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Write arithmetic.
|
||||
template <typename T>
|
||||
NetPacket& operator<<(const T& value)
|
||||
requires(std::is_arithmetic_v<T> && !std::is_same_v<T, bool>)
|
||||
{
|
||||
return *this << Common::BigEndianValue<T>{value};
|
||||
}
|
||||
|
||||
// Read arithmetic.
|
||||
template <typename T>
|
||||
NetPacket& operator>>(T& value) requires(std::is_arithmetic_v<T> && !std::is_same_v<T, bool>)
|
||||
{
|
||||
Common::BigEndianValue<T> big_endian_value{};
|
||||
if (*this >> big_endian_value)
|
||||
value = T(big_endian_value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Write BigEndianValue.
|
||||
template <typename T>
|
||||
NetPacket& operator<<(Common::BigEndianValue<T> value)
|
||||
{
|
||||
return append(Common::AsU8Span(value));
|
||||
}
|
||||
|
||||
// Read BigEndianValue.
|
||||
template <typename T>
|
||||
NetPacket& operator>>(Common::BigEndianValue<T>& value)
|
||||
{
|
||||
return read(Common::AsWritableU8Span(value));
|
||||
}
|
||||
|
||||
NetPacket& append(std::span<const u8> bytes)
|
||||
{
|
||||
m_data.insert(m_data.end(), bytes.begin(), bytes.end());
|
||||
return *this;
|
||||
}
|
||||
|
||||
NetPacket& read(std::span<u8> 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<u8> m_data;
|
||||
};
|
||||
|
||||
} // namespace NetPlay
|
||||
@@ -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)
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
// Copyright 2026 Dolphin Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <numbers>
|
||||
|
||||
#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<u16> 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<float> << std::numbers::pi_v<double>;
|
||||
|
||||
float flt{};
|
||||
double dbl{};
|
||||
EXPECT_TRUE(pkt >> flt >> dbl);
|
||||
|
||||
EXPECT_EQ(flt, std::numbers::pi_v<float>);
|
||||
EXPECT_EQ(dbl, std::numbers::pi_v<double>);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
Reference in new issue
Block a user