NetworkMessage.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #include "NetworkMessage.h"
  2. #include "Chunk.h"
  3. #include "Entity.h"
  4. #include "Game.h"
  5. NetworkMessage::NetworkMessage()
  6. : Framework::ReferenceCounter()
  7. {
  8. address = 0;
  9. addressLength = 0;
  10. broadcast = 0;
  11. message = 0;
  12. msgLength = 0;
  13. useBackground = 0;
  14. }
  15. NetworkMessage::~NetworkMessage()
  16. {
  17. delete[] message;
  18. delete[] address;
  19. }
  20. void NetworkMessage::addressChunck(const Chunk* zChunk)
  21. {
  22. delete[] address;
  23. addressLength = 14;
  24. address = new char[addressLength];
  25. address[0] = 1; // dimension response
  26. *(int*)(address + 1) = zChunk->getDimensionId();
  27. address[5] = 1; // chunck
  28. Framework::Punkt center = zChunk->getCenter();
  29. *(int*)(address + 6) = center.x;
  30. *(int*)(address + 10) = center.y;
  31. }
  32. void NetworkMessage::addressEntity(const Entity* zEntity)
  33. {
  34. delete[] address;
  35. addressLength = 10;
  36. address = new char[addressLength];
  37. address[0] = 1; // dimension response
  38. *(int*)(address + 1) = zEntity->getCurrentDimensionId();
  39. address[5] = 2; // entity
  40. *(int*)(address + 6) = zEntity->getId();
  41. }
  42. void NetworkMessage::addressDimension(const Dimension *zDim)
  43. {
  44. delete[] address;
  45. addressLength = 5;
  46. address = new char[5];
  47. address[0] = 1; // dimension response
  48. *(int*)(address + 1) = zDim->getDimensionId();
  49. }
  50. void NetworkMessage::addressBlock(const Block* zBlock)
  51. {
  52. delete[] address;
  53. addressLength = 18;
  54. address = new char[addressLength];
  55. address[0] = 1; // dimension response
  56. *(int*)(address + 1) = zBlock->getDimensionId();
  57. address[5] = 3; // block
  58. Framework::Vec3<int> pos = zBlock->getPos();
  59. *(int*)(address + 6) = pos.x;
  60. *(int*)(address + 10) = pos.y;
  61. *(int*)(address + 14) = pos.z;
  62. }
  63. void NetworkMessage::openDialog(Framework::Text dialogName)
  64. {
  65. delete[] address;
  66. addressLength = (char)(4 + dialogName.getLength());
  67. address = new char[addressLength];
  68. address[0] = 2; // gui message
  69. address[1] = 0; // open dialog
  70. *(short*)(address + 2) = (short)dialogName.getLength(); // block
  71. memcpy(address + 4, dialogName.getText(), dialogName.getLength());
  72. }
  73. void NetworkMessage::addressGui(Framework::Text elementId)
  74. {
  75. delete[] address;
  76. addressLength = (char)(4 + elementId.getLength());
  77. address = new char[addressLength];
  78. address[0] = 2; // gui message
  79. address[1] = 1; // element message
  80. *(short*)(address + 2) = (short)elementId.getLength(); // block
  81. memcpy(address + 4, elementId.getText(), elementId.getLength());
  82. }
  83. void NetworkMessage::setMessage(char* msg, int length)
  84. {
  85. delete[] message;
  86. message = msg;
  87. msgLength = length;
  88. }
  89. void NetworkMessage::sendChatMessage(ChatMessage* zMsg)
  90. {
  91. delete[] address;
  92. addressLength = 1;
  93. address = new char[1];
  94. address[0] = 4; // chat message
  95. InMemoryBuffer buffer;
  96. zMsg->writeTo(&buffer);
  97. char* msg = new char[buffer.getSize()];
  98. buffer.lese(msg, (int)buffer.getSize());
  99. setMessage(msg, (int)buffer.getSize());
  100. }
  101. void NetworkMessage::sendChatOptions(ChatObserver* zOptions)
  102. {
  103. delete[] address;
  104. addressLength = 1;
  105. address = new char[1];
  106. address[0] = 5; // chat options
  107. InMemoryBuffer buffer;
  108. zOptions->writeTo(&buffer);
  109. char* msg = new char[buffer.getSize()];
  110. buffer.lese(msg, (int)buffer.getSize());
  111. setMessage(msg, (int)buffer.getSize());
  112. }
  113. void NetworkMessage::sendMap(ChunkMap* zMap)
  114. {
  115. delete[] address;
  116. addressLength = 1;
  117. address = new char[1];
  118. address[0] = 6; // map data
  119. InMemoryBuffer buffer;
  120. zMap->writeTo(&buffer);
  121. char* msg = new char[buffer.getSize()];
  122. buffer.lese(msg, (int)buffer.getSize());
  123. setMessage(msg, (int)buffer.getSize());
  124. }
  125. void NetworkMessage::setUseBackground()
  126. {
  127. useBackground = 1;
  128. }
  129. void NetworkMessage::sendToAll()
  130. {
  131. broadcast = true;
  132. }
  133. void NetworkMessage::writeTo(Framework::StreamWriter* zWriter) const
  134. {
  135. int total = msgLength + addressLength;
  136. if (total)
  137. {
  138. zWriter->schreibe((char*)&GameClient::Message::API_MESSAGE, 1);
  139. zWriter->schreibe((char*)&total, 4);
  140. zWriter->schreibe(address, addressLength);
  141. zWriter->schreibe(message, msgLength);
  142. }
  143. }
  144. bool NetworkMessage::isBroadcast() const
  145. {
  146. return broadcast;
  147. }
  148. bool NetworkMessage::isEmpty() const
  149. {
  150. return msgLength + addressLength <= 0;
  151. }
  152. bool NetworkMessage::isUseBackground() const
  153. {
  154. return useBackground;
  155. }
  156. NetworkMessage* NetworkMessage::clone() const
  157. {
  158. NetworkMessage* result = new NetworkMessage();
  159. result->message = new char[(int)msgLength];
  160. memcpy(result->message, message, (int)msgLength);
  161. result->msgLength = msgLength;
  162. result->address = new char[(int)addressLength];
  163. memcpy(result->address, address, (int)addressLength);
  164. result->addressLength = addressLength;
  165. result->broadcast = broadcast;
  166. result->useBackground = useBackground;
  167. return result;
  168. }