Skip to content

Commit 2219f63

Browse files
authored
refactor(netpacket): Simplify NetPacket functions for packet buffer reads (TheSuperHackers#2463)
1 parent 8951ad9 commit 2219f63

10 files changed

Lines changed: 828 additions & 1056 deletions

File tree

Core/GameEngine/Include/GameNetwork/NetCommandList.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class NetCommandList : public MemoryPoolObject
5050
void init(); ///< Initialize the list
5151
void reset(); ///< Reset the list to the initial state.
5252
NetCommandRef * addMessage(NetCommandMsg *cmdMsg); ///< Add message to the list in its properly ordered place.
53+
NetCommandRef * addMessage(NetCommandRef *&msg); ///< Add message to the list in its properly ordered place.
5354
Bool isEqualCommandMsg(NetCommandMsg *msg1, NetCommandMsg *msg2);
5455
NetCommandRef * getFirstMessage(); ///< Get the first message on the list.
5556
NetCommandRef * findMessage(NetCommandMsg *msg); ///< Find and return a reference to the given message if one exists.

Core/GameEngine/Include/GameNetwork/NetCommandMsg.h

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,61 @@
3535

3636
class NetCommandRef;
3737

38+
//-----------------------------------------------------------------------------
39+
class NetCommandDataChunk
40+
{
41+
NetCommandDataChunk(const NetCommandDataChunk&) CPP_11(= delete);
42+
void operator=(const NetCommandDataChunk&) CPP_11(= delete);
43+
44+
public:
45+
NetCommandDataChunk(Byte *data, UnsignedInt size)
46+
: m_data(reinterpret_cast<UnsignedByte *>(data))
47+
, m_size(size)
48+
{}
49+
50+
NetCommandDataChunk(UnsignedByte *data, UnsignedInt size)
51+
: m_data(data)
52+
, m_size(size)
53+
{}
54+
55+
NetCommandDataChunk(UnsignedInt size)
56+
: m_data(NEW UnsignedByte[size])
57+
, m_size(size)
58+
{}
59+
60+
~NetCommandDataChunk()
61+
{
62+
delete[] m_data;
63+
}
64+
65+
const UnsignedByte *data() const
66+
{
67+
return m_data;
68+
}
69+
70+
UnsignedByte *data()
71+
{
72+
return m_data;
73+
}
74+
75+
UnsignedInt size() const
76+
{
77+
return m_size;
78+
}
79+
80+
UnsignedByte *release()
81+
{
82+
UnsignedByte *ret = m_data;
83+
m_data = nullptr;
84+
m_size = 0;
85+
return ret;
86+
}
87+
88+
private:
89+
UnsignedByte *m_data;
90+
UnsignedInt m_size;
91+
};
92+
3893
//-----------------------------------------------------------------------------
3994
class NetCommandMsg : public MemoryPoolObject
4095
{
@@ -60,6 +115,7 @@ class NetCommandMsg : public MemoryPoolObject
60115
virtual size_t getSizeForSmallNetPacket(const Select* select = nullptr) const = 0;
61116
virtual size_t copyBytesForSmallNetPacket(UnsignedByte* buffer, const NetCommandRef& ref, const Select* select = nullptr) const = 0;
62117
virtual Select getSmallNetPacketSelect() const = 0;
118+
virtual size_t readMessageData(NetCommandRef& ref, NetPacketBuf buf) const = 0;
63119
void attach();
64120
void detach();
65121

@@ -96,6 +152,11 @@ class NetCommandMsgT : public NetCommandMsg
96152
{
97153
return SmallNetPacketType::copyBytes(buffer, ref, select);
98154
}
155+
156+
virtual size_t readMessageData(NetCommandRef& ref, NetPacketBuf buf) const override
157+
{
158+
return SmallNetPacketType::CommandData::readMessage(ref, buf);
159+
}
99160
};
100161

101162
//-----------------------------------------------------------------------------
@@ -440,7 +501,7 @@ class NetWrapperCommandMsg : public NetCommandMsgT<NetPacketWrapperCommand, Smal
440501

441502
const UnsignedByte * getData() const;
442503
UnsignedByte * getData();
443-
void setData(UnsignedByte *data, UnsignedInt dataLength);
504+
void setData(NetCommandDataChunk &dataChunk);
444505

445506
UnsignedInt getChunkNumber() const;
446507
void setChunkNumber(UnsignedInt chunkNumber);
@@ -490,7 +551,7 @@ class NetFileCommandMsg : public NetCommandMsgT<NetPacketFileCommand, SmallNetPa
490551

491552
const UnsignedByte * getFileData() const;
492553
UnsignedByte * getFileData();
493-
void setFileData(UnsignedByte *data, UnsignedInt dataLength);
554+
void setFileData(NetCommandDataChunk &dataChunk);
494555

495556
virtual Select getSmallNetPacketSelect() const override;
496557

Core/GameEngine/Include/GameNetwork/NetPacket.h

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class NetPacket : public MemoryPoolObject
6161

6262
NetCommandList *getCommandList();
6363

64-
static NetCommandRef * ConstructNetCommandMsgFromRawData(UnsignedByte *data, UnsignedShort dataLength);
64+
static NetCommandRef *ConstructNetCommandMsgFromRawData(const UnsignedByte *data, UnsignedInt dataLength);
6565
static NetPacketList ConstructBigCommandPacketList(NetCommandRef *ref);
6666

6767
UnsignedByte *getData();
@@ -80,38 +80,7 @@ class NetPacket : public MemoryPoolObject
8080
Bool isAckStage2Repeat(NetCommandRef *msg);
8181
Bool isFrameRepeat(NetCommandRef *msg);
8282

83-
static NetCommandMsg * readGameMessage(UnsignedByte *data, Int &i);
84-
static NetCommandMsg * readAckBothMessage(UnsignedByte *data, Int &i);
85-
static NetCommandMsg * readAckStage1Message(UnsignedByte *data, Int &i);
86-
static NetCommandMsg * readAckStage2Message(UnsignedByte *data, Int &i);
87-
static NetCommandMsg * readFrameMessage(UnsignedByte *data, Int &i);
88-
static NetCommandMsg * readPlayerLeaveMessage(UnsignedByte *data, Int &i);
89-
static NetCommandMsg * readRunAheadMetricsMessage(UnsignedByte *data, Int &i);
90-
static NetCommandMsg * readRunAheadMessage(UnsignedByte *data, Int &i);
91-
static NetCommandMsg * readDestroyPlayerMessage(UnsignedByte *data, Int &i);
92-
static NetCommandMsg * readKeepAliveMessage(UnsignedByte *data, Int &i);
93-
static NetCommandMsg * readDisconnectKeepAliveMessage(UnsignedByte *data, Int &i);
94-
static NetCommandMsg * readDisconnectPlayerMessage(UnsignedByte *data, Int &i);
95-
static NetCommandMsg * readPacketRouterQueryMessage(UnsignedByte *data, Int &i);
96-
static NetCommandMsg * readPacketRouterAckMessage(UnsignedByte *data, Int &i);
97-
static NetCommandMsg * readDisconnectChatMessage(UnsignedByte *data, Int &i);
98-
static NetCommandMsg * readDisconnectVoteMessage(UnsignedByte *data, Int &i);
99-
static NetCommandMsg * readChatMessage(UnsignedByte *data, Int &i);
100-
static NetCommandMsg * readProgressMessage(UnsignedByte *data, Int &i);
101-
static NetCommandMsg * readLoadCompleteMessage(UnsignedByte *data, Int &i);
102-
static NetCommandMsg * readTimeOutGameStartMessage(UnsignedByte *data, Int &i);
103-
static NetCommandMsg * readWrapperMessage(UnsignedByte *data, Int &i);
104-
static NetCommandMsg * readFileMessage(UnsignedByte *data, Int &i);
105-
static NetCommandMsg * readFileAnnounceMessage(UnsignedByte *data, Int &i);
106-
static NetCommandMsg * readFileProgressMessage(UnsignedByte *data, Int &i);
107-
static NetCommandMsg * readDisconnectFrameMessage(UnsignedByte *data, Int &i);
108-
static NetCommandMsg * readDisconnectScreenOffMessage(UnsignedByte *data, Int &i);
109-
static NetCommandMsg * readFrameResendRequestMessage(UnsignedByte *data, Int &i);
110-
111-
void writeGameMessageArgumentToPacket(GameMessageArgumentDataType type, GameMessageArgumentType arg);
112-
static void readGameMessageArgumentFromPacket(GameMessageArgumentDataType type, NetGameCommandMsg *msg, UnsignedByte *data, Int &i);
113-
114-
void dumpPacketToLog();
83+
static void dumpPacketToLog(const UnsignedByte *packet, Int packetLen);
11584

11685
protected:
11786
UnsignedByte m_packet[MAX_PACKET_SIZE];

0 commit comments

Comments
 (0)