NetworkMessage.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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::setUseBackground()
  90. {
  91. useBackground = 1;
  92. }
  93. void NetworkMessage::sendToAll()
  94. {
  95. broadcast = true;
  96. }
  97. void NetworkMessage::writeTo(Framework::StreamWriter* zWriter) const
  98. {
  99. int total = msgLength + addressLength;
  100. if (total)
  101. {
  102. zWriter->schreibe((char*)&GameClient::Message::API_MESSAGE, 1);
  103. zWriter->schreibe((char*)&total, 4);
  104. zWriter->schreibe(address, addressLength);
  105. zWriter->schreibe(message, msgLength);
  106. }
  107. }
  108. bool NetworkMessage::isBroadcast() const
  109. {
  110. return broadcast;
  111. }
  112. bool NetworkMessage::isEmpty() const
  113. {
  114. return msgLength + addressLength <= 0;
  115. }
  116. bool NetworkMessage::isUseBackground() const
  117. {
  118. return useBackground;
  119. }
  120. NetworkMessage* NetworkMessage::clone() const
  121. {
  122. NetworkMessage* result = new NetworkMessage();
  123. result->message = new char[(int)msgLength];
  124. memcpy(result->message, message, (int)msgLength);
  125. result->msgLength = msgLength;
  126. result->address = new char[(int)addressLength];
  127. memcpy(result->address, address, (int)addressLength);
  128. result->addressLength = addressLength;
  129. result->broadcast = broadcast;
  130. result->useBackground = useBackground;
  131. return result;
  132. }