From 452e816c264fe4aded8226b0756c76a45712e2d3 Mon Sep 17 00:00:00 2001 From: xezon <4720891+xezon@users.noreply.github.com> Date: Wed, 18 Feb 2026 00:15:41 +0100 Subject: [PATCH 1/5] fix(network): Fix constness around NetCommandMsg (#2329) --- .../Include/GameNetwork/GameMessageParser.h | 2 +- .../Include/GameNetwork/NetCommandMsg.h | 90 ++++++++++--------- .../Include/GameNetwork/NetCommandRef.h | 9 ++ .../Source/GameNetwork/GameMessageParser.cpp | 2 +- .../Source/GameNetwork/NetCommandMsg.cpp | 90 ++++++++++--------- .../GameEngine/Include/Common/MessageStream.h | 2 +- .../Source/Common/MessageStream.cpp | 2 +- .../GameEngine/Include/Common/MessageStream.h | 2 +- .../Source/Common/MessageStream.cpp | 2 +- 9 files changed, 110 insertions(+), 91 deletions(-) diff --git a/Core/GameEngine/Include/GameNetwork/GameMessageParser.h b/Core/GameEngine/Include/GameNetwork/GameMessageParser.h index e9b5af774ef..72640b0943a 100644 --- a/Core/GameEngine/Include/GameNetwork/GameMessageParser.h +++ b/Core/GameEngine/Include/GameNetwork/GameMessageParser.h @@ -78,7 +78,7 @@ class GameMessageParser : public MemoryPoolObject MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(GameMessageParser, "GameMessageParser") public: GameMessageParser(); - GameMessageParser(GameMessage *msg); + GameMessageParser(const GameMessage *msg); //virtual ~GameMessageParser(); GameMessageParserArgumentType *getFirstArgumentType(); diff --git a/Core/GameEngine/Include/GameNetwork/NetCommandMsg.h b/Core/GameEngine/Include/GameNetwork/NetCommandMsg.h index fabcd167c10..296f03cb80d 100644 --- a/Core/GameEngine/Include/GameNetwork/NetCommandMsg.h +++ b/Core/GameEngine/Include/GameNetwork/NetCommandMsg.h @@ -48,8 +48,8 @@ class NetCommandMsg : public MemoryPoolObject UnsignedInt getPlayerID() const { return m_playerID; } UnsignedShort getID() const { return m_id; } void setNetCommandType(NetCommandType type) { m_commandType = type; } - NetCommandType getNetCommandType() { return m_commandType; } - virtual Int getSortNumber(); + NetCommandType getNetCommandType() const { return m_commandType; } + virtual Int getSortNumber() const; virtual size_t getPackedByteCount() const = 0; void attach(); void detach(); @@ -103,11 +103,11 @@ class NetAckBothCommandMsg : public NetCommandMsg NetAckBothCommandMsg(); //virtual ~NetAckBothCommandMsg(); - UnsignedShort getCommandID(); + UnsignedShort getCommandID() const; void setCommandID(UnsignedShort commandID); - UnsignedByte getOriginalPlayerID(); + UnsignedByte getOriginalPlayerID() const; void setOriginalPlayerID(UnsignedByte originalPlayerID); - virtual Int getSortNumber(); + virtual Int getSortNumber() const; virtual size_t getPackedByteCount() const; @@ -129,11 +129,11 @@ class NetAckStage1CommandMsg : public NetCommandMsg NetAckStage1CommandMsg(); //virtual ~NetAckStage1CommandMsg(); - UnsignedShort getCommandID(); + UnsignedShort getCommandID() const; void setCommandID(UnsignedShort commandID); - UnsignedByte getOriginalPlayerID(); + UnsignedByte getOriginalPlayerID() const; void setOriginalPlayerID(UnsignedByte originalPlayerID); - virtual Int getSortNumber(); + virtual Int getSortNumber() const; virtual size_t getPackedByteCount() const; @@ -155,11 +155,11 @@ class NetAckStage2CommandMsg : public NetCommandMsg NetAckStage2CommandMsg(); //virtual ~NetAckStage2CommandMsg(); - UnsignedShort getCommandID(); + UnsignedShort getCommandID() const; void setCommandID(UnsignedShort commandID); - UnsignedByte getOriginalPlayerID(); + UnsignedByte getOriginalPlayerID() const; void setOriginalPlayerID(UnsignedByte originalPlayerID); - virtual Int getSortNumber(); + virtual Int getSortNumber() const; virtual size_t getPackedByteCount() const; @@ -177,7 +177,7 @@ class NetFrameCommandMsg : public NetCommandMsg //virtual ~NetFrameCommandMsg(); void setCommandCount(UnsignedShort commandCount); - UnsignedShort getCommandCount(); + UnsignedShort getCommandCount() const; virtual size_t getPackedByteCount() const; @@ -193,7 +193,7 @@ class NetPlayerLeaveCommandMsg : public NetCommandMsg NetPlayerLeaveCommandMsg(); //virtual ~NetPlayerLeaveCommandMsg(); - UnsignedByte getLeavingPlayerID(); + UnsignedByte getLeavingPlayerID() const; void setLeavingPlayerID(UnsignedByte id); virtual size_t getPackedByteCount() const; @@ -210,9 +210,9 @@ class NetRunAheadMetricsCommandMsg : public NetCommandMsg NetRunAheadMetricsCommandMsg(); //virtual ~NetRunAheadMetricsCommandMsg(); - Real getAverageLatency(); + Real getAverageLatency() const; void setAverageLatency(Real avgLat); - Int getAverageFps(); + Int getAverageFps() const; void setAverageFps(Int fps); virtual size_t getPackedByteCount() const; @@ -230,10 +230,10 @@ class NetRunAheadCommandMsg : public NetCommandMsg NetRunAheadCommandMsg(); //virtual ~NetRunAheadCommandMsg(); - UnsignedShort getRunAhead(); + UnsignedShort getRunAhead() const; void setRunAhead(UnsignedShort runAhead); - UnsignedByte getFrameRate(); + UnsignedByte getFrameRate() const; void setFrameRate(UnsignedByte frameRate); virtual size_t getPackedByteCount() const; @@ -251,7 +251,7 @@ class NetDestroyPlayerCommandMsg : public NetCommandMsg NetDestroyPlayerCommandMsg(); //virtual ~NetDestroyPlayerCommandMsg(); - UnsignedInt getPlayerIndex(); + UnsignedInt getPlayerIndex() const; void setPlayerIndex(UnsignedInt playerIndex); virtual size_t getPackedByteCount() const; @@ -290,10 +290,10 @@ class NetDisconnectPlayerCommandMsg : public NetCommandMsg NetDisconnectPlayerCommandMsg(); //virtual ~NetDisconnectPlayerCommandMsg(); - UnsignedByte getDisconnectSlot(); + UnsignedByte getDisconnectSlot() const; void setDisconnectSlot(UnsignedByte slot); - UnsignedInt getDisconnectFrame(); + UnsignedInt getDisconnectFrame() const; void setDisconnectFrame(UnsignedInt frame); virtual size_t getPackedByteCount() const; @@ -333,7 +333,7 @@ class NetDisconnectChatCommandMsg : public NetCommandMsg NetDisconnectChatCommandMsg(); //virtual ~NetDisconnectChatCommandMsg(); - UnicodeString getText(); + UnicodeString getText() const; void setText(UnicodeString text); virtual size_t getPackedByteCount() const; @@ -350,10 +350,10 @@ class NetChatCommandMsg : public NetCommandMsg NetChatCommandMsg(); //virtual ~NetChatCommandMsg(); - UnicodeString getText(); + UnicodeString getText() const; void setText(UnicodeString text); - Int getPlayerMask(); + Int getPlayerMask() const; void setPlayerMask( Int playerMask ); virtual size_t getPackedByteCount() const; @@ -371,10 +371,10 @@ class NetDisconnectVoteCommandMsg : public NetCommandMsg NetDisconnectVoteCommandMsg(); //virtual ~NetDisconnectVoteCommandMsg(); - UnsignedByte getSlot(); + UnsignedByte getSlot() const; void setSlot(UnsignedByte slot); - UnsignedInt getVoteFrame(); + UnsignedInt getVoteFrame() const; void setVoteFrame(UnsignedInt voteFrame); virtual size_t getPackedByteCount() const; @@ -392,7 +392,7 @@ class NetProgressCommandMsg: public NetCommandMsg NetProgressCommandMsg(); //virtual ~NetProgressCommandMsg(); - UnsignedByte getPercentage(); + UnsignedByte getPercentage() const; void setPercentage( UnsignedByte percent ); virtual size_t getPackedByteCount() const; @@ -409,26 +409,27 @@ class NetWrapperCommandMsg : public NetCommandMsg NetWrapperCommandMsg(); //virtual ~NetWrapperCommandMsg(); + const UnsignedByte * getData() const; UnsignedByte * getData(); void setData(UnsignedByte *data, UnsignedInt dataLength); virtual size_t getPackedByteCount() const; - UnsignedInt getChunkNumber(); + UnsignedInt getChunkNumber() const; void setChunkNumber(UnsignedInt chunkNumber); - UnsignedInt getNumChunks(); + UnsignedInt getNumChunks() const; void setNumChunks(UnsignedInt numChunks); - UnsignedInt getDataLength(); + UnsignedInt getDataLength() const; - UnsignedInt getTotalDataLength(); + UnsignedInt getTotalDataLength() const; void setTotalDataLength(UnsignedInt totalDataLength); - UnsignedInt getDataOffset(); + UnsignedInt getDataOffset() const; void setDataOffset(UnsignedInt offset); - UnsignedShort getWrappedCommandID(); + UnsignedShort getWrappedCommandID() const; void setWrappedCommandID(UnsignedShort wrappedCommandID); private: @@ -450,14 +451,15 @@ class NetFileCommandMsg : public NetCommandMsg NetFileCommandMsg(); //virtual ~NetFileCommandMsg(); - AsciiString getRealFilename(); + AsciiString getRealFilename() const; void setRealFilename(AsciiString filename); - AsciiString getPortableFilename() { return m_portableFilename; } + AsciiString getPortableFilename() const { return m_portableFilename; } void setPortableFilename(AsciiString filename) { m_portableFilename = filename; } - UnsignedInt getFileLength(); + UnsignedInt getFileLength() const; + const UnsignedByte * getFileData() const; UnsignedByte * getFileData(); void setFileData(UnsignedByte *data, UnsignedInt dataLength); @@ -478,16 +480,16 @@ class NetFileAnnounceCommandMsg : public NetCommandMsg NetFileAnnounceCommandMsg(); //virtual ~NetFileAnnounceCommandMsg(); - AsciiString getRealFilename(); + AsciiString getRealFilename() const; void setRealFilename(AsciiString filename); - AsciiString getPortableFilename() { return m_portableFilename; } + AsciiString getPortableFilename() const { return m_portableFilename; } void setPortableFilename(AsciiString filename) { m_portableFilename = filename; } - UnsignedShort getFileID(); + UnsignedShort getFileID() const; void setFileID(UnsignedShort fileID); - UnsignedByte getPlayerMask(); + UnsignedByte getPlayerMask() const; void setPlayerMask(UnsignedByte playerMask); virtual size_t getPackedByteCount() const; @@ -506,10 +508,10 @@ class NetFileProgressCommandMsg : public NetCommandMsg NetFileProgressCommandMsg(); //virtual ~NetFileProgressCommandMsg(); - UnsignedShort getFileID(); + UnsignedShort getFileID() const; void setFileID(UnsignedShort val); - Int getProgress(); + Int getProgress() const; void setProgress(Int val); virtual size_t getPackedByteCount() const; @@ -526,7 +528,7 @@ class NetDisconnectFrameCommandMsg : public NetCommandMsg public: NetDisconnectFrameCommandMsg(); - UnsignedInt getDisconnectFrame(); + UnsignedInt getDisconnectFrame() const; void setDisconnectFrame(UnsignedInt disconnectFrame); virtual size_t getPackedByteCount() const; @@ -542,7 +544,7 @@ class NetDisconnectScreenOffCommandMsg : public NetCommandMsg public: NetDisconnectScreenOffCommandMsg(); - UnsignedInt getNewFrame(); + UnsignedInt getNewFrame() const; void setNewFrame(UnsignedInt newFrame); virtual size_t getPackedByteCount() const; @@ -558,7 +560,7 @@ class NetFrameResendRequestCommandMsg : public NetCommandMsg public: NetFrameResendRequestCommandMsg(); - UnsignedInt getFrameToResend(); + UnsignedInt getFrameToResend() const; void setFrameToResend(UnsignedInt frame); virtual size_t getPackedByteCount() const; diff --git a/Core/GameEngine/Include/GameNetwork/NetCommandRef.h b/Core/GameEngine/Include/GameNetwork/NetCommandRef.h index 33e0d46b2d9..4a3ae4226ca 100644 --- a/Core/GameEngine/Include/GameNetwork/NetCommandRef.h +++ b/Core/GameEngine/Include/GameNetwork/NetCommandRef.h @@ -49,6 +49,7 @@ class NetCommandRef : public MemoryPoolObject #endif //~NetCommandRef(); + const NetCommandMsg *getCommand() const; NetCommandMsg *getCommand(); NetCommandRef *getNext(); NetCommandRef *getPrev(); @@ -73,6 +74,14 @@ class NetCommandRef : public MemoryPoolObject #endif }; +/** + * Return the const command message. + */ +inline const NetCommandMsg * NetCommandRef::getCommand() const +{ + return m_msg; +} + /** * Return the command message. */ diff --git a/Core/GameEngine/Source/GameNetwork/GameMessageParser.cpp b/Core/GameEngine/Source/GameNetwork/GameMessageParser.cpp index 3c7d4092425..f21baa6535d 100644 --- a/Core/GameEngine/Source/GameNetwork/GameMessageParser.cpp +++ b/Core/GameEngine/Source/GameNetwork/GameMessageParser.cpp @@ -35,7 +35,7 @@ GameMessageParser::GameMessageParser() } //---------------------------------------------------------------------------- -GameMessageParser::GameMessageParser(GameMessage *msg) +GameMessageParser::GameMessageParser(const GameMessage *msg) { m_first = nullptr; m_argTypeCount = 0; diff --git a/Core/GameEngine/Source/GameNetwork/NetCommandMsg.cpp b/Core/GameEngine/Source/GameNetwork/NetCommandMsg.cpp index 283cec1e61b..e21357d0b48 100644 --- a/Core/GameEngine/Source/GameNetwork/NetCommandMsg.cpp +++ b/Core/GameEngine/Source/GameNetwork/NetCommandMsg.cpp @@ -76,7 +76,7 @@ void NetCommandMsg::detach() { /** * Returns the value by which this type of message should be sorted. */ -Int NetCommandMsg::getSortNumber() { +Int NetCommandMsg::getSortNumber() const { return m_id; } @@ -318,7 +318,7 @@ NetAckBothCommandMsg::~NetAckBothCommandMsg() { /** * Returns the command ID of the command being ack'd. */ -UnsignedShort NetAckBothCommandMsg::getCommandID() { +UnsignedShort NetAckBothCommandMsg::getCommandID() const { return m_commandID; } @@ -332,7 +332,7 @@ void NetAckBothCommandMsg::setCommandID(UnsignedShort commandID) { /** * Get the player id of the player who originally sent the command. */ -UnsignedByte NetAckBothCommandMsg::getOriginalPlayerID() { +UnsignedByte NetAckBothCommandMsg::getOriginalPlayerID() const { return m_originalPlayerID; } @@ -343,7 +343,7 @@ void NetAckBothCommandMsg::setOriginalPlayerID(UnsignedByte originalPlayerID) { m_originalPlayerID = originalPlayerID; } -Int NetAckBothCommandMsg::getSortNumber() { +Int NetAckBothCommandMsg::getSortNumber() const { return m_commandID; } @@ -379,7 +379,7 @@ NetAckStage1CommandMsg::~NetAckStage1CommandMsg() { /** * Returns the command ID of the command being ack'd. */ -UnsignedShort NetAckStage1CommandMsg::getCommandID() { +UnsignedShort NetAckStage1CommandMsg::getCommandID() const { return m_commandID; } @@ -393,7 +393,7 @@ void NetAckStage1CommandMsg::setCommandID(UnsignedShort commandID) { /** * Get the player id of the player who originally sent the command. */ -UnsignedByte NetAckStage1CommandMsg::getOriginalPlayerID() { +UnsignedByte NetAckStage1CommandMsg::getOriginalPlayerID() const { return m_originalPlayerID; } @@ -404,7 +404,7 @@ void NetAckStage1CommandMsg::setOriginalPlayerID(UnsignedByte originalPlayerID) m_originalPlayerID = originalPlayerID; } -Int NetAckStage1CommandMsg::getSortNumber() { +Int NetAckStage1CommandMsg::getSortNumber() const { return m_commandID; } @@ -440,7 +440,7 @@ NetAckStage2CommandMsg::~NetAckStage2CommandMsg() { /** * Returns the command ID of the command being ack'd. */ -UnsignedShort NetAckStage2CommandMsg::getCommandID() { +UnsignedShort NetAckStage2CommandMsg::getCommandID() const { return m_commandID; } @@ -454,7 +454,7 @@ void NetAckStage2CommandMsg::setCommandID(UnsignedShort commandID) { /** * Get the player id of the player who originally sent the command. */ -UnsignedByte NetAckStage2CommandMsg::getOriginalPlayerID() { +UnsignedByte NetAckStage2CommandMsg::getOriginalPlayerID() const { return m_originalPlayerID; } @@ -465,7 +465,7 @@ void NetAckStage2CommandMsg::setOriginalPlayerID(UnsignedByte originalPlayerID) m_originalPlayerID = originalPlayerID; } -Int NetAckStage2CommandMsg::getSortNumber() { +Int NetAckStage2CommandMsg::getSortNumber() const { return m_commandID; } @@ -500,7 +500,7 @@ void NetFrameCommandMsg::setCommandCount(UnsignedShort commandCount) { /** * Return the command count of this frame. */ -UnsignedShort NetFrameCommandMsg::getCommandCount() { +UnsignedShort NetFrameCommandMsg::getCommandCount() const { return m_commandCount; } @@ -528,7 +528,7 @@ NetPlayerLeaveCommandMsg::~NetPlayerLeaveCommandMsg() { /** * Get the id of the player leaving the game. */ -UnsignedByte NetPlayerLeaveCommandMsg::getLeavingPlayerID() { +UnsignedByte NetPlayerLeaveCommandMsg::getLeavingPlayerID() const { return m_leavingPlayerID; } @@ -571,7 +571,7 @@ void NetRunAheadMetricsCommandMsg::setAverageLatency(Real avgLat) { /** * get the average latency */ -Real NetRunAheadMetricsCommandMsg::getAverageLatency() { +Real NetRunAheadMetricsCommandMsg::getAverageLatency() const { return m_averageLatency; } @@ -585,7 +585,7 @@ void NetRunAheadMetricsCommandMsg::setAverageFps(Int fps) { /** * get the average fps */ -Int NetRunAheadMetricsCommandMsg::getAverageFps() { +Int NetRunAheadMetricsCommandMsg::getAverageFps() const { return m_averageFps; } @@ -605,7 +605,7 @@ NetRunAheadCommandMsg::NetRunAheadCommandMsg() : NetCommandMsg() { NetRunAheadCommandMsg::~NetRunAheadCommandMsg() { } -UnsignedShort NetRunAheadCommandMsg::getRunAhead() { +UnsignedShort NetRunAheadCommandMsg::getRunAhead() const { return m_runAhead; } @@ -613,7 +613,7 @@ void NetRunAheadCommandMsg::setRunAhead(UnsignedShort runAhead) { m_runAhead = runAhead; } -UnsignedByte NetRunAheadCommandMsg::getFrameRate() { +UnsignedByte NetRunAheadCommandMsg::getFrameRate() const { return m_frameRate; } @@ -655,7 +655,7 @@ void NetDestroyPlayerCommandMsg::setPlayerIndex( UnsignedInt playerIndex ) /** * get the average CRC */ -UnsignedInt NetDestroyPlayerCommandMsg::getPlayerIndex() +UnsignedInt NetDestroyPlayerCommandMsg::getPlayerIndex() const { return m_playerIndex; } @@ -718,7 +718,7 @@ NetDisconnectPlayerCommandMsg::~NetDisconnectPlayerCommandMsg() { /** * Returns the disconnecting slot number */ -UnsignedByte NetDisconnectPlayerCommandMsg::getDisconnectSlot() { +UnsignedByte NetDisconnectPlayerCommandMsg::getDisconnectSlot() const { return m_disconnectSlot; } @@ -739,7 +739,7 @@ void NetDisconnectPlayerCommandMsg::setDisconnectFrame(UnsignedInt frame) { /** * returns the disconnect frame */ -UnsignedInt NetDisconnectPlayerCommandMsg::getDisconnectFrame() { +UnsignedInt NetDisconnectPlayerCommandMsg::getDisconnectFrame() const { return m_disconnectFrame; } @@ -813,7 +813,7 @@ void NetDisconnectChatCommandMsg::setText(UnicodeString text) { /** * Get the chat text for this message. */ -UnicodeString NetDisconnectChatCommandMsg::getText() { +UnicodeString NetDisconnectChatCommandMsg::getText() const { return m_text; } @@ -847,7 +847,7 @@ void NetChatCommandMsg::setText(UnicodeString text) /** * Get the chat text for this message. */ -UnicodeString NetChatCommandMsg::getText() +UnicodeString NetChatCommandMsg::getText() const { return m_text; } @@ -855,7 +855,7 @@ UnicodeString NetChatCommandMsg::getText() /** * Get the bitmask of chat recipients from this message. */ -Int NetChatCommandMsg::getPlayerMask() +Int NetChatCommandMsg::getPlayerMask() const { return m_playerMask; } @@ -911,14 +911,14 @@ void NetDisconnectVoteCommandMsg::setSlot(UnsignedByte slot) { /** * Get the slot that is being voted for. */ -UnsignedByte NetDisconnectVoteCommandMsg::getSlot() { +UnsignedByte NetDisconnectVoteCommandMsg::getSlot() const { return m_slot; } /** * Get the vote frame. */ -UnsignedInt NetDisconnectVoteCommandMsg::getVoteFrame() { +UnsignedInt NetDisconnectVoteCommandMsg::getVoteFrame() const { return m_voteFrame; } @@ -944,7 +944,7 @@ NetProgressCommandMsg::NetProgressCommandMsg() : NetCommandMsg() NetProgressCommandMsg::~NetProgressCommandMsg() {} -UnsignedByte NetProgressCommandMsg::getPercentage() +UnsignedByte NetProgressCommandMsg::getPercentage() const { return m_percent; } @@ -977,6 +977,10 @@ NetWrapperCommandMsg::~NetWrapperCommandMsg() { m_data = nullptr; } +const UnsignedByte * NetWrapperCommandMsg::getData() const { + return m_data; +} + UnsignedByte * NetWrapperCommandMsg::getData() { return m_data; } @@ -989,11 +993,11 @@ void NetWrapperCommandMsg::setData(UnsignedByte *data, UnsignedInt dataLength) m_dataLength = dataLength; } -UnsignedInt NetWrapperCommandMsg::getDataLength() { +UnsignedInt NetWrapperCommandMsg::getDataLength() const { return m_dataLength; } -UnsignedInt NetWrapperCommandMsg::getDataOffset() { +UnsignedInt NetWrapperCommandMsg::getDataOffset() const { return m_dataOffset; } @@ -1001,7 +1005,7 @@ void NetWrapperCommandMsg::setDataOffset(UnsignedInt offset) { m_dataOffset = offset; } -UnsignedInt NetWrapperCommandMsg::getChunkNumber() { +UnsignedInt NetWrapperCommandMsg::getChunkNumber() const { return m_chunkNumber; } @@ -1009,7 +1013,7 @@ void NetWrapperCommandMsg::setChunkNumber(UnsignedInt chunkNumber) { m_chunkNumber = chunkNumber; } -UnsignedInt NetWrapperCommandMsg::getNumChunks() { +UnsignedInt NetWrapperCommandMsg::getNumChunks() const { return m_numChunks; } @@ -1017,7 +1021,7 @@ void NetWrapperCommandMsg::setNumChunks(UnsignedInt numChunks) { m_numChunks = numChunks; } -UnsignedInt NetWrapperCommandMsg::getTotalDataLength() { +UnsignedInt NetWrapperCommandMsg::getTotalDataLength() const { return m_totalDataLength; } @@ -1025,7 +1029,7 @@ void NetWrapperCommandMsg::setTotalDataLength(UnsignedInt totalDataLength) { m_totalDataLength = totalDataLength; } -UnsignedShort NetWrapperCommandMsg::getWrappedCommandID() { +UnsignedShort NetWrapperCommandMsg::getWrappedCommandID() const { return m_wrappedCommandID; } @@ -1052,7 +1056,7 @@ NetFileCommandMsg::~NetFileCommandMsg() { m_data = nullptr; } -AsciiString NetFileCommandMsg::getRealFilename() +AsciiString NetFileCommandMsg::getRealFilename() const { return TheGameState->portableMapPathToRealMapPath(m_portableFilename); } @@ -1062,10 +1066,14 @@ void NetFileCommandMsg::setRealFilename(AsciiString filename) m_portableFilename = TheGameState->realMapPathToPortableMapPath(filename); } -UnsignedInt NetFileCommandMsg::getFileLength() { +UnsignedInt NetFileCommandMsg::getFileLength() const { return m_dataLength; } +const UnsignedByte * NetFileCommandMsg::getFileData() const { + return m_data; +} + UnsignedByte * NetFileCommandMsg::getFileData() { return m_data; } @@ -1098,7 +1106,7 @@ NetFileAnnounceCommandMsg::NetFileAnnounceCommandMsg() : NetCommandMsg() { NetFileAnnounceCommandMsg::~NetFileAnnounceCommandMsg() { } -AsciiString NetFileAnnounceCommandMsg::getRealFilename() +AsciiString NetFileAnnounceCommandMsg::getRealFilename() const { return TheGameState->portableMapPathToRealMapPath(m_portableFilename); } @@ -1108,7 +1116,7 @@ void NetFileAnnounceCommandMsg::setRealFilename(AsciiString filename) m_portableFilename = TheGameState->realMapPathToPortableMapPath(filename); } -UnsignedShort NetFileAnnounceCommandMsg::getFileID() { +UnsignedShort NetFileAnnounceCommandMsg::getFileID() const { return m_fileID; } @@ -1116,7 +1124,7 @@ void NetFileAnnounceCommandMsg::setFileID(UnsignedShort fileID) { m_fileID = fileID; } -UnsignedByte NetFileAnnounceCommandMsg::getPlayerMask() { +UnsignedByte NetFileAnnounceCommandMsg::getPlayerMask() const { return m_playerMask; } @@ -1144,7 +1152,7 @@ NetFileProgressCommandMsg::NetFileProgressCommandMsg() : NetCommandMsg() { NetFileProgressCommandMsg::~NetFileProgressCommandMsg() { } -UnsignedShort NetFileProgressCommandMsg::getFileID() { +UnsignedShort NetFileProgressCommandMsg::getFileID() const { return m_fileID; } @@ -1152,7 +1160,7 @@ void NetFileProgressCommandMsg::setFileID(UnsignedShort val) { m_fileID = val; } -Int NetFileProgressCommandMsg::getProgress() { +Int NetFileProgressCommandMsg::getProgress() const { return m_progress; } @@ -1175,7 +1183,7 @@ NetDisconnectFrameCommandMsg::NetDisconnectFrameCommandMsg() : NetCommandMsg() { NetDisconnectFrameCommandMsg::~NetDisconnectFrameCommandMsg() { } -UnsignedInt NetDisconnectFrameCommandMsg::getDisconnectFrame() { +UnsignedInt NetDisconnectFrameCommandMsg::getDisconnectFrame() const { return m_disconnectFrame; } @@ -1198,7 +1206,7 @@ NetDisconnectScreenOffCommandMsg::NetDisconnectScreenOffCommandMsg() : NetComman NetDisconnectScreenOffCommandMsg::~NetDisconnectScreenOffCommandMsg() { } -UnsignedInt NetDisconnectScreenOffCommandMsg::getNewFrame() { +UnsignedInt NetDisconnectScreenOffCommandMsg::getNewFrame() const { return m_newFrame; } @@ -1221,7 +1229,7 @@ NetFrameResendRequestCommandMsg::NetFrameResendRequestCommandMsg() : NetCommandM NetFrameResendRequestCommandMsg::~NetFrameResendRequestCommandMsg() { } -UnsignedInt NetFrameResendRequestCommandMsg::getFrameToResend() { +UnsignedInt NetFrameResendRequestCommandMsg::getFrameToResend() const { return m_frameToResend; } diff --git a/Generals/Code/GameEngine/Include/Common/MessageStream.h b/Generals/Code/GameEngine/Include/Common/MessageStream.h index a8b937ef538..61efa69b9be 100644 --- a/Generals/Code/GameEngine/Include/Common/MessageStream.h +++ b/Generals/Code/GameEngine/Include/Common/MessageStream.h @@ -628,7 +628,7 @@ class GameMessage : public MemoryPoolObject * @todo This should be a more list-like interface. Very inefficient. */ const GameMessageArgumentType *getArgument( Int argIndex ) const; - GameMessageArgumentDataType getArgumentDataType( Int argIndex ); + GameMessageArgumentDataType getArgumentDataType( Int argIndex ) const; void friend_setNext(GameMessage* m) { m_next = m; } void friend_setPrev(GameMessage* m) { m_prev = m; } diff --git a/Generals/Code/GameEngine/Source/Common/MessageStream.cpp b/Generals/Code/GameEngine/Source/Common/MessageStream.cpp index 92d65b98970..5df63dfb0e3 100644 --- a/Generals/Code/GameEngine/Source/Common/MessageStream.cpp +++ b/Generals/Code/GameEngine/Source/Common/MessageStream.cpp @@ -102,7 +102,7 @@ const GameMessageArgumentType *GameMessage::getArgument( Int argIndex ) const /** * Return the given argument data type */ -GameMessageArgumentDataType GameMessage::getArgumentDataType( Int argIndex ) +GameMessageArgumentDataType GameMessage::getArgumentDataType( Int argIndex ) const { if (argIndex >= m_argCount) { return ARGUMENTDATATYPE_UNKNOWN; diff --git a/GeneralsMD/Code/GameEngine/Include/Common/MessageStream.h b/GeneralsMD/Code/GameEngine/Include/Common/MessageStream.h index 100e6dd682a..4f36e5cc89f 100644 --- a/GeneralsMD/Code/GameEngine/Include/Common/MessageStream.h +++ b/GeneralsMD/Code/GameEngine/Include/Common/MessageStream.h @@ -662,7 +662,7 @@ class GameMessage : public MemoryPoolObject * @todo This should be a more list-like interface. Very inefficient. */ const GameMessageArgumentType *getArgument( Int argIndex ) const; - GameMessageArgumentDataType getArgumentDataType( Int argIndex ); + GameMessageArgumentDataType getArgumentDataType( Int argIndex ) const; void friend_setNext(GameMessage* m) { m_next = m; } void friend_setPrev(GameMessage* m) { m_prev = m; } diff --git a/GeneralsMD/Code/GameEngine/Source/Common/MessageStream.cpp b/GeneralsMD/Code/GameEngine/Source/Common/MessageStream.cpp index 500179a4e92..6d558c0a4f2 100644 --- a/GeneralsMD/Code/GameEngine/Source/Common/MessageStream.cpp +++ b/GeneralsMD/Code/GameEngine/Source/Common/MessageStream.cpp @@ -102,7 +102,7 @@ const GameMessageArgumentType *GameMessage::getArgument( Int argIndex ) const /** * Return the given argument data type */ -GameMessageArgumentDataType GameMessage::getArgumentDataType( Int argIndex ) +GameMessageArgumentDataType GameMessage::getArgumentDataType( Int argIndex ) const { if (argIndex >= m_argCount) { return ARGUMENTDATATYPE_UNKNOWN; From 396e2d96e44db4bb04f0e7941424ee08c61fe9d0 Mon Sep 17 00:00:00 2001 From: xezon <4720891+xezon@users.noreply.github.com> Date: Wed, 18 Feb 2026 00:21:08 +0100 Subject: [PATCH 2/5] chore(network): Add new NetAckCommandMsg (#2329) --- .../Include/GameNetwork/NetCommandMsg.h | 32 +++++++++++++++ .../Source/GameNetwork/NetCommandMsg.cpp | 40 +++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/Core/GameEngine/Include/GameNetwork/NetCommandMsg.h b/Core/GameEngine/Include/GameNetwork/NetCommandMsg.h index 296f03cb80d..d25b8258e27 100644 --- a/Core/GameEngine/Include/GameNetwork/NetCommandMsg.h +++ b/Core/GameEngine/Include/GameNetwork/NetCommandMsg.h @@ -90,6 +90,38 @@ class NetGameCommandMsg : public NetCommandMsg GameMessageArgument *m_argList, *m_argTail; }; +//----------------------------------------------------------------------------- +/** + * The NetAckCommandMsg is the base class for other ack command messages. + */ +class NetAckCommandMsg : public NetCommandMsg +{ +protected: + NetAckCommandMsg(NetCommandMsg *msg) + { + m_commandID = msg->getID(); + m_originalPlayerID = msg->getPlayerID(); + } + NetAckCommandMsg() + { + m_commandID = 0; + m_originalPlayerID = 0; + } + +public: + UnsignedShort getCommandID() const; + void setCommandID(UnsignedShort commandID); + UnsignedByte getOriginalPlayerID() const; + void setOriginalPlayerID(UnsignedByte originalPlayerID); + virtual Int getSortNumber() const; + + virtual size_t getPackedByteCount() const; + +protected: + UnsignedShort m_commandID; + UnsignedByte m_originalPlayerID; +}; + //----------------------------------------------------------------------------- /** * The NetAckBothCommandMsg is the NetCommandMsg representation of the combination of a diff --git a/Core/GameEngine/Source/GameNetwork/NetCommandMsg.cpp b/Core/GameEngine/Source/GameNetwork/NetCommandMsg.cpp index e21357d0b48..d28c229e134 100644 --- a/Core/GameEngine/Source/GameNetwork/NetCommandMsg.cpp +++ b/Core/GameEngine/Source/GameNetwork/NetCommandMsg.cpp @@ -290,6 +290,46 @@ size_t NetGameCommandMsg::getPackedByteCount() const { return msglen; } +//------------------------- +// NetAckCommandMsg +//------------------------- + +/** + * Returns the command ID of the command being ack'd. + */ +UnsignedShort NetAckCommandMsg::getCommandID() const { + return m_commandID; +} + +/** + * Set the command ID of the command being ack'd. + */ +void NetAckCommandMsg::setCommandID(UnsignedShort commandID) { + m_commandID = commandID; +} + +/** + * Get the player id of the player who originally sent the command. + */ +UnsignedByte NetAckCommandMsg::getOriginalPlayerID() const { + return m_originalPlayerID; +} + +/** + * Set the player id of the player who originally sent the command. + */ +void NetAckCommandMsg::setOriginalPlayerID(UnsignedByte originalPlayerID) { + m_originalPlayerID = originalPlayerID; +} + +Int NetAckCommandMsg::getSortNumber() const { + return m_commandID; +} + +size_t NetAckCommandMsg::getPackedByteCount() const { + return sizeof(NetPacketAckCommand); +} + //------------------------- // NetAckBothCommandMsg //------------------------- From bdcb6189b1136308f59c1d3afd3c89480d57f0b1 Mon Sep 17 00:00:00 2001 From: xezon <4720891+xezon@users.noreply.github.com> Date: Wed, 18 Feb 2026 00:24:39 +0100 Subject: [PATCH 3/5] refactor(netcommandmsg): Simplify net messages NetAckBothCommandMsg, NetAckStage1CommandMsg, NetAckStage2CommandMsg (#2329) --- .../Include/GameNetwork/NetCommandMsg.h | 42 +----- .../Source/GameNetwork/NetCommandMsg.cpp | 126 +----------------- 2 files changed, 9 insertions(+), 159 deletions(-) diff --git a/Core/GameEngine/Include/GameNetwork/NetCommandMsg.h b/Core/GameEngine/Include/GameNetwork/NetCommandMsg.h index d25b8258e27..7207e008d63 100644 --- a/Core/GameEngine/Include/GameNetwork/NetCommandMsg.h +++ b/Core/GameEngine/Include/GameNetwork/NetCommandMsg.h @@ -127,25 +127,13 @@ class NetAckCommandMsg : public NetCommandMsg * The NetAckBothCommandMsg is the NetCommandMsg representation of the combination of a * stage 1 ack and a stage 2 ack. */ -class NetAckBothCommandMsg : public NetCommandMsg +class NetAckBothCommandMsg : public NetAckCommandMsg { MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(NetAckBothCommandMsg, "NetAckBothCommandMsg") public: NetAckBothCommandMsg(NetCommandMsg *msg); NetAckBothCommandMsg(); //virtual ~NetAckBothCommandMsg(); - - UnsignedShort getCommandID() const; - void setCommandID(UnsignedShort commandID); - UnsignedByte getOriginalPlayerID() const; - void setOriginalPlayerID(UnsignedByte originalPlayerID); - virtual Int getSortNumber() const; - - virtual size_t getPackedByteCount() const; - -protected: - UnsignedShort m_commandID; - UnsignedByte m_originalPlayerID; }; //----------------------------------------------------------------------------- @@ -153,25 +141,13 @@ class NetAckBothCommandMsg : public NetCommandMsg * The NetAckStage1CommandMsg is the NetCommandMsg representation of an ack message for the initial * recipient. */ -class NetAckStage1CommandMsg : public NetCommandMsg +class NetAckStage1CommandMsg : public NetAckCommandMsg { MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(NetAckStage1CommandMsg, "NetAckStage1CommandMsg") public: NetAckStage1CommandMsg(NetCommandMsg *msg); NetAckStage1CommandMsg(); //virtual ~NetAckStage1CommandMsg(); - - UnsignedShort getCommandID() const; - void setCommandID(UnsignedShort commandID); - UnsignedByte getOriginalPlayerID() const; - void setOriginalPlayerID(UnsignedByte originalPlayerID); - virtual Int getSortNumber() const; - - virtual size_t getPackedByteCount() const; - -protected: - UnsignedShort m_commandID; - UnsignedByte m_originalPlayerID; }; //----------------------------------------------------------------------------- @@ -179,25 +155,13 @@ class NetAckStage1CommandMsg : public NetCommandMsg * The NetAckStage2CommandMsg is the NetCommandMsg representation of an ack message for all eventual * recipients. (when this is returned, all the players in the relay mask have received the packet) */ -class NetAckStage2CommandMsg : public NetCommandMsg +class NetAckStage2CommandMsg : public NetAckCommandMsg { MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(NetAckStage2CommandMsg, "NetAckStage2CommandMsg") public: NetAckStage2CommandMsg(NetCommandMsg *msg); NetAckStage2CommandMsg(); //virtual ~NetAckStage2CommandMsg(); - - UnsignedShort getCommandID() const; - void setCommandID(UnsignedShort commandID); - UnsignedByte getOriginalPlayerID() const; - void setOriginalPlayerID(UnsignedByte originalPlayerID); - virtual Int getSortNumber() const; - - virtual size_t getPackedByteCount() const; - -protected: - UnsignedShort m_commandID; - UnsignedByte m_originalPlayerID; }; //----------------------------------------------------------------------------- diff --git a/Core/GameEngine/Source/GameNetwork/NetCommandMsg.cpp b/Core/GameEngine/Source/GameNetwork/NetCommandMsg.cpp index d28c229e134..c8b81df6f2c 100644 --- a/Core/GameEngine/Source/GameNetwork/NetCommandMsg.cpp +++ b/Core/GameEngine/Source/GameNetwork/NetCommandMsg.cpp @@ -336,16 +336,14 @@ size_t NetAckCommandMsg::getPackedByteCount() const { /** * Constructor. Sets the member variables according to the given message. */ -NetAckBothCommandMsg::NetAckBothCommandMsg(NetCommandMsg *msg) : NetCommandMsg() { - m_commandID = msg->getID(); +NetAckBothCommandMsg::NetAckBothCommandMsg(NetCommandMsg *msg) : NetAckCommandMsg(msg) { m_commandType = NETCOMMANDTYPE_ACKBOTH; - m_originalPlayerID = msg->getPlayerID(); } /** * Constructor. Sets the member variables to default values. */ -NetAckBothCommandMsg::NetAckBothCommandMsg() : NetCommandMsg() { +NetAckBothCommandMsg::NetAckBothCommandMsg() { m_commandType = NETCOMMANDTYPE_ACKBOTH; } @@ -355,58 +353,20 @@ NetAckBothCommandMsg::NetAckBothCommandMsg() : NetCommandMsg() { NetAckBothCommandMsg::~NetAckBothCommandMsg() { } -/** - * Returns the command ID of the command being ack'd. - */ -UnsignedShort NetAckBothCommandMsg::getCommandID() const { - return m_commandID; -} - -/** - * Set the command ID of the command being ack'd. - */ -void NetAckBothCommandMsg::setCommandID(UnsignedShort commandID) { - m_commandID = commandID; -} - -/** - * Get the player id of the player who originally sent the command. - */ -UnsignedByte NetAckBothCommandMsg::getOriginalPlayerID() const { - return m_originalPlayerID; -} - -/** - * Set the player id of the player who originally sent the command. - */ -void NetAckBothCommandMsg::setOriginalPlayerID(UnsignedByte originalPlayerID) { - m_originalPlayerID = originalPlayerID; -} - -Int NetAckBothCommandMsg::getSortNumber() const { - return m_commandID; -} - -size_t NetAckBothCommandMsg::getPackedByteCount() const { - return sizeof(NetPacketAckCommand); -} - //------------------------- // NetAckStage1CommandMsg //------------------------- /** * Constructor. Sets the member variables according to the given message. */ -NetAckStage1CommandMsg::NetAckStage1CommandMsg(NetCommandMsg *msg) : NetCommandMsg() { - m_commandID = msg->getID(); +NetAckStage1CommandMsg::NetAckStage1CommandMsg(NetCommandMsg *msg) : NetAckCommandMsg(msg) { m_commandType = NETCOMMANDTYPE_ACKSTAGE1; - m_originalPlayerID = msg->getPlayerID(); } /** * Constructor. Sets the member variables to default values. */ -NetAckStage1CommandMsg::NetAckStage1CommandMsg() : NetCommandMsg() { +NetAckStage1CommandMsg::NetAckStage1CommandMsg() { m_commandType = NETCOMMANDTYPE_ACKSTAGE1; } @@ -416,58 +376,20 @@ NetAckStage1CommandMsg::NetAckStage1CommandMsg() : NetCommandMsg() { NetAckStage1CommandMsg::~NetAckStage1CommandMsg() { } -/** - * Returns the command ID of the command being ack'd. - */ -UnsignedShort NetAckStage1CommandMsg::getCommandID() const { - return m_commandID; -} - -/** - * Set the command ID of the command being ack'd. - */ -void NetAckStage1CommandMsg::setCommandID(UnsignedShort commandID) { - m_commandID = commandID; -} - -/** - * Get the player id of the player who originally sent the command. - */ -UnsignedByte NetAckStage1CommandMsg::getOriginalPlayerID() const { - return m_originalPlayerID; -} - -/** - * Set the player id of the player who originally sent the command. - */ -void NetAckStage1CommandMsg::setOriginalPlayerID(UnsignedByte originalPlayerID) { - m_originalPlayerID = originalPlayerID; -} - -Int NetAckStage1CommandMsg::getSortNumber() const { - return m_commandID; -} - -size_t NetAckStage1CommandMsg::getPackedByteCount() const { - return sizeof(NetPacketAckCommand); -} - //------------------------- // NetAckStage2CommandMsg //------------------------- /** * Constructor. Sets the member variables according to the given message. */ -NetAckStage2CommandMsg::NetAckStage2CommandMsg(NetCommandMsg *msg) : NetCommandMsg() { - m_commandID = msg->getID(); +NetAckStage2CommandMsg::NetAckStage2CommandMsg(NetCommandMsg *msg) : NetAckCommandMsg(msg) { m_commandType = NETCOMMANDTYPE_ACKSTAGE2; - m_originalPlayerID = msg->getPlayerID(); } /** * Constructor. Sets the member variables to default values. */ -NetAckStage2CommandMsg::NetAckStage2CommandMsg() : NetCommandMsg() { +NetAckStage2CommandMsg::NetAckStage2CommandMsg() { m_commandType = NETCOMMANDTYPE_ACKSTAGE2; } @@ -477,42 +399,6 @@ NetAckStage2CommandMsg::NetAckStage2CommandMsg() : NetCommandMsg() { NetAckStage2CommandMsg::~NetAckStage2CommandMsg() { } -/** - * Returns the command ID of the command being ack'd. - */ -UnsignedShort NetAckStage2CommandMsg::getCommandID() const { - return m_commandID; -} - -/** - * Set the command ID of the command being ack'd. - */ -void NetAckStage2CommandMsg::setCommandID(UnsignedShort commandID) { - m_commandID = commandID; -} - -/** - * Get the player id of the player who originally sent the command. - */ -UnsignedByte NetAckStage2CommandMsg::getOriginalPlayerID() const { - return m_originalPlayerID; -} - -/** - * Set the player id of the player who originally sent the command. - */ -void NetAckStage2CommandMsg::setOriginalPlayerID(UnsignedByte originalPlayerID) { - m_originalPlayerID = originalPlayerID; -} - -Int NetAckStage2CommandMsg::getSortNumber() const { - return m_commandID; -} - -size_t NetAckStage2CommandMsg::getPackedByteCount() const { - return sizeof(NetPacketAckCommand); -} - //------------------------- // NetFrameCommandMsg //------------------------- From 89ff3d8d0ec743c2700c761a0aafca415a4ceb0a Mon Sep 17 00:00:00 2001 From: xezon <4720891+xezon@users.noreply.github.com> Date: Wed, 18 Feb 2026 20:25:10 +0100 Subject: [PATCH 4/5] refactor(netcommandmsg): Rename NetCommandMsg::getPackedByteCount to NetCommandMsg::getSizeForNetPacket (#2329) --- .../Include/GameNetwork/NetCommandMsg.h | 52 +++++++++---------- .../Source/GameNetwork/NetCommandMsg.cpp | 50 +++++++++--------- .../Source/GameNetwork/NetPacket.cpp | 2 +- 3 files changed, 52 insertions(+), 52 deletions(-) diff --git a/Core/GameEngine/Include/GameNetwork/NetCommandMsg.h b/Core/GameEngine/Include/GameNetwork/NetCommandMsg.h index 7207e008d63..ed19a3a7b6b 100644 --- a/Core/GameEngine/Include/GameNetwork/NetCommandMsg.h +++ b/Core/GameEngine/Include/GameNetwork/NetCommandMsg.h @@ -50,7 +50,7 @@ class NetCommandMsg : public MemoryPoolObject void setNetCommandType(NetCommandType type) { m_commandType = type; } NetCommandType getNetCommandType() const { return m_commandType; } virtual Int getSortNumber() const; - virtual size_t getPackedByteCount() const = 0; + virtual size_t getSizeForNetPacket() const = 0; void attach(); void detach(); @@ -81,7 +81,7 @@ class NetGameCommandMsg : public NetCommandMsg void addArgument(const GameMessageArgumentDataType type, GameMessageArgumentType arg); void setGameMessageType(GameMessage::Type type); - virtual size_t getPackedByteCount() const; + virtual size_t getSizeForNetPacket() const; protected: Int m_numArgs; @@ -115,7 +115,7 @@ class NetAckCommandMsg : public NetCommandMsg void setOriginalPlayerID(UnsignedByte originalPlayerID); virtual Int getSortNumber() const; - virtual size_t getPackedByteCount() const; + virtual size_t getSizeForNetPacket() const; protected: UnsignedShort m_commandID; @@ -175,7 +175,7 @@ class NetFrameCommandMsg : public NetCommandMsg void setCommandCount(UnsignedShort commandCount); UnsignedShort getCommandCount() const; - virtual size_t getPackedByteCount() const; + virtual size_t getSizeForNetPacket() const; protected: UnsignedShort m_commandCount; @@ -192,7 +192,7 @@ class NetPlayerLeaveCommandMsg : public NetCommandMsg UnsignedByte getLeavingPlayerID() const; void setLeavingPlayerID(UnsignedByte id); - virtual size_t getPackedByteCount() const; + virtual size_t getSizeForNetPacket() const; protected: UnsignedByte m_leavingPlayerID; @@ -211,7 +211,7 @@ class NetRunAheadMetricsCommandMsg : public NetCommandMsg Int getAverageFps() const; void setAverageFps(Int fps); - virtual size_t getPackedByteCount() const; + virtual size_t getSizeForNetPacket() const; protected: Real m_averageLatency; @@ -232,7 +232,7 @@ class NetRunAheadCommandMsg : public NetCommandMsg UnsignedByte getFrameRate() const; void setFrameRate(UnsignedByte frameRate); - virtual size_t getPackedByteCount() const; + virtual size_t getSizeForNetPacket() const; protected: UnsignedShort m_runAhead; @@ -250,7 +250,7 @@ class NetDestroyPlayerCommandMsg : public NetCommandMsg UnsignedInt getPlayerIndex() const; void setPlayerIndex(UnsignedInt playerIndex); - virtual size_t getPackedByteCount() const; + virtual size_t getSizeForNetPacket() const; protected: UnsignedInt m_playerIndex; @@ -264,7 +264,7 @@ class NetKeepAliveCommandMsg : public NetCommandMsg NetKeepAliveCommandMsg(); //virtual ~NetKeepAliveCommandMsg(); - virtual size_t getPackedByteCount() const; + virtual size_t getSizeForNetPacket() const; }; //----------------------------------------------------------------------------- @@ -275,7 +275,7 @@ class NetDisconnectKeepAliveCommandMsg : public NetCommandMsg NetDisconnectKeepAliveCommandMsg(); //virtual ~NetDisconnectKeepAliveCommandMsg(); - virtual size_t getPackedByteCount() const; + virtual size_t getSizeForNetPacket() const; }; //----------------------------------------------------------------------------- @@ -292,7 +292,7 @@ class NetDisconnectPlayerCommandMsg : public NetCommandMsg UnsignedInt getDisconnectFrame() const; void setDisconnectFrame(UnsignedInt frame); - virtual size_t getPackedByteCount() const; + virtual size_t getSizeForNetPacket() const; protected: UnsignedByte m_disconnectSlot; @@ -307,7 +307,7 @@ class NetPacketRouterQueryCommandMsg : public NetCommandMsg NetPacketRouterQueryCommandMsg(); //virtual ~NetPacketRouterQueryCommandMsg(); - virtual size_t getPackedByteCount() const; + virtual size_t getSizeForNetPacket() const; }; //----------------------------------------------------------------------------- @@ -318,7 +318,7 @@ class NetPacketRouterAckCommandMsg : public NetCommandMsg NetPacketRouterAckCommandMsg(); //virtual ~NetPacketRouterAckCommandMsg(); - virtual size_t getPackedByteCount() const; + virtual size_t getSizeForNetPacket() const; }; //----------------------------------------------------------------------------- @@ -332,7 +332,7 @@ class NetDisconnectChatCommandMsg : public NetCommandMsg UnicodeString getText() const; void setText(UnicodeString text); - virtual size_t getPackedByteCount() const; + virtual size_t getSizeForNetPacket() const; protected: UnicodeString m_text; @@ -352,7 +352,7 @@ class NetChatCommandMsg : public NetCommandMsg Int getPlayerMask() const; void setPlayerMask( Int playerMask ); - virtual size_t getPackedByteCount() const; + virtual size_t getSizeForNetPacket() const; protected: UnicodeString m_text; @@ -373,7 +373,7 @@ class NetDisconnectVoteCommandMsg : public NetCommandMsg UnsignedInt getVoteFrame() const; void setVoteFrame(UnsignedInt voteFrame); - virtual size_t getPackedByteCount() const; + virtual size_t getSizeForNetPacket() const; protected: UnsignedByte m_slot; @@ -391,7 +391,7 @@ class NetProgressCommandMsg: public NetCommandMsg UnsignedByte getPercentage() const; void setPercentage( UnsignedByte percent ); - virtual size_t getPackedByteCount() const; + virtual size_t getSizeForNetPacket() const; protected: UnsignedByte m_percent; @@ -409,7 +409,7 @@ class NetWrapperCommandMsg : public NetCommandMsg UnsignedByte * getData(); void setData(UnsignedByte *data, UnsignedInt dataLength); - virtual size_t getPackedByteCount() const; + virtual size_t getSizeForNetPacket() const; UnsignedInt getChunkNumber() const; void setChunkNumber(UnsignedInt chunkNumber); @@ -459,7 +459,7 @@ class NetFileCommandMsg : public NetCommandMsg UnsignedByte * getFileData(); void setFileData(UnsignedByte *data, UnsignedInt dataLength); - virtual size_t getPackedByteCount() const; + virtual size_t getSizeForNetPacket() const; protected: AsciiString m_portableFilename; @@ -488,7 +488,7 @@ class NetFileAnnounceCommandMsg : public NetCommandMsg UnsignedByte getPlayerMask() const; void setPlayerMask(UnsignedByte playerMask); - virtual size_t getPackedByteCount() const; + virtual size_t getSizeForNetPacket() const; protected: AsciiString m_portableFilename; @@ -510,7 +510,7 @@ class NetFileProgressCommandMsg : public NetCommandMsg Int getProgress() const; void setProgress(Int val); - virtual size_t getPackedByteCount() const; + virtual size_t getSizeForNetPacket() const; protected: UnsignedShort m_fileID; @@ -527,7 +527,7 @@ class NetDisconnectFrameCommandMsg : public NetCommandMsg UnsignedInt getDisconnectFrame() const; void setDisconnectFrame(UnsignedInt disconnectFrame); - virtual size_t getPackedByteCount() const; + virtual size_t getSizeForNetPacket() const; protected: UnsignedInt m_disconnectFrame; @@ -543,7 +543,7 @@ class NetDisconnectScreenOffCommandMsg : public NetCommandMsg UnsignedInt getNewFrame() const; void setNewFrame(UnsignedInt newFrame); - virtual size_t getPackedByteCount() const; + virtual size_t getSizeForNetPacket() const; protected: UnsignedInt m_newFrame; @@ -559,7 +559,7 @@ class NetFrameResendRequestCommandMsg : public NetCommandMsg UnsignedInt getFrameToResend() const; void setFrameToResend(UnsignedInt frame); - virtual size_t getPackedByteCount() const; + virtual size_t getSizeForNetPacket() const; protected: UnsignedInt m_frameToResend; @@ -572,7 +572,7 @@ class NetLoadCompleteCommandMsg : public NetCommandMsg NetLoadCompleteCommandMsg(); //virtual ~NetLoadCompleteCommandMsg(); - virtual size_t getPackedByteCount() const; + virtual size_t getSizeForNetPacket() const; }; class NetTimeOutGameStartCommandMsg : public NetCommandMsg @@ -582,5 +582,5 @@ class NetTimeOutGameStartCommandMsg : public NetCommandMsg NetTimeOutGameStartCommandMsg(); //virtual ~NetTimeOutGameStartCommandMsg(); - virtual size_t getPackedByteCount() const; + virtual size_t getSizeForNetPacket() const; }; diff --git a/Core/GameEngine/Source/GameNetwork/NetCommandMsg.cpp b/Core/GameEngine/Source/GameNetwork/NetCommandMsg.cpp index c8b81df6f2c..34c20a2f59c 100644 --- a/Core/GameEngine/Source/GameNetwork/NetCommandMsg.cpp +++ b/Core/GameEngine/Source/GameNetwork/NetCommandMsg.cpp @@ -224,7 +224,7 @@ void NetGameCommandMsg::setGameMessageType(GameMessage::Type type) { m_type = type; } -size_t NetGameCommandMsg::getPackedByteCount() const { +size_t NetGameCommandMsg::getSizeForNetPacket() const { UnsignedShort msglen = sizeof(NetPacketGameCommand); // Variable data portion @@ -326,7 +326,7 @@ Int NetAckCommandMsg::getSortNumber() const { return m_commandID; } -size_t NetAckCommandMsg::getPackedByteCount() const { +size_t NetAckCommandMsg::getSizeForNetPacket() const { return sizeof(NetPacketAckCommand); } @@ -430,7 +430,7 @@ UnsignedShort NetFrameCommandMsg::getCommandCount() const { return m_commandCount; } -size_t NetFrameCommandMsg::getPackedByteCount() const { +size_t NetFrameCommandMsg::getSizeForNetPacket() const { return sizeof(NetPacketFrameCommand); } @@ -465,7 +465,7 @@ void NetPlayerLeaveCommandMsg::setLeavingPlayerID(UnsignedByte id) { m_leavingPlayerID = id; } -size_t NetPlayerLeaveCommandMsg::getPackedByteCount() const { +size_t NetPlayerLeaveCommandMsg::getSizeForNetPacket() const { return sizeof(NetPacketPlayerLeaveCommand); } @@ -515,7 +515,7 @@ Int NetRunAheadMetricsCommandMsg::getAverageFps() const { return m_averageFps; } -size_t NetRunAheadMetricsCommandMsg::getPackedByteCount() const { +size_t NetRunAheadMetricsCommandMsg::getSizeForNetPacket() const { return sizeof(NetPacketRunAheadMetricsCommand); } @@ -547,7 +547,7 @@ void NetRunAheadCommandMsg::setFrameRate(UnsignedByte frameRate) { m_frameRate = frameRate; } -size_t NetRunAheadCommandMsg::getPackedByteCount() const { +size_t NetRunAheadCommandMsg::getSizeForNetPacket() const { return sizeof(NetPacketRunAheadCommand); } @@ -586,7 +586,7 @@ UnsignedInt NetDestroyPlayerCommandMsg::getPlayerIndex() const return m_playerIndex; } -size_t NetDestroyPlayerCommandMsg::getPackedByteCount() const { +size_t NetDestroyPlayerCommandMsg::getSizeForNetPacket() const { return sizeof(NetPacketDestroyPlayerCommand); } @@ -603,7 +603,7 @@ NetKeepAliveCommandMsg::NetKeepAliveCommandMsg() : NetCommandMsg() { NetKeepAliveCommandMsg::~NetKeepAliveCommandMsg() { } -size_t NetKeepAliveCommandMsg::getPackedByteCount() const { +size_t NetKeepAliveCommandMsg::getSizeForNetPacket() const { return sizeof(NetPacketKeepAliveCommand); } @@ -620,7 +620,7 @@ NetDisconnectKeepAliveCommandMsg::NetDisconnectKeepAliveCommandMsg() : NetComman NetDisconnectKeepAliveCommandMsg::~NetDisconnectKeepAliveCommandMsg() { } -size_t NetDisconnectKeepAliveCommandMsg::getPackedByteCount() const { +size_t NetDisconnectKeepAliveCommandMsg::getSizeForNetPacket() const { return sizeof(NetPacketDisconnectKeepAliveCommand); } @@ -669,7 +669,7 @@ UnsignedInt NetDisconnectPlayerCommandMsg::getDisconnectFrame() const { return m_disconnectFrame; } -size_t NetDisconnectPlayerCommandMsg::getPackedByteCount() const { +size_t NetDisconnectPlayerCommandMsg::getSizeForNetPacket() const { return sizeof(NetPacketDisconnectPlayerCommand); } @@ -689,7 +689,7 @@ NetPacketRouterQueryCommandMsg::NetPacketRouterQueryCommandMsg() : NetCommandMsg NetPacketRouterQueryCommandMsg::~NetPacketRouterQueryCommandMsg() { } -size_t NetPacketRouterQueryCommandMsg::getPackedByteCount() const { +size_t NetPacketRouterQueryCommandMsg::getSizeForNetPacket() const { return sizeof(NetPacketRouterQueryCommand); } @@ -709,7 +709,7 @@ NetPacketRouterAckCommandMsg::NetPacketRouterAckCommandMsg() : NetCommandMsg() { NetPacketRouterAckCommandMsg::~NetPacketRouterAckCommandMsg() { } -size_t NetPacketRouterAckCommandMsg::getPackedByteCount() const { +size_t NetPacketRouterAckCommandMsg::getSizeForNetPacket() const { return sizeof(NetPacketRouterAckCommand); } @@ -794,7 +794,7 @@ void NetChatCommandMsg::setPlayerMask( Int playerMask ) m_playerMask = playerMask; } -size_t NetChatCommandMsg::getPackedByteCount() const +size_t NetChatCommandMsg::getSizeForNetPacket() const { return sizeof(NetPacketChatCommand) + m_text.getByteCount() @@ -804,7 +804,7 @@ size_t NetChatCommandMsg::getPackedByteCount() const //------------------------- // NetDisconnectChatCommandMsg //------------------------- -size_t NetDisconnectChatCommandMsg::getPackedByteCount() const +size_t NetDisconnectChatCommandMsg::getSizeForNetPacket() const { return sizeof(NetPacketDisconnectChatCommand) + m_text.getByteCount(); @@ -855,7 +855,7 @@ void NetDisconnectVoteCommandMsg::setVoteFrame(UnsignedInt voteFrame) { m_voteFrame = voteFrame; } -size_t NetDisconnectVoteCommandMsg::getPackedByteCount() const { +size_t NetDisconnectVoteCommandMsg::getSizeForNetPacket() const { return sizeof(NetPacketDisconnectVoteCommand); } @@ -880,7 +880,7 @@ void NetProgressCommandMsg::setPercentage( UnsignedByte percent ) m_percent = percent; } -size_t NetProgressCommandMsg::getPackedByteCount() const { +size_t NetProgressCommandMsg::getSizeForNetPacket() const { return sizeof(NetPacketProgressCommand); } @@ -963,7 +963,7 @@ void NetWrapperCommandMsg::setWrappedCommandID(UnsignedShort wrappedCommandID) { m_wrappedCommandID = wrappedCommandID; } -size_t NetWrapperCommandMsg::getPackedByteCount() const { +size_t NetWrapperCommandMsg::getSizeForNetPacket() const { return sizeof(NetPacketWrapperCommand) + m_dataLength; } @@ -1011,7 +1011,7 @@ void NetFileCommandMsg::setFileData(UnsignedByte *data, UnsignedInt dataLength) memcpy(m_data, data, dataLength); } -size_t NetFileCommandMsg::getPackedByteCount() const +size_t NetFileCommandMsg::getSizeForNetPacket() const { return sizeof(NetPacketFileCommand) + m_portableFilename.getLength() + 1 @@ -1058,7 +1058,7 @@ void NetFileAnnounceCommandMsg::setPlayerMask(UnsignedByte playerMask) { m_playerMask = playerMask; } -size_t NetFileAnnounceCommandMsg::getPackedByteCount() const +size_t NetFileAnnounceCommandMsg::getSizeForNetPacket() const { return sizeof(NetPacketFileAnnounceCommand) + m_portableFilename.getLength() + 1 @@ -1094,7 +1094,7 @@ void NetFileProgressCommandMsg::setProgress(Int val) { m_progress = val; } -size_t NetFileProgressCommandMsg::getPackedByteCount() const { +size_t NetFileProgressCommandMsg::getSizeForNetPacket() const { return sizeof(NetPacketFileProgressCommand); } @@ -1117,7 +1117,7 @@ void NetDisconnectFrameCommandMsg::setDisconnectFrame(UnsignedInt disconnectFram m_disconnectFrame = disconnectFrame; } -size_t NetDisconnectFrameCommandMsg::getPackedByteCount() const { +size_t NetDisconnectFrameCommandMsg::getSizeForNetPacket() const { return sizeof(NetPacketDisconnectFrameCommand); } @@ -1140,7 +1140,7 @@ void NetDisconnectScreenOffCommandMsg::setNewFrame(UnsignedInt newFrame) { m_newFrame = newFrame; } -size_t NetDisconnectScreenOffCommandMsg::getPackedByteCount() const { +size_t NetDisconnectScreenOffCommandMsg::getSizeForNetPacket() const { return sizeof(NetPacketDisconnectScreenOffCommand); } @@ -1163,7 +1163,7 @@ void NetFrameResendRequestCommandMsg::setFrameToResend(UnsignedInt frame) { m_frameToResend = frame; } -size_t NetFrameResendRequestCommandMsg::getPackedByteCount() const { +size_t NetFrameResendRequestCommandMsg::getSizeForNetPacket() const { return sizeof(NetPacketFrameResendRequestCommand); } @@ -1177,7 +1177,7 @@ NetLoadCompleteCommandMsg::NetLoadCompleteCommandMsg() : NetCommandMsg() { NetLoadCompleteCommandMsg::~NetLoadCompleteCommandMsg() { } -size_t NetLoadCompleteCommandMsg::getPackedByteCount() const { +size_t NetLoadCompleteCommandMsg::getSizeForNetPacket() const { return sizeof(NetPacketLoadCompleteCommand); } @@ -1191,6 +1191,6 @@ NetTimeOutGameStartCommandMsg::NetTimeOutGameStartCommandMsg() : NetCommandMsg() NetTimeOutGameStartCommandMsg::~NetTimeOutGameStartCommandMsg() { } -size_t NetTimeOutGameStartCommandMsg::getPackedByteCount() const { +size_t NetTimeOutGameStartCommandMsg::getSizeForNetPacket() const { return sizeof(NetPacketTimeOutGameStartCommand); } diff --git a/Core/GameEngine/Source/GameNetwork/NetPacket.cpp b/Core/GameEngine/Source/GameNetwork/NetPacket.cpp index 3d4315f7f2d..608a3c2c092 100644 --- a/Core/GameEngine/Source/GameNetwork/NetPacket.cpp +++ b/Core/GameEngine/Source/GameNetwork/NetPacket.cpp @@ -279,7 +279,7 @@ UnsignedInt NetPacket::GetBufferSizeNeededForCommand(NetCommandMsg *msg) { return 0; // There was nothing to add. } // Use the virtual function for all command message types - return msg->getPackedByteCount(); + return msg->getSizeForNetPacket(); } From 35fa72ac87e3290781031048c9f6e53612cd6332 Mon Sep 17 00:00:00 2001 From: xezon <4720891+xezon@users.noreply.github.com> Date: Thu, 19 Feb 2026 22:58:41 +0100 Subject: [PATCH 5/5] refactor(netpacket): Simplify NetPacket functions for packet buffer writes and size tests (#2329) --- Core/GameEngine/CMakeLists.txt | 1 + .../Include/GameNetwork/NetCommandMsg.h | 135 +- .../Include/GameNetwork/NetPacket.h | 111 +- .../Include/GameNetwork/NetPacketStructs.h | 1292 ++++-- .../Source/GameNetwork/NetCommandMsg.cpp | 382 +- .../Source/GameNetwork/NetPacket.cpp | 3529 +---------------- .../Source/GameNetwork/NetPacketStructs.cpp | 905 +++++ 7 files changed, 2346 insertions(+), 4009 deletions(-) create mode 100644 Core/GameEngine/Source/GameNetwork/NetPacketStructs.cpp diff --git a/Core/GameEngine/CMakeLists.txt b/Core/GameEngine/CMakeLists.txt index fe0f700648c..cba050dd079 100644 --- a/Core/GameEngine/CMakeLists.txt +++ b/Core/GameEngine/CMakeLists.txt @@ -1138,6 +1138,7 @@ set(GAMEENGINE_SRC Source/GameNetwork/NetCommandWrapperList.cpp Source/GameNetwork/NetMessageStream.cpp Source/GameNetwork/NetPacket.cpp + Source/GameNetwork/NetPacketStructs.cpp Source/GameNetwork/Network.cpp Source/GameNetwork/NetworkUtil.cpp Source/GameNetwork/Transport.cpp diff --git a/Core/GameEngine/Include/GameNetwork/NetCommandMsg.h b/Core/GameEngine/Include/GameNetwork/NetCommandMsg.h index ed19a3a7b6b..6b97d8fbef4 100644 --- a/Core/GameEngine/Include/GameNetwork/NetCommandMsg.h +++ b/Core/GameEngine/Include/GameNetwork/NetCommandMsg.h @@ -30,13 +30,18 @@ #include "Lib/BaseType.h" #include "GameNetwork/NetworkDefs.h" +#include "GameNetwork/NetPacketStructs.h" #include "Common/UnicodeString.h" +class NetCommandRef; + //----------------------------------------------------------------------------- class NetCommandMsg : public MemoryPoolObject { MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(NetCommandMsg, "NetCommandMsg") public: + typedef SmallNetPacketCommandBaseSelect Select; + NetCommandMsg(); //virtual ~NetCommandMsg(); UnsignedInt GetTimestamp() { return m_timestamp; } @@ -51,6 +56,10 @@ class NetCommandMsg : public MemoryPoolObject NetCommandType getNetCommandType() const { return m_commandType; } virtual Int getSortNumber() const; virtual size_t getSizeForNetPacket() const = 0; + virtual size_t copyBytesForNetPacket(UnsignedByte* buffer, const NetCommandRef& ref) const = 0; + virtual size_t getSizeForSmallNetPacket(const Select* select = nullptr) const = 0; + virtual size_t copyBytesForSmallNetPacket(UnsignedByte* buffer, const NetCommandRef& ref, const Select* select = nullptr) const = 0; + virtual Select getSmallNetPacketSelect() const = 0; void attach(); void detach(); @@ -64,12 +73,36 @@ class NetCommandMsg : public MemoryPoolObject Int m_referenceCount; }; +//----------------------------------------------------------------------------- +template +class NetCommandMsgT : public NetCommandMsg +{ + virtual size_t getSizeForNetPacket() const + { + return NetPacketType::getSize(*this); + } + + virtual size_t copyBytesForNetPacket(UnsignedByte* buffer, const NetCommandRef& ref) const + { + return NetPacketType::copyBytes(buffer, ref); + } + + virtual size_t getSizeForSmallNetPacket(const Select* select = nullptr) const + { + return SmallNetPacketType::getSize(*this, select); + } + + virtual size_t copyBytesForSmallNetPacket(UnsignedByte* buffer, const NetCommandRef& ref, const Select* select = nullptr) const + { + return SmallNetPacketType::copyBytes(buffer, ref, select); + } +}; //----------------------------------------------------------------------------- /** * The NetGameCommandMsg is the NetCommandMsg representation of a GameMessage */ -class NetGameCommandMsg : public NetCommandMsg +class NetGameCommandMsg : public NetCommandMsgT { MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(NetGameCommandMsg, "NetGameCommandMsg") public: @@ -81,7 +114,7 @@ class NetGameCommandMsg : public NetCommandMsg void addArgument(const GameMessageArgumentDataType type, GameMessageArgumentType arg); void setGameMessageType(GameMessage::Type type); - virtual size_t getSizeForNetPacket() const; + virtual Select getSmallNetPacketSelect() const; protected: Int m_numArgs; @@ -94,7 +127,7 @@ class NetGameCommandMsg : public NetCommandMsg /** * The NetAckCommandMsg is the base class for other ack command messages. */ -class NetAckCommandMsg : public NetCommandMsg +class NetAckCommandMsg : public NetCommandMsgT { protected: NetAckCommandMsg(NetCommandMsg *msg) @@ -115,7 +148,7 @@ class NetAckCommandMsg : public NetCommandMsg void setOriginalPlayerID(UnsignedByte originalPlayerID); virtual Int getSortNumber() const; - virtual size_t getSizeForNetPacket() const; + virtual Select getSmallNetPacketSelect() const; protected: UnsignedShort m_commandID; @@ -165,7 +198,7 @@ class NetAckStage2CommandMsg : public NetAckCommandMsg }; //----------------------------------------------------------------------------- -class NetFrameCommandMsg : public NetCommandMsg +class NetFrameCommandMsg : public NetCommandMsgT { MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(NetFrameCommandMsg, "NetFrameCommandMsg") public: @@ -175,14 +208,14 @@ class NetFrameCommandMsg : public NetCommandMsg void setCommandCount(UnsignedShort commandCount); UnsignedShort getCommandCount() const; - virtual size_t getSizeForNetPacket() const; + virtual Select getSmallNetPacketSelect() const; protected: UnsignedShort m_commandCount; }; //----------------------------------------------------------------------------- -class NetPlayerLeaveCommandMsg : public NetCommandMsg +class NetPlayerLeaveCommandMsg : public NetCommandMsgT { MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(NetPlayerLeaveCommandMsg, "NetPlayerLeaveCommandMsg") public: @@ -192,14 +225,14 @@ class NetPlayerLeaveCommandMsg : public NetCommandMsg UnsignedByte getLeavingPlayerID() const; void setLeavingPlayerID(UnsignedByte id); - virtual size_t getSizeForNetPacket() const; + virtual Select getSmallNetPacketSelect() const; protected: UnsignedByte m_leavingPlayerID; }; //----------------------------------------------------------------------------- -class NetRunAheadMetricsCommandMsg : public NetCommandMsg +class NetRunAheadMetricsCommandMsg : public NetCommandMsgT { MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(NetRunAheadMetricsCommandMsg, "NetRunAheadMetricsCommandMsg") public: @@ -211,7 +244,7 @@ class NetRunAheadMetricsCommandMsg : public NetCommandMsg Int getAverageFps() const; void setAverageFps(Int fps); - virtual size_t getSizeForNetPacket() const; + virtual Select getSmallNetPacketSelect() const; protected: Real m_averageLatency; @@ -219,7 +252,7 @@ class NetRunAheadMetricsCommandMsg : public NetCommandMsg }; //----------------------------------------------------------------------------- -class NetRunAheadCommandMsg : public NetCommandMsg +class NetRunAheadCommandMsg : public NetCommandMsgT { MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(NetRunAheadCommandMsg, "NetRunAheadCommandMsg") public: @@ -232,7 +265,7 @@ class NetRunAheadCommandMsg : public NetCommandMsg UnsignedByte getFrameRate() const; void setFrameRate(UnsignedByte frameRate); - virtual size_t getSizeForNetPacket() const; + virtual Select getSmallNetPacketSelect() const; protected: UnsignedShort m_runAhead; @@ -240,7 +273,7 @@ class NetRunAheadCommandMsg : public NetCommandMsg }; //----------------------------------------------------------------------------- -class NetDestroyPlayerCommandMsg : public NetCommandMsg +class NetDestroyPlayerCommandMsg : public NetCommandMsgT { MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(NetDestroyPlayerCommandMsg, "NetDestroyPlayerCommandMsg") public: @@ -250,36 +283,36 @@ class NetDestroyPlayerCommandMsg : public NetCommandMsg UnsignedInt getPlayerIndex() const; void setPlayerIndex(UnsignedInt playerIndex); - virtual size_t getSizeForNetPacket() const; + virtual Select getSmallNetPacketSelect() const; protected: UnsignedInt m_playerIndex; }; //----------------------------------------------------------------------------- -class NetKeepAliveCommandMsg : public NetCommandMsg +class NetKeepAliveCommandMsg : public NetCommandMsgT { MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(NetKeepAliveCommandMsg, "NetKeepAliveCommandMsg") public: NetKeepAliveCommandMsg(); //virtual ~NetKeepAliveCommandMsg(); - virtual size_t getSizeForNetPacket() const; + virtual Select getSmallNetPacketSelect() const; }; //----------------------------------------------------------------------------- -class NetDisconnectKeepAliveCommandMsg : public NetCommandMsg +class NetDisconnectKeepAliveCommandMsg : public NetCommandMsgT { MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(NetDisconnectKeepAliveCommandMsg, "NetDisconnectKeepAliveCommandMsg") public: NetDisconnectKeepAliveCommandMsg(); //virtual ~NetDisconnectKeepAliveCommandMsg(); - virtual size_t getSizeForNetPacket() const; + virtual Select getSmallNetPacketSelect() const; }; //----------------------------------------------------------------------------- -class NetDisconnectPlayerCommandMsg : public NetCommandMsg +class NetDisconnectPlayerCommandMsg : public NetCommandMsgT { MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(NetDisconnectPlayerCommandMsg, "NetDisconnectPlayerCommandMsg") public: @@ -292,7 +325,7 @@ class NetDisconnectPlayerCommandMsg : public NetCommandMsg UnsignedInt getDisconnectFrame() const; void setDisconnectFrame(UnsignedInt frame); - virtual size_t getSizeForNetPacket() const; + virtual Select getSmallNetPacketSelect() const; protected: UnsignedByte m_disconnectSlot; @@ -300,29 +333,29 @@ class NetDisconnectPlayerCommandMsg : public NetCommandMsg }; //----------------------------------------------------------------------------- -class NetPacketRouterQueryCommandMsg : public NetCommandMsg +class NetPacketRouterQueryCommandMsg : public NetCommandMsgT { MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(NetPacketRouterQueryCommandMsg, "NetPacketRouterQueryCommandMsg") public: NetPacketRouterQueryCommandMsg(); //virtual ~NetPacketRouterQueryCommandMsg(); - virtual size_t getSizeForNetPacket() const; + virtual Select getSmallNetPacketSelect() const; }; //----------------------------------------------------------------------------- -class NetPacketRouterAckCommandMsg : public NetCommandMsg +class NetPacketRouterAckCommandMsg : public NetCommandMsgT { MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(NetPacketRouterAckCommandMsg, "NetPacketRouterAckCommandMsg") public: NetPacketRouterAckCommandMsg(); //virtual ~NetPacketRouterAckCommandMsg(); - virtual size_t getSizeForNetPacket() const; + virtual Select getSmallNetPacketSelect() const; }; //----------------------------------------------------------------------------- -class NetDisconnectChatCommandMsg : public NetCommandMsg +class NetDisconnectChatCommandMsg : public NetCommandMsgT { MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(NetDisconnectChatCommandMsg, "NetDisconnectChatCommandMsg") public: @@ -332,14 +365,14 @@ class NetDisconnectChatCommandMsg : public NetCommandMsg UnicodeString getText() const; void setText(UnicodeString text); - virtual size_t getSizeForNetPacket() const; + virtual Select getSmallNetPacketSelect() const; protected: UnicodeString m_text; }; //----------------------------------------------------------------------------- -class NetChatCommandMsg : public NetCommandMsg +class NetChatCommandMsg : public NetCommandMsgT { MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(NetChatCommandMsg, "NetChatCommandMsg") public: @@ -352,7 +385,7 @@ class NetChatCommandMsg : public NetCommandMsg Int getPlayerMask() const; void setPlayerMask( Int playerMask ); - virtual size_t getSizeForNetPacket() const; + virtual Select getSmallNetPacketSelect() const; protected: UnicodeString m_text; @@ -360,7 +393,7 @@ class NetChatCommandMsg : public NetCommandMsg }; //----------------------------------------------------------------------------- -class NetDisconnectVoteCommandMsg : public NetCommandMsg +class NetDisconnectVoteCommandMsg : public NetCommandMsgT { MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(NetDisconnectVoteCommandMsg, "NetDisconnectVoteCommandMsg") public: @@ -373,7 +406,7 @@ class NetDisconnectVoteCommandMsg : public NetCommandMsg UnsignedInt getVoteFrame() const; void setVoteFrame(UnsignedInt voteFrame); - virtual size_t getSizeForNetPacket() const; + virtual Select getSmallNetPacketSelect() const; protected: UnsignedByte m_slot; @@ -381,7 +414,7 @@ class NetDisconnectVoteCommandMsg : public NetCommandMsg }; //----------------------------------------------------------------------------- -class NetProgressCommandMsg: public NetCommandMsg +class NetProgressCommandMsg: public NetCommandMsgT { MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(NetProgressCommandMsg, "NetProgressCommandMsg") public: @@ -391,14 +424,14 @@ class NetProgressCommandMsg: public NetCommandMsg UnsignedByte getPercentage() const; void setPercentage( UnsignedByte percent ); - virtual size_t getSizeForNetPacket() const; + virtual Select getSmallNetPacketSelect() const; protected: UnsignedByte m_percent; }; //----------------------------------------------------------------------------- -class NetWrapperCommandMsg : public NetCommandMsg +class NetWrapperCommandMsg : public NetCommandMsgT { MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(NetWrapperCommandMsg, "NetWrapperCommandMsg") public: @@ -409,8 +442,6 @@ class NetWrapperCommandMsg : public NetCommandMsg UnsignedByte * getData(); void setData(UnsignedByte *data, UnsignedInt dataLength); - virtual size_t getSizeForNetPacket() const; - UnsignedInt getChunkNumber() const; void setChunkNumber(UnsignedInt chunkNumber); @@ -428,6 +459,8 @@ class NetWrapperCommandMsg : public NetCommandMsg UnsignedShort getWrappedCommandID() const; void setWrappedCommandID(UnsignedShort wrappedCommandID); + virtual Select getSmallNetPacketSelect() const; + private: UnsignedByte *m_data; // using UnsignedInt's so we can send around files of effectively unlimited size easily @@ -440,7 +473,7 @@ class NetWrapperCommandMsg : public NetCommandMsg }; //----------------------------------------------------------------------------- -class NetFileCommandMsg : public NetCommandMsg +class NetFileCommandMsg : public NetCommandMsgT { MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(NetFileCommandMsg, "NetFileCommandMsg") public: @@ -459,7 +492,7 @@ class NetFileCommandMsg : public NetCommandMsg UnsignedByte * getFileData(); void setFileData(UnsignedByte *data, UnsignedInt dataLength); - virtual size_t getSizeForNetPacket() const; + virtual Select getSmallNetPacketSelect() const; protected: AsciiString m_portableFilename; @@ -469,7 +502,7 @@ class NetFileCommandMsg : public NetCommandMsg }; //----------------------------------------------------------------------------- -class NetFileAnnounceCommandMsg : public NetCommandMsg +class NetFileAnnounceCommandMsg : public NetCommandMsgT { MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(NetFileAnnounceCommandMsg, "NetFileAnnounceCommandMsg") public: @@ -488,7 +521,7 @@ class NetFileAnnounceCommandMsg : public NetCommandMsg UnsignedByte getPlayerMask() const; void setPlayerMask(UnsignedByte playerMask); - virtual size_t getSizeForNetPacket() const; + virtual Select getSmallNetPacketSelect() const; protected: AsciiString m_portableFilename; @@ -497,7 +530,7 @@ class NetFileAnnounceCommandMsg : public NetCommandMsg }; //----------------------------------------------------------------------------- -class NetFileProgressCommandMsg : public NetCommandMsg +class NetFileProgressCommandMsg : public NetCommandMsgT { MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(NetFileProgressCommandMsg, "NetFileProgressCommandMsg") public: @@ -510,7 +543,7 @@ class NetFileProgressCommandMsg : public NetCommandMsg Int getProgress() const; void setProgress(Int val); - virtual size_t getSizeForNetPacket() const; + virtual Select getSmallNetPacketSelect() const; protected: UnsignedShort m_fileID; @@ -518,7 +551,7 @@ class NetFileProgressCommandMsg : public NetCommandMsg }; //----------------------------------------------------------------------------- -class NetDisconnectFrameCommandMsg : public NetCommandMsg +class NetDisconnectFrameCommandMsg : public NetCommandMsgT { MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(NetDisconnectFrameCommandMsg, "NetDisconnectFrameCommandMsg") public: @@ -527,14 +560,14 @@ class NetDisconnectFrameCommandMsg : public NetCommandMsg UnsignedInt getDisconnectFrame() const; void setDisconnectFrame(UnsignedInt disconnectFrame); - virtual size_t getSizeForNetPacket() const; + virtual Select getSmallNetPacketSelect() const; protected: UnsignedInt m_disconnectFrame; }; //----------------------------------------------------------------------------- -class NetDisconnectScreenOffCommandMsg : public NetCommandMsg +class NetDisconnectScreenOffCommandMsg : public NetCommandMsgT { MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(NetDisconnectScreenOffCommandMsg, "NetDisconnectScreenOffCommandMsg") public: @@ -543,14 +576,14 @@ class NetDisconnectScreenOffCommandMsg : public NetCommandMsg UnsignedInt getNewFrame() const; void setNewFrame(UnsignedInt newFrame); - virtual size_t getSizeForNetPacket() const; + virtual Select getSmallNetPacketSelect() const; protected: UnsignedInt m_newFrame; }; //----------------------------------------------------------------------------- -class NetFrameResendRequestCommandMsg : public NetCommandMsg +class NetFrameResendRequestCommandMsg : public NetCommandMsgT { MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(NetFrameResendRequestCommandMsg, "NetFrameResendRequestCommandMsg") public: @@ -559,28 +592,28 @@ class NetFrameResendRequestCommandMsg : public NetCommandMsg UnsignedInt getFrameToResend() const; void setFrameToResend(UnsignedInt frame); - virtual size_t getSizeForNetPacket() const; + virtual Select getSmallNetPacketSelect() const; protected: UnsignedInt m_frameToResend; }; -class NetLoadCompleteCommandMsg : public NetCommandMsg +class NetLoadCompleteCommandMsg : public NetCommandMsgT { MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(NetLoadCompleteCommandMsg, "NetLoadCompleteCommandMsg") public: NetLoadCompleteCommandMsg(); //virtual ~NetLoadCompleteCommandMsg(); - virtual size_t getSizeForNetPacket() const; + virtual Select getSmallNetPacketSelect() const; }; -class NetTimeOutGameStartCommandMsg : public NetCommandMsg +class NetTimeOutGameStartCommandMsg : public NetCommandMsgT { MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(NetTimeOutGameStartCommandMsg, "NetTimeOutGameStartCommandMsg") public: NetTimeOutGameStartCommandMsg(); //virtual ~NetTimeOutGameStartCommandMsg(); - virtual size_t getSizeForNetPacket() const; + virtual Select getSmallNetPacketSelect() const; }; diff --git a/Core/GameEngine/Include/GameNetwork/NetPacket.h b/Core/GameEngine/Include/GameNetwork/NetPacket.h index d4980a9eef0..bdba5780ed5 100644 --- a/Core/GameEngine/Include/GameNetwork/NetPacket.h +++ b/Core/GameEngine/Include/GameNetwork/NetPacket.h @@ -69,118 +69,11 @@ class NetPacket : public MemoryPoolObject UnsignedInt getAddr(); UnsignedShort getPort(); + // This function returns the size of the command without any compression, repetition, etc. + // i.e. All of the required fields are taken into account when returning the size. static UnsignedInt GetBufferSizeNeededForCommand(NetCommandMsg *msg); - static void FillBufferWithCommand(UnsignedByte *buffer, NetCommandRef *msg); protected: - - // These functions return the size of the command without any compression, repetition, etc. - // i.e. All of the required fields are taken into account when returning the size. - static UnsignedInt GetGameCommandSize(NetCommandMsg *msg); - static UnsignedInt GetAckCommandSize(NetCommandMsg *msg); - static UnsignedInt GetFrameCommandSize(NetCommandMsg *msg); - static UnsignedInt GetPlayerLeaveCommandSize(NetCommandMsg *msg); - static UnsignedInt GetRunAheadMetricsCommandSize(NetCommandMsg *msg); - static UnsignedInt GetRunAheadCommandSize(NetCommandMsg *msg); - static UnsignedInt GetDestroyPlayerCommandSize(NetCommandMsg *msg); - static UnsignedInt GetKeepAliveCommandSize(NetCommandMsg *msg); - static UnsignedInt GetDisconnectKeepAliveCommandSize(NetCommandMsg *msg); - static UnsignedInt GetDisconnectPlayerCommandSize(NetCommandMsg *msg); - static UnsignedInt GetPacketRouterQueryCommandSize(NetCommandMsg *msg); - static UnsignedInt GetPacketRouterAckCommandSize(NetCommandMsg *msg); - static UnsignedInt GetDisconnectChatCommandSize(NetCommandMsg *msg); - static UnsignedInt GetDisconnectVoteCommandSize(NetCommandMsg *msg); - static UnsignedInt GetChatCommandSize(NetCommandMsg *msg); - static UnsignedInt GetProgressMessageSize(NetCommandMsg *msg); - static UnsignedInt GetLoadCompleteMessageSize(NetCommandMsg *msg); - static UnsignedInt GetTimeOutGameStartMessageSize(NetCommandMsg *msg); - static UnsignedInt GetWrapperCommandSize(NetCommandMsg *msg); - static UnsignedInt GetFileCommandSize(NetCommandMsg *msg); - static UnsignedInt GetFileAnnounceCommandSize(NetCommandMsg *msg); - static UnsignedInt GetFileProgressCommandSize(NetCommandMsg *msg); - static UnsignedInt GetDisconnectFrameCommandSize(NetCommandMsg *msg); - static UnsignedInt GetDisconnectScreenOffCommandSize(NetCommandMsg *msg); - static UnsignedInt GetFrameResendRequestCommandSize(NetCommandMsg *msg); - - static void FillBufferWithGameCommand(UnsignedByte *buffer, NetCommandRef *msg); - static void FillBufferWithAckCommand(UnsignedByte *buffer, NetCommandRef *msg); - static void FillBufferWithFrameCommand(UnsignedByte *buffer, NetCommandRef *msg); - static void FillBufferWithPlayerLeaveCommand(UnsignedByte *buffer, NetCommandRef *msg); - static void FillBufferWithRunAheadMetricsCommand(UnsignedByte *buffer, NetCommandRef *msg); - static void FillBufferWithRunAheadCommand(UnsignedByte *buffer, NetCommandRef *msg); - static void FillBufferWithDestroyPlayerCommand(UnsignedByte *buffer, NetCommandRef *msg); - static void FillBufferWithKeepAliveCommand(UnsignedByte *buffer, NetCommandRef *msg); - static void FillBufferWithDisconnectKeepAliveCommand(UnsignedByte *buffer, NetCommandRef *msg); - static void FillBufferWithDisconnectPlayerCommand(UnsignedByte *buffer, NetCommandRef *msg); - static void FillBufferWithPacketRouterQueryCommand(UnsignedByte *buffer, NetCommandRef *msg); - static void FillBufferWithPacketRouterAckCommand(UnsignedByte *buffer, NetCommandRef *msg); - static void FillBufferWithDisconnectChatCommand(UnsignedByte *buffer, NetCommandRef *msg); - static void FillBufferWithDisconnectVoteCommand(UnsignedByte *buffer, NetCommandRef *msg); - static void FillBufferWithChatCommand(UnsignedByte *buffer, NetCommandRef *msg); - static void FillBufferWithProgressMessage(UnsignedByte *buffer, NetCommandRef *msg); - static void FillBufferWithLoadCompleteMessage(UnsignedByte *buffer, NetCommandRef *msg); - static void FillBufferWithTimeOutGameStartMessage(UnsignedByte *buffer, NetCommandRef *msg); - static void FillBufferWithFileMessage(UnsignedByte *buffer, NetCommandRef *msg); - static void FillBufferWithFileProgressMessage(UnsignedByte *buffer, NetCommandRef *msg); - static void FillBufferWithFileAnnounceMessage(UnsignedByte *buffer, NetCommandRef *msg); - static void FillBufferWithDisconnectFrameMessage(UnsignedByte *buffer, NetCommandRef *msg); - static void FillBufferWithDisconnectScreenOffMessage(UnsignedByte *buffer, NetCommandRef *msg); - static void FillBufferWithFrameResendRequestMessage(UnsignedByte *buffer, NetCommandRef *msg); - - Bool addFrameCommand(NetCommandRef *msg); - Bool isRoomForFrameMessage(NetCommandRef *msg); - Bool addAckCommand(NetCommandRef *msg, UnsignedShort commandID, UnsignedByte originalPlayerID); - Bool addAckStage1Command(NetCommandRef *msg); - Bool addAckStage2Command(NetCommandRef *msg); - Bool addAckBothCommand(NetCommandRef *msg); - Bool isRoomForAckMessage(NetCommandRef *msg); - Bool addGameCommand(NetCommandRef *msg); - Bool isRoomForGameMessage(NetCommandRef *msg, GameMessage *gmsg); - Bool addPlayerLeaveCommand(NetCommandRef *msg); - Bool isRoomForPlayerLeaveMessage(NetCommandRef *msg); - Bool addRunAheadMetricsCommand(NetCommandRef *msg); - Bool isRoomForRunAheadMetricsMessage(NetCommandRef *msg); - Bool addRunAheadCommand(NetCommandRef *msg); - Bool isRoomForRunAheadMessage(NetCommandRef *msg); - Bool addDestroyPlayerCommand(NetCommandRef *msg); - Bool isRoomForDestroyPlayerMessage(NetCommandRef *msg); - Bool addKeepAliveCommand(NetCommandRef *msg); - Bool isRoomForKeepAliveMessage(NetCommandRef *msg); - Bool addDisconnectKeepAliveCommand(NetCommandRef *msg); - Bool isRoomForDisconnectKeepAliveMessage(NetCommandRef *msg); - Bool addDisconnectPlayerCommand(NetCommandRef *msg); - Bool isRoomForDisconnectPlayerMessage(NetCommandRef *msg); - Bool addPacketRouterQueryCommand(NetCommandRef *msg); - Bool isRoomForPacketRouterQueryMessage(NetCommandRef *msg); - Bool addPacketRouterAckCommand(NetCommandRef *msg); - Bool isRoomForPacketRouterAckMessage(NetCommandRef *msg); - Bool addDisconnectChatCommand(NetCommandRef *msg); - Bool isRoomForDisconnectChatMessage(NetCommandRef *msg); - Bool addChatCommand(NetCommandRef *msg); - Bool isRoomForChatMessage(NetCommandRef *msg); - Bool addDisconnectVoteCommand(NetCommandRef *msg); - Bool isRoomForDisconnectVoteMessage(NetCommandRef *msg); - Bool addProgressMessage( NetCommandRef *msg ); - Bool isRoomForProgressMessage( NetCommandRef *msg ); - Bool addLoadCompleteMessage( NetCommandRef *msg ); - Bool isRoomForLoadCompleteMessage( NetCommandRef *msg ); - Bool addTimeOutGameStartMessage( NetCommandRef *msg ); - Bool isRoomForTimeOutGameStartMessage( NetCommandRef *msg ); - Bool addWrapperCommand(NetCommandRef *msg); - Bool isRoomForWrapperMessage(NetCommandRef *msg); - Bool addFileCommand(NetCommandRef *msg); - Bool isRoomForFileMessage(NetCommandRef *msg); - Bool addFileAnnounceCommand(NetCommandRef *msg); - Bool isRoomForFileAnnounceMessage(NetCommandRef *msg); - Bool addFileProgressCommand(NetCommandRef *msg); - Bool isRoomForFileProgressMessage(NetCommandRef *msg); - Bool addDisconnectFrameCommand(NetCommandRef *msg); - Bool isRoomForDisconnectFrameMessage(NetCommandRef *msg); - Bool addDisconnectScreenOffCommand(NetCommandRef *msg); - Bool isRoomForDisconnectScreenOffMessage(NetCommandRef *msg); - Bool addFrameResendRequestCommand(NetCommandRef *msg); - Bool isRoomForFrameResendRequestMessage(NetCommandRef *msg); - Bool isAckRepeat(NetCommandRef *msg); Bool isAckBothRepeat(NetCommandRef *msg); Bool isAckStage1Repeat(NetCommandRef *msg); diff --git a/Core/GameEngine/Include/GameNetwork/NetPacketStructs.h b/Core/GameEngine/Include/GameNetwork/NetPacketStructs.h index 7f3dd418db2..2913d07456b 100644 --- a/Core/GameEngine/Include/GameNetwork/NetPacketStructs.h +++ b/Core/GameEngine/Include/GameNetwork/NetPacketStructs.h @@ -1,6 +1,6 @@ /* ** Command & Conquer Generals Zero Hour(tm) -** Copyright 2025 TheSuperHackers +** Copyright 2026 TheSuperHackers ** ** This program is free software: you can redistribute it and/or modify ** it under the terms of the GNU General Public License as published by @@ -16,13 +16,88 @@ ** along with this program. If not, see . */ -// TheSuperHackers @refactor BobTista 07/10/2025 -// Packed struct definitions for network packet serialization. - #pragma once #include "GameNetwork/NetworkDefs.h" +class AsciiString; +class UnicodeString; +class GameMessage; + +class NetCommandRef; +class NetCommandMsg; +class NetGameCommandMsg; +class NetAckCommandMsg; +class NetAckBothCommandMsg; +class NetAckStage1CommandMsg; +class NetAckStage2CommandMsg; +class NetFrameCommandMsg; +class NetPlayerLeaveCommandMsg; +class NetRunAheadMetricsCommandMsg; +class NetRunAheadCommandMsg; +class NetDestroyPlayerCommandMsg; +class NetKeepAliveCommandMsg; +class NetDisconnectKeepAliveCommandMsg; +class NetDisconnectPlayerCommandMsg; +class NetPacketRouterQueryCommandMsg; +class NetPacketRouterAckCommandMsg; +class NetDisconnectChatCommandMsg; +class NetChatCommandMsg; +class NetDisconnectVoteCommandMsg; +class NetProgressCommandMsg; +class NetWrapperCommandMsg; +class NetFileCommandMsg; +class NetFileAnnounceCommandMsg; +class NetFileProgressCommandMsg; +class NetDisconnectFrameCommandMsg; +class NetDisconnectScreenOffCommandMsg; +class NetFrameResendRequestCommandMsg; +class NetLoadCompleteCommandMsg; +class NetTimeOutGameStartCommandMsg; + +//////////////////////////////////////////////////////////////////////////////// +// Helper functions for raw byte data handling +//////////////////////////////////////////////////////////////////////////////// + +namespace network +{ + +template +size_t writePrimitive(UnsignedByte *dest, T value) +{ + memcpy(dest, &value, sizeof(value)); + return sizeof(value); +} + +template +size_t writeObject(UnsignedByte *dest, const T& value) +{ + memcpy(dest, &value, sizeof(value)); + return sizeof(value); +} + +inline size_t writeBytes(UnsignedByte *dest, const UnsignedByte* src, size_t len) +{ + memcpy(dest, src, len); + return len; +} + +inline size_t writeStringWithoutNull(UnsignedByte *dest, const UnicodeString& value, size_t maxLen) +{ + const size_t copyLen = std::min(value.getLength(), maxLen); + const size_t copyBytes = copyLen * sizeof(WideChar); + memcpy(dest, value.str(), copyBytes); + return copyBytes; +} + +inline size_t writeStringWithNull(UnsignedByte *dest, const AsciiString& value) +{ + memcpy(dest, value.str(), value.getByteCount() + 1); + return static_cast(value.getByteCount() + 1); +} + +} // namespace network + // Ensure structs are packed to 1-byte alignment for network protocol compatibility #pragma pack(push, 1) @@ -32,293 +107,1020 @@ typedef UnsignedByte NetPacketFieldType; -namespace NetPacketFieldTypes { +namespace NetPacketFieldTypes +{ constexpr const NetPacketFieldType CommandType = 'T'; constexpr const NetPacketFieldType Relay = 'R'; constexpr const NetPacketFieldType Frame = 'F'; constexpr const NetPacketFieldType PlayerId = 'P'; constexpr const NetPacketFieldType CommandId = 'C'; constexpr const NetPacketFieldType Data = 'D'; + constexpr const NetPacketFieldType Repeat = 'Z'; } //////////////////////////////////////////////////////////////////////////////// // Common packet field structures //////////////////////////////////////////////////////////////////////////////// -struct NetPacketCommandTypeField { +struct NetPacketCommandTypeField +{ NetPacketCommandTypeField() : fieldType(NetPacketFieldTypes::CommandType) {} char fieldType; UnsignedByte commandType; }; -struct NetPacketRelayField { +struct NetPacketRelayField +{ NetPacketRelayField() : fieldType(NetPacketFieldTypes::Relay) {} char fieldType; UnsignedByte relay; }; -struct NetPacketFrameField { +struct NetPacketFrameField +{ NetPacketFrameField() : fieldType(NetPacketFieldTypes::Frame) {} char fieldType; UnsignedInt frame; }; -struct NetPacketPlayerIdField { +struct NetPacketPlayerIdField +{ NetPacketPlayerIdField() : fieldType(NetPacketFieldTypes::PlayerId) {} char fieldType; UnsignedByte playerId; }; -struct NetPacketCommandIdField { +struct NetPacketCommandIdField +{ NetPacketCommandIdField() : fieldType(NetPacketFieldTypes::CommandId) {} char fieldType; UnsignedShort commandId; }; -struct NetPacketDataField { +struct NetPacketDataField +{ NetPacketDataField() : fieldType(NetPacketFieldTypes::Data) {} char fieldType; }; +struct NetPacketRepeatField +{ + NetPacketRepeatField() : fieldType(NetPacketFieldTypes::Repeat) {} + char fieldType; +}; + //////////////////////////////////////////////////////////////////////////////// // Packed Network structures //////////////////////////////////////////////////////////////////////////////// -struct NetPacketAckCommand { - NetPacketCommandTypeField commandType; - NetPacketPlayerIdField playerId; - NetPacketDataField dataHeader; - UnsignedShort commandId; // Command ID being acknowledged - UnsignedByte originalPlayerId; // Original player who sent the command -}; - -struct NetPacketFrameCommand { - NetPacketCommandTypeField commandType; - NetPacketFrameField frame; - NetPacketRelayField relay; - NetPacketPlayerIdField playerId; - NetPacketCommandIdField commandId; - NetPacketDataField dataHeader; - UnsignedShort commandCount; -}; - -struct NetPacketPlayerLeaveCommand { - NetPacketCommandTypeField commandType; - NetPacketRelayField relay; - NetPacketFrameField frame; - NetPacketPlayerIdField playerId; - NetPacketCommandIdField commandId; - NetPacketDataField dataHeader; - UnsignedByte leavingPlayerId; -}; - -struct NetPacketRunAheadMetricsCommand { - NetPacketCommandTypeField commandType; - NetPacketRelayField relay; - NetPacketPlayerIdField playerId; - NetPacketCommandIdField commandId; - NetPacketDataField dataHeader; - Real averageLatency; - UnsignedShort averageFps; -}; - -struct NetPacketRunAheadCommand { - NetPacketCommandTypeField commandType; - NetPacketRelayField relay; - NetPacketFrameField frame; - NetPacketPlayerIdField playerId; - NetPacketCommandIdField commandId; - NetPacketDataField dataHeader; - UnsignedShort runAhead; - UnsignedByte frameRate; -}; - -struct NetPacketDestroyPlayerCommand { - NetPacketCommandTypeField commandType; - NetPacketRelayField relay; - NetPacketFrameField frame; - NetPacketPlayerIdField playerId; - NetPacketCommandIdField commandId; - NetPacketDataField dataHeader; - UnsignedInt playerIndex; -}; - -struct NetPacketKeepAliveCommand { - NetPacketCommandTypeField commandType; - NetPacketRelayField relay; - NetPacketPlayerIdField playerId; - NetPacketDataField dataHeader; -}; - -struct NetPacketDisconnectKeepAliveCommand { - NetPacketCommandTypeField commandType; - NetPacketRelayField relay; - NetPacketPlayerIdField playerId; - NetPacketDataField dataHeader; -}; - -struct NetPacketDisconnectPlayerCommand { - NetPacketCommandTypeField commandType; - NetPacketRelayField relay; - NetPacketPlayerIdField playerId; - NetPacketCommandIdField commandId; - NetPacketDataField dataHeader; - UnsignedByte slot; - UnsignedInt disconnectFrame; -}; - -struct NetPacketRouterQueryCommand { - NetPacketCommandTypeField commandType; - NetPacketRelayField relay; - NetPacketPlayerIdField playerId; - NetPacketDataField dataHeader; -}; - -struct NetPacketRouterAckCommand { - NetPacketCommandTypeField commandType; - NetPacketRelayField relay; - NetPacketPlayerIdField playerId; - NetPacketDataField dataHeader; -}; - -struct NetPacketDisconnectVoteCommand { - NetPacketCommandTypeField commandType; - NetPacketRelayField relay; - NetPacketPlayerIdField playerId; - NetPacketCommandIdField commandId; - NetPacketDataField dataHeader; - UnsignedByte slot; - UnsignedInt voteFrame; -}; - -struct NetPacketChatCommand { - NetPacketCommandTypeField commandType; - NetPacketFrameField frame; - NetPacketRelayField relay; - NetPacketPlayerIdField playerId; - NetPacketCommandIdField commandId; - NetPacketDataField dataHeader; - UnsignedByte textLength; - // Variable fields: WideChar text[textLength] + Int playerMask - - enum { MaxTextLen = 255 }; - static Int getUsableTextLength(const UnicodeString& text) { return min(text.getLength(), (Int)MaxTextLen); } -}; - -struct NetPacketDisconnectChatCommand { - NetPacketCommandTypeField commandType; - NetPacketRelayField relay; - NetPacketPlayerIdField playerId; - NetPacketDataField dataHeader; - UnsignedByte textLength; - // Variable fields: WideChar text[textLength] - - enum { MaxTextLen = 255 }; - static Int getUsableTextLength(const UnicodeString& text) { return min(text.getLength(), (Int)MaxTextLen); } -}; - -struct NetPacketGameCommand { - NetPacketCommandTypeField commandType; - NetPacketFrameField frame; - NetPacketRelayField relay; - NetPacketPlayerIdField playerId; - NetPacketCommandIdField commandId; - NetPacketDataField dataHeader; - // Variable fields: GameMessage type + argument types + argument data -}; - -struct NetPacketWrapperCommand { - NetPacketCommandTypeField commandType; - NetPacketRelayField relay; - NetPacketPlayerIdField playerId; - NetPacketCommandIdField commandId; - NetPacketDataField dataHeader; - UnsignedShort wrappedCommandId; - UnsignedInt chunkNumber; - UnsignedInt numChunks; - UnsignedInt totalDataLength; - UnsignedInt dataLength; - UnsignedInt dataOffset; -}; - -struct NetPacketFileCommand { - NetPacketCommandTypeField commandType; - NetPacketRelayField relay; - NetPacketPlayerIdField playerId; - NetPacketCommandIdField commandId; - NetPacketDataField dataHeader; - // Variable fields: null-terminated filename + UnsignedInt fileDataLength + file data -}; - -struct NetPacketFileAnnounceCommand { - NetPacketCommandTypeField commandType; - NetPacketRelayField relay; - NetPacketPlayerIdField playerId; - NetPacketCommandIdField commandId; - NetPacketDataField dataHeader; - // Variable fields: null-terminated filename + UnsignedShort fileID + UnsignedByte playerMask -}; - -struct NetPacketFileProgressCommand { - NetPacketCommandTypeField commandType; - NetPacketRelayField relay; - NetPacketPlayerIdField playerId; - NetPacketCommandIdField commandId; - NetPacketDataField dataHeader; - UnsignedShort fileId; - Int progress; -}; - -struct NetPacketProgressCommand { - NetPacketCommandTypeField commandType; - NetPacketRelayField relay; - NetPacketPlayerIdField playerId; - NetPacketDataField dataHeader; - UnsignedByte percentage; -}; - -struct NetPacketLoadCompleteCommand { - NetPacketCommandTypeField commandType; - NetPacketRelayField relay; - NetPacketPlayerIdField playerId; - NetPacketCommandIdField commandId; - NetPacketDataField dataHeader; -}; - -struct NetPacketTimeOutGameStartCommand { - NetPacketCommandTypeField commandType; - NetPacketRelayField relay; - NetPacketPlayerIdField playerId; - NetPacketCommandIdField commandId; - NetPacketDataField dataHeader; -}; - -struct NetPacketDisconnectFrameCommand { - NetPacketCommandTypeField commandType; - NetPacketRelayField relay; - NetPacketPlayerIdField playerId; - NetPacketCommandIdField commandId; - NetPacketDataField dataHeader; - UnsignedInt disconnectFrame; -}; - -struct NetPacketDisconnectScreenOffCommand { - NetPacketCommandTypeField commandType; - NetPacketRelayField relay; - NetPacketPlayerIdField playerId; - NetPacketCommandIdField commandId; - NetPacketDataField dataHeader; - UnsignedInt newFrame; -}; - -struct NetPacketFrameResendRequestCommand { - NetPacketCommandTypeField commandType; - NetPacketRelayField relay; - NetPacketPlayerIdField playerId; - NetPacketCommandIdField commandId; - NetPacketDataField dataHeader; - UnsignedInt frameToResend; +//////////////////////////////////////////////////////////////////////////////// +// NetPacketRepeatCommand +//////////////////////////////////////////////////////////////////////////////// + +struct NetPacketRepeatCommand +{ + struct CommandBase + { + NetPacketRepeatField repeat; + }; + + static size_t getSize() + { + return sizeof(CommandBase); + } + static size_t copyBytes(UnsignedByte *buffer) + { + CommandBase base; + return network::writeObject(buffer, base); + } +}; + +//////////////////////////////////////////////////////////////////////////////// +// SmallNetPacketCommandBase +//////////////////////////////////////////////////////////////////////////////// + +struct SmallNetPacketCommandBaseSelect +{ + UnsignedByte useCommandType : 1; + UnsignedByte useRelay : 1; + UnsignedByte useFrame : 1; + UnsignedByte usePlayerId : 1; + UnsignedByte useCommandId : 1; +}; + +struct SmallNetPacketCommandBase +{ + struct CommandBase + { + NetPacketCommandTypeField commandType; + NetPacketRelayField relay; + NetPacketFrameField frame; + NetPacketPlayerIdField playerId; + NetPacketCommandIdField commandId; + NetPacketDataField dataHeader; + }; + + static size_t getSize(const SmallNetPacketCommandBaseSelect *select = nullptr); + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref, const SmallNetPacketCommandBaseSelect *select = nullptr); +}; + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketCommandTemplate +//////////////////////////////////////////////////////////////////////////////// + +template +struct NetPacketCommandTemplate +{ + typedef Base CommandBase; + typedef Data CommandData; + + static size_t getSize(const NetCommandMsg &msg) + { + size_t size = 0; + size += CommandBase::getSize(); + size += CommandData::getSize(msg); + return size; + } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) + { + size_t size = 0; + size += CommandBase::copyBytes(buffer + size, ref); + size += CommandData::copyBytes(buffer + size, ref); + return size; + } +}; + +//////////////////////////////////////////////////////////////////////////////// +// SmallNetPacketCommandTemplate +//////////////////////////////////////////////////////////////////////////////// + +template +struct SmallNetPacketCommandTemplate +{ + typedef Base CommandBase; + typedef Data CommandData; + + static size_t getSize(const NetCommandMsg &msg, const SmallNetPacketCommandBaseSelect *select = nullptr) + { + size_t size = 0; + size += CommandBase::getSize(select); + size += CommandData::getSize(msg); + return size; + } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref, const SmallNetPacketCommandBaseSelect *select = nullptr) + { + size_t size = 0; + size += CommandBase::copyBytes(buffer + size, ref, select); + size += CommandData::copyBytes(buffer + size, ref); + return size; + } +}; + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketNoData +//////////////////////////////////////////////////////////////////////////////// + +struct NetPacketNoData +{ + typedef void CommandMsg; + + static size_t getSize(const NetCommandMsg &) { return 0; } + static size_t copyBytes(UnsignedByte *, const NetCommandRef &) { return 0; } +}; + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketAckCommand +//////////////////////////////////////////////////////////////////////////////// + +struct NetPacketAckCommandData +{ + typedef NetAckCommandMsg CommandMsg; + + struct FixedData + { + UnsignedShort commandId; // Command ID being acknowledged + UnsignedByte originalPlayerId; // Original player who sent the command + }; + + static size_t getSize(const NetCommandMsg &msg) { return sizeof(FixedData); } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +struct NetPacketAckCommandBase +{ + typedef NetAckCommandMsg CommandMsg; + + struct CommandBase + { + NetPacketCommandTypeField commandType; + //NetPacketRelayField relay; + //NetPacketFrameField frame; + NetPacketPlayerIdField playerId; + //NetPacketCommandIdField commandId; + NetPacketDataField dataHeader; + }; + + static size_t getSize() { return sizeof(CommandBase); } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); }; +//////////////////////////////////////////////////////////////////////////////// +// NetPacketFrameCommand +//////////////////////////////////////////////////////////////////////////////// + +struct NetPacketFrameCommandData +{ + typedef NetFrameCommandMsg CommandMsg; + + struct FixedData + { + UnsignedShort commandCount; + }; + + static size_t getSize(const NetCommandMsg &msg) { return sizeof(FixedData); } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +struct NetPacketFrameCommandBase +{ + typedef NetFrameCommandMsg CommandMsg; + + struct CommandBase + { + NetPacketCommandTypeField commandType; + NetPacketFrameField frame; + NetPacketRelayField relay; + NetPacketPlayerIdField playerId; + NetPacketCommandIdField commandId; + NetPacketDataField dataHeader; + }; + + static size_t getSize() { return sizeof(CommandBase); } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketPlayerLeaveCommand +//////////////////////////////////////////////////////////////////////////////// + +struct NetPacketPlayerLeaveCommandData +{ + typedef NetPlayerLeaveCommandMsg CommandMsg; + + struct FixedData + { + UnsignedByte leavingPlayerId; + }; + + static size_t getSize(const NetCommandMsg &msg) { return sizeof(FixedData); } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +struct NetPacketPlayerLeaveCommandBase +{ + typedef NetPlayerLeaveCommandMsg CommandMsg; + + struct CommandBase + { + NetPacketCommandTypeField commandType; + NetPacketRelayField relay; + NetPacketFrameField frame; + NetPacketPlayerIdField playerId; + NetPacketCommandIdField commandId; + NetPacketDataField dataHeader; + }; + + static size_t getSize() { return sizeof(CommandBase); } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketRunAheadMetricsCommand +//////////////////////////////////////////////////////////////////////////////// + +struct NetPacketRunAheadMetricsCommandData +{ + typedef NetRunAheadMetricsCommandMsg CommandMsg; + + struct FixedData + { + Real averageLatency; + UnsignedShort averageFps; + }; + + static size_t getSize(const NetCommandMsg &msg) { return sizeof(FixedData); } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +struct NetPacketRunAheadMetricsCommandBase +{ + typedef NetRunAheadMetricsCommandMsg CommandMsg; + + struct CommandBase + { + NetPacketCommandTypeField commandType; + NetPacketRelayField relay; + //NetPacketFrameField frame; + NetPacketPlayerIdField playerId; + NetPacketCommandIdField commandId; + NetPacketDataField dataHeader; + }; + + static size_t getSize() { return sizeof(CommandBase); } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketRunAheadCommand +//////////////////////////////////////////////////////////////////////////////// + +struct NetPacketRunAheadCommandData +{ + typedef NetRunAheadCommandMsg CommandMsg; + + struct FixedData + { + UnsignedShort runAhead; + UnsignedByte frameRate; + }; + + static size_t getSize(const NetCommandMsg &msg) { return sizeof(FixedData); } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +struct NetPacketRunAheadCommandBase +{ + typedef NetRunAheadCommandMsg CommandMsg; + + struct CommandBase + { + NetPacketCommandTypeField commandType; + NetPacketRelayField relay; + NetPacketFrameField frame; + NetPacketPlayerIdField playerId; + NetPacketCommandIdField commandId; + NetPacketDataField dataHeader; + }; + + static size_t getSize() { return sizeof(CommandBase); } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketDestroyPlayerCommand +//////////////////////////////////////////////////////////////////////////////// + +struct NetPacketDestroyPlayerCommandData +{ + typedef NetDestroyPlayerCommandMsg CommandMsg; + + struct FixedData + { + UnsignedInt playerIndex; + }; + + static size_t getSize(const NetCommandMsg &msg) { return sizeof(FixedData); } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +struct NetPacketDestroyPlayerCommandBase +{ + typedef NetDestroyPlayerCommandMsg CommandMsg; + + struct CommandBase + { + NetPacketCommandTypeField commandType; + NetPacketRelayField relay; + NetPacketFrameField frame; + NetPacketPlayerIdField playerId; + NetPacketCommandIdField commandId; + NetPacketDataField dataHeader; + }; + + static size_t getSize() { return sizeof(CommandBase); } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketKeepAliveCommand +//////////////////////////////////////////////////////////////////////////////// + +struct NetPacketKeepAliveCommandBase +{ + typedef NetKeepAliveCommandMsg CommandMsg; + + struct CommandBase + { + NetPacketCommandTypeField commandType; + NetPacketRelayField relay; + //NetPacketFrameField frame; + NetPacketPlayerIdField playerId; + //NetPacketCommandIdField commandId; + NetPacketDataField dataHeader; + }; + + static size_t getSize() { return sizeof(CommandBase); } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketDisconnectKeepAliveCommand +//////////////////////////////////////////////////////////////////////////////// + +struct NetPacketDisconnectKeepAliveCommandBase +{ + typedef NetDisconnectKeepAliveCommandMsg CommandMsg; + + struct CommandBase + { + NetPacketCommandTypeField commandType; + NetPacketRelayField relay; + //NetPacketFrameField frame; + NetPacketPlayerIdField playerId; + NetPacketDataField dataHeader; + }; + + static size_t getSize() { return sizeof(CommandBase); } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketDisconnectPlayerCommand +//////////////////////////////////////////////////////////////////////////////// + +struct NetPacketDisconnectPlayerCommandData +{ + typedef NetDisconnectPlayerCommandMsg CommandMsg; + + struct FixedData + { + UnsignedByte disconnectSlot; + UnsignedInt disconnectFrame; + }; + + static size_t getSize(const NetCommandMsg &msg) { return sizeof(FixedData); } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +struct NetPacketDisconnectPlayerCommandBase +{ + typedef NetDisconnectPlayerCommandMsg CommandMsg; + + struct CommandBase + { + NetPacketCommandTypeField commandType; + NetPacketRelayField relay; + //NetPacketFrameField frame; + NetPacketPlayerIdField playerId; + NetPacketCommandIdField commandId; + NetPacketDataField dataHeader; + }; + + static size_t getSize() { return sizeof(CommandBase); } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketRouterQueryCommand +//////////////////////////////////////////////////////////////////////////////// + +struct NetPacketRouterQueryCommandBase +{ + typedef NetPacketRouterQueryCommandMsg CommandMsg; + + struct CommandBase + { + NetPacketCommandTypeField commandType; + NetPacketRelayField relay; + //NetPacketFrameField frame; + NetPacketPlayerIdField playerId; + //NetPacketCommandIdField commandId; + NetPacketDataField dataHeader; + }; + + static size_t getSize() { return sizeof(CommandBase); } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketRouterAckCommand +//////////////////////////////////////////////////////////////////////////////// + +struct NetPacketRouterAckCommandBase +{ + typedef NetPacketRouterAckCommandMsg CommandMsg; + + struct CommandBase + { + NetPacketCommandTypeField commandType; + NetPacketRelayField relay; + //NetPacketFrameField frame; + NetPacketPlayerIdField playerId; + NetPacketDataField dataHeader; + }; + + static size_t getSize() { return sizeof(CommandBase); } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketDisconnectVoteCommand +//////////////////////////////////////////////////////////////////////////////// + +struct NetPacketDisconnectVoteCommandData +{ + typedef NetDisconnectVoteCommandMsg CommandMsg; + + struct FixedData + { + UnsignedByte slot; + UnsignedInt voteFrame; + }; + + static size_t getSize(const NetCommandMsg &msg) { return sizeof(FixedData); } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +struct NetPacketDisconnectVoteCommandBase +{ + typedef NetDisconnectVoteCommandMsg CommandMsg; + + struct CommandBase + { + NetPacketCommandTypeField commandType; + NetPacketRelayField relay; + //NetPacketFrameField frame; + NetPacketPlayerIdField playerId; + NetPacketCommandIdField commandId; + NetPacketDataField dataHeader; + }; + + static size_t getSize() { return sizeof(CommandBase); } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketChatCommand +//////////////////////////////////////////////////////////////////////////////// + +struct NetPacketChatCommandData +{ + typedef NetChatCommandMsg CommandMsg; + + static size_t getSize(const NetCommandMsg &msg); + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +struct NetPacketChatCommandBase +{ + typedef NetChatCommandMsg CommandMsg; + + struct CommandBase + { + NetPacketCommandTypeField commandType; + NetPacketFrameField frame; + NetPacketRelayField relay; + NetPacketPlayerIdField playerId; + NetPacketCommandIdField commandId; + NetPacketDataField dataHeader; + }; + + static size_t getSize() { return sizeof(CommandBase); } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketDisconnectChatCommand +//////////////////////////////////////////////////////////////////////////////// + +struct NetPacketDisconnectChatCommandData +{ + typedef NetDisconnectChatCommandMsg CommandMsg; + + static size_t getSize(const NetCommandMsg &msg); + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +struct NetPacketDisconnectChatCommandBase +{ + typedef NetDisconnectChatCommandMsg CommandMsg; + + struct CommandBase + { + NetPacketCommandTypeField commandType; + NetPacketRelayField relay; + //NetPacketFrameField frame; + NetPacketPlayerIdField playerId; + //NetPacketCommandIdField commandId; + NetPacketDataField dataHeader; + }; + + static size_t getSize() { return sizeof(CommandBase); } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketGameCommand +//////////////////////////////////////////////////////////////////////////////// + +struct NetPacketGameCommandData +{ + typedef NetGameCommandMsg CommandMsg; + + static size_t getSize(const NetCommandMsg &msg); + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +struct NetPacketGameCommandBase +{ + typedef NetGameCommandMsg CommandMsg; + + struct CommandBase + { + NetPacketCommandTypeField commandType; + NetPacketFrameField frame; + NetPacketRelayField relay; + NetPacketPlayerIdField playerId; + NetPacketCommandIdField commandId; + NetPacketDataField dataHeader; + }; + + static size_t getSize() { return sizeof(CommandBase); } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketWrapperCommand +//////////////////////////////////////////////////////////////////////////////// + +struct NetPacketWrapperCommandData +{ + typedef NetWrapperCommandMsg CommandMsg; + + struct FixedData + { + UnsignedShort wrappedCommandId; + UnsignedInt chunkNumber; + UnsignedInt numChunks; + UnsignedInt totalDataLength; + UnsignedInt dataLength; + UnsignedInt dataOffset; + }; + + static size_t getSize(const NetCommandMsg &msg); + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +struct NetPacketWrapperCommandBase +{ + typedef NetWrapperCommandMsg CommandMsg; + + struct CommandBase + { + NetPacketCommandTypeField commandType; + NetPacketRelayField relay; + NetPacketPlayerIdField playerId; + NetPacketCommandIdField commandId; + NetPacketDataField dataHeader; + }; + + static size_t getSize() { return sizeof(CommandBase); } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketFileCommand +//////////////////////////////////////////////////////////////////////////////// + +struct NetPacketFileCommandData +{ + typedef NetFileCommandMsg CommandMsg; + + static size_t getSize(const NetCommandMsg &msg); + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +struct NetPacketFileCommandBase +{ + typedef NetFileCommandMsg CommandMsg; + + struct CommandBase + { + NetPacketCommandTypeField commandType; + NetPacketRelayField relay; + //NetPacketFrameField frame; + NetPacketPlayerIdField playerId; + NetPacketCommandIdField commandId; + NetPacketDataField dataHeader; + }; + + static size_t getSize() { return sizeof(CommandBase); } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketFileAnnounceCommand +//////////////////////////////////////////////////////////////////////////////// + +struct NetPacketFileAnnounceCommandData +{ + typedef NetFileAnnounceCommandMsg CommandMsg; + + static size_t getSize(const NetCommandMsg &msg); + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +struct NetPacketFileAnnounceCommandBase +{ + typedef NetFileAnnounceCommandMsg CommandMsg; + + struct CommandBase + { + NetPacketCommandTypeField commandType; + NetPacketRelayField relay; + //NetPacketFrameField frame; + NetPacketPlayerIdField playerId; + NetPacketCommandIdField commandId; + NetPacketDataField dataHeader; + }; + + static size_t getSize() { return sizeof(CommandBase); } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketFileProgressCommand +//////////////////////////////////////////////////////////////////////////////// + +struct NetPacketFileProgressCommandData +{ + typedef NetFileProgressCommandMsg CommandMsg; + + struct FixedData + { + UnsignedShort fileId; + Int progress; + }; + + static size_t getSize(const NetCommandMsg &msg) { return sizeof(FixedData); } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +struct NetPacketFileProgressCommandBase +{ + typedef NetFileProgressCommandMsg CommandMsg; + + struct CommandBase + { + NetPacketCommandTypeField commandType; + NetPacketRelayField relay; + //NetPacketFrameField frame; + NetPacketPlayerIdField playerId; + NetPacketCommandIdField commandId; + NetPacketDataField dataHeader; + }; + + static size_t getSize() { return sizeof(CommandBase); } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketProgressCommand +//////////////////////////////////////////////////////////////////////////////// + +struct NetPacketProgressCommandData +{ + typedef NetProgressCommandMsg CommandMsg; + + struct FixedData + { + UnsignedByte percentage; + }; + + static size_t getSize(const NetCommandMsg &msg) { return sizeof(FixedData); } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +struct NetPacketProgressCommandBase +{ + typedef NetProgressCommandMsg CommandMsg; + + struct CommandBase + { + NetPacketCommandTypeField commandType; + NetPacketRelayField relay; + //NetPacketFrameField frame; + NetPacketPlayerIdField playerId; + //NetPacketCommandIdField commandId; + NetPacketDataField dataHeader; + }; + + static size_t getSize() { return sizeof(CommandBase); } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketLoadCompleteCommand +//////////////////////////////////////////////////////////////////////////////// + +struct NetPacketLoadCompleteCommandBase +{ + typedef NetLoadCompleteCommandMsg CommandMsg; + + struct CommandBase + { + NetPacketCommandTypeField commandType; + NetPacketRelayField relay; + //NetPacketFrameField frame; + NetPacketPlayerIdField playerId; + NetPacketCommandIdField commandId; + NetPacketDataField dataHeader; + }; + + static size_t getSize() { return sizeof(CommandBase); } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketTimeOutGameStartCommand +//////////////////////////////////////////////////////////////////////////////// + +struct NetPacketTimeOutGameStartCommandBase +{ + typedef NetTimeOutGameStartCommandMsg CommandMsg; + + struct CommandBase + { + NetPacketCommandTypeField commandType; + NetPacketRelayField relay; + //NetPacketFrameField frame; + NetPacketPlayerIdField playerId; + NetPacketCommandIdField commandId; + NetPacketDataField dataHeader; + }; + + static size_t getSize() { return sizeof(CommandBase); } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketDisconnectFrameCommand +//////////////////////////////////////////////////////////////////////////////// + +struct NetPacketDisconnectFrameCommandData +{ + typedef NetDisconnectFrameCommandMsg CommandMsg; + + struct FixedData + { + UnsignedInt disconnectFrame; + }; + + static size_t getSize(const NetCommandMsg &msg) { return sizeof(FixedData); } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +struct NetPacketDisconnectFrameCommandBase +{ + typedef NetDisconnectFrameCommandMsg CommandMsg; + + struct CommandBase + { + NetPacketCommandTypeField commandType; + NetPacketRelayField relay; + //NetPacketFrameField frame; + NetPacketPlayerIdField playerId; + NetPacketCommandIdField commandId; + NetPacketDataField dataHeader; + }; + + static size_t getSize() { return sizeof(CommandBase); } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketDisconnectScreenOffCommand +//////////////////////////////////////////////////////////////////////////////// + +struct NetPacketDisconnectScreenOffCommandData +{ + typedef NetDisconnectScreenOffCommandMsg CommandMsg; + + struct FixedData + { + UnsignedInt newFrame; + }; + + static size_t getSize(const NetCommandMsg &msg) { return sizeof(FixedData); } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +struct NetPacketDisconnectScreenOffCommandBase +{ + typedef NetDisconnectScreenOffCommandMsg CommandMsg; + + struct CommandBase + { + NetPacketCommandTypeField commandType; + NetPacketRelayField relay; + //NetPacketFrameField frame; + NetPacketPlayerIdField playerId; + NetPacketCommandIdField commandId; + NetPacketDataField dataHeader; + }; + + static size_t getSize() { return sizeof(CommandBase); } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketFrameResendRequestCommand +//////////////////////////////////////////////////////////////////////////////// + +struct NetPacketFrameResendRequestCommandData +{ + typedef NetFrameResendRequestCommandMsg CommandMsg; + + struct FixedData + { + UnsignedInt frameToResend; + }; + + static size_t getSize(const NetCommandMsg &msg) { return sizeof(FixedData); } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +struct NetPacketFrameResendRequestCommandBase +{ + typedef NetFrameResendRequestCommandMsg CommandMsg; + + struct CommandBase + { + NetPacketCommandTypeField commandType; + NetPacketRelayField relay; + NetPacketPlayerIdField playerId; + //NetPacketFrameField frame; + NetPacketCommandIdField commandId; + NetPacketDataField dataHeader; + }; + + static size_t getSize() { return sizeof(CommandBase); } + static size_t copyBytes(UnsignedByte *buffer, const NetCommandRef &ref); +}; + +//////////////////////////////////////////////////////////////////////////////// + +struct NetPacketAckCommand : public NetPacketCommandTemplate {}; +struct NetPacketFrameCommand : public NetPacketCommandTemplate {}; +struct NetPacketPlayerLeaveCommand : public NetPacketCommandTemplate {}; +struct NetPacketRunAheadMetricsCommand : public NetPacketCommandTemplate {}; +struct NetPacketRunAheadCommand : public NetPacketCommandTemplate {}; +struct NetPacketDestroyPlayerCommand : public NetPacketCommandTemplate {}; +struct NetPacketKeepAliveCommand : public NetPacketCommandTemplate {}; +struct NetPacketDisconnectKeepAliveCommand : public NetPacketCommandTemplate {}; +struct NetPacketDisconnectPlayerCommand : public NetPacketCommandTemplate {}; +struct NetPacketRouterQueryCommand : public NetPacketCommandTemplate {}; +struct NetPacketRouterAckCommand : public NetPacketCommandTemplate {}; +struct NetPacketDisconnectVoteCommand : public NetPacketCommandTemplate {}; +struct NetPacketChatCommand : public NetPacketCommandTemplate {}; +struct NetPacketDisconnectChatCommand : public NetPacketCommandTemplate {}; +struct NetPacketGameCommand : public NetPacketCommandTemplate {}; +struct NetPacketWrapperCommand : public NetPacketCommandTemplate {}; +struct NetPacketFileCommand : public NetPacketCommandTemplate {}; +struct NetPacketFileAnnounceCommand : public NetPacketCommandTemplate {}; +struct NetPacketFileProgressCommand : public NetPacketCommandTemplate {}; +struct NetPacketProgressCommand : public NetPacketCommandTemplate {}; +struct NetPacketLoadCompleteCommand : public NetPacketCommandTemplate {}; +struct NetPacketTimeOutGameStartCommand : public NetPacketCommandTemplate {}; +struct NetPacketDisconnectFrameCommand : public NetPacketCommandTemplate {}; +struct NetPacketDisconnectScreenOffCommand : public NetPacketCommandTemplate {}; +struct NetPacketFrameResendRequestCommand : public NetPacketCommandTemplate {}; + +struct SmallNetPacketAckCommand : public SmallNetPacketCommandTemplate {}; +struct SmallNetPacketFrameCommand : public SmallNetPacketCommandTemplate {}; +struct SmallNetPacketPlayerLeaveCommand : public SmallNetPacketCommandTemplate {}; +struct SmallNetPacketRunAheadMetricsCommand : public SmallNetPacketCommandTemplate {}; +struct SmallNetPacketRunAheadCommand : public SmallNetPacketCommandTemplate {}; +struct SmallNetPacketDestroyPlayerCommand : public SmallNetPacketCommandTemplate {}; +struct SmallNetPacketKeepAliveCommand : public SmallNetPacketCommandTemplate {}; +struct SmallNetPacketDisconnectKeepAliveCommand : public SmallNetPacketCommandTemplate {}; +struct SmallNetPacketDisconnectPlayerCommand : public SmallNetPacketCommandTemplate {}; +struct SmallNetPacketRouterQueryCommand : public SmallNetPacketCommandTemplate {}; +struct SmallNetPacketRouterAckCommand : public SmallNetPacketCommandTemplate {}; +struct SmallNetPacketDisconnectVoteCommand : public SmallNetPacketCommandTemplate {}; +struct SmallNetPacketChatCommand : public SmallNetPacketCommandTemplate {}; +struct SmallNetPacketDisconnectChatCommand : public SmallNetPacketCommandTemplate {}; +struct SmallNetPacketGameCommand : public SmallNetPacketCommandTemplate {}; +struct SmallNetPacketWrapperCommand : public SmallNetPacketCommandTemplate {}; +struct SmallNetPacketFileCommand : public SmallNetPacketCommandTemplate {}; +struct SmallNetPacketFileAnnounceCommand : public SmallNetPacketCommandTemplate {}; +struct SmallNetPacketFileProgressCommand : public SmallNetPacketCommandTemplate {}; +struct SmallNetPacketProgressCommand : public SmallNetPacketCommandTemplate {}; +struct SmallNetPacketLoadCompleteCommand : public SmallNetPacketCommandTemplate {}; +struct SmallNetPacketTimeOutGameStartCommand : public SmallNetPacketCommandTemplate {}; +struct SmallNetPacketDisconnectFrameCommand : public SmallNetPacketCommandTemplate {}; +struct SmallNetPacketDisconnectScreenOffCommand : public SmallNetPacketCommandTemplate {}; +struct SmallNetPacketFrameResendRequestCommand : public SmallNetPacketCommandTemplate {}; + // Restore normal struct packing #pragma pack(pop) diff --git a/Core/GameEngine/Source/GameNetwork/NetCommandMsg.cpp b/Core/GameEngine/Source/GameNetwork/NetCommandMsg.cpp index 34c20a2f59c..dd652a6cba3 100644 --- a/Core/GameEngine/Source/GameNetwork/NetCommandMsg.cpp +++ b/Core/GameEngine/Source/GameNetwork/NetCommandMsg.cpp @@ -87,7 +87,7 @@ Int NetCommandMsg::getSortNumber() const { /** * Constructor with no argument, sets everything to default values. */ -NetGameCommandMsg::NetGameCommandMsg() : NetCommandMsg() { +NetGameCommandMsg::NetGameCommandMsg() { m_argSize = 0; m_numArgs = 0; m_type = (GameMessage::Type)0; @@ -100,7 +100,7 @@ NetGameCommandMsg::NetGameCommandMsg() : NetCommandMsg() { * Constructor with a GameMessage argument. Sets member variables appropriately for this GameMessage. * Also copies all the arguments. */ -NetGameCommandMsg::NetGameCommandMsg(GameMessage *msg) : NetCommandMsg() { +NetGameCommandMsg::NetGameCommandMsg(GameMessage *msg) { m_commandType = NETCOMMANDTYPE_GAMECOMMAND; m_type = msg->getType(); @@ -224,70 +224,14 @@ void NetGameCommandMsg::setGameMessageType(GameMessage::Type type) { m_type = type; } -size_t NetGameCommandMsg::getSizeForNetPacket() const { - UnsignedShort msglen = sizeof(NetPacketGameCommand); - - // Variable data portion - GameMessage *gmsg = constructGameMessage(); - GameMessageParser *parser = newInstance(GameMessageParser)(gmsg); - - msglen += sizeof(GameMessage::Type); - msglen += sizeof(UnsignedByte); - - GameMessageParserArgumentType *arg = parser->getFirstArgumentType(); - while (arg != nullptr) { - msglen += sizeof(UnsignedByte); // argument type - msglen += sizeof(UnsignedByte); // argument count - GameMessageArgumentDataType type = arg->getType(); - - switch (type) { - - case ARGUMENTDATATYPE_INTEGER: - msglen += arg->getArgCount() * sizeof(Int); - break; - case ARGUMENTDATATYPE_REAL: - msglen += arg->getArgCount() * sizeof(Real); - break; - case ARGUMENTDATATYPE_BOOLEAN: - msglen += arg->getArgCount() * sizeof(Bool); - break; - case ARGUMENTDATATYPE_OBJECTID: - msglen += arg->getArgCount() * sizeof(ObjectID); - break; - case ARGUMENTDATATYPE_DRAWABLEID: - msglen += arg->getArgCount() * sizeof(DrawableID); - break; - case ARGUMENTDATATYPE_TEAMID: - msglen += arg->getArgCount() * sizeof(UnsignedInt); - break; - case ARGUMENTDATATYPE_LOCATION: - msglen += arg->getArgCount() * sizeof(Coord3D); - break; - case ARGUMENTDATATYPE_PIXEL: - msglen += arg->getArgCount() * sizeof(ICoord2D); - break; - case ARGUMENTDATATYPE_PIXELREGION: - msglen += arg->getArgCount() * sizeof(IRegion2D); - break; - case ARGUMENTDATATYPE_TIMESTAMP: - msglen += arg->getArgCount() * sizeof(UnsignedInt); - break; - case ARGUMENTDATATYPE_WIDECHAR: - msglen += arg->getArgCount() * sizeof(WideChar); - break; - - } - - arg = arg->getNext(); - } - - deleteInstance(parser); - parser = nullptr; - - deleteInstance(gmsg); - gmsg = nullptr; - - return msglen; +NetCommandMsg::Select NetGameCommandMsg::getSmallNetPacketSelect() const { + Select select; + select.useCommandType = 1; + select.useRelay = 1; + select.useFrame = 1; + select.usePlayerId = 1; + select.useCommandId = 1; + return select; } //------------------------- @@ -326,8 +270,14 @@ Int NetAckCommandMsg::getSortNumber() const { return m_commandID; } -size_t NetAckCommandMsg::getSizeForNetPacket() const { - return sizeof(NetPacketAckCommand); +NetCommandMsg::Select NetAckCommandMsg::getSmallNetPacketSelect() const { + Select select; + select.useCommandType = 1; + select.useRelay = 0; + select.useFrame = 0; + select.usePlayerId = 1; + select.useCommandId = 0; + return select; } //------------------------- @@ -405,7 +355,7 @@ NetAckStage2CommandMsg::~NetAckStage2CommandMsg() { /** * Constructor. */ -NetFrameCommandMsg::NetFrameCommandMsg() : NetCommandMsg() { +NetFrameCommandMsg::NetFrameCommandMsg() { m_commandCount = 0; m_commandType = NETCOMMANDTYPE_FRAMEINFO; } @@ -430,8 +380,14 @@ UnsignedShort NetFrameCommandMsg::getCommandCount() const { return m_commandCount; } -size_t NetFrameCommandMsg::getSizeForNetPacket() const { - return sizeof(NetPacketFrameCommand); +NetCommandMsg::Select NetFrameCommandMsg::getSmallNetPacketSelect() const { + Select select; + select.useCommandType = 1; + select.useRelay = 1; + select.useFrame = 1; + select.usePlayerId = 1; + select.useCommandId = 1; + return select; } //------------------------- @@ -440,7 +396,7 @@ size_t NetFrameCommandMsg::getSizeForNetPacket() const { /** * Constructor */ -NetPlayerLeaveCommandMsg::NetPlayerLeaveCommandMsg() : NetCommandMsg() { +NetPlayerLeaveCommandMsg::NetPlayerLeaveCommandMsg() { m_leavingPlayerID = 0; m_commandType = NETCOMMANDTYPE_PLAYERLEAVE; } @@ -465,8 +421,14 @@ void NetPlayerLeaveCommandMsg::setLeavingPlayerID(UnsignedByte id) { m_leavingPlayerID = id; } -size_t NetPlayerLeaveCommandMsg::getSizeForNetPacket() const { - return sizeof(NetPacketPlayerLeaveCommand); +NetCommandMsg::Select NetPlayerLeaveCommandMsg::getSmallNetPacketSelect() const { + Select select; + select.useCommandType = 1; + select.useRelay = 1; + select.useFrame = 1; + select.usePlayerId = 1; + select.useCommandId = 1; + return select; } //------------------------- @@ -475,7 +437,7 @@ size_t NetPlayerLeaveCommandMsg::getSizeForNetPacket() const { /** * Constructor */ -NetRunAheadMetricsCommandMsg::NetRunAheadMetricsCommandMsg() : NetCommandMsg() { +NetRunAheadMetricsCommandMsg::NetRunAheadMetricsCommandMsg() { m_averageLatency = 0.0; m_averageFps = 0; m_commandType = NETCOMMANDTYPE_RUNAHEADMETRICS; @@ -515,14 +477,20 @@ Int NetRunAheadMetricsCommandMsg::getAverageFps() const { return m_averageFps; } -size_t NetRunAheadMetricsCommandMsg::getSizeForNetPacket() const { - return sizeof(NetPacketRunAheadMetricsCommand); +NetCommandMsg::Select NetRunAheadMetricsCommandMsg::getSmallNetPacketSelect() const { + Select select; + select.useCommandType = 1; + select.useRelay = 1; + select.useFrame = 0; + select.usePlayerId = 1; + select.useCommandId = 1; + return select; } //------------------------- // NetRunAheadCommandMsg //------------------------- -NetRunAheadCommandMsg::NetRunAheadCommandMsg() : NetCommandMsg() { +NetRunAheadCommandMsg::NetRunAheadCommandMsg() { m_runAhead = min(max(20, MIN_RUNAHEAD), MAX_FRAMES_AHEAD/2); m_frameRate = 30; m_commandType = NETCOMMANDTYPE_RUNAHEAD; @@ -547,8 +515,14 @@ void NetRunAheadCommandMsg::setFrameRate(UnsignedByte frameRate) { m_frameRate = frameRate; } -size_t NetRunAheadCommandMsg::getSizeForNetPacket() const { - return sizeof(NetPacketRunAheadCommand); +NetCommandMsg::Select NetRunAheadCommandMsg::getSmallNetPacketSelect() const { + Select select; + select.useCommandType = 1; + select.useRelay = 1; + select.useFrame = 1; + select.usePlayerId = 1; + select.useCommandId = 1; + return select; } //------------------------- @@ -557,7 +531,7 @@ size_t NetRunAheadCommandMsg::getSizeForNetPacket() const { /** * Constructor */ -NetDestroyPlayerCommandMsg::NetDestroyPlayerCommandMsg() : NetCommandMsg() +NetDestroyPlayerCommandMsg::NetDestroyPlayerCommandMsg() { m_playerIndex = 0; m_commandType = NETCOMMANDTYPE_DESTROYPLAYER; @@ -586,8 +560,14 @@ UnsignedInt NetDestroyPlayerCommandMsg::getPlayerIndex() const return m_playerIndex; } -size_t NetDestroyPlayerCommandMsg::getSizeForNetPacket() const { - return sizeof(NetPacketDestroyPlayerCommand); +NetCommandMsg::Select NetDestroyPlayerCommandMsg::getSmallNetPacketSelect() const { + Select select; + select.useCommandType = 1; + select.useRelay = 1; + select.useFrame = 1; + select.usePlayerId = 1; + select.useCommandId = 1; + return select; } //------------------------- @@ -596,15 +576,21 @@ size_t NetDestroyPlayerCommandMsg::getSizeForNetPacket() const { /** * Constructor */ -NetKeepAliveCommandMsg::NetKeepAliveCommandMsg() : NetCommandMsg() { +NetKeepAliveCommandMsg::NetKeepAliveCommandMsg() { m_commandType = NETCOMMANDTYPE_KEEPALIVE; } NetKeepAliveCommandMsg::~NetKeepAliveCommandMsg() { } -size_t NetKeepAliveCommandMsg::getSizeForNetPacket() const { - return sizeof(NetPacketKeepAliveCommand); +NetCommandMsg::Select NetKeepAliveCommandMsg::getSmallNetPacketSelect() const { + Select select; + select.useCommandType = 1; + select.useRelay = 1; + select.useFrame = 0; + select.usePlayerId = 1; + select.useCommandId = 0; + return select; } //------------------------- @@ -613,15 +599,21 @@ size_t NetKeepAliveCommandMsg::getSizeForNetPacket() const { /** * Constructor */ -NetDisconnectKeepAliveCommandMsg::NetDisconnectKeepAliveCommandMsg() : NetCommandMsg() { +NetDisconnectKeepAliveCommandMsg::NetDisconnectKeepAliveCommandMsg() { m_commandType = NETCOMMANDTYPE_DISCONNECTKEEPALIVE; } NetDisconnectKeepAliveCommandMsg::~NetDisconnectKeepAliveCommandMsg() { } -size_t NetDisconnectKeepAliveCommandMsg::getSizeForNetPacket() const { - return sizeof(NetPacketDisconnectKeepAliveCommand); +NetCommandMsg::Select NetDisconnectKeepAliveCommandMsg::getSmallNetPacketSelect() const { + Select select; + select.useCommandType = 1; + select.useRelay = 1; + select.useFrame = 0; + select.usePlayerId = 1; + select.useCommandId = 0; + return select; } //------------------------- @@ -630,7 +622,7 @@ size_t NetDisconnectKeepAliveCommandMsg::getSizeForNetPacket() const { /** * Constructor */ -NetDisconnectPlayerCommandMsg::NetDisconnectPlayerCommandMsg() : NetCommandMsg() { +NetDisconnectPlayerCommandMsg::NetDisconnectPlayerCommandMsg() { m_commandType = NETCOMMANDTYPE_DISCONNECTPLAYER; m_disconnectSlot = 0; } @@ -669,8 +661,14 @@ UnsignedInt NetDisconnectPlayerCommandMsg::getDisconnectFrame() const { return m_disconnectFrame; } -size_t NetDisconnectPlayerCommandMsg::getSizeForNetPacket() const { - return sizeof(NetPacketDisconnectPlayerCommand); +NetCommandMsg::Select NetDisconnectPlayerCommandMsg::getSmallNetPacketSelect() const { + Select select; + select.useCommandType = 1; + select.useRelay = 1; + select.useFrame = 0; + select.usePlayerId = 1; + select.useCommandId = 1; + return select; } //------------------------- @@ -679,7 +677,7 @@ size_t NetDisconnectPlayerCommandMsg::getSizeForNetPacket() const { /** * Constructor */ -NetPacketRouterQueryCommandMsg::NetPacketRouterQueryCommandMsg() : NetCommandMsg() { +NetPacketRouterQueryCommandMsg::NetPacketRouterQueryCommandMsg() { m_commandType = NETCOMMANDTYPE_PACKETROUTERQUERY; } @@ -689,8 +687,14 @@ NetPacketRouterQueryCommandMsg::NetPacketRouterQueryCommandMsg() : NetCommandMsg NetPacketRouterQueryCommandMsg::~NetPacketRouterQueryCommandMsg() { } -size_t NetPacketRouterQueryCommandMsg::getSizeForNetPacket() const { - return sizeof(NetPacketRouterQueryCommand); +NetCommandMsg::Select NetPacketRouterQueryCommandMsg::getSmallNetPacketSelect() const { + Select select; + select.useCommandType = 1; + select.useRelay = 1; + select.useFrame = 0; + select.usePlayerId = 1; + select.useCommandId = 0; + return select; } //------------------------- @@ -699,7 +703,7 @@ size_t NetPacketRouterQueryCommandMsg::getSizeForNetPacket() const { /** * Constructor */ -NetPacketRouterAckCommandMsg::NetPacketRouterAckCommandMsg() : NetCommandMsg() { +NetPacketRouterAckCommandMsg::NetPacketRouterAckCommandMsg() { m_commandType = NETCOMMANDTYPE_PACKETROUTERACK; } @@ -709,8 +713,14 @@ NetPacketRouterAckCommandMsg::NetPacketRouterAckCommandMsg() : NetCommandMsg() { NetPacketRouterAckCommandMsg::~NetPacketRouterAckCommandMsg() { } -size_t NetPacketRouterAckCommandMsg::getSizeForNetPacket() const { - return sizeof(NetPacketRouterAckCommand); +NetCommandMsg::Select NetPacketRouterAckCommandMsg::getSmallNetPacketSelect() const { + Select select; + select.useCommandType = 1; + select.useRelay = 1; + select.useFrame = 0; + select.usePlayerId = 1; + select.useCommandId = 0; + return select; } //------------------------- @@ -719,7 +729,7 @@ size_t NetPacketRouterAckCommandMsg::getSizeForNetPacket() const { /** * Constructor */ -NetDisconnectChatCommandMsg::NetDisconnectChatCommandMsg() : NetCommandMsg() { +NetDisconnectChatCommandMsg::NetDisconnectChatCommandMsg() { m_commandType = NETCOMMANDTYPE_DISCONNECTCHAT; } @@ -743,13 +753,23 @@ UnicodeString NetDisconnectChatCommandMsg::getText() const { return m_text; } +NetCommandMsg::Select NetDisconnectChatCommandMsg::getSmallNetPacketSelect() const { + Select select; + select.useCommandType = 1; + select.useRelay = 1; + select.useFrame = 0; + select.usePlayerId = 1; + select.useCommandId = 0; + return select; +} + //------------------------- // NetChatCommandMsg //------------------------- /** * Constructor */ -NetChatCommandMsg::NetChatCommandMsg() : NetCommandMsg() +NetChatCommandMsg::NetChatCommandMsg() { m_commandType = NETCOMMANDTYPE_CHAT; m_playerMask = 0; @@ -794,20 +814,14 @@ void NetChatCommandMsg::setPlayerMask( Int playerMask ) m_playerMask = playerMask; } -size_t NetChatCommandMsg::getSizeForNetPacket() const -{ - return sizeof(NetPacketChatCommand) - + m_text.getByteCount() - + sizeof(m_playerMask); -} - -//------------------------- -// NetDisconnectChatCommandMsg -//------------------------- -size_t NetDisconnectChatCommandMsg::getSizeForNetPacket() const -{ - return sizeof(NetPacketDisconnectChatCommand) - + m_text.getByteCount(); +NetCommandMsg::Select NetChatCommandMsg::getSmallNetPacketSelect() const { + Select select; + select.useCommandType = 1; + select.useRelay = 1; + select.useFrame = 1; + select.usePlayerId = 1; + select.useCommandId = 1; + return select; } //------------------------- @@ -816,7 +830,7 @@ size_t NetDisconnectChatCommandMsg::getSizeForNetPacket() const /** * Constructor */ -NetDisconnectVoteCommandMsg::NetDisconnectVoteCommandMsg() : NetCommandMsg() { +NetDisconnectVoteCommandMsg::NetDisconnectVoteCommandMsg() { m_commandType = NETCOMMANDTYPE_DISCONNECTVOTE; m_slot = 0; } @@ -855,14 +869,20 @@ void NetDisconnectVoteCommandMsg::setVoteFrame(UnsignedInt voteFrame) { m_voteFrame = voteFrame; } -size_t NetDisconnectVoteCommandMsg::getSizeForNetPacket() const { - return sizeof(NetPacketDisconnectVoteCommand); +NetCommandMsg::Select NetDisconnectVoteCommandMsg::getSmallNetPacketSelect() const { + Select select; + select.useCommandType = 1; + select.useRelay = 1; + select.useFrame = 0; + select.usePlayerId = 1; + select.useCommandId = 1; + return select; } //------------------------- // NetProgressCommandMsg //------------------------- -NetProgressCommandMsg::NetProgressCommandMsg() : NetCommandMsg() +NetProgressCommandMsg::NetProgressCommandMsg() { m_commandType = NETCOMMANDTYPE_PROGRESS; m_percent = 0; @@ -880,14 +900,20 @@ void NetProgressCommandMsg::setPercentage( UnsignedByte percent ) m_percent = percent; } -size_t NetProgressCommandMsg::getSizeForNetPacket() const { - return sizeof(NetPacketProgressCommand); +NetCommandMsg::Select NetProgressCommandMsg::getSmallNetPacketSelect() const { + Select select; + select.useCommandType = 1; + select.useRelay = 1; + select.useFrame = 0; + select.usePlayerId = 1; + select.useCommandId = 0; + return select; } //------------------------- // NetWrapperCommandMsg //------------------------- -NetWrapperCommandMsg::NetWrapperCommandMsg() : NetCommandMsg() { +NetWrapperCommandMsg::NetWrapperCommandMsg() { m_commandType = NETCOMMANDTYPE_WRAPPER; m_numChunks = 0; m_data = nullptr; @@ -963,14 +989,20 @@ void NetWrapperCommandMsg::setWrappedCommandID(UnsignedShort wrappedCommandID) { m_wrappedCommandID = wrappedCommandID; } -size_t NetWrapperCommandMsg::getSizeForNetPacket() const { - return sizeof(NetPacketWrapperCommand) + m_dataLength; +NetCommandMsg::Select NetWrapperCommandMsg::getSmallNetPacketSelect() const { + Select select; + select.useCommandType = 1; + select.useRelay = 1; + select.useFrame = 0; + select.usePlayerId = 1; + select.useCommandId = 1; + return select; } //------------------------- // NetFileCommandMsg //------------------------- -NetFileCommandMsg::NetFileCommandMsg() : NetCommandMsg() { +NetFileCommandMsg::NetFileCommandMsg() { m_commandType = NETCOMMANDTYPE_FILE; m_data = nullptr; m_portableFilename.clear(); @@ -1011,18 +1043,20 @@ void NetFileCommandMsg::setFileData(UnsignedByte *data, UnsignedInt dataLength) memcpy(m_data, data, dataLength); } -size_t NetFileCommandMsg::getSizeForNetPacket() const -{ - return sizeof(NetPacketFileCommand) - + m_portableFilename.getLength() + 1 - + sizeof(m_dataLength) - + m_dataLength; +NetCommandMsg::Select NetFileCommandMsg::getSmallNetPacketSelect() const { + Select select; + select.useCommandType = 1; + select.useRelay = 1; + select.useFrame = 0; + select.usePlayerId = 1; + select.useCommandId = 1; + return select; } //------------------------- // NetFileAnnounceCommandMsg //------------------------- -NetFileAnnounceCommandMsg::NetFileAnnounceCommandMsg() : NetCommandMsg() { +NetFileAnnounceCommandMsg::NetFileAnnounceCommandMsg() { m_commandType = NETCOMMANDTYPE_FILEANNOUNCE; m_portableFilename.clear(); m_fileID = 0; @@ -1058,18 +1092,20 @@ void NetFileAnnounceCommandMsg::setPlayerMask(UnsignedByte playerMask) { m_playerMask = playerMask; } -size_t NetFileAnnounceCommandMsg::getSizeForNetPacket() const -{ - return sizeof(NetPacketFileAnnounceCommand) - + m_portableFilename.getLength() + 1 - + sizeof(m_fileID) - + sizeof(m_playerMask); +NetCommandMsg::Select NetFileAnnounceCommandMsg::getSmallNetPacketSelect() const { + Select select; + select.useCommandType = 1; + select.useRelay = 1; + select.useFrame = 0; + select.usePlayerId = 1; + select.useCommandId = 1; + return select; } //------------------------- // NetFileProgressCommandMsg //------------------------- -NetFileProgressCommandMsg::NetFileProgressCommandMsg() : NetCommandMsg() { +NetFileProgressCommandMsg::NetFileProgressCommandMsg() { m_commandType = NETCOMMANDTYPE_FILEPROGRESS; m_fileID = 0; m_progress = 0; @@ -1094,14 +1130,20 @@ void NetFileProgressCommandMsg::setProgress(Int val) { m_progress = val; } -size_t NetFileProgressCommandMsg::getSizeForNetPacket() const { - return sizeof(NetPacketFileProgressCommand); +NetCommandMsg::Select NetFileProgressCommandMsg::getSmallNetPacketSelect() const { + Select select; + select.useCommandType = 1; + select.useRelay = 1; + select.useFrame = 0; + select.usePlayerId = 1; + select.useCommandId = 1; + return select; } //------------------------- // NetDisconnectFrameCommandMsg //------------------------- -NetDisconnectFrameCommandMsg::NetDisconnectFrameCommandMsg() : NetCommandMsg() { +NetDisconnectFrameCommandMsg::NetDisconnectFrameCommandMsg() { m_commandType = NETCOMMANDTYPE_DISCONNECTFRAME; m_disconnectFrame = 0; } @@ -1117,14 +1159,20 @@ void NetDisconnectFrameCommandMsg::setDisconnectFrame(UnsignedInt disconnectFram m_disconnectFrame = disconnectFrame; } -size_t NetDisconnectFrameCommandMsg::getSizeForNetPacket() const { - return sizeof(NetPacketDisconnectFrameCommand); +NetCommandMsg::Select NetDisconnectFrameCommandMsg::getSmallNetPacketSelect() const { + Select select; + select.useCommandType = 1; + select.useRelay = 1; + select.useFrame = 1; + select.usePlayerId = 1; + select.useCommandId = 1; + return select; } //------------------------- // NetDisconnectScreenOffCommandMsg //------------------------- -NetDisconnectScreenOffCommandMsg::NetDisconnectScreenOffCommandMsg() : NetCommandMsg() { +NetDisconnectScreenOffCommandMsg::NetDisconnectScreenOffCommandMsg() { m_commandType = NETCOMMANDTYPE_DISCONNECTSCREENOFF; m_newFrame = 0; } @@ -1140,14 +1188,20 @@ void NetDisconnectScreenOffCommandMsg::setNewFrame(UnsignedInt newFrame) { m_newFrame = newFrame; } -size_t NetDisconnectScreenOffCommandMsg::getSizeForNetPacket() const { - return sizeof(NetPacketDisconnectScreenOffCommand); +NetCommandMsg::Select NetDisconnectScreenOffCommandMsg::getSmallNetPacketSelect() const { + Select select; + select.useCommandType = 1; + select.useRelay = 1; + select.useFrame = 1; + select.usePlayerId = 1; + select.useCommandId = 1; + return select; } //------------------------- // NetFrameResendRequestCommandMsg //------------------------- -NetFrameResendRequestCommandMsg::NetFrameResendRequestCommandMsg() : NetCommandMsg() { +NetFrameResendRequestCommandMsg::NetFrameResendRequestCommandMsg() { m_commandType = NETCOMMANDTYPE_FRAMERESENDREQUEST; m_frameToResend = 0; } @@ -1163,34 +1217,52 @@ void NetFrameResendRequestCommandMsg::setFrameToResend(UnsignedInt frame) { m_frameToResend = frame; } -size_t NetFrameResendRequestCommandMsg::getSizeForNetPacket() const { - return sizeof(NetPacketFrameResendRequestCommand); +NetCommandMsg::Select NetFrameResendRequestCommandMsg::getSmallNetPacketSelect() const { + Select select; + select.useCommandType = 1; + select.useRelay = 1; + select.useFrame = 1; + select.usePlayerId = 1; + select.useCommandId = 1; + return select; } //------------------------- // NetLoadCompleteCommandMsg //------------------------- -NetLoadCompleteCommandMsg::NetLoadCompleteCommandMsg() : NetCommandMsg() { +NetLoadCompleteCommandMsg::NetLoadCompleteCommandMsg() { m_commandType = NETCOMMANDTYPE_LOADCOMPLETE; } NetLoadCompleteCommandMsg::~NetLoadCompleteCommandMsg() { } -size_t NetLoadCompleteCommandMsg::getSizeForNetPacket() const { - return sizeof(NetPacketLoadCompleteCommand); +NetCommandMsg::Select NetLoadCompleteCommandMsg::getSmallNetPacketSelect() const { + Select select; + select.useCommandType = 1; + select.useRelay = 1; + select.useFrame = 0; + select.usePlayerId = 1; + select.useCommandId = 1; + return select; } //------------------------- // NetTimeOutGameStartCommandMsg //------------------------- -NetTimeOutGameStartCommandMsg::NetTimeOutGameStartCommandMsg() : NetCommandMsg() { +NetTimeOutGameStartCommandMsg::NetTimeOutGameStartCommandMsg() { m_commandType = NETCOMMANDTYPE_TIMEOUTSTART; } NetTimeOutGameStartCommandMsg::~NetTimeOutGameStartCommandMsg() { } -size_t NetTimeOutGameStartCommandMsg::getSizeForNetPacket() const { - return sizeof(NetPacketTimeOutGameStartCommand); +NetCommandMsg::Select NetTimeOutGameStartCommandMsg::getSmallNetPacketSelect() const { + Select select; + select.useCommandType = 1; + select.useRelay = 1; + select.useFrame = 0; + select.usePlayerId = 1; + select.useCommandId = 1; + return select; } diff --git a/Core/GameEngine/Source/GameNetwork/NetPacket.cpp b/Core/GameEngine/Source/GameNetwork/NetPacket.cpp index 608a3c2c092..2c59457c84c 100644 --- a/Core/GameEngine/Source/GameNetwork/NetPacket.cpp +++ b/Core/GameEngine/Source/GameNetwork/NetPacket.cpp @@ -33,12 +33,12 @@ #include "GameNetwork/GameMessageParser.h" #include "GameNetwork/NetPacketStructs.h" -// TheSuperHackers @refactor BobTista 31/12/2025 Use packed structs for FillBufferWithXXX serialization // This function assumes that all of the fields are either of default value or are // present in the raw data. NetCommandRef * NetPacket::ConstructNetCommandMsgFromRawData(UnsignedByte *data, UnsignedShort dataLength) { NetCommandType commandType = NETCOMMANDTYPE_GAMECOMMAND; + UnsignedByte commandTypeByte = static_cast(commandType); UnsignedShort commandID = 0; UnsignedInt frame = 0; UnsignedByte playerID = 0; @@ -54,32 +54,33 @@ NetCommandRef * NetPacket::ConstructNetCommandMsgFromRawData(UnsignedByte *data, case NetPacketFieldTypes::CommandType: ++offset; - memcpy(&commandType, data + offset, sizeof(UnsignedByte)); - offset += sizeof(UnsignedByte); + memcpy(&commandTypeByte, data + offset, sizeof(commandTypeByte)); + offset += sizeof(commandTypeByte); + commandType = static_cast(commandTypeByte); break; case NetPacketFieldTypes::Relay: ++offset; - memcpy(&relay, data + offset, sizeof(UnsignedByte)); - offset += sizeof(UnsignedByte); + memcpy(&relay, data + offset, sizeof(relay)); + offset += sizeof(relay); break; - case NetPacketFieldTypes::PlayerId: + case NetPacketFieldTypes::Frame: ++offset; - memcpy(&playerID, data + offset, sizeof(UnsignedByte)); - offset += sizeof(UnsignedByte); + memcpy(&frame, data + offset, sizeof(frame)); + offset += sizeof(frame); break; - case NetPacketFieldTypes::CommandId: + case NetPacketFieldTypes::PlayerId: ++offset; - memcpy(&commandID, data + offset, sizeof(UnsignedShort)); - offset += sizeof(UnsignedShort); + memcpy(&playerID, data + offset, sizeof(playerID)); + offset += sizeof(playerID); break; - case NetPacketFieldTypes::Frame: + case NetPacketFieldTypes::CommandId: ++offset; - memcpy(&frame, data + offset, sizeof(UnsignedInt)); - offset += sizeof(UnsignedInt); + memcpy(&commandID, data + offset, sizeof(commandID)); + offset += sizeof(commandID); break; case NetPacketFieldTypes::Data: @@ -211,7 +212,7 @@ NetPacketList NetPacket::ConstructBigCommandPacketList(NetCommandRef *ref) { // create the buffer for the huge message and fill the buffer with that message. UnsignedInt bigPacketCurrentOffset = 0; bigPacketData = NEW UnsignedByte[bufferSize]; - FillBufferWithCommand(bigPacketData, ref); + ref->getCommand()->copyBytesForNetPacket(bigPacketData, *ref); // create the wrapper command message we'll be using. NetWrapperCommandMsg *wrapperMsg = newInstance(NetWrapperCommandMsg); @@ -282,601 +283,6 @@ UnsignedInt NetPacket::GetBufferSizeNeededForCommand(NetCommandMsg *msg) { return msg->getSizeForNetPacket(); } - - -// this function assumes that buffer is already the correct size. -void NetPacket::FillBufferWithCommand(UnsignedByte *buffer, NetCommandRef *ref) { - NetCommandMsg *msg = ref->getCommand(); - - switch(msg->getNetCommandType()) - { - case NETCOMMANDTYPE_GAMECOMMAND: - FillBufferWithGameCommand(buffer, ref); - break; - case NETCOMMANDTYPE_ACKSTAGE1: - case NETCOMMANDTYPE_ACKSTAGE2: - case NETCOMMANDTYPE_ACKBOTH: - FillBufferWithAckCommand(buffer, ref); - break; - case NETCOMMANDTYPE_FRAMEINFO: - FillBufferWithFrameCommand(buffer, ref); - break; - case NETCOMMANDTYPE_PLAYERLEAVE: - FillBufferWithPlayerLeaveCommand(buffer, ref); - break; - case NETCOMMANDTYPE_RUNAHEADMETRICS: - FillBufferWithRunAheadMetricsCommand(buffer, ref); - break; - case NETCOMMANDTYPE_RUNAHEAD: - FillBufferWithRunAheadCommand(buffer, ref); - break; - case NETCOMMANDTYPE_DESTROYPLAYER: - FillBufferWithDestroyPlayerCommand(buffer, ref); - break; - case NETCOMMANDTYPE_KEEPALIVE: - FillBufferWithKeepAliveCommand(buffer, ref); - break; - case NETCOMMANDTYPE_DISCONNECTKEEPALIVE: - FillBufferWithDisconnectKeepAliveCommand(buffer, ref); - break; - case NETCOMMANDTYPE_DISCONNECTPLAYER: - FillBufferWithDisconnectPlayerCommand(buffer, ref); - break; - case NETCOMMANDTYPE_PACKETROUTERQUERY: - FillBufferWithPacketRouterQueryCommand(buffer, ref); - break; - case NETCOMMANDTYPE_PACKETROUTERACK: - FillBufferWithPacketRouterAckCommand(buffer, ref); - break; - case NETCOMMANDTYPE_DISCONNECTCHAT: - FillBufferWithDisconnectChatCommand(buffer, ref); - break; - case NETCOMMANDTYPE_DISCONNECTVOTE: - FillBufferWithDisconnectVoteCommand(buffer, ref); - break; - case NETCOMMANDTYPE_CHAT: - FillBufferWithChatCommand(buffer, ref); - break; - case NETCOMMANDTYPE_PROGRESS: - FillBufferWithProgressMessage(buffer, ref); - break; - case NETCOMMANDTYPE_LOADCOMPLETE: - FillBufferWithLoadCompleteMessage(buffer, ref); - break; - case NETCOMMANDTYPE_TIMEOUTSTART: - FillBufferWithTimeOutGameStartMessage(buffer, ref); - break; - case NETCOMMANDTYPE_FILE: - FillBufferWithFileMessage(buffer, ref); - break; - case NETCOMMANDTYPE_FILEANNOUNCE: - FillBufferWithFileAnnounceMessage(buffer, ref); - break; - case NETCOMMANDTYPE_FILEPROGRESS: - FillBufferWithFileProgressMessage(buffer, ref); - break; - case NETCOMMANDTYPE_DISCONNECTFRAME: - FillBufferWithDisconnectFrameMessage(buffer, ref); - break; - case NETCOMMANDTYPE_DISCONNECTSCREENOFF: - FillBufferWithDisconnectScreenOffMessage(buffer, ref); - break; - case NETCOMMANDTYPE_FRAMERESENDREQUEST: - FillBufferWithFrameResendRequestMessage(buffer, ref); - break; - default: - DEBUG_CRASH(("Unknown NETCOMMANDTYPE %d", msg->getNetCommandType())); - break; - } -} - -void NetPacket::FillBufferWithGameCommand(UnsignedByte *buffer, NetCommandRef *msg) { - NetGameCommandMsg *cmdMsg = static_cast(msg->getCommand()); - // get the game message from the NetCommandMsg - GameMessage *gmsg = cmdMsg->constructGameMessage(); - - //DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::FillBufferWithGameCommand for command ID %d", cmdMsg->getID())); - - NetPacketGameCommand packet; - packet.commandType.commandType = cmdMsg->getNetCommandType(); - packet.frame.frame = cmdMsg->getExecutionFrame(); - packet.relay.relay = msg->getRelay(); - packet.playerId.playerId = cmdMsg->getPlayerID(); - packet.commandId.commandId = cmdMsg->getID(); - - memcpy(buffer, &packet, sizeof(packet)); - - // Variable data portion - UnsignedShort offset = sizeof(NetPacketGameCommand); - - // Now copy the GameMessage type into the packet. - GameMessage::Type newType = gmsg->getType(); - memcpy(buffer + offset, &newType, sizeof(GameMessage::Type)); - offset += sizeof(GameMessage::Type); - - - GameMessageParser *parser = newInstance(GameMessageParser)(gmsg); - UnsignedByte numTypes = parser->getNumTypes(); - memcpy(buffer + offset, &numTypes, sizeof(numTypes)); - offset += sizeof(numTypes); - - GameMessageParserArgumentType *argType = parser->getFirstArgumentType(); - while (argType != nullptr) { - UnsignedByte type = (UnsignedByte)(argType->getType()); - memcpy(buffer + offset, &type, sizeof(type)); - offset += sizeof(type); - - UnsignedByte argTypeCount = argType->getArgCount(); - memcpy(buffer + offset, &argTypeCount, sizeof(argTypeCount)); - offset += sizeof(argTypeCount); - - argType = argType->getNext(); - } - - Int numArgs = gmsg->getArgumentCount(); - for (Int i = 0; i < numArgs; ++i) { - GameMessageArgumentDataType type = gmsg->getArgumentDataType(i); - GameMessageArgumentType arg = *(gmsg->getArgument(i)); - - switch (type) { - - case ARGUMENTDATATYPE_INTEGER: - memcpy(buffer + offset, &(arg.integer), sizeof(arg.integer)); - offset += sizeof(arg.integer); - break; - case ARGUMENTDATATYPE_REAL: - memcpy(buffer + offset, &(arg.real), sizeof(arg.real)); - offset += sizeof(arg.real); - break; - case ARGUMENTDATATYPE_BOOLEAN: - memcpy(buffer + offset, &(arg.boolean), sizeof(arg.boolean)); - offset += sizeof(arg.boolean); - break; - case ARGUMENTDATATYPE_OBJECTID: - memcpy(buffer + offset, &(arg.objectID), sizeof(arg.objectID)); - offset += sizeof(arg.objectID); - break; - case ARGUMENTDATATYPE_DRAWABLEID: - memcpy(buffer + offset, &(arg.drawableID), sizeof(arg.drawableID)); - offset += sizeof(arg.drawableID); - break; - case ARGUMENTDATATYPE_TEAMID: - memcpy(buffer + offset, &(arg.teamID), sizeof(arg.teamID)); - offset += sizeof(arg.teamID); - break; - case ARGUMENTDATATYPE_LOCATION: - memcpy(buffer + offset, &(arg.location), sizeof(arg.location)); - offset += sizeof(arg.location); - break; - case ARGUMENTDATATYPE_PIXEL: - memcpy(buffer + offset, &(arg.pixel), sizeof(arg.pixel)); - offset += sizeof(arg.pixel); - break; - case ARGUMENTDATATYPE_PIXELREGION: - memcpy(buffer + offset, &(arg.pixelRegion), sizeof(arg.pixelRegion)); - offset += sizeof(arg.pixelRegion); - break; - case ARGUMENTDATATYPE_TIMESTAMP: - memcpy(buffer + offset, &(arg.timestamp), sizeof(arg.timestamp)); - offset += sizeof(arg.timestamp); - break; - case ARGUMENTDATATYPE_WIDECHAR: - memcpy(buffer + offset, &(arg.wChar), sizeof(arg.wChar)); - offset += sizeof(arg.wChar); - break; - } - - } - - deleteInstance(parser); - parser = nullptr; - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addGameMessage - added game message, frame %d, player %d, command ID %d", m_lastFrame, m_lastPlayerID, m_lastCommandID)); - - deleteInstance(gmsg); - gmsg = nullptr; -} - -void NetPacket::FillBufferWithAckCommand(UnsignedByte *buffer, NetCommandRef *msg) { -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::FillBufferWithAckCommand - adding ack for command %d for player %d", cmdMsg->getCommandID(), msg->getCommand()->getPlayerID())); - - NetCommandMsg *cmdMsg = msg->getCommand(); - - UnsignedShort commandID = 0; - UnsignedByte originalPlayerID = 0; - - NetCommandType type = cmdMsg->getNetCommandType(); - - switch (type) { - - case NETCOMMANDTYPE_ACKBOTH: { - NetAckBothCommandMsg* ackbothmsg = (NetAckBothCommandMsg*)msg; - commandID = ackbothmsg->getCommandID(); - originalPlayerID = ackbothmsg->getOriginalPlayerID(); - break; - } - - case NETCOMMANDTYPE_ACKSTAGE1: { - NetAckStage1CommandMsg* ackstageonemsg = (NetAckStage1CommandMsg*)msg; - commandID = ackstageonemsg->getCommandID(); - originalPlayerID = ackstageonemsg->getOriginalPlayerID(); - break; - } - - case NETCOMMANDTYPE_ACKSTAGE2: { - NetAckStage2CommandMsg* ackstagetwomsg = (NetAckStage2CommandMsg*)msg; - commandID = ackstagetwomsg->getCommandID(); - originalPlayerID = ackstagetwomsg->getOriginalPlayerID(); - break; - } - - } - - NetPacketAckCommand packet; - packet.commandType.commandType = type; - packet.playerId.playerId = cmdMsg->getPlayerID(); - packet.commandId = commandID; - packet.originalPlayerId = originalPlayerID; - - memcpy(buffer, &packet, sizeof(packet)); - - // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("outgoing - added ACK, original player %d, command id %d", origPlayerID, cmdID)); -} - -void NetPacket::FillBufferWithFrameCommand(UnsignedByte *buffer, NetCommandRef *msg) { - NetFrameCommandMsg *cmdMsg = static_cast(msg->getCommand()); - // DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addFrameCommand - adding frame command for frame %d, command count = %d, command id = %d", cmdMsg->getExecutionFrame(), cmdMsg->getCommandCount(), cmdMsg->getID())); - - NetPacketFrameCommand packet; - packet.commandType.commandType = cmdMsg->getNetCommandType(); - packet.frame.frame = cmdMsg->getExecutionFrame(); - packet.relay.relay = msg->getRelay(); - packet.playerId.playerId = cmdMsg->getPlayerID(); - packet.commandId.commandId = cmdMsg->getID(); - packet.commandCount = cmdMsg->getCommandCount(); - - memcpy(buffer, &packet, sizeof(packet)); - - // frameinfodebug -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("outgoing - added frame %d, player %d, command count = %d, command id = %d", cmdMsg->getExecutionFrame(), cmdMsg->getPlayerID(), cmdMsg->getCommandCount(), cmdMsg->getID())); -} - -void NetPacket::FillBufferWithPlayerLeaveCommand(UnsignedByte *buffer, NetCommandRef *msg) { - NetPlayerLeaveCommandMsg *cmdMsg = static_cast(msg->getCommand()); -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addPlayerLeaveCommand - adding player leave command for player %d", cmdMsg->getLeavingPlayerID())); - - NetPacketPlayerLeaveCommand packet; - packet.commandType.commandType = cmdMsg->getNetCommandType(); - packet.relay.relay = msg->getRelay(); - packet.frame.frame = cmdMsg->getExecutionFrame(); - packet.playerId.playerId = cmdMsg->getPlayerID(); - packet.commandId.commandId = cmdMsg->getID(); - packet.leavingPlayerId = cmdMsg->getLeavingPlayerID(); - - memcpy(buffer, &packet, sizeof(packet)); -} - -void NetPacket::FillBufferWithRunAheadMetricsCommand(UnsignedByte *buffer, NetCommandRef *msg) { - NetRunAheadMetricsCommandMsg *cmdMsg = static_cast(msg->getCommand()); -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addRunAheadMetricsCommand - adding run ahead metrics for player %d, fps = %d, latency = %f", cmdMsg->getPlayerID(), cmdMsg->getAverageFps(), cmdMsg->getAverageLatency())); - - NetPacketRunAheadMetricsCommand packet; - packet.commandType.commandType = cmdMsg->getNetCommandType(); - packet.relay.relay = msg->getRelay(); - packet.playerId.playerId = cmdMsg->getPlayerID(); - packet.commandId.commandId = cmdMsg->getID(); - packet.averageLatency = cmdMsg->getAverageLatency(); - packet.averageFps = static_cast(cmdMsg->getAverageFps()); - - memcpy(buffer, &packet, sizeof(packet)); -} - -void NetPacket::FillBufferWithRunAheadCommand(UnsignedByte *buffer, NetCommandRef *msg) { - NetRunAheadCommandMsg *cmdMsg = static_cast(msg->getCommand()); - //DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::FillBufferWithRunAheadCommand - adding run ahead command")); - - NetPacketRunAheadCommand packet; - packet.commandType.commandType = cmdMsg->getNetCommandType(); - packet.relay.relay = msg->getRelay(); - packet.frame.frame = cmdMsg->getExecutionFrame(); - packet.playerId.playerId = cmdMsg->getPlayerID(); - packet.commandId.commandId = cmdMsg->getID(); - packet.runAhead = cmdMsg->getRunAhead(); - packet.frameRate = cmdMsg->getFrameRate(); - - memcpy(buffer, &packet, sizeof(packet)); - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket - added run ahead command, frame %d, player id %d command id %d", m_lastFrame, m_lastPlayerID, m_lastCommandID)); -} - -void NetPacket::FillBufferWithDestroyPlayerCommand(UnsignedByte *buffer, NetCommandRef *msg) { - NetDestroyPlayerCommandMsg *cmdMsg = static_cast(msg->getCommand()); -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addRunAheadCommand - adding run ahead command")); - - NetPacketDestroyPlayerCommand packet; - packet.commandType.commandType = cmdMsg->getNetCommandType(); - packet.relay.relay = msg->getRelay(); - packet.frame.frame = cmdMsg->getExecutionFrame(); - packet.playerId.playerId = cmdMsg->getPlayerID(); - packet.commandId.commandId = cmdMsg->getID(); - packet.playerIndex = cmdMsg->getPlayerIndex(); - - memcpy(buffer, &packet, sizeof(packet)); -} - -void NetPacket::FillBufferWithKeepAliveCommand(UnsignedByte *buffer, NetCommandRef *msg) { - NetKeepAliveCommandMsg *cmdMsg = static_cast(msg->getCommand()); - - NetPacketKeepAliveCommand packet; - packet.commandType.commandType = cmdMsg->getNetCommandType(); - packet.relay.relay = msg->getRelay(); - packet.playerId.playerId = cmdMsg->getPlayerID(); - - memcpy(buffer, &packet, sizeof(packet)); -} - -void NetPacket::FillBufferWithDisconnectKeepAliveCommand(UnsignedByte *buffer, NetCommandRef *msg) { - NetDisconnectKeepAliveCommandMsg *cmdMsg = static_cast(msg->getCommand()); - - NetPacketDisconnectKeepAliveCommand packet; - packet.commandType.commandType = cmdMsg->getNetCommandType(); - packet.relay.relay = msg->getRelay(); - packet.playerId.playerId = cmdMsg->getPlayerID(); - - memcpy(buffer, &packet, sizeof(packet)); -} - -void NetPacket::FillBufferWithDisconnectPlayerCommand(UnsignedByte *buffer, NetCommandRef *msg) { - NetDisconnectPlayerCommandMsg *cmdMsg = static_cast(msg->getCommand()); -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addDisconnectPlayerCommand - adding run ahead command")); - - NetPacketDisconnectPlayerCommand packet; - packet.commandType.commandType = cmdMsg->getNetCommandType(); - packet.relay.relay = msg->getRelay(); - packet.playerId.playerId = cmdMsg->getPlayerID(); - packet.commandId.commandId = cmdMsg->getID(); - packet.slot = cmdMsg->getDisconnectSlot(); - packet.disconnectFrame = cmdMsg->getDisconnectFrame(); - - memcpy(buffer, &packet, sizeof(packet)); -} - -void NetPacket::FillBufferWithPacketRouterQueryCommand(UnsignedByte *buffer, NetCommandRef *msg) { - NetPacketRouterQueryCommandMsg *cmdMsg = static_cast(msg->getCommand()); -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addPacketRouterQueryCommand - adding packet router query command")); - - NetPacketRouterQueryCommand packet; - packet.commandType.commandType = cmdMsg->getNetCommandType(); - packet.relay.relay = msg->getRelay(); - packet.playerId.playerId = cmdMsg->getPlayerID(); - - memcpy(buffer, &packet, sizeof(packet)); -} - -void NetPacket::FillBufferWithPacketRouterAckCommand(UnsignedByte *buffer, NetCommandRef *msg) { - NetPacketRouterAckCommandMsg *cmdMsg = (NetPacketRouterAckCommandMsg *)(msg->getCommand()); -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addPacketRouterAckCommand - adding packet router query command")); - - NetPacketRouterAckCommand packet; - packet.commandType.commandType = cmdMsg->getNetCommandType(); - packet.relay.relay = msg->getRelay(); - packet.playerId.playerId = cmdMsg->getPlayerID(); - - memcpy(buffer, &packet, sizeof(packet)); -} - -void NetPacket::FillBufferWithDisconnectChatCommand(UnsignedByte *buffer, NetCommandRef *msg) { - NetDisconnectChatCommandMsg *cmdMsg = (NetDisconnectChatCommandMsg *)(msg->getCommand()); -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addDisconnectChatCommand - adding run ahead command")); - - UnicodeString unitext = cmdMsg->getText(); - - NetPacketDisconnectChatCommand packet; - packet.commandType.commandType = cmdMsg->getNetCommandType(); - packet.relay.relay = msg->getRelay(); - packet.playerId.playerId = cmdMsg->getPlayerID(); - packet.textLength = NetPacketDisconnectChatCommand::getUsableTextLength(unitext); - - memcpy(buffer, &packet, sizeof(packet)); - - // Variable data portion - UnsignedShort offset = sizeof(NetPacketDisconnectChatCommand); - memcpy(buffer + offset, unitext.str(), packet.textLength * sizeof(UnsignedShort)); - offset += packet.textLength * sizeof(UnsignedShort); -} - -void NetPacket::FillBufferWithDisconnectVoteCommand(UnsignedByte *buffer, NetCommandRef *msg) { - NetDisconnectVoteCommandMsg *cmdMsg = (NetDisconnectVoteCommandMsg *)(msg->getCommand()); -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addDisconnectVoteCommand - adding run ahead command")); - - NetPacketDisconnectVoteCommand packet; - packet.commandType.commandType = cmdMsg->getNetCommandType(); - packet.relay.relay = msg->getRelay(); - packet.playerId.playerId = cmdMsg->getPlayerID(); - packet.commandId.commandId = cmdMsg->getID(); - packet.slot = cmdMsg->getSlot(); - packet.voteFrame = cmdMsg->getVoteFrame(); - - memcpy(buffer, &packet, sizeof(packet)); -} - -void NetPacket::FillBufferWithChatCommand(UnsignedByte *buffer, NetCommandRef *msg) { - NetChatCommandMsg *cmdMsg = static_cast(msg->getCommand()); -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addDisconnectChatCommand - adding run ahead command")); - - UnicodeString unitext = cmdMsg->getText(); - - NetPacketChatCommand packet; - packet.commandType.commandType = cmdMsg->getNetCommandType(); - packet.frame.frame = cmdMsg->getExecutionFrame(); - packet.relay.relay = msg->getRelay(); - packet.playerId.playerId = cmdMsg->getPlayerID(); - packet.commandId.commandId = cmdMsg->getID(); - packet.textLength = NetPacketChatCommand::getUsableTextLength(unitext); - - memcpy(buffer, &packet, sizeof(packet)); - - // Variable data portion - UnsignedShort offset = sizeof(NetPacketChatCommand); - memcpy(buffer + offset, unitext.str(), packet.textLength * sizeof(UnsignedShort)); - offset += packet.textLength * sizeof(UnsignedShort); - - Int playerMask = cmdMsg->getPlayerMask(); - memcpy(buffer + offset, &playerMask, sizeof(Int)); - offset += sizeof(Int); -} - -void NetPacket::FillBufferWithProgressMessage(UnsignedByte *buffer, NetCommandRef *msg) { - NetProgressCommandMsg *cmdMsg = (NetProgressCommandMsg *)(msg->getCommand()); - - NetPacketProgressCommand packet; - packet.commandType.commandType = cmdMsg->getNetCommandType(); - packet.relay.relay = msg->getRelay(); - packet.playerId.playerId = cmdMsg->getPlayerID(); - packet.percentage = cmdMsg->getPercentage(); - - memcpy(buffer, &packet, sizeof(packet)); -} - -void NetPacket::FillBufferWithLoadCompleteMessage(UnsignedByte *buffer, NetCommandRef *msg) { - NetCommandMsg *cmdMsg = static_cast(msg->getCommand()); - - NetPacketLoadCompleteCommand packet; - packet.commandType.commandType = cmdMsg->getNetCommandType(); - packet.relay.relay = msg->getRelay(); - packet.playerId.playerId = cmdMsg->getPlayerID(); - packet.commandId.commandId = cmdMsg->getID(); - - memcpy(buffer, &packet, sizeof(packet)); -} - -void NetPacket::FillBufferWithTimeOutGameStartMessage(UnsignedByte *buffer, NetCommandRef *msg) { - NetCommandMsg *cmdMsg = static_cast(msg->getCommand()); - - NetPacketTimeOutGameStartCommand packet; - packet.commandType.commandType = cmdMsg->getNetCommandType(); - packet.relay.relay = msg->getRelay(); - packet.playerId.playerId = cmdMsg->getPlayerID(); - packet.commandId.commandId = cmdMsg->getID(); - - memcpy(buffer, &packet, sizeof(packet)); -} - -void NetPacket::FillBufferWithFileMessage(UnsignedByte *buffer, NetCommandRef *msg) { - NetFileCommandMsg *cmdMsg = static_cast(msg->getCommand()); - - NetPacketFileCommand packet; - packet.commandType.commandType = cmdMsg->getNetCommandType(); - packet.relay.relay = msg->getRelay(); - packet.playerId.playerId = cmdMsg->getPlayerID(); - packet.commandId.commandId = cmdMsg->getID(); - - memcpy(buffer, &packet, sizeof(packet)); - - // Variable data portion - UnsignedInt offset = sizeof(NetPacketFileCommand); - - AsciiString filename = cmdMsg->getPortableFilename(); // PORTABLE - for (Int i = 0; i < filename.getLength(); ++i) { - buffer[offset] = filename.getCharAt(i); - ++offset; - } - buffer[offset] = 0; - ++offset; - - UnsignedInt newInt = cmdMsg->getFileLength(); - memcpy(buffer + offset, &newInt, sizeof(newInt)); - offset += sizeof(newInt); - - memcpy(buffer + offset, cmdMsg->getFileData(), cmdMsg->getFileLength()); - offset += cmdMsg->getFileLength(); -} - -void NetPacket::FillBufferWithFileAnnounceMessage(UnsignedByte *buffer, NetCommandRef *msg) { - NetFileAnnounceCommandMsg *cmdMsg = static_cast(msg->getCommand()); - - NetPacketFileAnnounceCommand packet; - packet.commandType.commandType = cmdMsg->getNetCommandType(); - packet.relay.relay = msg->getRelay(); - packet.playerId.playerId = cmdMsg->getPlayerID(); - packet.commandId.commandId = cmdMsg->getID(); - - memcpy(buffer, &packet, sizeof(packet)); - - // Variable data portion - UnsignedInt offset = sizeof(NetPacketFileAnnounceCommand); - - AsciiString filename = cmdMsg->getPortableFilename(); // PORTABLE - for (Int i = 0; i < filename.getLength(); ++i) { - buffer[offset] = filename.getCharAt(i); - ++offset; - } - buffer[offset] = 0; - ++offset; - - UnsignedShort fileID = cmdMsg->getFileID(); - memcpy(buffer + offset, &fileID, sizeof(fileID)); - offset += sizeof(fileID); - - UnsignedByte playerMask = cmdMsg->getPlayerMask(); - memcpy(buffer + offset, &playerMask, sizeof(playerMask)); - offset += sizeof(playerMask); -} - -void NetPacket::FillBufferWithFileProgressMessage(UnsignedByte *buffer, NetCommandRef *msg) { - NetFileProgressCommandMsg *cmdMsg = (NetFileProgressCommandMsg *)(msg->getCommand()); - - NetPacketFileProgressCommand packet; - packet.commandType.commandType = cmdMsg->getNetCommandType(); - packet.relay.relay = msg->getRelay(); - packet.playerId.playerId = cmdMsg->getPlayerID(); - packet.commandId.commandId = cmdMsg->getID(); - packet.fileId = cmdMsg->getFileID(); - packet.progress = cmdMsg->getProgress(); - - memcpy(buffer, &packet, sizeof(packet)); -} - -void NetPacket::FillBufferWithDisconnectFrameMessage(UnsignedByte *buffer, NetCommandRef *msg) { - NetDisconnectFrameCommandMsg *cmdMsg = (NetDisconnectFrameCommandMsg *)(msg->getCommand()); - - NetPacketDisconnectFrameCommand packet; - packet.commandType.commandType = cmdMsg->getNetCommandType(); - packet.relay.relay = msg->getRelay(); - packet.playerId.playerId = cmdMsg->getPlayerID(); - packet.commandId.commandId = cmdMsg->getID(); - packet.disconnectFrame = cmdMsg->getDisconnectFrame(); - - memcpy(buffer, &packet, sizeof(packet)); -} - -void NetPacket::FillBufferWithDisconnectScreenOffMessage(UnsignedByte *buffer, NetCommandRef *msg) { - NetDisconnectScreenOffCommandMsg *cmdMsg = (NetDisconnectScreenOffCommandMsg *)(msg->getCommand()); - - NetPacketDisconnectScreenOffCommand packet; - packet.commandType.commandType = cmdMsg->getNetCommandType(); - packet.relay.relay = msg->getRelay(); - packet.playerId.playerId = cmdMsg->getPlayerID(); - packet.commandId.commandId = cmdMsg->getID(); - packet.newFrame = cmdMsg->getNewFrame(); - - memcpy(buffer, &packet, sizeof(packet)); -} - -void NetPacket::FillBufferWithFrameResendRequestMessage(UnsignedByte *buffer, NetCommandRef *msg) { - NetFrameResendRequestCommandMsg *cmdMsg = (NetFrameResendRequestCommandMsg *)(msg->getCommand()); - - NetPacketFrameResendRequestCommand packet; - packet.commandType.commandType = cmdMsg->getNetCommandType(); - packet.relay.relay = msg->getRelay(); - packet.playerId.playerId = cmdMsg->getPlayerID(); - packet.commandId.commandId = cmdMsg->getID(); - packet.frameToResend = cmdMsg->getFrameToResend(); - - memcpy(buffer, &packet, sizeof(packet)); -} - - /** * Constructor */ @@ -951,2609 +357,102 @@ Bool NetPacket::addCommand(NetCommandRef *msg) { NetCommandMsg *cmdMsg = msg->getCommand(); - switch(cmdMsg->getNetCommandType()) + Bool ackRepeat = FALSE; + Bool frameRepeat = FALSE; + + switch (cmdMsg->getNetCommandType()) { - case NETCOMMANDTYPE_GAMECOMMAND: - return addGameCommand(msg); case NETCOMMANDTYPE_ACKSTAGE1: - return addAckStage1Command(msg); case NETCOMMANDTYPE_ACKSTAGE2: - return addAckStage2Command(msg); case NETCOMMANDTYPE_ACKBOTH: - return addAckBothCommand(msg); + ackRepeat = isAckRepeat(msg); + break; case NETCOMMANDTYPE_FRAMEINFO: - return addFrameCommand(msg); - case NETCOMMANDTYPE_PLAYERLEAVE: - return addPlayerLeaveCommand(msg); - case NETCOMMANDTYPE_RUNAHEADMETRICS: - return addRunAheadMetricsCommand(msg); - case NETCOMMANDTYPE_RUNAHEAD: - return addRunAheadCommand(msg); - case NETCOMMANDTYPE_DESTROYPLAYER: - return addDestroyPlayerCommand(msg); - case NETCOMMANDTYPE_KEEPALIVE: - return addKeepAliveCommand(msg); - case NETCOMMANDTYPE_DISCONNECTKEEPALIVE: - return addDisconnectKeepAliveCommand(msg); - case NETCOMMANDTYPE_DISCONNECTPLAYER: - return addDisconnectPlayerCommand(msg); - case NETCOMMANDTYPE_PACKETROUTERQUERY: - return addPacketRouterQueryCommand(msg); - case NETCOMMANDTYPE_PACKETROUTERACK: - return addPacketRouterAckCommand(msg); - case NETCOMMANDTYPE_DISCONNECTCHAT: - return addDisconnectChatCommand(msg); - case NETCOMMANDTYPE_DISCONNECTVOTE: - return addDisconnectVoteCommand(msg); - case NETCOMMANDTYPE_CHAT: - return addChatCommand(msg); - case NETCOMMANDTYPE_PROGRESS: - return addProgressMessage(msg); - case NETCOMMANDTYPE_LOADCOMPLETE: - return addLoadCompleteMessage(msg); - case NETCOMMANDTYPE_TIMEOUTSTART: - return addTimeOutGameStartMessage(msg); - case NETCOMMANDTYPE_WRAPPER: - return addWrapperCommand(msg); - case NETCOMMANDTYPE_FILE: - return addFileCommand(msg); - case NETCOMMANDTYPE_FILEANNOUNCE: - return addFileAnnounceCommand(msg); - case NETCOMMANDTYPE_FILEPROGRESS: - return addFileProgressCommand(msg); - case NETCOMMANDTYPE_DISCONNECTFRAME: - return addDisconnectFrameCommand(msg); - case NETCOMMANDTYPE_DISCONNECTSCREENOFF: - return addDisconnectScreenOffCommand(msg); - case NETCOMMANDTYPE_FRAMERESENDREQUEST: - return addFrameResendRequestCommand(msg); + frameRepeat = isFrameRepeat(msg); + break; default: - DEBUG_CRASH(("Unknown NETCOMMANDTYPE %d", cmdMsg->getNetCommandType())); break; } - return TRUE; -} - -/* -T = Net command type -F = Execution frame -P = Player ID -C = Command ID -R = Relay -D = Command Data -Z = Repeat last command -*/ -Bool NetPacket::addFrameResendRequestCommand(NetCommandRef *msg) { - Bool needNewCommandID = FALSE; - if (isRoomForFrameResendRequestMessage(msg)) { - NetFrameResendRequestCommandMsg *cmdMsg = (NetFrameResendRequestCommandMsg *)(msg->getCommand()); - - // If necessary, put the NetCommandType into the packet. - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandType; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getNetCommandType(); - m_packetLen += sizeof(UnsignedByte); + if (ackRepeat || frameRepeat) + { + // Is there enough room in the packet for this message? + if (NetPacketRepeatCommand::getSize() > (MAX_PACKET_SIZE - m_packetLen)) { + return FALSE; + } - m_lastCommandType = cmdMsg->getNetCommandType(); + if (frameRepeat) + { + m_lastCommandID = cmdMsg->getID(); + ++m_lastFrame; // Need this cause we're actually advancing to the next frame by adding this command. } - // If necessary, put the execution frame into the packet. - if (m_lastFrame != cmdMsg->getExecutionFrame()) { - m_packet[m_packetLen] = NetPacketFieldTypes::Frame; - ++m_packetLen; - UnsignedInt newframe = cmdMsg->getExecutionFrame(); - memcpy(m_packet+m_packetLen, &newframe, sizeof(UnsignedInt)); - m_packetLen += sizeof(UnsignedInt); + m_packetLen += NetPacketRepeatCommand::copyBytes(m_packet + m_packetLen); + } + else + { + SmallNetPacketCommandBaseSelect select = cmdMsg->getSmallNetPacketSelect(); + const UnsignedByte updateLastCommandId = select.useCommandId; - m_lastFrame = newframe; - } + select.useCommandType &= m_lastCommandType != cmdMsg->getNetCommandType(); + select.useRelay &= m_lastRelay != msg->getRelay(); + select.useFrame &= m_lastFrame != cmdMsg->getExecutionFrame(); + select.usePlayerId &= m_lastPlayerID != cmdMsg->getPlayerID(); + select.useCommandId &= ((m_lastCommandID + 1) != cmdMsg->getID()) | select.usePlayerId; - // If necessary, put the relay into the packet. - if (m_lastRelay != msg->getRelay()) { - m_packet[m_packetLen] = NetPacketFieldTypes::Relay; - ++m_packetLen; - UnsignedByte newRelay = msg->getRelay(); - memcpy(m_packet + m_packetLen, &newRelay, sizeof(UnsignedByte)); - m_packetLen += sizeof(UnsignedByte); + const size_t msglen = cmdMsg->getSizeForSmallNetPacket(&select); - m_lastRelay = newRelay; + // Is there enough room in the packet for this message? + if (msglen > (MAX_PACKET_SIZE - m_packetLen)) { + return FALSE; } - // If necessary put the player ID into the packet. - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - m_packet[m_packetLen] = NetPacketFieldTypes::PlayerId; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getPlayerID(); - m_packetLen += sizeof(UnsignedByte); + if (select.useCommandType) + m_lastCommandType = cmdMsg->getNetCommandType(); - m_lastPlayerID = cmdMsg->getPlayerID(); - needNewCommandID = TRUE; - } + if (select.useRelay) + m_lastRelay = msg->getRelay(); - // If necessary, specify the command ID of this command. - if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandId; - ++m_packetLen; - UnsignedShort newID = cmdMsg->getID(); - memcpy(m_packet + m_packetLen, &newID, sizeof(UnsignedShort)); - m_packetLen += sizeof(UnsignedShort); - } - m_lastCommandID = cmdMsg->getID(); + if (select.useFrame) + m_lastFrame = cmdMsg->getExecutionFrame(); - m_packet[m_packetLen] = NetPacketFieldTypes::Data; - ++m_packetLen; + if (select.usePlayerId) + m_lastPlayerID = cmdMsg->getPlayerID(); - UnsignedInt frameToResend = cmdMsg->getFrameToResend(); - memcpy(m_packet + m_packetLen, &frameToResend, sizeof(frameToResend)); - m_packetLen += sizeof(frameToResend); + if (updateLastCommandId) + m_lastCommandID = cmdMsg->getID(); - DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addFrameResendRequest - added frame resend request command from player %d for frame %d, command id = %d", m_lastPlayerID, frameToResend, m_lastCommandID)); + m_packetLen += cmdMsg->copyBytesForSmallNetPacket(m_packet + m_packetLen, *msg, &select); + } - ++m_numCommands; + ++m_numCommands; - deleteInstance(m_lastCommand); - m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand()); - m_lastCommand->setRelay(msg->getRelay()); + deleteInstance(m_lastCommand); + m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand()); + m_lastCommand->setRelay(msg->getRelay()); - return TRUE; - } - return FALSE; + return TRUE; } -Bool NetPacket::isRoomForFrameResendRequestMessage(NetCommandRef *msg) { - Int len = 0; - Bool needNewCommandID = FALSE; - NetFrameResendRequestCommandMsg *cmdMsg = (NetFrameResendRequestCommandMsg *)(msg->getCommand()); - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - ++len; - len += sizeof(UnsignedByte); +Bool NetPacket::isFrameRepeat(NetCommandRef *msg) { + if (m_lastCommand == nullptr) { + return FALSE; } - if (m_lastRelay != msg->getRelay()) { - len += sizeof(UnsignedByte) + sizeof(UnsignedByte); + if (m_lastCommand->getCommand()->getNetCommandType() != NETCOMMANDTYPE_FRAMEINFO) { + return FALSE; } - if (m_lastFrame != cmdMsg->getExecutionFrame()) { - len += sizeof(UnsignedInt) + sizeof(UnsignedByte); + NetFrameCommandMsg *framemsg = (NetFrameCommandMsg *)(msg->getCommand()); + NetFrameCommandMsg *lastmsg = (NetFrameCommandMsg *)(m_lastCommand->getCommand()); + if (framemsg->getCommandCount() != 0) { + return FALSE; } - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - ++len; - len += sizeof(UnsignedByte); - needNewCommandID = TRUE; + if (framemsg->getExecutionFrame() != (lastmsg->getExecutionFrame() + 1)) { + return FALSE; } - if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) { - len += sizeof(UnsignedShort) + sizeof(UnsignedByte); + if (msg->getRelay() != m_lastCommand->getRelay()) { + return FALSE; } - - ++len; // for NetPacketFieldTypes::Data - len += sizeof(UnsignedInt); // for the frame to be resent - if ((len + m_packetLen) > MAX_PACKET_SIZE) { - return FALSE; - } - return TRUE; -} - -Bool NetPacket::addDisconnectScreenOffCommand(NetCommandRef *msg) { - Bool needNewCommandID = FALSE; - if (isRoomForDisconnectScreenOffMessage(msg)) { - NetDisconnectScreenOffCommandMsg *cmdMsg = (NetDisconnectScreenOffCommandMsg *)(msg->getCommand()); - - // If necessary, put the NetCommandType into the packet. - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandType; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getNetCommandType(); - m_packetLen += sizeof(UnsignedByte); - - m_lastCommandType = cmdMsg->getNetCommandType(); - } - - // If necessary, put the execution frame into the packet. - if (m_lastFrame != cmdMsg->getExecutionFrame()) { - m_packet[m_packetLen] = NetPacketFieldTypes::Frame; - ++m_packetLen; - UnsignedInt newframe = cmdMsg->getExecutionFrame(); - memcpy(m_packet+m_packetLen, &newframe, sizeof(UnsignedInt)); - m_packetLen += sizeof(UnsignedInt); - - m_lastFrame = newframe; - } - - // If necessary, put the relay into the packet. - if (m_lastRelay != msg->getRelay()) { - m_packet[m_packetLen] = NetPacketFieldTypes::Relay; - ++m_packetLen; - UnsignedByte newRelay = msg->getRelay(); - memcpy(m_packet + m_packetLen, &newRelay, sizeof(UnsignedByte)); - m_packetLen += sizeof(UnsignedByte); - - m_lastRelay = newRelay; - } - - // If necessary put the player ID into the packet. - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - m_packet[m_packetLen] = NetPacketFieldTypes::PlayerId; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getPlayerID(); - m_packetLen += sizeof(UnsignedByte); - - m_lastPlayerID = cmdMsg->getPlayerID(); - needNewCommandID = TRUE; - } - - // If necessary, specify the command ID of this command. - if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandId; - ++m_packetLen; - UnsignedShort newID = cmdMsg->getID(); - memcpy(m_packet + m_packetLen, &newID, sizeof(UnsignedShort)); - m_packetLen += sizeof(UnsignedShort); - } - m_lastCommandID = cmdMsg->getID(); - - m_packet[m_packetLen] = NetPacketFieldTypes::Data; - ++m_packetLen; - - UnsignedInt newFrame = cmdMsg->getNewFrame(); - memcpy(m_packet + m_packetLen, &newFrame, sizeof(newFrame)); - m_packetLen += sizeof(newFrame); - - DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addDisconnectScreenOff - added disconnect screen off command from player %d for frame %d, command id = %d", m_lastPlayerID, newFrame, m_lastCommandID)); - - ++m_numCommands; - - deleteInstance(m_lastCommand); - m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand()); - m_lastCommand->setRelay(msg->getRelay()); - - return TRUE; - } - return FALSE; -} - -Bool NetPacket::isRoomForDisconnectScreenOffMessage(NetCommandRef *msg) { - Int len = 0; - Bool needNewCommandID = FALSE; - NetDisconnectScreenOffCommandMsg *cmdMsg = (NetDisconnectScreenOffCommandMsg *)(msg->getCommand()); - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - ++len; - len += sizeof(UnsignedByte); - } - if (m_lastRelay != msg->getRelay()) { - len += sizeof(UnsignedByte) + sizeof(UnsignedByte); - } - if (m_lastFrame != cmdMsg->getExecutionFrame()) { - len += sizeof(UnsignedInt) + sizeof(UnsignedByte); - } - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - ++len; - len += sizeof(UnsignedByte); - needNewCommandID = TRUE; - } - if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) { - len += sizeof(UnsignedShort) + sizeof(UnsignedByte); - } - - ++len; // for NetPacketFieldTypes::Data - len += sizeof(UnsignedInt); // for the disconnect frame - if ((len + m_packetLen) > MAX_PACKET_SIZE) { - return FALSE; - } - return TRUE; -} - -Bool NetPacket::addDisconnectFrameCommand(NetCommandRef *msg) { - Bool needNewCommandID = FALSE; - if (isRoomForDisconnectFrameMessage(msg)) { - NetDisconnectFrameCommandMsg *cmdMsg = (NetDisconnectFrameCommandMsg *)(msg->getCommand()); - - // If necessary, put the NetCommandType into the packet. - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandType; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getNetCommandType(); - m_packetLen += sizeof(UnsignedByte); - - m_lastCommandType = cmdMsg->getNetCommandType(); - } - - // If necessary, put the execution frame into the packet. - if (m_lastFrame != cmdMsg->getExecutionFrame()) { - m_packet[m_packetLen] = NetPacketFieldTypes::Frame; - ++m_packetLen; - UnsignedInt newframe = cmdMsg->getExecutionFrame(); - memcpy(m_packet+m_packetLen, &newframe, sizeof(UnsignedInt)); - m_packetLen += sizeof(UnsignedInt); - - m_lastFrame = newframe; - } - - // If necessary, put the relay into the packet. - if (m_lastRelay != msg->getRelay()) { - m_packet[m_packetLen] = NetPacketFieldTypes::Relay; - ++m_packetLen; - UnsignedByte newRelay = msg->getRelay(); - memcpy(m_packet + m_packetLen, &newRelay, sizeof(UnsignedByte)); - m_packetLen += sizeof(UnsignedByte); - - m_lastRelay = newRelay; - } - - // If necessary put the player ID into the packet. - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - m_packet[m_packetLen] = NetPacketFieldTypes::PlayerId; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getPlayerID(); - m_packetLen += sizeof(UnsignedByte); - - m_lastPlayerID = cmdMsg->getPlayerID(); - needNewCommandID = TRUE; - } - - // If necessary, specify the command ID of this command. - if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandId; - ++m_packetLen; - UnsignedShort newID = cmdMsg->getID(); - memcpy(m_packet + m_packetLen, &newID, sizeof(UnsignedShort)); - m_packetLen += sizeof(UnsignedShort); - } - m_lastCommandID = cmdMsg->getID(); - - m_packet[m_packetLen] = NetPacketFieldTypes::Data; - ++m_packetLen; - - UnsignedInt disconnectFrame = cmdMsg->getDisconnectFrame(); - memcpy(m_packet + m_packetLen, &disconnectFrame, sizeof(disconnectFrame)); - m_packetLen += sizeof(disconnectFrame); - - ++m_numCommands; - - deleteInstance(m_lastCommand); - m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand()); - m_lastCommand->setRelay(msg->getRelay()); - - DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addDisconnectFrame - added disconnect frame command from player %d for frame %d, command id = %d", m_lastPlayerID, disconnectFrame, m_lastCommandID)); - - return TRUE; - } - return FALSE; -} - -Bool NetPacket::isRoomForDisconnectFrameMessage(NetCommandRef *msg) { - Int len = 0; - Bool needNewCommandID = FALSE; - NetDisconnectFrameCommandMsg *cmdMsg = (NetDisconnectFrameCommandMsg *)(msg->getCommand()); - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - ++len; - len += sizeof(UnsignedByte); - } - if (m_lastRelay != msg->getRelay()) { - len += sizeof(UnsignedByte) + sizeof(UnsignedByte); - } - if (m_lastFrame != cmdMsg->getExecutionFrame()) { - len += sizeof(UnsignedInt) + sizeof(UnsignedByte); - } - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - ++len; - len += sizeof(UnsignedByte); - needNewCommandID = TRUE; - } - if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) { - len += sizeof(UnsignedShort) + sizeof(UnsignedByte); - } - - ++len; // for NetPacketFieldTypes::Data - len += sizeof(UnsignedInt); // for the disconnect frame - if ((len + m_packetLen) > MAX_PACKET_SIZE) { - return FALSE; - } - return TRUE; -} - -Bool NetPacket::addFileCommand(NetCommandRef *msg) { - Bool needNewCommandID = FALSE; - if (isRoomForFileMessage(msg)) { - NetFileCommandMsg *cmdMsg = static_cast(msg->getCommand()); - - // If necessary, put the NetCommandType into the packet. - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandType; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getNetCommandType(); - m_packetLen += sizeof(UnsignedByte); - - m_lastCommandType = cmdMsg->getNetCommandType(); - } - - // If necessary, put the relay into the packet. - if (m_lastRelay != msg->getRelay()) { - m_packet[m_packetLen] = NetPacketFieldTypes::Relay; - ++m_packetLen; - UnsignedByte newRelay = msg->getRelay(); - memcpy(m_packet + m_packetLen, &newRelay, sizeof(UnsignedByte)); - m_packetLen += sizeof(UnsignedByte); - - m_lastRelay = newRelay; - } - - // If necessary put the player ID into the packet. - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - m_packet[m_packetLen] = NetPacketFieldTypes::PlayerId; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getPlayerID(); - m_packetLen += sizeof(UnsignedByte); - - m_lastPlayerID = cmdMsg->getPlayerID(); - needNewCommandID = TRUE; - } - - // If necessary, specify the command ID of this command. - if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandId; - ++m_packetLen; - UnsignedShort newID = cmdMsg->getID(); - memcpy(m_packet + m_packetLen, &newID, sizeof(UnsignedShort)); - m_packetLen += sizeof(UnsignedShort); - } - m_lastCommandID = cmdMsg->getID(); - - - m_packet[m_packetLen] = NetPacketFieldTypes::Data; - ++m_packetLen; - - AsciiString filename = cmdMsg->getPortableFilename(); // PORTABLE - strcpy((char *)(m_packet + m_packetLen), filename.str()); - m_packetLen += filename.getLength() + 1; - - UnsignedInt fileLength = cmdMsg->getFileLength(); - memcpy(m_packet + m_packetLen, &fileLength, sizeof(fileLength)); - m_packetLen += sizeof(fileLength); - - memcpy(m_packet + m_packetLen, cmdMsg->getFileData(), fileLength); - m_packetLen += fileLength; - - ++m_numCommands; - - deleteInstance(m_lastCommand); - m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand()); - m_lastCommand->setRelay(msg->getRelay()); - return TRUE; - } - return FALSE; -} - -Bool NetPacket::isRoomForFileMessage(NetCommandRef *msg) { - Int len = 0; - Bool needNewCommandID = FALSE; - NetFileCommandMsg *cmdMsg = static_cast(msg->getCommand()); - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - len += sizeof(UnsignedByte) + sizeof(UnsignedByte); - } - if (m_lastRelay != msg->getRelay()) { - len += sizeof(UnsignedByte) + sizeof(UnsignedByte); - } - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - len += sizeof(UnsignedByte) + sizeof(UnsignedByte); - needNewCommandID = TRUE; - } - if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) { - len += sizeof(UnsignedByte) + sizeof(UnsignedShort); - } - - ++len; // for NetPacketFieldTypes::Data - len += cmdMsg->getPortableFilename().getLength() + 1; // PORTABLE filename + the terminating 0 - len += sizeof(UnsignedInt); // filedata length - len += cmdMsg->getFileLength(); - - if ((len + m_packetLen) > MAX_PACKET_SIZE) { - return FALSE; - } - - return TRUE; -} - -Bool NetPacket::addFileAnnounceCommand(NetCommandRef *msg) { - Bool needNewCommandID = FALSE; - if (isRoomForFileAnnounceMessage(msg)) { - NetFileAnnounceCommandMsg *cmdMsg = static_cast(msg->getCommand()); - - // If necessary, put the NetCommandType into the packet. - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandType; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getNetCommandType(); - m_packetLen += sizeof(UnsignedByte); - - m_lastCommandType = cmdMsg->getNetCommandType(); - } - - // If necessary, put the relay into the packet. - if (m_lastRelay != msg->getRelay()) { - m_packet[m_packetLen] = NetPacketFieldTypes::Relay; - ++m_packetLen; - UnsignedByte newRelay = msg->getRelay(); - memcpy(m_packet + m_packetLen, &newRelay, sizeof(UnsignedByte)); - m_packetLen += sizeof(UnsignedByte); - - m_lastRelay = newRelay; - } - - // If necessary put the player ID into the packet. - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - m_packet[m_packetLen] = NetPacketFieldTypes::PlayerId; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getPlayerID(); - m_packetLen += sizeof(UnsignedByte); - - m_lastPlayerID = cmdMsg->getPlayerID(); - needNewCommandID = TRUE; - } - - // If necessary, specify the command ID of this command. - if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandId; - ++m_packetLen; - UnsignedShort newID = cmdMsg->getID(); - memcpy(m_packet + m_packetLen, &newID, sizeof(UnsignedShort)); - m_packetLen += sizeof(UnsignedShort); - } - m_lastCommandID = cmdMsg->getID(); - - - m_packet[m_packetLen] = NetPacketFieldTypes::Data; - ++m_packetLen; - - AsciiString filename = cmdMsg->getPortableFilename(); // PORTABLE - strcpy((char *)(m_packet + m_packetLen), filename.str()); - m_packetLen += filename.getLength() + 1; - - UnsignedShort fileID = cmdMsg->getFileID(); - memcpy(m_packet + m_packetLen, &fileID, sizeof(fileID)); - m_packetLen += sizeof(fileID); - - UnsignedByte playerMask = cmdMsg->getPlayerMask(); - memcpy(m_packet + m_packetLen, &playerMask, sizeof(playerMask)); - m_packetLen += sizeof(playerMask); - - ++m_numCommands; - - deleteInstance(m_lastCommand); - m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand()); - m_lastCommand->setRelay(msg->getRelay()); - - DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("Adding file announce message for fileID %d, ID %d to packet", - cmdMsg->getFileID(), cmdMsg->getID())); - return TRUE; - } - DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("No room to add file announce message to packet")); - return FALSE; -} - -Bool NetPacket::isRoomForFileAnnounceMessage(NetCommandRef *msg) { - Int len = 0; - Bool needNewCommandID = FALSE; - NetFileAnnounceCommandMsg *cmdMsg = static_cast(msg->getCommand()); - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - len += sizeof(UnsignedByte) + sizeof(UnsignedByte); - } - if (m_lastRelay != msg->getRelay()) { - len += sizeof(UnsignedByte) + sizeof(UnsignedByte); - } - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - len += sizeof(UnsignedByte) + sizeof(UnsignedByte); - needNewCommandID = TRUE; - } - if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) { - len += sizeof(UnsignedByte) + sizeof(UnsignedShort); - } - - ++len; // for NetPacketFieldTypes::Data - len += cmdMsg->getPortableFilename().getLength() + 1; // PORTABLE filename + the terminating 0 - len += sizeof(UnsignedShort); // m_fileID - len += sizeof(UnsignedByte); // m_playerMask - - if ((len + m_packetLen) > MAX_PACKET_SIZE) { - return FALSE; - } - - return TRUE; -} - -Bool NetPacket::addFileProgressCommand(NetCommandRef *msg) { - Bool needNewCommandID = FALSE; - if (isRoomForFileProgressMessage(msg)) { - NetFileProgressCommandMsg *cmdMsg = (NetFileProgressCommandMsg *)(msg->getCommand()); - - // If necessary, put the NetCommandType into the packet. - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandType; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getNetCommandType(); - m_packetLen += sizeof(UnsignedByte); - - m_lastCommandType = cmdMsg->getNetCommandType(); - } - - // If necessary, put the relay into the packet. - if (m_lastRelay != msg->getRelay()) { - m_packet[m_packetLen] = NetPacketFieldTypes::Relay; - ++m_packetLen; - UnsignedByte newRelay = msg->getRelay(); - memcpy(m_packet + m_packetLen, &newRelay, sizeof(UnsignedByte)); - m_packetLen += sizeof(UnsignedByte); - - m_lastRelay = newRelay; - } - - // If necessary put the player ID into the packet. - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - m_packet[m_packetLen] = NetPacketFieldTypes::PlayerId; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getPlayerID(); - m_packetLen += sizeof(UnsignedByte); - - m_lastPlayerID = cmdMsg->getPlayerID(); - needNewCommandID = TRUE; - } - - // If necessary, specify the command ID of this command. - if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandId; - ++m_packetLen; - UnsignedShort newID = cmdMsg->getID(); - memcpy(m_packet + m_packetLen, &newID, sizeof(UnsignedShort)); - m_packetLen += sizeof(UnsignedShort); - } - m_lastCommandID = cmdMsg->getID(); - - - m_packet[m_packetLen] = NetPacketFieldTypes::Data; - ++m_packetLen; - - UnsignedShort fileID = cmdMsg->getFileID(); - memcpy(m_packet + m_packetLen, &fileID, sizeof(fileID)); - m_packetLen += sizeof(fileID); - - Int progress = cmdMsg->getProgress(); - memcpy(m_packet + m_packetLen, &progress, sizeof(progress)); - m_packetLen += sizeof(progress); - - ++m_numCommands; - - deleteInstance(m_lastCommand); - m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand()); - m_lastCommand->setRelay(msg->getRelay()); - - return TRUE; - } - return FALSE; -} - -Bool NetPacket::isRoomForFileProgressMessage(NetCommandRef *msg) { - Int len = 0; - Bool needNewCommandID = FALSE; - NetFileProgressCommandMsg *cmdMsg = (NetFileProgressCommandMsg *)(msg->getCommand()); - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - len += sizeof(UnsignedByte) + sizeof(UnsignedByte); - } - if (m_lastRelay != msg->getRelay()) { - len += sizeof(UnsignedByte) + sizeof(UnsignedByte); - } - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - len += sizeof(UnsignedByte) + sizeof(UnsignedByte); - needNewCommandID = TRUE; - } - if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) { - len += sizeof(UnsignedByte) + sizeof(UnsignedShort); - } - - ++len; // for NetPacketFieldTypes::Data - len += sizeof(UnsignedShort); // m_fileID - len += sizeof(Int); // m_progress - - if ((len + m_packetLen) > MAX_PACKET_SIZE) { - return FALSE; - } - - return TRUE; -} - -Bool NetPacket::addWrapperCommand(NetCommandRef *msg) { - Bool needNewCommandID = FALSE; - if (isRoomForWrapperMessage(msg)) { - NetWrapperCommandMsg *cmdMsg = (NetWrapperCommandMsg *)(msg->getCommand()); - - // If necessary, put the NetCommandType into the packet. - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandType; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getNetCommandType(); - m_packetLen += sizeof(UnsignedByte); - - m_lastCommandType = cmdMsg->getNetCommandType(); - } - - // If necessary, put the relay into the packet. - if (m_lastRelay != msg->getRelay()) { - m_packet[m_packetLen] = NetPacketFieldTypes::Relay; - ++m_packetLen; - UnsignedByte newRelay = msg->getRelay(); - memcpy(m_packet + m_packetLen, &newRelay, sizeof(UnsignedByte)); - m_packetLen += sizeof(UnsignedByte); - - m_lastRelay = newRelay; - } - - // If necessary put the player ID into the packet. - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - m_packet[m_packetLen] = NetPacketFieldTypes::PlayerId; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getPlayerID(); - m_packetLen += sizeof(UnsignedByte); - - m_lastPlayerID = cmdMsg->getPlayerID(); - needNewCommandID = TRUE; - } - - // If necessary, specify the command ID of this command. - if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandId; - ++m_packetLen; - UnsignedShort newID = cmdMsg->getID(); - memcpy(m_packet + m_packetLen, &newID, sizeof(UnsignedShort)); - m_packetLen += sizeof(UnsignedShort); - } - m_lastCommandID = cmdMsg->getID(); - - - m_packet[m_packetLen] = NetPacketFieldTypes::Data; - ++m_packetLen; - - // wrapped command ID - UnsignedShort wrappedCommandID = cmdMsg->getWrappedCommandID(); - memcpy(m_packet + m_packetLen, &wrappedCommandID, sizeof(wrappedCommandID)); - m_packetLen += sizeof(wrappedCommandID); - - // chunk number -// m_packet[m_packetLen] = cmdMsg->getChunkNumber(); -// ++m_packetLen; - UnsignedInt chunkNumber = cmdMsg->getChunkNumber(); - memcpy(m_packet + m_packetLen, &chunkNumber, sizeof(chunkNumber)); - m_packetLen += sizeof(chunkNumber); - - // number of chunks -// m_packet[m_packetLen] = cmdMsg->getNumChunks(); -// ++m_packetLen; - UnsignedInt numChunks = cmdMsg->getNumChunks(); - memcpy(m_packet + m_packetLen, &numChunks, sizeof(numChunks)); - m_packetLen += sizeof(numChunks); - - // total length of data for all chunks - UnsignedInt totalDataLength = cmdMsg->getTotalDataLength(); - memcpy(m_packet + m_packetLen, &totalDataLength, sizeof(totalDataLength)); - m_packetLen += sizeof(totalDataLength); - - // data length for this chunk - UnsignedInt dataLength = cmdMsg->getDataLength(); - memcpy(m_packet + m_packetLen, &dataLength, sizeof(dataLength)); - m_packetLen += sizeof(dataLength); - - // the offset into the data of this chunk - UnsignedInt dataOffset = cmdMsg->getDataOffset(); - memcpy(m_packet + m_packetLen, &dataOffset, sizeof(dataOffset)); - m_packetLen += sizeof(dataOffset); - - // the data for this chunk - UnsignedByte *data = cmdMsg->getData(); - memcpy(m_packet + m_packetLen, data, dataLength); - m_packetLen += dataLength; - - ++m_numCommands; - - deleteInstance(m_lastCommand); - m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand()); - m_lastCommand->setRelay(msg->getRelay()); - - return TRUE; - } - return FALSE; -} - -Bool NetPacket::isRoomForWrapperMessage(NetCommandRef *msg) { - Int len = 0; - Bool needNewCommandID = FALSE; - NetWrapperCommandMsg *cmdMsg = (NetWrapperCommandMsg *)(msg->getCommand()); - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - len += sizeof(UnsignedByte) + sizeof(UnsignedByte); - } - if (m_lastRelay != msg->getRelay()) { - len += sizeof(UnsignedByte) + sizeof(UnsignedByte); - } - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - len += sizeof(UnsignedByte) + sizeof(UnsignedByte); - needNewCommandID = TRUE; - } - if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) { - len += sizeof(UnsignedByte) + sizeof(UnsignedShort); - } - - ++len; // for NetPacketFieldTypes::Data - len += sizeof(UnsignedShort); // wrapped command ID - len += sizeof(UnsignedInt); // chunk number - len += sizeof(UnsignedInt); // number of chunks - len += sizeof(UnsignedInt); // total data length - len += sizeof(UnsignedInt); // data length of this chunk - len += sizeof(UnsignedInt); // offset of this chunk - len += cmdMsg->getDataLength(); // for the data of this chunk - - if ((len + m_packetLen) > MAX_PACKET_SIZE) { - return FALSE; - } - - return TRUE; -} - -/** - * Add a TimeOutGameStart to the packet. Returns true if successful. - */ -Bool NetPacket::addTimeOutGameStartMessage(NetCommandRef *msg) { - Bool needNewCommandID = FALSE; - if (isRoomForTimeOutGameStartMessage(msg)) { - NetCommandMsg *cmdMsg = static_cast(msg->getCommand()); - - // If necessary, put the NetCommandType into the packet. - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandType; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getNetCommandType(); - m_packetLen += sizeof(UnsignedByte); - - m_lastCommandType = cmdMsg->getNetCommandType(); - } - - // If necessary, put the relay into the packet. - if (m_lastRelay != msg->getRelay()) { - m_packet[m_packetLen] = NetPacketFieldTypes::Relay; - ++m_packetLen; - UnsignedByte newRelay = msg->getRelay(); - memcpy(m_packet+m_packetLen, &newRelay, sizeof(UnsignedByte)); - m_packetLen += sizeof(UnsignedByte); - - m_lastRelay = newRelay; - } - - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - m_packet[m_packetLen] = NetPacketFieldTypes::PlayerId; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getPlayerID(); - m_packetLen += sizeof(UnsignedByte); - - m_lastPlayerID = cmdMsg->getPlayerID(); - needNewCommandID = TRUE; - } - - // If necessary, specify the command ID of this command. - if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandId; - ++m_packetLen; - UnsignedShort newID = cmdMsg->getID(); - memcpy(m_packet + m_packetLen, &newID, sizeof(UnsignedShort)); - m_packetLen += sizeof(UnsignedShort); - } - m_lastCommandID = cmdMsg->getID(); - - m_packet[m_packetLen] = NetPacketFieldTypes::Data; - ++m_packetLen; - - ++m_numCommands; - - deleteInstance(m_lastCommand); - m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand()); - m_lastCommand->setRelay(msg->getRelay()); - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("Added keep alive command to packet.")); - - return TRUE; - } - return FALSE; -} - -/** - * Returns true if there is room in the packet for this command. - */ -Bool NetPacket::isRoomForTimeOutGameStartMessage(NetCommandRef *msg) { - Int len = 0; - Bool needNewCommandID = FALSE; - NetCommandMsg *cmdMsg = static_cast(msg->getCommand()); - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - ++len; - len += sizeof(UnsignedByte); - } - if (m_lastRelay != msg->getRelay()) { - len += sizeof(UnsignedByte) + sizeof(UnsignedByte); - } - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - ++len; - len += sizeof(UnsignedByte); - needNewCommandID = TRUE; - } - if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) { - len += sizeof(UnsignedByte) + sizeof(UnsignedShort); - } - - ++len; // for NetPacketFieldTypes::Data - if ((len + m_packetLen) > MAX_PACKET_SIZE) { - return FALSE; - } - return TRUE; -} - - - -/** - * Add a Progress command to the packet. Returns true if successful. - */ -Bool NetPacket::addLoadCompleteMessage(NetCommandRef *msg) { - Bool needNewCommandID = FALSE; - if (isRoomForLoadCompleteMessage(msg)) { - NetCommandMsg *cmdMsg = static_cast(msg->getCommand()); - - // If necessary, put the NetCommandType into the packet. - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandType; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getNetCommandType(); - m_packetLen += sizeof(UnsignedByte); - - m_lastCommandType = cmdMsg->getNetCommandType(); - } - - // If necessary, put the relay into the packet. - if (m_lastRelay != msg->getRelay()) { - m_packet[m_packetLen] = NetPacketFieldTypes::Relay; - ++m_packetLen; - UnsignedByte newRelay = msg->getRelay(); - memcpy(m_packet+m_packetLen, &newRelay, sizeof(UnsignedByte)); - m_packetLen += sizeof(UnsignedByte); - - m_lastRelay = newRelay; - } - - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - m_packet[m_packetLen] = NetPacketFieldTypes::PlayerId; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getPlayerID(); - m_packetLen += sizeof(UnsignedByte); - - m_lastPlayerID = cmdMsg->getPlayerID(); - needNewCommandID = TRUE; - } - - // If necessary, specify the command ID of this command. - if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandId; - ++m_packetLen; - UnsignedShort newID = cmdMsg->getID(); - memcpy(m_packet + m_packetLen, &newID, sizeof(UnsignedShort)); - m_packetLen += sizeof(UnsignedShort); - } - m_lastCommandID = cmdMsg->getID(); - - m_packet[m_packetLen] = NetPacketFieldTypes::Data; - ++m_packetLen; - - ++m_numCommands; - - deleteInstance(m_lastCommand); - m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand()); - m_lastCommand->setRelay(msg->getRelay()); - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("Added keep alive command to packet.")); - - return TRUE; - } - return FALSE; -} - -/** - * Returns true if there is room in the packet for this command. - */ -Bool NetPacket::isRoomForLoadCompleteMessage(NetCommandRef *msg) { - Int len = 0; - Bool needNewCommandID = FALSE; - NetCommandMsg *cmdMsg = static_cast(msg->getCommand()); - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - ++len; - len += sizeof(UnsignedByte); - } - if (m_lastRelay != msg->getRelay()) { - len += sizeof(UnsignedByte) + sizeof(UnsignedByte); - } - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - ++len; - len += sizeof(UnsignedByte); - needNewCommandID = TRUE; - } - if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) { - len += sizeof(UnsignedByte) + sizeof(UnsignedShort); - } - - ++len; // for NetPacketFieldTypes::Data - if ((len + m_packetLen) > MAX_PACKET_SIZE) { - return FALSE; - } - return TRUE; -} - - - - -/** - * Add a Progress command to the packet. Returns true if successful. - */ -Bool NetPacket::addProgressMessage(NetCommandRef *msg) { - if (isRoomForProgressMessage(msg)) { - NetProgressCommandMsg *cmdMsg = (NetProgressCommandMsg *)(msg->getCommand()); - - // If necessary, put the NetCommandType into the packet. - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandType; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getNetCommandType(); - m_packetLen += sizeof(UnsignedByte); - - m_lastCommandType = cmdMsg->getNetCommandType(); - } - - // If necessary, put the relay into the packet. - if (m_lastRelay != msg->getRelay()) { - m_packet[m_packetLen] = NetPacketFieldTypes::Relay; - ++m_packetLen; - UnsignedByte newRelay = msg->getRelay(); - memcpy(m_packet+m_packetLen, &newRelay, sizeof(UnsignedByte)); - m_packetLen += sizeof(UnsignedByte); - - m_lastRelay = newRelay; - } - - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - m_packet[m_packetLen] = NetPacketFieldTypes::PlayerId; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getPlayerID(); - m_packetLen += sizeof(UnsignedByte); - - m_lastPlayerID = cmdMsg->getPlayerID(); - } - - m_packet[m_packetLen] = NetPacketFieldTypes::Data; - ++m_packetLen; - - m_packet[m_packetLen] = cmdMsg->getPercentage(); - ++m_packetLen; - - ++m_numCommands; - - deleteInstance(m_lastCommand); - m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand()); - m_lastCommand->setRelay(msg->getRelay()); - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("Added keep alive command to packet.")); - - return TRUE; - } - return FALSE; -} - -/** - * Returns true if there is room in the packet for this command. - */ -Bool NetPacket::isRoomForProgressMessage(NetCommandRef *msg) { - Int len = 0; - NetProgressCommandMsg *cmdMsg = (NetProgressCommandMsg *)(msg->getCommand()); - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - ++len; - len += sizeof(UnsignedByte); - } - if (m_lastRelay != msg->getRelay()) { - len += sizeof(UnsignedByte) + sizeof(UnsignedByte); - } - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - ++len; - len += sizeof(UnsignedByte); - } - - ++len; // for NetPacketFieldTypes::Data - ++len; // percentage - if ((len + m_packetLen) > MAX_PACKET_SIZE) { - return FALSE; - } - return TRUE; -} - - - -Bool NetPacket::addDisconnectVoteCommand(NetCommandRef *msg) { - Bool needNewCommandID = FALSE; - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addDisconnectVoteCommand - entering...")); - // need type, player id, relay, command id, slot number - if (isRoomForDisconnectVoteMessage(msg)) { - NetDisconnectVoteCommandMsg *cmdMsg = (NetDisconnectVoteCommandMsg *)(msg->getCommand()); -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addDisconnectVoteCommand - adding run ahead command")); - - // If necessary, put the NetCommandType into the packet. - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandType; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getNetCommandType(); - m_packetLen += sizeof(UnsignedByte); - - m_lastCommandType = cmdMsg->getNetCommandType(); - } - - // If necessary, put the relay into the packet. - if (m_lastRelay != msg->getRelay()) { - m_packet[m_packetLen] = NetPacketFieldTypes::Relay; - ++m_packetLen; - UnsignedByte newRelay = msg->getRelay(); - memcpy(m_packet+m_packetLen, &newRelay, sizeof(UnsignedByte)); - m_packetLen += sizeof(UnsignedByte); - - m_lastRelay = newRelay; - } - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("relay = %d, ", m_lastRelay)); - - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - m_packet[m_packetLen] = NetPacketFieldTypes::PlayerId; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getPlayerID(); - m_packetLen += sizeof(UnsignedByte); - - m_lastPlayerID = cmdMsg->getPlayerID(); - needNewCommandID = TRUE; - } - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("player = %d", m_lastPlayerID)); - - // If necessary, specify the command ID of this command. - if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandId; - ++m_packetLen; - UnsignedShort newID = cmdMsg->getID(); - memcpy(m_packet + m_packetLen, &newID, sizeof(UnsignedShort)); - m_packetLen += sizeof(UnsignedShort); - } - m_lastCommandID = cmdMsg->getID(); - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("command id = %d", m_lastCommandID)); - - m_packet[m_packetLen] = NetPacketFieldTypes::Data; - ++m_packetLen; - UnsignedByte slot = cmdMsg->getSlot(); - memcpy(m_packet + m_packetLen, &slot, sizeof(slot)); - m_packetLen += sizeof(slot); - - UnsignedInt voteFrame = cmdMsg->getVoteFrame(); - memcpy(m_packet + m_packetLen, &voteFrame, sizeof(voteFrame)); - m_packetLen += sizeof(voteFrame); - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addDisconnectVoteCommand - added disconnect vote command, player id %d command id %d, voted slot %d", m_lastPlayerID, m_lastCommandID, slot)); - - ++m_numCommands; - - deleteInstance(m_lastCommand); - m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand()); - m_lastCommand->setRelay(msg->getRelay()); - return TRUE; - } - return FALSE; -} - -/** - * Returns true if there is room for this player disconnect command in this packet. - */ -Bool NetPacket::isRoomForDisconnectVoteMessage(NetCommandRef *msg) { - Int len = 0; - Bool needNewCommandID = FALSE; - NetDisconnectVoteCommandMsg *cmdMsg = (NetDisconnectVoteCommandMsg *)(msg->getCommand()); - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - ++len; - len += sizeof(UnsignedByte); - } - if (m_lastRelay != msg->getRelay()) { - len += sizeof(UnsignedByte) + sizeof(UnsignedByte); - } - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - ++len; - len += sizeof(UnsignedByte); - needNewCommandID = TRUE; - } - if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) { - len += sizeof(UnsignedShort) + sizeof(UnsignedByte); - } - - ++len; // for NetPacketFieldTypes::Data - len += sizeof(UnsignedByte); // slot number - len += sizeof(UnsignedInt); // vote frame - - if ((len + m_packetLen) > MAX_PACKET_SIZE) { - return FALSE; - } - return TRUE; -} - -Bool NetPacket::addDisconnectChatCommand(NetCommandRef *msg) { - // type, player, id, relay, data - // data format: 1 byte string length, string (two bytes per character) -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addDisconnectChatCommand - Entering...")); - if (isRoomForDisconnectChatMessage(msg)) { - NetDisconnectChatCommandMsg *cmdMsg = (NetDisconnectChatCommandMsg *)(msg->getCommand()); -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addDisconnectChatCommand - adding run ahead command")); - - // If necessary, put the NetCommandType into the packet. - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandType; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getNetCommandType(); - m_packetLen += sizeof(UnsignedByte); - - m_lastCommandType = cmdMsg->getNetCommandType(); - } - - // If necessary, put the relay into the packet. - if (m_lastRelay != msg->getRelay()) { - m_packet[m_packetLen] = NetPacketFieldTypes::Relay; - ++m_packetLen; - UnsignedByte newRelay = msg->getRelay(); - memcpy(m_packet+m_packetLen, &newRelay, sizeof(UnsignedByte)); - m_packetLen += sizeof(UnsignedByte); - - m_lastRelay = newRelay; - } - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("relay = %d, ", m_lastRelay)); - - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - m_packet[m_packetLen] = NetPacketFieldTypes::PlayerId; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getPlayerID(); - m_packetLen += sizeof(UnsignedByte); - - m_lastPlayerID = cmdMsg->getPlayerID(); - } - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("player = %d", m_lastPlayerID)); - - m_packet[m_packetLen] = NetPacketFieldTypes::Data; - ++m_packetLen; - UnicodeString unitext = cmdMsg->getText(); - UnsignedByte textLen = NetPacketDisconnectChatCommand::getUsableTextLength(unitext); - memcpy(m_packet + m_packetLen, &textLen, sizeof(UnsignedByte)); - m_packetLen += sizeof(UnsignedByte); - - memcpy(m_packet + m_packetLen, unitext.str(), textLen * sizeof(UnsignedShort)); - m_packetLen += textLen * sizeof(UnsignedShort); - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket - added disconnect chat command")); - - ++m_numCommands; - - deleteInstance(m_lastCommand); - m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand()); - m_lastCommand->setRelay(msg->getRelay()); - return TRUE; - } - return FALSE; -} - -Bool NetPacket::isRoomForDisconnectChatMessage(NetCommandRef *msg) { - Int len = 0; - NetDisconnectChatCommandMsg *cmdMsg = (NetDisconnectChatCommandMsg *)(msg->getCommand()); - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - ++len; - len += sizeof(UnsignedByte); - } - if (m_lastRelay != msg->getRelay()) { - len += sizeof(UnsignedByte) + sizeof(UnsignedByte); - } - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - ++len; - len += sizeof(UnsignedByte); - } - - ++len; // for NetPacketFieldTypes::Data - len += sizeof(UnsignedByte); // string length - UnsignedByte textLen = NetPacketDisconnectChatCommand::getUsableTextLength(cmdMsg->getText()); - len += textLen * sizeof(UnsignedShort); - if ((len + m_packetLen) > MAX_PACKET_SIZE) { - return FALSE; - } - return TRUE; -} - -Bool NetPacket::addChatCommand(NetCommandRef *msg) { - Bool needNewCommandID = FALSE; - if (isRoomForChatMessage(msg)) { - NetChatCommandMsg *cmdMsg = static_cast(msg->getCommand()); -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addDisconnectChatCommand - adding run ahead command")); - - // If necessary, put the NetCommandType into the packet. - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandType; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getNetCommandType(); - m_packetLen += sizeof(UnsignedByte); - - m_lastCommandType = cmdMsg->getNetCommandType(); - } - - // If necessary, put the execution frame into the packet. - if (m_lastFrame != cmdMsg->getExecutionFrame()) { - m_packet[m_packetLen] = NetPacketFieldTypes::Frame; - ++m_packetLen; - UnsignedInt newframe = cmdMsg->getExecutionFrame(); - memcpy(m_packet+m_packetLen, &newframe, sizeof(UnsignedInt)); - m_packetLen += sizeof(UnsignedInt); - - m_lastFrame = newframe; - } - - // If necessary, put the relay into the packet. - if (m_lastRelay != msg->getRelay()) { - m_packet[m_packetLen] = NetPacketFieldTypes::Relay; - ++m_packetLen; - UnsignedByte newRelay = msg->getRelay(); - memcpy(m_packet+m_packetLen, &newRelay, sizeof(UnsignedByte)); - m_packetLen += sizeof(UnsignedByte); - - m_lastRelay = newRelay; - } - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("relay = %d, ", m_lastRelay)); - - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - m_packet[m_packetLen] = NetPacketFieldTypes::PlayerId; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getPlayerID(); - m_packetLen += sizeof(UnsignedByte); - - m_lastPlayerID = cmdMsg->getPlayerID(); - needNewCommandID = TRUE; - } - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("player = %d", m_lastPlayerID)); - - // If necessary, specify the command ID of this command. - if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandId; - ++m_packetLen; - UnsignedShort newID = cmdMsg->getID(); - memcpy(m_packet + m_packetLen, &newID, sizeof(UnsignedShort)); - m_packetLen += sizeof(UnsignedShort); - } - m_lastCommandID = cmdMsg->getID(); - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("command id = %d", m_lastCommandID)); - - m_packet[m_packetLen] = NetPacketFieldTypes::Data; - ++m_packetLen; - UnicodeString unitext = cmdMsg->getText(); - UnsignedByte textLen = NetPacketChatCommand::getUsableTextLength(unitext); - Int playerMask = cmdMsg->getPlayerMask(); - memcpy(m_packet + m_packetLen, &textLen, sizeof(UnsignedByte)); - m_packetLen += sizeof(UnsignedByte); - - memcpy(m_packet + m_packetLen, unitext.str(), textLen * sizeof(UnsignedShort)); - m_packetLen += textLen * sizeof(UnsignedShort); - - memcpy(m_packet + m_packetLen, &playerMask, sizeof(Int)); - m_packetLen += sizeof(Int); - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket - added chat command")); - - ++m_numCommands; - - deleteInstance(m_lastCommand); - m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand()); - m_lastCommand->setRelay(msg->getRelay()); - return TRUE; - } - return FALSE; -} - -Bool NetPacket::isRoomForChatMessage(NetCommandRef *msg) { - Bool needNewCommandID = FALSE; - Int len = 0; - NetChatCommandMsg *cmdMsg = static_cast(msg->getCommand()); - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - ++len; - len += sizeof(UnsignedByte); - } - if (m_lastFrame != cmdMsg->getExecutionFrame()) { - len += sizeof(UnsignedInt) + sizeof(UnsignedByte); - } - if (m_lastRelay != msg->getRelay()) { - len += sizeof(UnsignedByte) + sizeof(UnsignedByte); - } - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - ++len; - len += sizeof(UnsignedByte); - needNewCommandID = TRUE; - } - if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) { - len += sizeof(UnsignedShort) + sizeof(UnsignedByte); - } - - ++len; // for NetPacketFieldTypes::Data - len += sizeof(UnsignedByte); // string length - UnsignedByte textLen = NetPacketChatCommand::getUsableTextLength(cmdMsg->getText()); - len += textLen * sizeof(UnsignedShort); - len += sizeof(Int); // playerMask - if ((len + m_packetLen) > MAX_PACKET_SIZE) { - return FALSE; - } - return TRUE; -} - -Bool NetPacket::addPacketRouterAckCommand(NetCommandRef *msg) { - // need type, player id, relay, command id, slot number - if (isRoomForPacketRouterAckMessage(msg)) { - NetPacketRouterAckCommandMsg *cmdMsg = (NetPacketRouterAckCommandMsg *)(msg->getCommand()); -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addPacketRouterAckCommand - adding packet router query command")); - - // If necessary, put the NetCommandType into the packet. - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandType; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getNetCommandType(); - m_packetLen += sizeof(UnsignedByte); - - m_lastCommandType = cmdMsg->getNetCommandType(); - } - - // If necessary, put the relay into the packet. - if (m_lastRelay != msg->getRelay()) { - m_packet[m_packetLen] = NetPacketFieldTypes::Relay; - ++m_packetLen; - UnsignedByte newRelay = msg->getRelay(); - memcpy(m_packet+m_packetLen, &newRelay, sizeof(UnsignedByte)); - m_packetLen += sizeof(UnsignedByte); - - m_lastRelay = newRelay; - } - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("relay = %d, ", m_lastRelay)); - - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - m_packet[m_packetLen] = NetPacketFieldTypes::PlayerId; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getPlayerID(); - m_packetLen += sizeof(UnsignedByte); - - m_lastPlayerID = cmdMsg->getPlayerID(); - } - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("player = %d", m_lastPlayerID)); - - m_packet[m_packetLen] = NetPacketFieldTypes::Data; - ++m_packetLen; - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket - added packet router ack command, player id %d", m_lastPlayerID)); - - ++m_numCommands; - - deleteInstance(m_lastCommand); - m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand()); - m_lastCommand->setRelay(msg->getRelay()); - return TRUE; - } - return FALSE; -} - -/** - * Returns true if there is room for this packet router ack command in this packet. - */ -Bool NetPacket::isRoomForPacketRouterAckMessage(NetCommandRef *msg) { - Int len = 0; - NetPacketRouterAckCommandMsg *cmdMsg = (NetPacketRouterAckCommandMsg *)(msg->getCommand()); - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - ++len; - len += sizeof(UnsignedByte); - } - if (m_lastRelay != msg->getRelay()) { - len += sizeof(UnsignedByte) + sizeof(UnsignedByte); - } - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - ++len; - len += sizeof(UnsignedByte); - } - - ++len; // for NetPacketFieldTypes::Data - if ((len + m_packetLen) > MAX_PACKET_SIZE) { - return FALSE; - } - return TRUE; -} - -Bool NetPacket::addPacketRouterQueryCommand(NetCommandRef *msg) { - // need type, player id, relay, command id, slot number - if (isRoomForPacketRouterQueryMessage(msg)) { - NetPacketRouterQueryCommandMsg *cmdMsg = static_cast(msg->getCommand()); -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addPacketRouterQueryCommand - adding packet router query command")); - - // If necessary, put the NetCommandType into the packet. - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandType; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getNetCommandType(); - m_packetLen += sizeof(UnsignedByte); - - m_lastCommandType = cmdMsg->getNetCommandType(); - } - - // If necessary, put the relay into the packet. - if (m_lastRelay != msg->getRelay()) { - m_packet[m_packetLen] = NetPacketFieldTypes::Relay; - ++m_packetLen; - UnsignedByte newRelay = msg->getRelay(); - memcpy(m_packet+m_packetLen, &newRelay, sizeof(UnsignedByte)); - m_packetLen += sizeof(UnsignedByte); - - m_lastRelay = newRelay; - } - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("relay = %d, ", m_lastRelay)); - - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - m_packet[m_packetLen] = NetPacketFieldTypes::PlayerId; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getPlayerID(); - m_packetLen += sizeof(UnsignedByte); - - m_lastPlayerID = cmdMsg->getPlayerID(); - } - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("player = %d", m_lastPlayerID)); - - m_packet[m_packetLen] = NetPacketFieldTypes::Data; - ++m_packetLen; - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket - added packet router query command, player id %d", m_lastPlayerID)); - - ++m_numCommands; - - deleteInstance(m_lastCommand); - m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand()); - m_lastCommand->setRelay(msg->getRelay()); - return TRUE; - } - return FALSE; -} - -/** - * Returns true if there is room for this packet router query command in this packet. - */ -Bool NetPacket::isRoomForPacketRouterQueryMessage(NetCommandRef *msg) { - Int len = 0; - NetPacketRouterQueryCommandMsg *cmdMsg = static_cast(msg->getCommand()); - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - ++len; - len += sizeof(UnsignedByte); - } - if (m_lastRelay != msg->getRelay()) { - len += sizeof(UnsignedByte) + sizeof(UnsignedByte); - } - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - ++len; - len += sizeof(UnsignedByte); - } - - ++len; // for NetPacketFieldTypes::Data - if ((len + m_packetLen) > MAX_PACKET_SIZE) { - return FALSE; - } - return TRUE; -} - -Bool NetPacket::addDisconnectPlayerCommand(NetCommandRef *msg) { - Bool needNewCommandID = FALSE; - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addDisconnectPlayerCommand - entering...")); - // need type, player id, relay, command id, slot number - if (isRoomForDisconnectPlayerMessage(msg)) { - NetDisconnectPlayerCommandMsg *cmdMsg = static_cast(msg->getCommand()); -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addDisconnectPlayerCommand - adding run ahead command")); - - // If necessary, put the NetCommandType into the packet. - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandType; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getNetCommandType(); - m_packetLen += sizeof(UnsignedByte); - - m_lastCommandType = cmdMsg->getNetCommandType(); - } - - // If necessary, put the relay into the packet. - if (m_lastRelay != msg->getRelay()) { - m_packet[m_packetLen] = NetPacketFieldTypes::Relay; - ++m_packetLen; - UnsignedByte newRelay = msg->getRelay(); - memcpy(m_packet+m_packetLen, &newRelay, sizeof(UnsignedByte)); - m_packetLen += sizeof(UnsignedByte); - - m_lastRelay = newRelay; - } - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("relay = %d, ", m_lastRelay)); - - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - m_packet[m_packetLen] = NetPacketFieldTypes::PlayerId; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getPlayerID(); - m_packetLen += sizeof(UnsignedByte); - - m_lastPlayerID = cmdMsg->getPlayerID(); - needNewCommandID = TRUE; - } - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("player = %d", m_lastPlayerID)); - - // If necessary, specify the command ID of this command. - if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandId; - ++m_packetLen; - UnsignedShort newID = cmdMsg->getID(); - memcpy(m_packet + m_packetLen, &newID, sizeof(UnsignedShort)); - m_packetLen += sizeof(UnsignedShort); - } - m_lastCommandID = cmdMsg->getID(); - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("command id = %d", m_lastCommandID)); - - m_packet[m_packetLen] = NetPacketFieldTypes::Data; - ++m_packetLen; - UnsignedByte slot = cmdMsg->getDisconnectSlot(); - memcpy(m_packet + m_packetLen, &slot, sizeof(slot)); - m_packetLen += sizeof(slot); - - UnsignedInt disconnectFrame = cmdMsg->getDisconnectFrame(); - memcpy(m_packet + m_packetLen, &disconnectFrame, sizeof(disconnectFrame)); - m_packetLen += sizeof(disconnectFrame); - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addDisconnectPlayerCommand - added disconnect player command, player id %d command id %d, disconnecting slot %d", m_lastPlayerID, m_lastCommandID, slot)); - - ++m_numCommands; - - deleteInstance(m_lastCommand); - m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand()); - m_lastCommand->setRelay(msg->getRelay()); - return TRUE; - } - return FALSE; -} - -/** - * Returns true if there is room for this player disconnect command in this packet. - */ -Bool NetPacket::isRoomForDisconnectPlayerMessage(NetCommandRef *msg) { - Int len = 0; - Bool needNewCommandID = FALSE; - NetDisconnectPlayerCommandMsg *cmdMsg = static_cast(msg->getCommand()); - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - ++len; - len += sizeof(UnsignedByte); - } - if (m_lastRelay != msg->getRelay()) { - len += sizeof(UnsignedByte) + sizeof(UnsignedByte); - } - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - ++len; - len += sizeof(UnsignedByte); - needNewCommandID = TRUE; - } - if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) { - len += sizeof(UnsignedShort) + sizeof(UnsignedByte); - } - - ++len; // for NetPacketFieldTypes::Data - len += sizeof(UnsignedByte); // slot number - len += sizeof(UnsignedInt); // disconnectFrame - if ((len + m_packetLen) > MAX_PACKET_SIZE) { - return FALSE; - } - return TRUE; -} - - -/** - * Add a keep alive command to the packet. Returns true if successful. - */ -Bool NetPacket::addDisconnectKeepAliveCommand(NetCommandRef *msg) { - if (isRoomForDisconnectKeepAliveMessage(msg)) { - NetDisconnectKeepAliveCommandMsg *cmdMsg = static_cast(msg->getCommand()); - - // If necessary, put the NetCommandType into the packet. - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandType; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getNetCommandType(); - m_packetLen += sizeof(UnsignedByte); - - m_lastCommandType = cmdMsg->getNetCommandType(); - } - - // If necessary, put the relay into the packet. - if (m_lastRelay != msg->getRelay()) { - m_packet[m_packetLen] = NetPacketFieldTypes::Relay; - ++m_packetLen; - UnsignedByte newRelay = msg->getRelay(); - memcpy(m_packet+m_packetLen, &newRelay, sizeof(UnsignedByte)); - m_packetLen += sizeof(UnsignedByte); - - m_lastRelay = newRelay; - } - - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - m_packet[m_packetLen] = NetPacketFieldTypes::PlayerId; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getPlayerID(); - m_packetLen += sizeof(UnsignedByte); - - m_lastPlayerID = cmdMsg->getPlayerID(); - } - - m_packet[m_packetLen] = NetPacketFieldTypes::Data; - ++m_packetLen; - - ++m_numCommands; - - deleteInstance(m_lastCommand); - m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand()); - m_lastCommand->setRelay(msg->getRelay()); - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("Added keep alive command to packet.")); - - return TRUE; - } - return FALSE; -} - -/** - * Returns true if there is room in the packet for this command. - */ -Bool NetPacket::isRoomForDisconnectKeepAliveMessage(NetCommandRef *msg) { - Int len = 0; - NetDisconnectKeepAliveCommandMsg *cmdMsg = static_cast(msg->getCommand()); - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - ++len; - len += sizeof(UnsignedByte); - } - if (m_lastRelay != msg->getRelay()) { - len += sizeof(UnsignedByte) + sizeof(UnsignedByte); - } - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - ++len; - len += sizeof(UnsignedByte); - } - - ++len; // for NetPacketFieldTypes::Data - if ((len + m_packetLen) > MAX_PACKET_SIZE) { - return FALSE; - } - return TRUE; -} - -/** - * Add a keep alive command to the packet. Returns true if successful. - */ -Bool NetPacket::addKeepAliveCommand(NetCommandRef *msg) { - if (isRoomForKeepAliveMessage(msg)) { - NetKeepAliveCommandMsg *cmdMsg = static_cast(msg->getCommand()); - - // If necessary, put the NetCommandType into the packet. - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandType; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getNetCommandType(); - m_packetLen += sizeof(UnsignedByte); - - m_lastCommandType = cmdMsg->getNetCommandType(); - } - - // If necessary, put the relay into the packet. - if (m_lastRelay != msg->getRelay()) { - m_packet[m_packetLen] = NetPacketFieldTypes::Relay; - ++m_packetLen; - UnsignedByte newRelay = msg->getRelay(); - memcpy(m_packet+m_packetLen, &newRelay, sizeof(UnsignedByte)); - m_packetLen += sizeof(UnsignedByte); - - m_lastRelay = newRelay; - } - - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - m_packet[m_packetLen] = NetPacketFieldTypes::PlayerId; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getPlayerID(); - m_packetLen += sizeof(UnsignedByte); - - m_lastPlayerID = cmdMsg->getPlayerID(); - } - - m_packet[m_packetLen] = NetPacketFieldTypes::Data; - ++m_packetLen; - - ++m_numCommands; - - deleteInstance(m_lastCommand); - m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand()); - m_lastCommand->setRelay(msg->getRelay()); - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("Added keep alive command to packet.")); - - return TRUE; - } - return FALSE; -} - -/** - * Returns true if there is room in the packet for this command. - */ -Bool NetPacket::isRoomForKeepAliveMessage(NetCommandRef *msg) { - Int len = 0; - NetKeepAliveCommandMsg *cmdMsg = static_cast(msg->getCommand()); - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - ++len; - len += sizeof(UnsignedByte); - } - if (m_lastRelay != msg->getRelay()) { - len += sizeof(UnsignedByte) + sizeof(UnsignedByte); - } - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - ++len; - len += sizeof(UnsignedByte); - } - - ++len; // for NetPacketFieldTypes::Data - if ((len + m_packetLen) > MAX_PACKET_SIZE) { - return FALSE; - } - return TRUE; -} - -/** - * Add a run ahead command to the packet. Returns true if successful. - */ -Bool NetPacket::addRunAheadCommand(NetCommandRef *msg) { - Bool needNewCommandID = FALSE; - if (isRoomForRunAheadMessage(msg)) { - NetRunAheadCommandMsg *cmdMsg = static_cast(msg->getCommand()); - //DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addRunAheadCommand - adding run ahead command")); - - // If necessary, put the NetCommandType into the packet. - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandType; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getNetCommandType(); - m_packetLen += sizeof(UnsignedByte); - - m_lastCommandType = cmdMsg->getNetCommandType(); - } - - // If necessary, put the relay into the packet. - if (m_lastRelay != msg->getRelay()) { - m_packet[m_packetLen] = NetPacketFieldTypes::Relay; - ++m_packetLen; - UnsignedByte newRelay = msg->getRelay(); - memcpy(m_packet+m_packetLen, &newRelay, sizeof(UnsignedByte)); - m_packetLen += sizeof(UnsignedByte); - - m_lastRelay = newRelay; - } - - // If necessary, put the execution frame into the packet. - if (m_lastFrame != cmdMsg->getExecutionFrame()) { - m_packet[m_packetLen] = NetPacketFieldTypes::Frame; - ++m_packetLen; - UnsignedInt newframe = cmdMsg->getExecutionFrame(); - memcpy(m_packet+m_packetLen, &newframe, sizeof(UnsignedInt)); - m_packetLen += sizeof(UnsignedInt); - - m_lastFrame = newframe; - } - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("relay = %d, ", m_lastRelay)); - - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - m_packet[m_packetLen] = NetPacketFieldTypes::PlayerId; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getPlayerID(); - m_packetLen += sizeof(UnsignedByte); - - m_lastPlayerID = cmdMsg->getPlayerID(); - needNewCommandID = TRUE; - } - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("player = %d", m_lastPlayerID)); - - // If necessary, specify the command ID of this command. - if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandId; - ++m_packetLen; - UnsignedShort newID = cmdMsg->getID(); - memcpy(m_packet + m_packetLen, &newID, sizeof(UnsignedShort)); - m_packetLen += sizeof(UnsignedShort); - } - m_lastCommandID = cmdMsg->getID(); - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("command id = %d", m_lastCommandID)); - - m_packet[m_packetLen] = NetPacketFieldTypes::Data; - ++m_packetLen; - UnsignedShort newRunAhead = cmdMsg->getRunAhead(); - memcpy(m_packet + m_packetLen, &newRunAhead, sizeof(UnsignedShort)); - m_packetLen += sizeof(UnsignedShort); - - UnsignedByte newFrameRate = cmdMsg->getFrameRate(); - memcpy(m_packet + m_packetLen, &newFrameRate, sizeof(UnsignedByte)); - m_packetLen += sizeof(UnsignedByte); - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket - added run ahead command, frame %d, player id %d command id %d", m_lastFrame, m_lastPlayerID, m_lastCommandID)); - - ++m_numCommands; - - deleteInstance(m_lastCommand); - m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand()); - m_lastCommand->setRelay(msg->getRelay()); - return TRUE; - } - return FALSE; -} - -/** - * Returns true if there is room for this run ahead command in this packet. - */ -Bool NetPacket::isRoomForRunAheadMessage(NetCommandRef *msg) { - Int len = 0; - Bool needNewCommandID = FALSE; - NetRunAheadCommandMsg *cmdMsg = static_cast(msg->getCommand()); - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - ++len; - len += sizeof(UnsignedByte); - } - if (m_lastRelay != msg->getRelay()) { - len += sizeof(UnsignedByte) + sizeof(UnsignedByte); - } - if (m_lastFrame != cmdMsg->getExecutionFrame()) { - len += sizeof(UnsignedInt) + sizeof(UnsignedByte); - } - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - ++len; - len += sizeof(UnsignedByte); - needNewCommandID = TRUE; - } - if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) { - len += sizeof(UnsignedShort) + sizeof(UnsignedByte); - } - - ++len; // for NetPacketFieldTypes::Data - len += sizeof(UnsignedShort); - len += sizeof(UnsignedByte); - if ((len + m_packetLen) > MAX_PACKET_SIZE) { - return FALSE; - } - return TRUE; -} - -/** - * Add a DestroyPlayer command to the packet. Returns true if successful. - */ -Bool NetPacket::addDestroyPlayerCommand(NetCommandRef *msg) { - Bool needNewCommandID = FALSE; - if (isRoomForDestroyPlayerMessage(msg)) { - NetDestroyPlayerCommandMsg *cmdMsg = static_cast(msg->getCommand()); - - // If necessary, put the NetCommandType into the packet. - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandType; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getNetCommandType(); - m_packetLen += sizeof(UnsignedByte); - - m_lastCommandType = cmdMsg->getNetCommandType(); - } - - // If necessary, put the relay into the packet. - if (m_lastRelay != msg->getRelay()) { - m_packet[m_packetLen] = NetPacketFieldTypes::Relay; - ++m_packetLen; - UnsignedByte newRelay = msg->getRelay(); - memcpy(m_packet+m_packetLen, &newRelay, sizeof(UnsignedByte)); - m_packetLen += sizeof(UnsignedByte); - - m_lastRelay = newRelay; - } - - // If necessary, put the execution frame into the packet. - if (m_lastFrame != cmdMsg->getExecutionFrame()) { - m_packet[m_packetLen] = NetPacketFieldTypes::Frame; - ++m_packetLen; - UnsignedInt newframe = cmdMsg->getExecutionFrame(); - memcpy(m_packet+m_packetLen, &newframe, sizeof(UnsignedInt)); - m_packetLen += sizeof(UnsignedInt); - - m_lastFrame = newframe; - } - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("relay = %d, ", m_lastRelay)); - - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - m_packet[m_packetLen] = NetPacketFieldTypes::PlayerId; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getPlayerID(); - m_packetLen += sizeof(UnsignedByte); - - m_lastPlayerID = cmdMsg->getPlayerID(); - needNewCommandID = TRUE; - } - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("player = %d", m_lastPlayerID)); - - // If necessary, specify the command ID of this command. - if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandId; - ++m_packetLen; - UnsignedShort newID = cmdMsg->getID(); - memcpy(m_packet + m_packetLen, &newID, sizeof(UnsignedShort)); - m_packetLen += sizeof(UnsignedShort); - } - m_lastCommandID = cmdMsg->getID(); - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("command id = %d", m_lastCommandID)); - - m_packet[m_packetLen] = NetPacketFieldTypes::Data; - ++m_packetLen; - UnsignedInt newVal = cmdMsg->getPlayerIndex(); - memcpy(m_packet + m_packetLen, &newVal, sizeof(UnsignedInt)); - m_packetLen += sizeof(UnsignedInt); - - //DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket - added CRC:0x%8.8X info command, frame %d, player id %d command id %d", newCRC, m_lastFrame, m_lastPlayerID, m_lastCommandID)); - - ++m_numCommands; - - deleteInstance(m_lastCommand); - m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand()); - m_lastCommand->setRelay(msg->getRelay()); - return TRUE; - } - return FALSE; -} - -/** - * Returns true if there is room for this DestroyPlayer command in this packet. - */ -Bool NetPacket::isRoomForDestroyPlayerMessage(NetCommandRef *msg) { - Int len = 0; - Bool needNewCommandID = FALSE; - NetDestroyPlayerCommandMsg *cmdMsg = static_cast(msg->getCommand()); - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - ++len; - len += sizeof(UnsignedByte); - } - if (m_lastRelay != msg->getRelay()) { - len += sizeof(UnsignedByte) + sizeof(UnsignedByte); - } - if (m_lastFrame != cmdMsg->getExecutionFrame()) { - len += sizeof(UnsignedInt) + sizeof(UnsignedByte); - } - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - ++len; - len += sizeof(UnsignedByte); - needNewCommandID = TRUE; - } - if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) { - len += sizeof(UnsignedShort) + sizeof(UnsignedByte); - } - - ++len; // for NetPacketFieldTypes::Data - len += sizeof(UnsignedInt); - if ((len + m_packetLen) > MAX_PACKET_SIZE) { - return FALSE; - } - return TRUE; -} - -/** - * Add a run ahead metrics command to the packet. Returns true if successful. - */ -Bool NetPacket::addRunAheadMetricsCommand(NetCommandRef *msg) { - Bool needNewCommandID = FALSE; - if (isRoomForRunAheadMetricsMessage(msg)) { - NetRunAheadMetricsCommandMsg *cmdMsg = static_cast(msg->getCommand()); -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addRunAheadMetricsCommand - adding run ahead metrics for player %d, fps = %d, latency = %f", cmdMsg->getPlayerID(), cmdMsg->getAverageFps(), cmdMsg->getAverageLatency())); - - // If necessary, put the NetCommandType into the packet. - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandType; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getNetCommandType(); - m_packetLen += sizeof(UnsignedByte); - - m_lastCommandType = cmdMsg->getNetCommandType(); - } - - // If necessary, put the relay into the packet. - if (m_lastRelay != msg->getRelay()) { - m_packet[m_packetLen] = NetPacketFieldTypes::Relay; - ++m_packetLen; - UnsignedByte newRelay = msg->getRelay(); - memcpy(m_packet+m_packetLen, &newRelay, sizeof(UnsignedByte)); - m_packetLen += sizeof(UnsignedByte); - - m_lastRelay = newRelay; - } - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("relay = %d, ", m_lastRelay)); - - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - m_packet[m_packetLen] = NetPacketFieldTypes::PlayerId; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getPlayerID(); - m_packetLen += sizeof(UnsignedByte); - - m_lastPlayerID = cmdMsg->getPlayerID(); - needNewCommandID = TRUE; - } - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("player = %d", m_lastPlayerID)); - - // If necessary, specify the command ID of this command. - if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandId; - ++m_packetLen; - UnsignedShort newID = cmdMsg->getID(); - memcpy(m_packet + m_packetLen, &newID, sizeof(UnsignedShort)); - m_packetLen += sizeof(UnsignedShort); - } - m_lastCommandID = cmdMsg->getID(); - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("command id = %d", m_lastCommandID)); - - m_packet[m_packetLen] = NetPacketFieldTypes::Data; - ++m_packetLen; - // write the average latency - Real averageLatency = cmdMsg->getAverageLatency(); - memcpy(m_packet + m_packetLen, &averageLatency, sizeof(averageLatency)); - m_packetLen += sizeof(averageLatency); - // write the average fps - UnsignedShort averageFps = (UnsignedShort)(cmdMsg->getAverageFps()); - memcpy(m_packet + m_packetLen, &averageFps, sizeof(averageFps)); - m_packetLen += sizeof(averageFps); - - deleteInstance(m_lastCommand); - m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand()); - m_lastCommand->setRelay(msg->getRelay()); - - ++m_numCommands; - return TRUE; - } - return FALSE; -} - -/** - * Returns true if there is enough room in the packet to fit this message. - */ -Bool NetPacket::isRoomForRunAheadMetricsMessage(NetCommandRef *msg) { - Int len = 0; - Bool needNewCommandID = FALSE; - NetRunAheadMetricsCommandMsg *cmdMsg = static_cast(msg->getCommand()); - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - ++len; - len += sizeof(UnsignedByte); - } - if (m_lastRelay != msg->getRelay()) { - len += sizeof(UnsignedByte) + sizeof(UnsignedByte); - } - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - ++len; - len += sizeof(UnsignedByte); - needNewCommandID = TRUE; - } - if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) { - len += sizeof(UnsignedShort) + sizeof(UnsignedByte); - } - - ++len; // for NetPacketFieldTypes::Data - len += sizeof(UnsignedShort); - len += sizeof(Real); - if ((len + m_packetLen) > MAX_PACKET_SIZE) { - return FALSE; - } - return TRUE; -} - - -/** - * Add a player leave command to the packet. Returns true if successful. - */ -Bool NetPacket::addPlayerLeaveCommand(NetCommandRef *msg) { - Bool needNewCommandID = FALSE; - if (isRoomForPlayerLeaveMessage(msg)) { - NetPlayerLeaveCommandMsg *cmdMsg = static_cast(msg->getCommand()); -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addPlayerLeaveCommand - adding player leave command for player %d", cmdMsg->getLeavingPlayerID())); - - // If necessary, put the NetCommandType into the packet. - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandType; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getNetCommandType(); - m_packetLen += sizeof(UnsignedByte); - - m_lastCommandType = cmdMsg->getNetCommandType(); - } - - // If necessary, put the relay into the packet. - if (m_lastRelay != msg->getRelay()) { - m_packet[m_packetLen] = NetPacketFieldTypes::Relay; - ++m_packetLen; - UnsignedByte newRelay = msg->getRelay(); - memcpy(m_packet+m_packetLen, &newRelay, sizeof(UnsignedByte)); - m_packetLen += sizeof(UnsignedByte); - - m_lastRelay = newRelay; - } - - // If necessary, put the execution frame into the packet. - if (m_lastFrame != cmdMsg->getExecutionFrame()) { - m_packet[m_packetLen] = NetPacketFieldTypes::Frame; - ++m_packetLen; - UnsignedInt newframe = cmdMsg->getExecutionFrame(); - memcpy(m_packet+m_packetLen, &newframe, sizeof(UnsignedInt)); - m_packetLen += sizeof(UnsignedInt); - - m_lastFrame = newframe; - } - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("relay = %d, ", m_lastRelay)); - - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - m_packet[m_packetLen] = NetPacketFieldTypes::PlayerId; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getPlayerID(); - m_packetLen += sizeof(UnsignedByte); - - m_lastPlayerID = cmdMsg->getPlayerID(); - needNewCommandID = TRUE; - } - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("player = %d", m_lastPlayerID)); - - // If necessary, specify the command ID of this command. - if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandId; - ++m_packetLen; - UnsignedShort newID = cmdMsg->getID(); - memcpy(m_packet + m_packetLen, &newID, sizeof(UnsignedShort)); - m_packetLen += sizeof(UnsignedShort); - } - m_lastCommandID = cmdMsg->getID(); - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("command id = %d", m_lastCommandID)); - - m_packet[m_packetLen] = NetPacketFieldTypes::Data; - ++m_packetLen; - UnsignedByte leavingPlayerID = cmdMsg->getLeavingPlayerID(); - memcpy(m_packet + m_packetLen, &leavingPlayerID, sizeof(UnsignedByte)); - m_packetLen += sizeof(UnsignedByte); - - deleteInstance(m_lastCommand); - m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand()); - m_lastCommand->setRelay(msg->getRelay()); - - ++m_numCommands; - return TRUE; - } - return FALSE; -} - -/** - * Returns true if there is enough room in the packet to fit this message. - */ -Bool NetPacket::isRoomForPlayerLeaveMessage(NetCommandRef *msg) { - Int len = 0; - Bool needNewCommandID = FALSE; - NetPlayerLeaveCommandMsg *cmdMsg = static_cast(msg->getCommand()); - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - ++len; - len += sizeof(UnsignedByte); - } - if (m_lastRelay != msg->getRelay()) { - len += sizeof(UnsignedByte) + sizeof(UnsignedByte); - } - if (m_lastFrame != cmdMsg->getExecutionFrame()) { - len += sizeof(UnsignedInt) + sizeof(UnsignedByte); - } - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - ++len; - len += sizeof(UnsignedByte); - needNewCommandID = TRUE; - } - if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) { - len += sizeof(UnsignedShort) + sizeof(UnsignedByte); - } - - ++len; // for NetPacketFieldTypes::Data - len += sizeof(UnsignedByte); - if ((len + m_packetLen) > MAX_PACKET_SIZE) { - return FALSE; - } - return TRUE; -} - -/** - * Add this frame command message. Returns true if successful. - */ -Bool NetPacket::addFrameCommand(NetCommandRef *msg) { - Bool needNewCommandID = FALSE; - if (isFrameRepeat(msg)) { - if (m_packetLen >= MAX_PACKET_SIZE) { - return FALSE; - } - m_packet[m_packetLen] = 'Z'; - ++m_packetLen; - m_lastCommandID = msg->getCommand()->getID(); - deleteInstance(m_lastCommand); - m_lastCommand = newInstance(NetCommandRef)(msg->getCommand()); - m_lastCommand->setRelay(msg->getRelay()); - ++m_lastFrame; // need this cause we're actually advancing to the next frame by adding this command. - ++m_numCommands; - // frameinfodebug -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("outgoing - added frame %d, player %d, command count = %d, command id = %d, repeat", m_lastFrame, m_lastPlayerID, 0, m_lastCommandID)); - return TRUE; - } - if (isRoomForFrameMessage(msg)) { - NetFrameCommandMsg *cmdMsg = static_cast(msg->getCommand()); -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addFrameCommand - adding frame command for frame %d, command count = %d, command id = %d", cmdMsg->getExecutionFrame(), cmdMsg->getCommandCount(), cmdMsg->getID())); - - // If necessary, put the NetCommandType into the packet. - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandType; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getNetCommandType(); - m_packetLen += sizeof(UnsignedByte); - - m_lastCommandType = cmdMsg->getNetCommandType(); - } - - // If necessary, put the execution frame into the packet. - if (m_lastFrame != cmdMsg->getExecutionFrame()) { - m_packet[m_packetLen] = NetPacketFieldTypes::Frame; - ++m_packetLen; - UnsignedInt newframe = cmdMsg->getExecutionFrame(); - memcpy(m_packet+m_packetLen, &newframe, sizeof(UnsignedInt)); - m_packetLen += sizeof(UnsignedInt); - - m_lastFrame = newframe; - } - - // If necessary, put the relay into the packet. - if (m_lastRelay != msg->getRelay()) { - m_packet[m_packetLen] = NetPacketFieldTypes::Relay; - ++m_packetLen; - UnsignedByte newRelay = msg->getRelay(); - memcpy(m_packet+m_packetLen, &newRelay, sizeof(UnsignedByte)); - m_packetLen += sizeof(UnsignedByte); - - m_lastRelay = newRelay; - } - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("relay = %d, ", m_lastRelay)); - - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - m_packet[m_packetLen] = NetPacketFieldTypes::PlayerId; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getPlayerID(); - m_packetLen += sizeof(UnsignedByte); - - m_lastPlayerID = cmdMsg->getPlayerID(); - needNewCommandID = TRUE; - } - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("player = %d", m_lastPlayerID)); - - // If necessary, specify the command ID of this command. - if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandId; - ++m_packetLen; - UnsignedShort newID = cmdMsg->getID(); - memcpy(m_packet + m_packetLen, &newID, sizeof(UnsignedShort)); - m_packetLen += sizeof(UnsignedShort); - } - m_lastCommandID = cmdMsg->getID(); - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("command id = %d", m_lastCommandID)); - - m_packet[m_packetLen] = NetPacketFieldTypes::Data; - ++m_packetLen; - UnsignedShort cmdCount = cmdMsg->getCommandCount(); - memcpy(m_packet + m_packetLen, &cmdCount, sizeof(UnsignedShort)); - m_packetLen += sizeof(UnsignedShort); - - // frameinfodebug -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("outgoing - added frame %d, player %d, command count = %d, command id = %d", cmdMsg->getExecutionFrame(), cmdMsg->getPlayerID(), cmdMsg->getCommandCount(), cmdMsg->getID())); - - deleteInstance(m_lastCommand); - m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand()); - m_lastCommand->setRelay(msg->getRelay()); - - ++m_numCommands; - return TRUE; - } - return FALSE; -} - -/** - * Returns true if there is enough room in this packet for this frame message. - */ -Bool NetPacket::isRoomForFrameMessage(NetCommandRef *msg) { - Int len = 0; - Bool needNewCommandID = FALSE; - NetFrameCommandMsg *cmdMsg = static_cast(msg->getCommand()); - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - ++len; - len += sizeof(UnsignedByte); - } - if (m_lastFrame != cmdMsg->getExecutionFrame()) { - len += sizeof(UnsignedInt) + sizeof(UnsignedByte); - } - if (m_lastRelay != msg->getRelay()) { - len += sizeof(UnsignedByte) + sizeof(UnsignedByte); - } - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - ++len; - len += sizeof(UnsignedByte); - needNewCommandID = TRUE; - } - if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) { - len += sizeof(UnsignedShort) + sizeof(UnsignedByte); - } - - ++len; // for NetPacketFieldTypes::Data - len += sizeof(UnsignedShort); - if ((len + m_packetLen) > MAX_PACKET_SIZE) { - return FALSE; - } - return TRUE; -} - -Bool NetPacket::isFrameRepeat(NetCommandRef *msg) { - if (m_lastCommand == nullptr) { - return FALSE; - } - if (m_lastCommand->getCommand()->getNetCommandType() != NETCOMMANDTYPE_FRAMEINFO) { - return FALSE; - } - NetFrameCommandMsg *framemsg = (NetFrameCommandMsg *)(msg->getCommand()); - NetFrameCommandMsg *lastmsg = (NetFrameCommandMsg *)(m_lastCommand->getCommand()); - if (framemsg->getCommandCount() != 0) { - return FALSE; - } - if (framemsg->getExecutionFrame() != (lastmsg->getExecutionFrame() + 1)) { - return FALSE; - } - if (msg->getRelay() != m_lastCommand->getRelay()) { - return FALSE; - } - if (framemsg->getID() != (lastmsg->getID() + 1)) { - return FALSE; - } - return TRUE; -} - -/** - * Add an ack "both" command. - */ -Bool NetPacket::addAckBothCommand(NetCommandRef *msg) { - NetAckBothCommandMsg *ackmsg = (NetAckBothCommandMsg *)(msg->getCommand()); - return addAckCommand(msg, ackmsg->getCommandID(), ackmsg->getOriginalPlayerID()); -} - -/** - * Add an ack stage 1 command. - */ -Bool NetPacket::addAckStage1Command(NetCommandRef *msg) { - NetAckStage1CommandMsg *ackmsg = (NetAckStage1CommandMsg *)(msg->getCommand()); - return addAckCommand(msg, ackmsg->getCommandID(), ackmsg->getOriginalPlayerID()); -} - -/** - * Add an ack stage 2 command. - */ -Bool NetPacket::addAckStage2Command(NetCommandRef *msg) { - NetAckStage2CommandMsg *ackmsg = (NetAckStage2CommandMsg *)(msg->getCommand()); - return addAckCommand(msg, ackmsg->getCommandID(), ackmsg->getOriginalPlayerID()); -} - -/** - * Add this ack command to the packet. Returns true if successful. - */ -Bool NetPacket::addAckCommand(NetCommandRef *msg, UnsignedShort commandID, UnsignedByte originalPlayerID) { - if (isAckRepeat(msg)) { - if (m_packetLen >= MAX_PACKET_SIZE) { - return FALSE; - } - m_packet[m_packetLen] = 'Z'; - ++m_packetLen; - ++m_numCommands; - - deleteInstance(m_lastCommand); - m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand()); - m_lastCommand->setRelay(msg->getRelay()); - return TRUE; - } - if (isRoomForAckMessage(msg)) { - NetCommandMsg *cmdMsg = msg->getCommand(); -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addAckCommand - adding ack for command %d for player %d", cmdMsg->getCommandID(), msg->getCommand()->getPlayerID())); - // If necessary, put the NetCommandType into the packet. - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandType; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getNetCommandType(); - m_packetLen += sizeof(UnsignedByte); - - m_lastCommandType = cmdMsg->getNetCommandType(); - } - - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - m_packet[m_packetLen] = NetPacketFieldTypes::PlayerId; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getPlayerID(); - m_packetLen += sizeof(UnsignedByte); - - m_lastPlayerID = cmdMsg->getPlayerID(); - } - - // Put in the command id of the command we are acking. - m_packet[m_packetLen] = NetPacketFieldTypes::Data; - ++m_packetLen; - memcpy(m_packet + m_packetLen, &commandID, sizeof(UnsignedShort)); - m_packetLen += sizeof(UnsignedShort); - memcpy(m_packet + m_packetLen, &originalPlayerID, sizeof(UnsignedByte)); - m_packetLen += sizeof(UnsignedByte); - - deleteInstance(m_lastCommand); - m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand()); - m_lastCommand->setRelay(msg->getRelay()); - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("outgoing - added ACK, original player %d, command id %d", origPlayerID, cmdID)); - ++m_numCommands; - return TRUE; - } - return FALSE; -} - -/** - * Returns true if there is enough room in the packet for this ack message. - */ -Bool NetPacket::isRoomForAckMessage(NetCommandRef *msg) { - Int len = 0; - NetCommandMsg *cmdMsg = msg->getCommand(); - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - ++len; - len += sizeof(UnsignedByte); - } - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - ++len; - len += sizeof(UnsignedByte); - } - - ++len; // for NetPacketFieldTypes::Data - len += sizeof(UnsignedShort); - len += sizeof(UnsignedByte); - if ((len + m_packetLen) > MAX_PACKET_SIZE) { + if (framemsg->getID() != (lastmsg->getID() + 1)) { return FALSE; } return TRUE; @@ -3623,274 +522,6 @@ Bool NetPacket::isAckStage2Repeat(NetCommandRef *msg) { return TRUE; } -/** - * Adds this game command to the packet. Returns true if successful. - */ -Bool NetPacket::addGameCommand(NetCommandRef *msg) { - Bool retval = FALSE; - NetGameCommandMsg *cmdMsg = (NetGameCommandMsg *)(msg->getCommand()); - // get the game message from the NetCommandMsg - GameMessage *gmsg = cmdMsg->constructGameMessage(); - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addGameCommand for command ID %d", cmdMsg->getID())); - - if (isRoomForGameMessage(msg, gmsg)) { - // Now we know there is enough room, put the new game message into the packet. - - Bool needNewCommandID = FALSE; // this is to allow us to force the starting command ID to be respecified with this command. - - // If necessary, put the NetCommandType into the packet. - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandType; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getNetCommandType(); - m_packetLen += sizeof(UnsignedByte); - - m_lastCommandType = cmdMsg->getNetCommandType(); - } - - // If necessary, put the execution frame into the packet. - if (m_lastFrame != cmdMsg->getExecutionFrame()) { - m_packet[m_packetLen] = NetPacketFieldTypes::Frame; - ++m_packetLen; - UnsignedInt newframe = cmdMsg->getExecutionFrame(); - memcpy(m_packet+m_packetLen, &newframe, sizeof(UnsignedInt)); - m_packetLen += sizeof(UnsignedInt); - - m_lastFrame = newframe; - } - - // If necessary, put the relay into the packet. - if (m_lastRelay != msg->getRelay()) { - m_packet[m_packetLen] = NetPacketFieldTypes::Relay; - ++m_packetLen; - UnsignedByte newRelay = msg->getRelay(); - memcpy(m_packet+m_packetLen, &newRelay, sizeof(UnsignedByte)); - m_packetLen += sizeof(UnsignedByte); - - m_lastRelay = newRelay; - } - - // If necessary, put the playerID into the packet. - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - m_packet[m_packetLen] = NetPacketFieldTypes::PlayerId; - ++m_packetLen; - m_packet[m_packetLen] = cmdMsg->getPlayerID(); - m_packetLen += sizeof(UnsignedByte); - //since if we have a new player we need to respecify the starting command ID, lets force the command id to be different. - needNewCommandID = TRUE; - - m_lastPlayerID = cmdMsg->getPlayerID(); - } - - // If necessary, specify the command ID of this command. - if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) { - m_packet[m_packetLen] = NetPacketFieldTypes::CommandId; - ++m_packetLen; - UnsignedShort newID = cmdMsg->getID(); - memcpy(m_packet + m_packetLen, &newID, sizeof(UnsignedShort)); - m_packetLen += sizeof(UnsignedShort); - } - m_lastCommandID = cmdMsg->getID(); - - m_packet[m_packetLen] = NetPacketFieldTypes::Data; - ++m_packetLen; - - // Now copy the GameMessage type into the packet. - GameMessage::Type newType = gmsg->getType(); - memcpy(m_packet + m_packetLen, &newType, sizeof(GameMessage::Type)); - m_packetLen += sizeof(GameMessage::Type); - - - GameMessageParser *parser = newInstance(GameMessageParser)(gmsg); - UnsignedByte numTypes = parser->getNumTypes(); - memcpy(m_packet + m_packetLen, &numTypes, sizeof(numTypes)); - m_packetLen += sizeof(numTypes); - - GameMessageParserArgumentType *argType = parser->getFirstArgumentType(); - while (argType != nullptr) { - UnsignedByte type = (UnsignedByte)(argType->getType()); - memcpy(m_packet + m_packetLen, &type, sizeof(type)); - m_packetLen += sizeof(type); - - UnsignedByte argTypeCount = argType->getArgCount(); - memcpy(m_packet + m_packetLen, &argTypeCount, sizeof(argTypeCount)); - m_packetLen += sizeof(argTypeCount); - - argType = argType->getNext(); - } - - Int numArgs = gmsg->getArgumentCount(); - for (Int i = 0; i < numArgs; ++i) { - GameMessageArgumentDataType type = gmsg->getArgumentDataType(i); - GameMessageArgumentType arg = *(gmsg->getArgument(i)); - writeGameMessageArgumentToPacket(type, arg); - } - - deleteInstance(parser); - parser = nullptr; - -// DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::addGameMessage - added game message, frame %d, player %d, command ID %d", m_lastFrame, m_lastPlayerID, m_lastCommandID)); - - ++m_numCommands; - - deleteInstance(m_lastCommand); - m_lastCommand = NEW_NETCOMMANDREF(msg->getCommand()); - m_lastCommand->setRelay(msg->getRelay()); - - retval = TRUE; - } - - deleteInstance(gmsg); - gmsg = nullptr; - - return retval; -} - -void NetPacket::writeGameMessageArgumentToPacket(GameMessageArgumentDataType type, GameMessageArgumentType arg) { - - switch(type) { - - case ARGUMENTDATATYPE_INTEGER: - memcpy(m_packet + m_packetLen, &(arg.integer), sizeof(arg.integer)); - m_packetLen += sizeof(arg.integer); - break; - case ARGUMENTDATATYPE_REAL: - memcpy(m_packet + m_packetLen, &(arg.real), sizeof(arg.real)); - m_packetLen += sizeof(arg.real); - break; - case ARGUMENTDATATYPE_BOOLEAN: - memcpy(m_packet + m_packetLen, &(arg.boolean), sizeof(arg.boolean)); - m_packetLen += sizeof(arg.boolean); - break; - case ARGUMENTDATATYPE_OBJECTID: - memcpy(m_packet + m_packetLen, &(arg.objectID), sizeof(arg.objectID)); - m_packetLen += sizeof(arg.objectID); - break; - case ARGUMENTDATATYPE_DRAWABLEID: - memcpy(m_packet + m_packetLen, &(arg.drawableID), sizeof(arg.drawableID)); - m_packetLen += sizeof(arg.drawableID); - break; - case ARGUMENTDATATYPE_TEAMID: - memcpy(m_packet + m_packetLen, &(arg.teamID), sizeof(arg.teamID)); - m_packetLen += sizeof(arg.teamID); - break; - case ARGUMENTDATATYPE_LOCATION: - memcpy(m_packet + m_packetLen, &(arg.location), sizeof(arg.location)); - m_packetLen += sizeof(arg.location); - break; - case ARGUMENTDATATYPE_PIXEL: - memcpy(m_packet + m_packetLen, &(arg.pixel), sizeof(arg.pixel)); - m_packetLen += sizeof(arg.pixel); - break; - case ARGUMENTDATATYPE_PIXELREGION: - memcpy(m_packet + m_packetLen, &(arg.pixelRegion), sizeof(arg.pixelRegion)); - m_packetLen += sizeof(arg.pixelRegion); - break; - case ARGUMENTDATATYPE_TIMESTAMP: - memcpy(m_packet + m_packetLen, &(arg.timestamp), sizeof(arg.timestamp)); - m_packetLen += sizeof(arg.timestamp); - break; - case ARGUMENTDATATYPE_WIDECHAR: - memcpy(m_packet + m_packetLen, &(arg.wChar), sizeof(arg.wChar)); - m_packetLen += sizeof(arg.wChar); - break; - - } -} - -/** - * Returns true if there is enough room in this packet for this message. - */ -Bool NetPacket::isRoomForGameMessage(NetCommandRef *msg, GameMessage *gmsg) { - // Calculate how much space the NetCommandMsg will take in this packet. - Int msglen = 0; - - NetGameCommandMsg *cmdMsg = (NetGameCommandMsg *)(msg->getCommand()); - - Bool needNewCommandID = FALSE; - - if (m_lastFrame != cmdMsg->getExecutionFrame()) { - msglen += sizeof(UnsignedInt) + sizeof(UnsignedByte); - } - if (m_lastPlayerID != cmdMsg->getPlayerID()) { - msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); - needNewCommandID = TRUE; - } - if (m_lastRelay != msg->getRelay()) { - msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); - } - if (m_lastCommandType != cmdMsg->getNetCommandType()) { - msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); - } - if (((m_lastCommandID + 1) != (UnsignedShort)(cmdMsg->getID())) || (needNewCommandID == TRUE)) { - msglen += sizeof(UnsignedShort) + sizeof(UnsignedByte); - } - - GameMessageParser *parser = newInstance(GameMessageParser)(gmsg); - - ++msglen; // for NetPacketFieldTypes::Data - msglen += sizeof(GameMessage::Type); - msglen += sizeof(UnsignedByte); -// Int numTypes = parser->getNumTypes(); - GameMessageParserArgumentType *arg = parser->getFirstArgumentType(); - while (arg != nullptr) { - msglen += sizeof(UnsignedByte); // argument type - msglen += sizeof(UnsignedByte); // argument count - GameMessageArgumentDataType type = arg->getType(); - - switch (type) { - - case ARGUMENTDATATYPE_INTEGER: - msglen += arg->getArgCount() * sizeof(Int); - break; - case ARGUMENTDATATYPE_REAL: - msglen += arg->getArgCount() * sizeof(Real); - break; - case ARGUMENTDATATYPE_BOOLEAN: - msglen += arg->getArgCount() * sizeof(Bool); - break; - case ARGUMENTDATATYPE_OBJECTID: - msglen += arg->getArgCount() * sizeof(ObjectID); - break; - case ARGUMENTDATATYPE_DRAWABLEID: - msglen += arg->getArgCount() * sizeof(DrawableID); - break; - case ARGUMENTDATATYPE_TEAMID: - msglen += arg->getArgCount() * sizeof(UnsignedInt); - break; - case ARGUMENTDATATYPE_LOCATION: - msglen += arg->getArgCount() * sizeof(Coord3D); - break; - case ARGUMENTDATATYPE_PIXEL: - msglen += arg->getArgCount() * sizeof(ICoord2D); - break; - case ARGUMENTDATATYPE_PIXELREGION: - msglen += arg->getArgCount() * sizeof(IRegion2D); - break; - case ARGUMENTDATATYPE_TIMESTAMP: - msglen += arg->getArgCount() * sizeof(UnsignedInt); - break; - case ARGUMENTDATATYPE_WIDECHAR: - msglen += arg->getArgCount() * sizeof(WideChar); - break; - - } - - arg = arg->getNext(); - - } - - deleteInstance(parser); - parser = nullptr; - - // Is there enough room in the packet for this message? - if (msglen > (MAX_PACKET_SIZE - m_packetLen)) { - return FALSE; // there isn't enough room in this packet for this new message. - } - return TRUE; -} - /** * Returns the list of commands that are in this packet. */ diff --git a/Core/GameEngine/Source/GameNetwork/NetPacketStructs.cpp b/Core/GameEngine/Source/GameNetwork/NetPacketStructs.cpp new file mode 100644 index 00000000000..5f42e3388c3 --- /dev/null +++ b/Core/GameEngine/Source/GameNetwork/NetPacketStructs.cpp @@ -0,0 +1,905 @@ +/* +** Command & Conquer Generals Zero Hour(tm) +** Copyright 2026 TheSuperHackers +** +** This program is free software: you can redistribute it and/or modify +** it under the terms of the GNU General Public License as published by +** the Free Software Foundation, either version 3 of the License, or +** (at your option) any later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program. If not, see . +*/ + +#include "PreRTS.h" +#include "GameNetwork/NetPacketStructs.h" + +#include "GameNetwork/GameMessageParser.h" +#include "GameNetwork/NetCommandRef.h" + +//////////////////////////////////////////////////////////////////////////////// + +size_t SmallNetPacketCommandBase::getSize(const SmallNetPacketCommandBaseSelect *select) +{ + size_t size = 0; + + if (select != nullptr) + { + if (select->useCommandType) + { + size += sizeof(NetPacketCommandTypeField); + } + if (select->useRelay) + { + size += sizeof(NetPacketRelayField); + } + if (select->useFrame) + { + size += sizeof(NetPacketFrameField); + } + if (select->usePlayerId) + { + size += sizeof(NetPacketPlayerIdField); + } + if (select->useCommandId) + { + size += sizeof(NetPacketCommandIdField); + } + size += sizeof(NetPacketDataField); + } + else + { + size += sizeof(SmallNetPacketCommandBase::CommandBase); + } + return size; +} + +size_t SmallNetPacketCommandBase::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref, const SmallNetPacketCommandBaseSelect *select) +{ + const NetCommandMsg *msg = ref.getCommand(); + CommandBase base; + base.commandType.commandType = msg->getNetCommandType(); + base.relay.relay = ref.getRelay(); + base.frame.frame = msg->getExecutionFrame(); + base.playerId.playerId = msg->getPlayerID(); + base.commandId.commandId = msg->getID(); + + size_t size = 0; + + if (select != nullptr) + { + // If necessary, put the command type into the packet. + if (select->useCommandType) + { + size += network::writePrimitive(buffer + size, base.commandType); + } + // If necessary, put the relay into the packet. + if (select->useRelay) + { + size += network::writePrimitive(buffer + size, base.relay); + } + // If necessary, put the execution frame into the packet. + if (select->useFrame) + { + size += network::writePrimitive(buffer + size, base.frame); + } + // If necessary, put the player ID into the packet. + if (select->usePlayerId) + { + size += network::writePrimitive(buffer + size, base.playerId); + } + // If necessary, put the command ID into the packet. + if (select->useCommandId) + { + size += network::writePrimitive(buffer + size, base.commandId); + } + // Always write the data header and mark the end of the command. + size += network::writePrimitive(buffer + size, base.dataHeader); + } + else + { + size += network::writeObject(buffer + size, base); + } + return size; +} + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketAckCommand +//////////////////////////////////////////////////////////////////////////////// + +size_t NetPacketAckCommandData::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const CommandMsg *cmdMsg = static_cast(ref.getCommand()); + FixedData data; + data.commandId = cmdMsg->getCommandID(); + data.originalPlayerId = cmdMsg->getOriginalPlayerID(); + + return network::writeObject(buffer, data); +} + +size_t NetPacketAckCommandBase::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const NetCommandMsg *msg = ref.getCommand(); + CommandBase base; + base.commandType.commandType = msg->getNetCommandType(); + //base.relay.relay = ref.getRelay(); + //base.frame.frame = msg->getExecutionFrame(); + base.playerId.playerId = msg->getPlayerID(); + //base.commandId.commandId = msg->getID(); + + return network::writeObject(buffer, base); +} + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketFrameCommand +//////////////////////////////////////////////////////////////////////////////// + +size_t NetPacketFrameCommandData::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const CommandMsg *cmdMsg = static_cast(ref.getCommand()); + FixedData data; + data.commandCount = cmdMsg->getCommandCount(); + + return network::writeObject(buffer, data); +} + +size_t NetPacketFrameCommandBase::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const NetCommandMsg *msg = ref.getCommand(); + CommandBase base; + base.commandType.commandType = msg->getNetCommandType(); + base.relay.relay = ref.getRelay(); + base.frame.frame = msg->getExecutionFrame(); + base.playerId.playerId = msg->getPlayerID(); + base.commandId.commandId = msg->getID(); + + return network::writeObject(buffer, base); +} + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketPlayerLeaveCommand +//////////////////////////////////////////////////////////////////////////////// + +size_t NetPacketPlayerLeaveCommandData::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const CommandMsg *cmdMsg = static_cast(ref.getCommand()); + FixedData data; + data.leavingPlayerId = cmdMsg->getLeavingPlayerID(); + + return network::writeObject(buffer, data); +} + +size_t NetPacketPlayerLeaveCommandBase::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const NetCommandMsg *msg = ref.getCommand(); + CommandBase base; + base.commandType.commandType = msg->getNetCommandType(); + base.relay.relay = ref.getRelay(); + base.frame.frame = msg->getExecutionFrame(); + base.playerId.playerId = msg->getPlayerID(); + base.commandId.commandId = msg->getID(); + + return network::writeObject(buffer, base); +} + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketRunAheadMetricsCommand +//////////////////////////////////////////////////////////////////////////////// + +size_t NetPacketRunAheadMetricsCommandData::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const CommandMsg *cmdMsg = static_cast(ref.getCommand()); + FixedData data; + data.averageLatency = cmdMsg->getAverageLatency(); + data.averageFps = cmdMsg->getAverageFps(); + + return network::writeObject(buffer, data); +} + +size_t NetPacketRunAheadMetricsCommandBase::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const NetCommandMsg *msg = ref.getCommand(); + CommandBase base; + base.commandType.commandType = msg->getNetCommandType(); + base.relay.relay = ref.getRelay(); + //base.frame.frame = msg->getExecutionFrame(); + base.playerId.playerId = msg->getPlayerID(); + base.commandId.commandId = msg->getID(); + + return network::writeObject(buffer, base); +} + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketRunAheadCommand +//////////////////////////////////////////////////////////////////////////////// + +size_t NetPacketRunAheadCommandData::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const CommandMsg *cmdMsg = static_cast(ref.getCommand()); + FixedData data; + data.runAhead = cmdMsg->getRunAhead(); + data.frameRate = cmdMsg->getFrameRate(); + + return network::writeObject(buffer, data); +} + +size_t NetPacketRunAheadCommandBase::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const NetCommandMsg *msg = ref.getCommand(); + CommandBase base; + base.commandType.commandType = msg->getNetCommandType(); + base.relay.relay = ref.getRelay(); + base.frame.frame = msg->getExecutionFrame(); + base.playerId.playerId = msg->getPlayerID(); + base.commandId.commandId = msg->getID(); + + return network::writeObject(buffer, base); +} + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketDestroyPlayerCommand +//////////////////////////////////////////////////////////////////////////////// + +size_t NetPacketDestroyPlayerCommandData::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const CommandMsg *cmdMsg = static_cast(ref.getCommand()); + FixedData data; + data.playerIndex = cmdMsg->getPlayerIndex(); + + return network::writeObject(buffer, data); +} + +size_t NetPacketDestroyPlayerCommandBase::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const NetCommandMsg *msg = ref.getCommand(); + CommandBase base; + base.commandType.commandType = msg->getNetCommandType(); + base.relay.relay = ref.getRelay(); + base.frame.frame = msg->getExecutionFrame(); + base.playerId.playerId = msg->getPlayerID(); + base.commandId.commandId = msg->getID(); + + return network::writeObject(buffer, base); +} + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketKeepAliveCommand +//////////////////////////////////////////////////////////////////////////////// + +size_t NetPacketKeepAliveCommandBase::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const NetCommandMsg *msg = ref.getCommand(); + CommandBase base; + base.commandType.commandType = msg->getNetCommandType(); + base.relay.relay = ref.getRelay(); + //base.frame.frame = msg->getExecutionFrame(); + base.playerId.playerId = msg->getPlayerID(); + //base.commandId.commandId = msg->getID(); + + return network::writeObject(buffer, base); +} + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketDisconnectKeepAliveCommand +//////////////////////////////////////////////////////////////////////////////// + +size_t NetPacketDisconnectKeepAliveCommandBase::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const NetCommandMsg *msg = ref.getCommand(); + CommandBase base; + base.commandType.commandType = msg->getNetCommandType(); + base.relay.relay = ref.getRelay(); + //base.frame.frame = msg->getExecutionFrame(); + base.playerId.playerId = msg->getPlayerID(); + //base.commandId.commandId = msg->getID(); + + return network::writeObject(buffer, base); +} + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketDisconnectPlayerCommand +//////////////////////////////////////////////////////////////////////////////// + +size_t NetPacketDisconnectPlayerCommandData::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const CommandMsg *cmdMsg = static_cast(ref.getCommand()); + FixedData data; + data.disconnectSlot = cmdMsg->getDisconnectSlot(); + data.disconnectFrame = cmdMsg->getDisconnectFrame(); + + return network::writeObject(buffer, data); +} + +size_t NetPacketDisconnectPlayerCommandBase::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const NetCommandMsg *msg = ref.getCommand(); + CommandBase base; + base.commandType.commandType = msg->getNetCommandType(); + base.relay.relay = ref.getRelay(); + //base.frame.frame = msg->getExecutionFrame(); + base.playerId.playerId = msg->getPlayerID(); + base.commandId.commandId = msg->getID(); + + return network::writeObject(buffer, base); +} + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketRouterQueryCommand +//////////////////////////////////////////////////////////////////////////////// + +size_t NetPacketRouterQueryCommandBase::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const NetCommandMsg *msg = ref.getCommand(); + CommandBase base; + base.commandType.commandType = msg->getNetCommandType(); + base.relay.relay = ref.getRelay(); + //base.frame.frame = msg->getExecutionFrame(); + base.playerId.playerId = msg->getPlayerID(); + //base.commandId.commandId = msg->getID(); + + return network::writeObject(buffer, base); +} + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketRouterAckCommand +//////////////////////////////////////////////////////////////////////////////// + +size_t NetPacketRouterAckCommandBase::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const NetCommandMsg *msg = ref.getCommand(); + CommandBase base; + base.commandType.commandType = msg->getNetCommandType(); + base.relay.relay = ref.getRelay(); + //base.frame.frame = msg->getExecutionFrame(); + base.playerId.playerId = msg->getPlayerID(); + //base.commandId.commandId = msg->getID(); + + return network::writeObject(buffer, base); +} + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketDisconnectVoteCommand +//////////////////////////////////////////////////////////////////////////////// + +size_t NetPacketDisconnectVoteCommandData::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const CommandMsg *cmdMsg = static_cast(ref.getCommand()); + FixedData data; + data.slot = cmdMsg->getSlot(); + data.voteFrame = cmdMsg->getVoteFrame(); + + return network::writeObject(buffer, data); +} + +size_t NetPacketDisconnectVoteCommandBase::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const NetCommandMsg *msg = ref.getCommand(); + CommandBase base; + base.commandType.commandType = msg->getNetCommandType(); + base.relay.relay = ref.getRelay(); + //base.frame.frame = msg->getExecutionFrame(); + base.playerId.playerId = msg->getPlayerID(); + base.commandId.commandId = msg->getID(); + + return network::writeObject(buffer, base); +} + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketChatCommand +//////////////////////////////////////////////////////////////////////////////// + +size_t NetPacketChatCommandData::getSize(const NetCommandMsg &msg) +{ + const CommandMsg *cmdMsg = static_cast(&msg); + const Int textLength = std::min(cmdMsg->getText().getLength(), 255); + + size_t size = 0; + size += sizeof(UnsignedByte); + size += textLength * sizeof(WideChar); + size += sizeof(Int); + return size; +} + +size_t NetPacketChatCommandData::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const CommandMsg *cmdMsg = static_cast(ref.getCommand()); + const Int textLength = std::min(cmdMsg->getText().getLength(), 255); + + size_t size = 0; + size += network::writePrimitive(buffer + size, (UnsignedByte)textLength); + size += network::writeStringWithoutNull(buffer + size, cmdMsg->getText(), textLength); + size += network::writePrimitive(buffer + size, (Int)cmdMsg->getPlayerMask()); + return size; +} + +size_t NetPacketChatCommandBase::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const NetCommandMsg *msg = ref.getCommand(); + CommandBase base; + base.commandType.commandType = msg->getNetCommandType(); + base.relay.relay = ref.getRelay(); + base.frame.frame = msg->getExecutionFrame(); + base.playerId.playerId = msg->getPlayerID(); + base.commandId.commandId = msg->getID(); + + return network::writeObject(buffer, base); +} + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketDisconnectChatCommand +//////////////////////////////////////////////////////////////////////////////// + +size_t NetPacketDisconnectChatCommandData::getSize(const NetCommandMsg &msg) +{ + const CommandMsg *cmdMsg = static_cast(&msg); + const Int textLength = std::min(cmdMsg->getText().getLength(), 255); + + size_t size = 0; + size += sizeof(UnsignedByte); + size += textLength * sizeof(WideChar); + return size; +} + +size_t NetPacketDisconnectChatCommandData::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const CommandMsg *cmdMsg = static_cast(ref.getCommand()); + const Int textLength = std::min(cmdMsg->getText().getLength(), 255); + + size_t size = 0; + size += network::writePrimitive(buffer + size, (UnsignedByte)textLength); + size += network::writeStringWithoutNull(buffer + size, cmdMsg->getText(), textLength); + return size; +} + +size_t NetPacketDisconnectChatCommandBase::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const NetCommandMsg *msg = ref.getCommand(); + CommandBase base; + base.commandType.commandType = msg->getNetCommandType(); + base.relay.relay = ref.getRelay(); + //base.frame.frame = msg->getExecutionFrame(); + base.playerId.playerId = msg->getPlayerID(); + //base.commandId.commandId = msg->getID(); + + return network::writeObject(buffer, base); +} + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketGameCommand +//////////////////////////////////////////////////////////////////////////////// + +size_t NetPacketGameCommandData::getSize(const NetCommandMsg &msg) +{ + const CommandMsg *cmdMsg = static_cast(&msg); + GameMessage *gmsg = cmdMsg->constructGameMessage(); + GameMessageParser *parser = newInstance(GameMessageParser)(gmsg); + + size_t size = 0; + + size += sizeof(Int); + size += sizeof(UnsignedByte); + + GameMessageParserArgumentType *arg = parser->getFirstArgumentType(); + while (arg != nullptr) + { + size += sizeof(UnsignedByte); // argument type + size += sizeof(UnsignedByte); // argument count + + const GameMessageArgumentDataType type = arg->getType(); + switch (type) + { + case ARGUMENTDATATYPE_INTEGER: + size += arg->getArgCount() * sizeof(Int); + break; + case ARGUMENTDATATYPE_REAL: + size += arg->getArgCount() * sizeof(Real); + break; + case ARGUMENTDATATYPE_BOOLEAN: + size += arg->getArgCount() * sizeof(Bool); + break; + case ARGUMENTDATATYPE_OBJECTID: + size += arg->getArgCount() * sizeof(ObjectID); + break; + case ARGUMENTDATATYPE_DRAWABLEID: + size += arg->getArgCount() * sizeof(DrawableID); + break; + case ARGUMENTDATATYPE_TEAMID: + size += arg->getArgCount() * sizeof(UnsignedInt); + break; + case ARGUMENTDATATYPE_LOCATION: + size += arg->getArgCount() * sizeof(Coord3D); + break; + case ARGUMENTDATATYPE_PIXEL: + size += arg->getArgCount() * sizeof(ICoord2D); + break; + case ARGUMENTDATATYPE_PIXELREGION: + size += arg->getArgCount() * sizeof(IRegion2D); + break; + case ARGUMENTDATATYPE_TIMESTAMP: + size += arg->getArgCount() * sizeof(UnsignedInt); + break; + case ARGUMENTDATATYPE_WIDECHAR: + size += arg->getArgCount() * sizeof(WideChar); + break; + } + arg = arg->getNext(); + } + + deleteInstance(parser); + deleteInstance(gmsg); + + return size; +} + +size_t NetPacketGameCommandData::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const CommandMsg *cmdMsg = static_cast(ref.getCommand()); + GameMessage *gmsg = cmdMsg->constructGameMessage(); + GameMessageParser *parser = newInstance(GameMessageParser)(gmsg); + + size_t size = 0; + + size += network::writePrimitive(buffer + size, (Int)gmsg->getType()); + size += network::writePrimitive(buffer + size, (UnsignedByte)parser->getNumTypes()); + + GameMessageParserArgumentType *argType = parser->getFirstArgumentType(); + while (argType != nullptr) + { + size += network::writePrimitive(buffer + size, (UnsignedByte)argType->getType()); + size += network::writePrimitive(buffer + size, (UnsignedByte)argType->getArgCount()); + argType = argType->getNext(); + } + + const Int numArgs = gmsg->getArgumentCount(); + for (Int i = 0; i < numArgs; ++i) + { + GameMessageArgumentDataType type = gmsg->getArgumentDataType(i); + GameMessageArgumentType arg = *gmsg->getArgument(i); + switch (type) + { + case ARGUMENTDATATYPE_INTEGER: + size += network::writePrimitive(buffer + size, arg.integer); + break; + case ARGUMENTDATATYPE_REAL: + size += network::writePrimitive(buffer + size, arg.real); + break; + case ARGUMENTDATATYPE_BOOLEAN: + size += network::writePrimitive(buffer + size, arg.boolean); + break; + case ARGUMENTDATATYPE_OBJECTID: + size += network::writePrimitive(buffer + size, arg.objectID); + break; + case ARGUMENTDATATYPE_DRAWABLEID: + size += network::writePrimitive(buffer + size, arg.drawableID); + break; + case ARGUMENTDATATYPE_TEAMID: + size += network::writePrimitive(buffer + size, arg.teamID); + break; + case ARGUMENTDATATYPE_LOCATION: + size += network::writePrimitive(buffer + size, arg.location); + break; + case ARGUMENTDATATYPE_PIXEL: + size += network::writePrimitive(buffer + size, arg.pixel); + break; + case ARGUMENTDATATYPE_PIXELREGION: + size += network::writePrimitive(buffer + size, arg.pixelRegion); + break; + case ARGUMENTDATATYPE_TIMESTAMP: + size += network::writePrimitive(buffer + size, arg.timestamp); + break; + case ARGUMENTDATATYPE_WIDECHAR: + size += network::writePrimitive(buffer + size, arg.wChar); + break; + } + } + + deleteInstance(parser); + deleteInstance(gmsg); + + return size; +} + +size_t NetPacketGameCommandBase::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const NetCommandMsg *msg = ref.getCommand(); + CommandBase base; + base.commandType.commandType = msg->getNetCommandType(); + base.relay.relay = ref.getRelay(); + base.frame.frame = msg->getExecutionFrame(); + base.playerId.playerId = msg->getPlayerID(); + base.commandId.commandId = msg->getID(); + + return network::writeObject(buffer, base); +} + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketWrapperCommand +//////////////////////////////////////////////////////////////////////////////// + +size_t NetPacketWrapperCommandData::getSize(const NetCommandMsg &msg) +{ + const CommandMsg *cmdMsg = static_cast(&msg); + + size_t size = 0; + size += sizeof(FixedData); + size += cmdMsg->getDataLength(); + return size; +} + +size_t NetPacketWrapperCommandData::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const CommandMsg *cmdMsg = static_cast(ref.getCommand()); + FixedData data; + data.wrappedCommandId = cmdMsg->getWrappedCommandID(); + data.chunkNumber = cmdMsg->getChunkNumber(); + data.numChunks = cmdMsg->getNumChunks(); + data.totalDataLength = cmdMsg->getTotalDataLength(); + data.dataLength = cmdMsg->getDataLength(); + data.dataOffset = cmdMsg->getDataOffset(); + + size_t size = 0; + size += network::writeObject(buffer + size, data); + size += network::writeBytes(buffer + size, cmdMsg->getData(), cmdMsg->getDataLength()); + return size; +} + +size_t NetPacketWrapperCommandBase::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const NetCommandMsg *msg = ref.getCommand(); + CommandBase base; + base.commandType.commandType = msg->getNetCommandType(); + base.relay.relay = ref.getRelay(); + //base.frame.frame = msg->getExecutionFrame(); + base.playerId.playerId = msg->getPlayerID(); + base.commandId.commandId = msg->getID(); + + return network::writeObject(buffer, base); +} + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketFileCommand +//////////////////////////////////////////////////////////////////////////////// + +size_t NetPacketFileCommandData::getSize(const NetCommandMsg &msg) +{ + const CommandMsg *cmdMsg = static_cast(&msg); + + size_t size = 0; + size += cmdMsg->getPortableFilename().getByteCount() + 1; + size += sizeof(UnsignedInt); + size += cmdMsg->getFileLength(); + return size; +} + +size_t NetPacketFileCommandData::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const CommandMsg *cmdMsg = static_cast(ref.getCommand()); + + size_t size = 0; + size += network::writeStringWithNull(buffer + size, cmdMsg->getPortableFilename()); + size += network::writePrimitive(buffer + size, (UnsignedInt)cmdMsg->getFileLength()); + size += network::writeBytes(buffer + size, cmdMsg->getFileData(), cmdMsg->getFileLength()); + return size; +} + +size_t NetPacketFileCommandBase::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const NetCommandMsg *msg = ref.getCommand(); + CommandBase base; + base.commandType.commandType = msg->getNetCommandType(); + base.relay.relay = ref.getRelay(); + //base.frame.frame = msg->getExecutionFrame(); + base.playerId.playerId = msg->getPlayerID(); + base.commandId.commandId = msg->getID(); + + return network::writeObject(buffer, base); +} + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketFileAnnounceCommand +//////////////////////////////////////////////////////////////////////////////// + +size_t NetPacketFileAnnounceCommandData::getSize(const NetCommandMsg &msg) +{ + const CommandMsg *cmdMsg = static_cast(&msg); + + size_t size = 0; + size += cmdMsg->getPortableFilename().getByteCount() + 1; + size += sizeof(UnsignedShort); + size += sizeof(UnsignedByte); + return size; +} + +size_t NetPacketFileAnnounceCommandData::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const CommandMsg *cmdMsg = static_cast(ref.getCommand()); + + size_t size = 0; + size += network::writeStringWithNull(buffer + size, cmdMsg->getPortableFilename()); + size += network::writePrimitive(buffer + size, (UnsignedShort)cmdMsg->getFileID()); + size += network::writePrimitive(buffer + size, (UnsignedByte)cmdMsg->getPlayerMask()); + return size; +} + +size_t NetPacketFileAnnounceCommandBase::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const NetCommandMsg *msg = ref.getCommand(); + CommandBase base; + base.commandType.commandType = msg->getNetCommandType(); + base.relay.relay = ref.getRelay(); + //base.frame.frame = msg->getExecutionFrame(); + base.playerId.playerId = msg->getPlayerID(); + base.commandId.commandId = msg->getID(); + + return network::writeObject(buffer, base); +} + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketFileProgressCommand +//////////////////////////////////////////////////////////////////////////////// + +size_t NetPacketFileProgressCommandData::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const CommandMsg *cmdMsg = static_cast(ref.getCommand()); + FixedData data; + data.fileId = cmdMsg->getFileID(); + data.progress = cmdMsg->getProgress(); + + return network::writeObject(buffer, data); +} + +size_t NetPacketFileProgressCommandBase::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const NetCommandMsg *msg = ref.getCommand(); + CommandBase base; + base.commandType.commandType = msg->getNetCommandType(); + base.relay.relay = ref.getRelay(); + //base.frame.frame = msg->getExecutionFrame(); + base.playerId.playerId = msg->getPlayerID(); + base.commandId.commandId = msg->getID(); + + return network::writeObject(buffer, base); +} + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketProgressCommand +//////////////////////////////////////////////////////////////////////////////// + +size_t NetPacketProgressCommandData::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const CommandMsg *cmdMsg = static_cast(ref.getCommand()); + FixedData data; + data.percentage = cmdMsg->getPercentage(); + + return network::writeObject(buffer, data); +} + +size_t NetPacketProgressCommandBase::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const NetCommandMsg *msg = ref.getCommand(); + CommandBase base; + base.commandType.commandType = msg->getNetCommandType(); + base.relay.relay = ref.getRelay(); + //base.frame.frame = msg->getExecutionFrame(); + base.playerId.playerId = msg->getPlayerID(); + //base.commandId.commandId = msg->getID(); + + return network::writeObject(buffer, base); +} + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketLoadCompleteCommand +//////////////////////////////////////////////////////////////////////////////// + +size_t NetPacketLoadCompleteCommandBase::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const NetCommandMsg *msg = ref.getCommand(); + CommandBase base; + base.commandType.commandType = msg->getNetCommandType(); + base.relay.relay = ref.getRelay(); + //base.frame.frame = msg->getExecutionFrame(); + base.playerId.playerId = msg->getPlayerID(); + base.commandId.commandId = msg->getID(); + + return network::writeObject(buffer, base); +} + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketTimeOutGameStartCommand +//////////////////////////////////////////////////////////////////////////////// + +size_t NetPacketTimeOutGameStartCommandBase::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const NetCommandMsg *msg = ref.getCommand(); + CommandBase base; + base.commandType.commandType = msg->getNetCommandType(); + base.relay.relay = ref.getRelay(); + //base.frame.frame = msg->getExecutionFrame(); + base.playerId.playerId = msg->getPlayerID(); + base.commandId.commandId = msg->getID(); + + return network::writeObject(buffer, base); +} + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketDisconnectFrameCommand +//////////////////////////////////////////////////////////////////////////////// + +size_t NetPacketDisconnectFrameCommandData::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const CommandMsg *cmdMsg = static_cast(ref.getCommand()); + FixedData data; + data.disconnectFrame = cmdMsg->getDisconnectFrame(); + + return network::writeObject(buffer, data); +} + +size_t NetPacketDisconnectFrameCommandBase::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const NetCommandMsg *msg = ref.getCommand(); + CommandBase base; + base.commandType.commandType = msg->getNetCommandType(); + base.relay.relay = ref.getRelay(); + //base.frame.frame = msg->getExecutionFrame(); + base.playerId.playerId = msg->getPlayerID(); + base.commandId.commandId = msg->getID(); + + return network::writeObject(buffer, base); +} + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketDisconnectScreenOffCommand +//////////////////////////////////////////////////////////////////////////////// + +size_t NetPacketDisconnectScreenOffCommandData::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const CommandMsg *cmdMsg = static_cast(ref.getCommand()); + FixedData data; + data.newFrame = cmdMsg->getNewFrame(); + + return network::writeObject(buffer, data); +} + +size_t NetPacketDisconnectScreenOffCommandBase::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const NetCommandMsg *msg = ref.getCommand(); + CommandBase base; + base.commandType.commandType = msg->getNetCommandType(); + base.relay.relay = ref.getRelay(); + //base.frame.frame = msg->getExecutionFrame(); + base.playerId.playerId = msg->getPlayerID(); + base.commandId.commandId = msg->getID(); + + return network::writeObject(buffer, base); +} + +//////////////////////////////////////////////////////////////////////////////// +// NetPacketFrameResendRequestCommand +//////////////////////////////////////////////////////////////////////////////// + +size_t NetPacketFrameResendRequestCommandData::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const CommandMsg *cmdMsg = static_cast(ref.getCommand()); + FixedData data; + data.frameToResend = cmdMsg->getFrameToResend(); + + return network::writeObject(buffer, data); +} + +size_t NetPacketFrameResendRequestCommandBase::copyBytes(UnsignedByte *buffer, const NetCommandRef &ref) +{ + const NetCommandMsg *msg = ref.getCommand(); + CommandBase base; + base.commandType.commandType = msg->getNetCommandType(); + base.relay.relay = ref.getRelay(); + //base.frame.frame = msg->getExecutionFrame(); + base.playerId.playerId = msg->getPlayerID(); + base.commandId.commandId = msg->getID(); + + return network::writeObject(buffer, base); +}