NetworkMessage.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 = 10;
  24. address = new char[addressLength];
  25. address[0] = 1; // dimension response
  26. address[1] = 1; // chunck
  27. Framework::Punkt center = zChunk->getCenter();
  28. *(int*)(address + 2) = center.x;
  29. *(int*)(address + 6) = center.y;
  30. }
  31. void NetworkMessage::addressEntity(const Entity* zEntity)
  32. {
  33. delete[] address;
  34. addressLength = 6;
  35. address = new char[addressLength];
  36. address[0] = 1; // dimension response
  37. address[1] = 2; // entity
  38. *(int*)(address + 2) = zEntity->getId();
  39. }
  40. void NetworkMessage::addressDimension()
  41. {
  42. delete[] address;
  43. addressLength = 1;
  44. address = new char[1];
  45. address[0] = 1; // dimension response
  46. }
  47. void NetworkMessage::addressBlock(const Block* zBlock)
  48. {
  49. delete[] address;
  50. addressLength = 14;
  51. address = new char[addressLength];
  52. address[0] = 1; // dimension response
  53. address[1] = 3; // block
  54. Framework::Vec3<int> pos = zBlock->getPos();
  55. *(int*)(address + 2) = pos.x;
  56. *(int*)(address + 6) = pos.y;
  57. *(int*)(address + 10) = pos.z;
  58. }
  59. void NetworkMessage::openDialog(Framework::Text dialogName)
  60. {
  61. delete[] address;
  62. addressLength = (char)(4 + dialogName.getLength());
  63. address = new char[addressLength];
  64. address[0] = 2; // gui message
  65. address[1] = 0; // open dialog
  66. *(short*)(address + 2) = (short)dialogName.getLength(); // block
  67. memcpy(address + 4, dialogName.getText(), dialogName.getLength());
  68. }
  69. void NetworkMessage::addressGui(Framework::Text elementId)
  70. {
  71. delete[] address;
  72. addressLength = (char)(4 + elementId.getLength());
  73. address = new char[addressLength];
  74. address[0] = 2; // gui message
  75. address[1] = 1; // element message
  76. *(short*)(address + 2) = (short)elementId.getLength(); // block
  77. memcpy(address + 4, elementId.getText(), elementId.getLength());
  78. }
  79. void NetworkMessage::setMessage(char* msg, int length)
  80. {
  81. delete[] message;
  82. message = msg;
  83. msgLength = length;
  84. }
  85. void NetworkMessage::setUseBackground()
  86. {
  87. useBackground = 1;
  88. }
  89. void NetworkMessage::sendToAll()
  90. {
  91. broadcast = true;
  92. }
  93. void NetworkMessage::writeTo(Framework::StreamWriter* zWriter) const
  94. {
  95. int total = msgLength + addressLength;
  96. if (total)
  97. {
  98. zWriter->schreibe((char*)&GameClient::Message::API_MESSAGE, 1);
  99. zWriter->schreibe((char*)&total, 4);
  100. zWriter->schreibe(address, addressLength);
  101. zWriter->schreibe(message, msgLength);
  102. }
  103. }
  104. bool NetworkMessage::isBroadcast() const
  105. {
  106. return broadcast;
  107. }
  108. bool NetworkMessage::isEmpty() const
  109. {
  110. return msgLength + addressLength <= 0;
  111. }
  112. bool NetworkMessage::isUseBackground() const
  113. {
  114. return useBackground;
  115. }
  116. NetworkMessage* NetworkMessage::clone() const
  117. {
  118. NetworkMessage* result = new NetworkMessage();
  119. result->message = new char[(int)msgLength];
  120. memcpy(result->message, message, (int)msgLength);
  121. result->msgLength = msgLength;
  122. result->address = new char[(int)addressLength];
  123. memcpy(result->address, address, (int)addressLength);
  124. result->addressLength = addressLength;
  125. result->broadcast = broadcast;
  126. result->useBackground = useBackground;
  127. return result;
  128. }