NetworkMessage.cpp 7.9 KB

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