GameClient.cpp 7.1 KB

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