NetworkMessage.cpp 9.4 KB

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