NetworkMessage.cpp 7.9 KB

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