mirror of
https://github.com/melonDS-emu/melonDS.git
synced 2025-07-28 16:50:15 -06:00
Refactor network implementations to be more reusable and less buggy (#2107)
encapsulate network interfaces
This commit is contained in:
@ -20,27 +20,72 @@
|
||||
#define LOCALMP_H
|
||||
|
||||
#include "types.h"
|
||||
#include "Platform.h"
|
||||
|
||||
namespace LocalMP
|
||||
namespace melonDS
|
||||
{
|
||||
struct MPStatusData
|
||||
{
|
||||
u16 ConnectedBitmask; // bitmask of which instances are ready to send/receive packets
|
||||
u32 PacketWriteOffset;
|
||||
u32 ReplyWriteOffset;
|
||||
u16 MPHostinst; // instance ID from which the last CMD frame was sent
|
||||
u16 MPReplyBitmask; // bitmask of which clients replied in time
|
||||
};
|
||||
|
||||
using namespace melonDS;
|
||||
bool Init();
|
||||
void DeInit();
|
||||
struct MPPacketHeader
|
||||
{
|
||||
u32 Magic;
|
||||
u32 SenderID;
|
||||
u32 Type; // 0=regular 1=CMD 2=reply 3=ack
|
||||
u32 Length;
|
||||
u64 Timestamp;
|
||||
};
|
||||
|
||||
void SetRecvTimeout(int timeout);
|
||||
constexpr u32 kPacketQueueSize = 0x10000;
|
||||
constexpr u32 kReplyQueueSize = 0x10000;
|
||||
constexpr u32 kMaxFrameSize = 0x948;
|
||||
|
||||
void Begin(int inst);
|
||||
void End(int inst);
|
||||
class LocalMP
|
||||
{
|
||||
public:
|
||||
LocalMP() noexcept;
|
||||
LocalMP(const LocalMP&) = delete;
|
||||
LocalMP& operator=(const LocalMP&) = delete;
|
||||
LocalMP(LocalMP&& other) = delete;
|
||||
LocalMP& operator=(LocalMP&& other) = delete;
|
||||
~LocalMP() noexcept;
|
||||
|
||||
int SendPacket(int inst, u8* data, int len, u64 timestamp);
|
||||
int RecvPacket(int inst, u8* data, u64* timestamp);
|
||||
int SendCmd(int inst, u8* data, int len, u64 timestamp);
|
||||
int SendReply(int inst, u8* data, int len, u64 timestamp, u16 aid);
|
||||
int SendAck(int inst, u8* data, int len, u64 timestamp);
|
||||
int RecvHostPacket(int inst, u8* data, u64* timestamp);
|
||||
u16 RecvReplies(int inst, u8* data, u64 timestamp, u16 aidmask);
|
||||
[[nodiscard]] int GetRecvTimeout() const noexcept { return RecvTimeout; }
|
||||
void SetRecvTimeout(int timeout) noexcept { RecvTimeout = timeout; }
|
||||
|
||||
void Begin(int inst);
|
||||
void End(int inst);
|
||||
|
||||
int SendPacket(int inst, u8* data, int len, u64 timestamp);
|
||||
int RecvPacket(int inst, u8* data, u64* timestamp);
|
||||
int SendCmd(int inst, u8* data, int len, u64 timestamp);
|
||||
int SendReply(int inst, u8* data, int len, u64 timestamp, u16 aid);
|
||||
int SendAck(int inst, u8* data, int len, u64 timestamp);
|
||||
int RecvHostPacket(int inst, u8* data, u64* timestamp);
|
||||
u16 RecvReplies(int inst, u8* data, u64 timestamp, u16 aidmask);
|
||||
private:
|
||||
void FIFORead(int inst, int fifo, void* buf, int len) noexcept;
|
||||
void FIFOWrite(int inst, int fifo, void* buf, int len) noexcept;
|
||||
int SendPacketGeneric(int inst, u32 type, u8* packet, int len, u64 timestamp) noexcept;
|
||||
int RecvPacketGeneric(int inst, u8* packet, bool block, u64* timestamp) noexcept;
|
||||
Platform::Mutex* MPQueueLock;
|
||||
MPStatusData MPStatus {};
|
||||
u8 MPPacketQueue[kPacketQueueSize] {};
|
||||
u8 MPReplyQueue[kReplyQueueSize] {};
|
||||
u32 PacketReadOffset[16] {};
|
||||
u32 ReplyReadOffset[16] {};
|
||||
|
||||
int RecvTimeout = 25;
|
||||
|
||||
int LastHostID = -1;
|
||||
Platform::Semaphore* SemPool[32] {};
|
||||
};
|
||||
}
|
||||
|
||||
#endif // LOCALMP_H
|
||||
|
Reference in New Issue
Block a user