GameClient.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #include "GameClient.h"
  2. #include <AsynchronCall.h>
  3. #include "Constants.h"
  4. #include "Dimension.h"
  5. #include "Game.h"
  6. #include "ItemType.h"
  7. #include "Player.h"
  8. #include "Server.h"
  9. GameClient::GameClient(Player* zPlayer, FCKlient* client)
  10. : Thread(),
  11. zPlayer(zPlayer),
  12. client(client),
  13. viewDistance(DEFAULT_VIEW_DISTANCE),
  14. first(1),
  15. online(1),
  16. backgroundFinished(0),
  17. foregroundFinished(0)
  18. {
  19. start();
  20. }
  21. GameClient::~GameClient()
  22. {
  23. online = 0;
  24. emptyForegroundQueueSync.notifyAll();
  25. emptyBackgroundQueueSync.notifyAll();
  26. foregroundQueueSync.notify();
  27. backgroundQueueSync.notify();
  28. while (!foregroundFinished || !backgroundFinished)
  29. Sleep(100);
  30. client->release();
  31. }
  32. void GameClient::thread()
  33. {
  34. new AsynchronCall("Game Client Background", [this]() {
  35. while (online)
  36. {
  37. queueCs.lock();
  38. if (backgroundQueue.hat(0))
  39. {
  40. NetworkMessage* message = backgroundQueue.get(0);
  41. backgroundQueue.remove(0);
  42. queueCs.unlock();
  43. background.lock();
  44. message->writeTo(client->zBackgroundWriter());
  45. background.unlock();
  46. message->release();
  47. }
  48. else
  49. {
  50. queueCs.unlock();
  51. emptyBackgroundQueueSync.notifyAll();
  52. while (!backgroundQueueSync.wait(1000))
  53. {
  54. emptyBackgroundQueueSync.notifyAll();
  55. }
  56. }
  57. }
  58. backgroundFinished = 1;
  59. });
  60. while (online)
  61. {
  62. queueCs.lock();
  63. if (foregroundQueue.hat(0))
  64. {
  65. NetworkMessage* message = foregroundQueue.get(0);
  66. foregroundQueue.remove(0);
  67. queueCs.unlock();
  68. foreground.lock();
  69. message->writeTo(client->zForegroundWriter());
  70. foreground.unlock();
  71. message->release();
  72. }
  73. else
  74. {
  75. queueCs.unlock();
  76. emptyForegroundQueueSync.notifyAll();
  77. while (!foregroundQueueSync.wait(1000))
  78. {
  79. emptyForegroundQueueSync.notifyAll();
  80. }
  81. }
  82. }
  83. foregroundFinished = 1;
  84. }
  85. void GameClient::reply()
  86. {
  87. other.lock();
  88. for (auto req : requests)
  89. Game::INSTANCE->api(req, this);
  90. requests.leeren();
  91. other.unlock();
  92. if (first)
  93. {
  94. foreground.lock();
  95. int id = zPlayer->getId();
  96. client->zForegroundWriter()->schreibe(
  97. (char*)&Message::POSITION_UPDATE, 1);
  98. client->zForegroundWriter()->schreibe((char*)&id, 4);
  99. id = zPlayer->getDimensionId();
  100. client->zForegroundWriter()->schreibe((char*)&id, 4);
  101. foreground.unlock();
  102. first = 0;
  103. }
  104. }
  105. void GameClient::logout()
  106. {
  107. online = 0;
  108. emptyForegroundQueueSync.notifyAll();
  109. emptyBackgroundQueueSync.notifyAll();
  110. foregroundQueueSync.notify();
  111. backgroundQueueSync.notify();
  112. }
  113. void GameClient::addMessage(StreamReader* reader)
  114. {
  115. short len = 0;
  116. reader->lese((char*)&len, 2);
  117. InMemoryBuffer* buffer = new InMemoryBuffer();
  118. char* tmp = new char[len];
  119. reader->lese(tmp, len);
  120. buffer->schreibe(tmp, len);
  121. delete[] tmp;
  122. other.lock();
  123. requests.add(buffer);
  124. other.unlock();
  125. }
  126. bool GameClient::isOnline() const
  127. {
  128. return online;
  129. }
  130. void GameClient::sendResponse(NetworkMessage* response)
  131. {
  132. queueCs.lock();
  133. if (response->isUseBackground())
  134. {
  135. if (backgroundQueue.getEintragAnzahl() > 20)
  136. {
  137. queueCs.unlock();
  138. while (!emptyBackgroundQueueSync.wait(1000))
  139. {
  140. backgroundQueueSync.notify();
  141. }
  142. queueCs.lock();
  143. }
  144. backgroundQueue.add(response);
  145. queueCs.unlock();
  146. backgroundQueueSync.notify();
  147. }
  148. else
  149. {
  150. if (foregroundQueue.getEintragAnzahl() > 100)
  151. {
  152. queueCs.unlock();
  153. Framework::Logging::warning()
  154. << "Game paused because nework connection to "
  155. << zPlayer->getName() << " is to slow.";
  156. ZeitMesser m;
  157. m.messungStart();
  158. while (foregroundQueue.getEintragAnzahl() > 0)
  159. {
  160. foregroundQueueSync.notify();
  161. emptyForegroundQueueSync.wait(100);
  162. }
  163. m.messungEnde();
  164. Framework::Logging::warning()
  165. << "Game resumed after " << m.getSekunden() << " seconds.";
  166. queueCs.lock();
  167. }
  168. foregroundQueue.add(response);
  169. queueCs.unlock();
  170. foregroundQueueSync.notify();
  171. }
  172. }
  173. Player* GameClient::zEntity() const
  174. {
  175. return zPlayer;
  176. }
  177. void GameClient::sendTypes()
  178. {
  179. foreground.lock();
  180. int count = 0;
  181. for (int i = 0; i < Game::INSTANCE->getBlockTypeCount(); i++)
  182. {
  183. if (Game::INSTANCE->zBlockType(i)) count++;
  184. }
  185. client->zForegroundWriter()->schreibe((char*)&count, 4);
  186. for (int i = 0; i < Game::INSTANCE->getBlockTypeCount(); i++)
  187. {
  188. const BlockType* t = Game::INSTANCE->zBlockType(i);
  189. if (t)
  190. {
  191. t->writeTypeInfo(client->zForegroundWriter());
  192. }
  193. }
  194. count = 0;
  195. for (int i = 0; i < Game::INSTANCE->getItemTypeCount(); i++)
  196. {
  197. if (Game::INSTANCE->zItemType(i)) count++;
  198. }
  199. client->zForegroundWriter()->schreibe((char*)&count, 4);
  200. for (int i = 0; i < Game::INSTANCE->getItemTypeCount(); i++)
  201. {
  202. const ItemType* t = Game::INSTANCE->zItemType(i);
  203. if (t)
  204. {
  205. int id = t->getId();
  206. client->zForegroundWriter()->schreibe((char*)&id, 4);
  207. char len = (char)t->getName().getLength();
  208. client->zForegroundWriter()->schreibe((char*)&len, 1);
  209. client->zForegroundWriter()->schreibe(t->getName().getText(), len);
  210. Framework::XML::Element* tooltip = t->getTooltipUIML();
  211. Framework::Text tooltipText
  212. = tooltip ? tooltip->toString() : Framework::Text("");
  213. short tlen = (short)tooltipText.getLength();
  214. client->zForegroundWriter()->schreibe((char*)&tlen, 2);
  215. client->zForegroundWriter()->schreibe(tooltipText.getText(), tlen);
  216. tooltip->release();
  217. if (t->zModel())
  218. {
  219. t->zModel()->writeTo(client->zForegroundWriter());
  220. }
  221. else
  222. {
  223. ModelInfo("", Framework::RCArray<Framework::Text>(), false, 1.f)
  224. .writeTo(client->zForegroundWriter());
  225. }
  226. }
  227. }
  228. count = 0;
  229. for (int i = 0; i < Game::INSTANCE->getEntityTypeCount(); i++)
  230. {
  231. if (Game::INSTANCE->zEntityType(i)) count++;
  232. }
  233. client->zForegroundWriter()->schreibe((char*)&count, 4);
  234. for (int i = 0; i < count; i++)
  235. {
  236. const EntityType* t = Game::INSTANCE->zEntityType(i);
  237. int id = t->getId();
  238. client->zForegroundWriter()->schreibe((char*)&id, 4);
  239. if (t->zModel())
  240. {
  241. t->zModel()->writeTo(client->zForegroundWriter());
  242. }
  243. else
  244. {
  245. ModelInfo("", Framework::RCArray<Framework::Text>(), false, 1.f)
  246. .writeTo(client->zForegroundWriter());
  247. }
  248. }
  249. foreground.unlock();
  250. }