NetCommandWrapperList.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. ////// NetCommandWrapperList.cpp ////////////////////////////////
  24. // Bryan Cleveland
  25. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  26. #include "GameNetwork/NetCommandWrapperList.h"
  27. #include "GameNetwork/NetPacket.h"
  28. ////////////////////////////////////////////////////////////////////////////////////////////////////
  29. ////// NetCommandWrapperListNode ///////////////////////////////////////////////////////////////////
  30. ////////////////////////////////////////////////////////////////////////////////////////////////////
  31. NetCommandWrapperListNode::NetCommandWrapperListNode(NetWrapperCommandMsg *msg)
  32. {
  33. //Added By Sadullah Nader
  34. //Initializations inserted
  35. m_next = NULL;
  36. //
  37. m_numChunks = msg->getNumChunks();
  38. m_chunksPresent = NEW Bool[m_numChunks]; // pool[]ify
  39. m_numChunksPresent = 0;
  40. for (Int i = 0; i < m_numChunks; ++i) {
  41. m_chunksPresent[i] = FALSE;
  42. }
  43. m_dataLength = msg->getTotalDataLength();
  44. m_data = NEW UnsignedByte[m_dataLength]; // pool[]ify
  45. m_commandID = msg->getWrappedCommandID();
  46. }
  47. NetCommandWrapperListNode::~NetCommandWrapperListNode() {
  48. if (m_chunksPresent != NULL) {
  49. delete[] m_chunksPresent;
  50. m_chunksPresent = NULL;
  51. }
  52. if (m_data != NULL) {
  53. delete[] m_data;
  54. m_data = NULL;
  55. }
  56. }
  57. Bool NetCommandWrapperListNode::isComplete() {
  58. return m_numChunksPresent == m_numChunks;
  59. }
  60. Int NetCommandWrapperListNode::getPercentComplete(void) {
  61. if (isComplete())
  62. return 100;
  63. else
  64. return min(99, REAL_TO_INT( ((Real)m_numChunksPresent)/((Real)m_numChunks)*100.0f ));
  65. }
  66. UnsignedShort NetCommandWrapperListNode::getCommandID() {
  67. return m_commandID;
  68. }
  69. UnsignedInt NetCommandWrapperListNode::getRawDataLength() {
  70. return m_dataLength;
  71. }
  72. void NetCommandWrapperListNode::copyChunkData(NetWrapperCommandMsg *msg) {
  73. if (msg == NULL) {
  74. DEBUG_CRASH(("Trying to copy data from a non-existent wrapper command message"));
  75. return;
  76. }
  77. DEBUG_ASSERTCRASH(msg->getChunkNumber() < m_numChunks, ("MunkeeChunk %d of %d\n",
  78. msg->getChunkNumber(), m_numChunks));
  79. if (msg->getChunkNumber() >= m_numChunks)
  80. return;
  81. DEBUG_LOG(("NetCommandWrapperListNode::copyChunkData() - copying chunk %d\n",
  82. msg->getChunkNumber()));
  83. if (m_chunksPresent[msg->getChunkNumber()] == TRUE) {
  84. // we already received this chunk, no need to recopy it.
  85. return;
  86. }
  87. m_chunksPresent[msg->getChunkNumber()] = TRUE;
  88. UnsignedInt offset = msg->getDataOffset();
  89. memcpy(m_data + offset, msg->getData(), msg->getDataLength());
  90. ++m_numChunksPresent;
  91. }
  92. UnsignedByte * NetCommandWrapperListNode::getRawData() {
  93. return m_data;
  94. }
  95. ////////////////////////////////////////////////////////////////////////////////////////////////////
  96. ////// NetCommandWrapperList ///////////////////////////////////////////////////////////////////////
  97. ////////////////////////////////////////////////////////////////////////////////////////////////////
  98. NetCommandWrapperList::NetCommandWrapperList() {
  99. m_list = NULL;
  100. }
  101. NetCommandWrapperList::~NetCommandWrapperList() {
  102. NetCommandWrapperListNode *temp;
  103. while (m_list != NULL) {
  104. temp = m_list->m_next;
  105. m_list->deleteInstance();
  106. m_list = temp;
  107. }
  108. }
  109. void NetCommandWrapperList::init() {
  110. m_list = NULL;
  111. }
  112. void NetCommandWrapperList::reset() {
  113. NetCommandWrapperListNode *temp;
  114. while (m_list != NULL) {
  115. temp = m_list->m_next;
  116. m_list->deleteInstance();
  117. m_list = temp;
  118. }
  119. }
  120. Int NetCommandWrapperList::getPercentComplete(UnsignedShort wrappedCommandID)
  121. {
  122. NetCommandWrapperListNode *temp = m_list;
  123. while ((temp != NULL) && (temp->getCommandID() != wrappedCommandID)) {
  124. temp = temp->m_next;
  125. }
  126. if (!temp)
  127. return 0;
  128. return temp->getPercentComplete();
  129. }
  130. void NetCommandWrapperList::processWrapper(NetCommandRef *ref) {
  131. NetCommandWrapperListNode *temp = m_list;
  132. NetWrapperCommandMsg *msg = (NetWrapperCommandMsg *)(ref->getCommand());
  133. while ((temp != NULL) && (temp->getCommandID() != msg->getWrappedCommandID())) {
  134. temp = temp->m_next;
  135. }
  136. if (temp == NULL) {
  137. temp = newInstance(NetCommandWrapperListNode)(msg);
  138. temp->m_next = m_list;
  139. m_list = temp;
  140. }
  141. temp->copyChunkData(msg);
  142. }
  143. NetCommandList * NetCommandWrapperList::getReadyCommands()
  144. {
  145. NetCommandList *retlist = newInstance(NetCommandList);
  146. retlist->init();
  147. NetCommandWrapperListNode *temp = m_list;
  148. NetCommandWrapperListNode *next = NULL;
  149. while (temp != NULL) {
  150. next = temp->m_next;
  151. if (temp->isComplete()) {
  152. NetCommandRef *msg = NetPacket::ConstructNetCommandMsgFromRawData(temp->getRawData(), temp->getRawDataLength());
  153. NetCommandRef *ret = retlist->addMessage(msg->getCommand());
  154. ret->setRelay(msg->getRelay());
  155. msg->deleteInstance();
  156. msg = NULL;
  157. removeFromList(temp);
  158. temp = NULL;
  159. }
  160. temp = next;
  161. }
  162. return retlist;
  163. }
  164. void NetCommandWrapperList::removeFromList(NetCommandWrapperListNode *node) {
  165. if (node == NULL) {
  166. return;
  167. }
  168. NetCommandWrapperListNode *temp = m_list;
  169. NetCommandWrapperListNode *prev = NULL;
  170. while ((temp != NULL) && (temp->getCommandID() != node->getCommandID())) {
  171. prev = temp;
  172. temp = temp->m_next;
  173. }
  174. if (temp == NULL) {
  175. return;
  176. }
  177. if (prev == NULL) {
  178. m_list = temp->m_next;
  179. temp->deleteInstance();
  180. temp = NULL;
  181. } else {
  182. prev->m_next = temp->m_next;
  183. temp->deleteInstance();
  184. temp = NULL;
  185. }
  186. }