NetworkMessage.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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::setUseBackground()
  114. {
  115. useBackground = 1;
  116. }
  117. void NetworkMessage::sendToAll()
  118. {
  119. broadcast = true;
  120. }
  121. void NetworkMessage::writeTo(Framework::StreamWriter* zWriter) const
  122. {
  123. int total = msgLength + addressLength;
  124. if (total)
  125. {
  126. zWriter->schreibe((char*)&GameClient::Message::API_MESSAGE, 1);
  127. zWriter->schreibe((char*)&total, 4);
  128. zWriter->schreibe(address, addressLength);
  129. zWriter->schreibe(message, msgLength);
  130. }
  131. }
  132. bool NetworkMessage::isBroadcast() const
  133. {
  134. return broadcast;
  135. }
  136. bool NetworkMessage::isEmpty() const
  137. {
  138. return msgLength + addressLength <= 0;
  139. }
  140. bool NetworkMessage::isUseBackground() const
  141. {
  142. return useBackground;
  143. }
  144. NetworkMessage* NetworkMessage::clone() const
  145. {
  146. NetworkMessage* result = new NetworkMessage();
  147. result->message = new char[(int)msgLength];
  148. memcpy(result->message, message, (int)msgLength);
  149. result->msgLength = msgLength;
  150. result->address = new char[(int)addressLength];
  151. memcpy(result->address, address, (int)addressLength);
  152. result->addressLength = addressLength;
  153. result->broadcast = broadcast;
  154. result->useBackground = useBackground;
  155. return result;
  156. }