NetworkMessage.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #include "NetworkMessage.h"
  2. #include "Chunk.h"
  3. #include "Entity.h"
  4. #include "Game.h"
  5. NetworkMessage::NetworkMessage()
  6. : Framework::ReferenceCounter(),
  7. onAfterSend(0)
  8. {
  9. address = 0;
  10. addressLength = 0;
  11. broadcast = 0;
  12. message = 0;
  13. msgLength = 0;
  14. useBackground = 0;
  15. }
  16. NetworkMessage::~NetworkMessage()
  17. {
  18. delete[] message;
  19. delete[] address;
  20. }
  21. void NetworkMessage::setOnAfterSend(std::function<void()> onAfterSend)
  22. {
  23. if (this->onAfterSend)
  24. throw "Illegal State exception: onAfterSend was already set";
  25. this->onAfterSend = onAfterSend;
  26. }
  27. void NetworkMessage::addressChunck(const Chunk* zChunk)
  28. {
  29. delete[] address;
  30. addressLength = 14;
  31. address = new char[addressLength];
  32. address[0] = 1; // dimension response
  33. *(int*)(address + 1) = zChunk->getDimensionId();
  34. address[5] = 1; // chunck
  35. Framework::Punkt center = zChunk->getCenter();
  36. *(int*)(address + 6) = center.x;
  37. *(int*)(address + 10) = center.y;
  38. }
  39. void NetworkMessage::addressEntity(const Entity* zEntity)
  40. {
  41. delete[] address;
  42. addressLength = 10;
  43. address = new char[addressLength];
  44. address[0] = 1; // dimension response
  45. *(int*)(address + 1) = zEntity->getDimensionId();
  46. address[5] = 2; // entity
  47. *(int*)(address + 6) = zEntity->getId();
  48. }
  49. void NetworkMessage::addressDimension(const Dimension *zDim)
  50. {
  51. delete[] address;
  52. addressLength = 5;
  53. address = new char[5];
  54. address[0] = 1; // dimension response
  55. *(int*)(address + 1) = zDim->getDimensionId();
  56. }
  57. void NetworkMessage::addressBlock(const Block* zBlock)
  58. {
  59. delete[] address;
  60. addressLength = 18;
  61. address = new char[addressLength];
  62. address[0] = 1; // dimension response
  63. *(int*)(address + 1) = zBlock->getDimensionId();
  64. address[5] = 3; // block
  65. Framework::Vec3<int> pos = zBlock->getPos();
  66. *(int*)(address + 6) = pos.x;
  67. *(int*)(address + 10) = pos.y;
  68. *(int*)(address + 14) = pos.z;
  69. }
  70. void NetworkMessage::openDialog(Framework::Text dialogName)
  71. {
  72. delete[] address;
  73. addressLength = (char)(4 + dialogName.getLength());
  74. address = new char[addressLength];
  75. address[0] = 2; // gui message
  76. address[1] = 0; // open dialog
  77. *(short*)(address + 2) = (short)dialogName.getLength(); // block
  78. memcpy(address + 4, dialogName.getText(), dialogName.getLength());
  79. }
  80. void NetworkMessage::addressGui(Framework::Text elementId)
  81. {
  82. delete[] address;
  83. addressLength = (char)(4 + elementId.getLength());
  84. address = new char[addressLength];
  85. address[0] = 2; // gui message
  86. address[1] = 1; // element message
  87. *(short*)(address + 2) = (short)elementId.getLength(); // block
  88. memcpy(address + 4, elementId.getText(), elementId.getLength());
  89. }
  90. void NetworkMessage::setMessage(char* msg, int length)
  91. {
  92. delete[] message;
  93. message = msg;
  94. msgLength = length;
  95. }
  96. void NetworkMessage::sendChatMessage(ChatMessage* zMsg)
  97. {
  98. delete[] address;
  99. addressLength = 1;
  100. address = new char[1];
  101. address[0] = 4; // chat message
  102. InMemoryBuffer buffer;
  103. zMsg->writeTo(&buffer);
  104. char* msg = new char[buffer.getSize()];
  105. buffer.lese(msg, (int)buffer.getSize());
  106. setMessage(msg, (int)buffer.getSize());
  107. }
  108. void NetworkMessage::sendChatOptions(ChatObserver* zOptions)
  109. {
  110. delete[] address;
  111. addressLength = 1;
  112. address = new char[1];
  113. address[0] = 5; // chat options
  114. InMemoryBuffer buffer;
  115. zOptions->writeTo(&buffer);
  116. char* msg = new char[buffer.getSize()];
  117. buffer.lese(msg, (int)buffer.getSize());
  118. setMessage(msg, (int)buffer.getSize());
  119. }
  120. void NetworkMessage::sendMap(ChunkMap* zMap)
  121. {
  122. delete[] address;
  123. addressLength = 1;
  124. address = new char[1];
  125. address[0] = 6; // map data
  126. InMemoryBuffer buffer;
  127. zMap->writeTo(&buffer);
  128. char* msg = new char[buffer.getSize()];
  129. buffer.lese(msg, (int)buffer.getSize());
  130. setMessage(msg, (int)buffer.getSize());
  131. }
  132. void NetworkMessage::sendPlayerPositions(char* msg, int length) {
  133. delete[] address;
  134. addressLength = 1;
  135. address = new char[1];
  136. address[0] = 7; // map player positions
  137. setMessage(msg, length);
  138. }
  139. void NetworkMessage::setUseBackground()
  140. {
  141. useBackground = 1;
  142. }
  143. void NetworkMessage::sendToAll()
  144. {
  145. broadcast = true;
  146. }
  147. void NetworkMessage::animateBlockBone(int dimensionId,
  148. Framework::Punkt chunkCenter,
  149. int index,
  150. int boneId,
  151. double time,
  152. Framework::Vec3<float> pos,
  153. Framework::Vec3<float> rotation)
  154. {
  155. delete[] address;
  156. addressLength = 15;
  157. address = new char[addressLength];
  158. address[0] = 1; // dimension api
  159. *(int*)(address + 1) = dimensionId;
  160. address[5] = 1; // chunk api
  161. *(int*)(address + 6) = chunkCenter.x;
  162. *(int*)(address + 10) = chunkCenter.y;
  163. address[14] = 1; // animate block
  164. char* msg = new char[40];
  165. *(int*)(msg) = index;
  166. *(int*)(msg + 4) = boneId;
  167. *(double*)(msg + 8) = time;
  168. *(float*)(msg + 16) = pos.x;
  169. *(float*)(msg + 20) = pos.y;
  170. *(float*)(msg + 24) = pos.z;
  171. *(float*)(msg + 28) = rotation.x;
  172. *(float*)(msg + 32) = rotation.y;
  173. *(float*)(msg + 36) = rotation.z;
  174. setMessage(msg, 40);
  175. }
  176. void NetworkMessage::syncTime(
  177. double time, double nightLength, double transitionLength, double dayLength)
  178. {
  179. delete[] address;
  180. addressLength = 1;
  181. address = new char[1];
  182. address[0] = 8;
  183. delete[] message;
  184. msgLength = 8 * 4;
  185. message = new char[msgLength];
  186. *(double*)(message) = time;
  187. *(double*)(message + 8) = nightLength;
  188. *(double*)(message + 16) = transitionLength;
  189. *(double*)(message + 24) = dayLength;
  190. }
  191. void NetworkMessage::writeTo(Framework::StreamWriter* zWriter) const
  192. {
  193. int total = msgLength + addressLength;
  194. if (total)
  195. {
  196. zWriter->schreibe((char*)&GameClient::Message::API_MESSAGE, 1);
  197. zWriter->schreibe((char*)&total, 4);
  198. zWriter->schreibe(address, addressLength);
  199. zWriter->schreibe(message, msgLength);
  200. if (onAfterSend) onAfterSend();
  201. }
  202. }
  203. bool NetworkMessage::isBroadcast() const
  204. {
  205. return broadcast;
  206. }
  207. bool NetworkMessage::isEmpty() const
  208. {
  209. return msgLength + addressLength <= 0;
  210. }
  211. bool NetworkMessage::isUseBackground() const
  212. {
  213. return useBackground;
  214. }
  215. NetworkMessage* NetworkMessage::clone() const
  216. {
  217. NetworkMessage* result = new NetworkMessage();
  218. result->message = new char[(int)msgLength];
  219. memcpy(result->message, message, (int)msgLength);
  220. result->msgLength = msgLength;
  221. result->address = new char[(int)addressLength];
  222. memcpy(result->address, address, (int)addressLength);
  223. result->addressLength = addressLength;
  224. result->broadcast = broadcast;
  225. result->useBackground = useBackground;
  226. return result;
  227. }