NetworkMessage.cpp 7.7 KB

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