GameClient.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #include "GameClient.h"
  2. #include <AsynchronCall.h>
  3. #include "Constants.h"
  4. #include "Dimension.h"
  5. #include "Game.h"
  6. #include "Player.h"
  7. #include "Server.h"
  8. GameClient::GameClient(Player* zPlayer, FCKlient* client)
  9. : Thread(),
  10. zPlayer(zPlayer),
  11. client(client),
  12. viewDistance(DEFAULT_VIEW_DISTANCE),
  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.hat(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.hat(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.leeren();
  90. other.unlock();
  91. if (first)
  92. {
  93. foreground.lock();
  94. int id = zPlayer->getId();
  95. client->zForegroundWriter()->schreibe(
  96. (char*)&Message::POSITION_UPDATE, 1);
  97. client->zForegroundWriter()->schreibe((char*)&id, 4);
  98. id = zPlayer->getDimensionId();
  99. client->zForegroundWriter()->schreibe((char*)&id, 4);
  100. client->zForegroundWriter()->schreibe((char*)&Message::API_MESSAGE, 1);
  101. int len = 10;
  102. client->zForegroundWriter()->schreibe((char*)&len, 4);
  103. client->zForegroundWriter()->schreibe("\1", 1);
  104. client->zForegroundWriter()->schreibe((char*)&id, 4);
  105. client->zForegroundWriter()->schreibe("\6", 1);
  106. float gravity = Game::INSTANCE->zDimension(zPlayer->getDimensionId())
  107. ->getGravity();
  108. client->zForegroundWriter()->schreibe((char*)&gravity, 4);
  109. foreground.unlock();
  110. first = 0;
  111. }
  112. }
  113. void GameClient::logout()
  114. {
  115. online = 0;
  116. emptyForegroundQueueSync.notifyAll();
  117. emptyBackgroundQueueSync.notifyAll();
  118. foregroundQueueSync.notify();
  119. backgroundQueueSync.notify();
  120. }
  121. void GameClient::addMessage(StreamReader* reader)
  122. {
  123. short len = 0;
  124. reader->lese((char*)&len, 2);
  125. InMemoryBuffer* buffer = new InMemoryBuffer();
  126. char* tmp = new char[len];
  127. reader->lese(tmp, len);
  128. buffer->schreibe(tmp, len);
  129. delete[] tmp;
  130. other.lock();
  131. requests.add(buffer);
  132. other.unlock();
  133. }
  134. bool GameClient::isOnline() const
  135. {
  136. return online;
  137. }
  138. void GameClient::sendResponse(NetworkMessage* response)
  139. {
  140. queueCs.lock();
  141. if (response->isUseBackground())
  142. {
  143. if (backgroundQueue.getEintragAnzahl() > 20)
  144. {
  145. queueCs.unlock();
  146. while (!emptyBackgroundQueueSync.wait(1000))
  147. {
  148. backgroundQueueSync.notify();
  149. }
  150. queueCs.lock();
  151. }
  152. backgroundQueue.add(response);
  153. queueCs.unlock();
  154. backgroundQueueSync.notify();
  155. }
  156. else
  157. {
  158. if (foregroundQueue.getEintragAnzahl() > 100)
  159. {
  160. queueCs.unlock();
  161. Framework::Logging::warning()
  162. << "Game paused because nework connection to "
  163. << zPlayer->getName() << " is to slow.";
  164. ZeitMesser m;
  165. m.messungStart();
  166. while (foregroundQueue.getEintragAnzahl() > 0)
  167. {
  168. foregroundQueueSync.notify();
  169. emptyForegroundQueueSync.wait(100);
  170. }
  171. m.messungEnde();
  172. Framework::Logging::warning()
  173. << "Game resumed after " << m.getSekunden() << " seconds.";
  174. queueCs.lock();
  175. }
  176. foregroundQueue.add(response);
  177. queueCs.unlock();
  178. foregroundQueueSync.notify();
  179. }
  180. }
  181. Player* GameClient::zEntity() const
  182. {
  183. return zPlayer;
  184. }
  185. void GameClient::sendTypes()
  186. {
  187. foreground.lock();
  188. int count = 0;
  189. for (int i = 0; i < Game::INSTANCE->getBlockTypeCount(); i++)
  190. {
  191. if (Game::INSTANCE->zBlockType(i)) count++;
  192. }
  193. client->zForegroundWriter()->schreibe((char*)&count, 4);
  194. for (int i = 0; i < Game::INSTANCE->getBlockTypeCount(); i++)
  195. {
  196. const BlockType* t = Game::INSTANCE->zBlockType(i);
  197. if (t)
  198. {
  199. t->writeTypeInfo(client->zForegroundWriter());
  200. }
  201. }
  202. count = 0;
  203. for (int i = 0; i < Game::INSTANCE->getItemTypeCount(); i++)
  204. {
  205. if (Game::INSTANCE->zItemType(i)) count++;
  206. }
  207. client->zForegroundWriter()->schreibe((char*)&count, 4);
  208. for (int i = 0; i < Game::INSTANCE->getItemTypeCount(); i++)
  209. {
  210. const ItemType* t = Game::INSTANCE->zItemType(i);
  211. if (t)
  212. {
  213. int id = t->getId();
  214. client->zForegroundWriter()->schreibe((char*)&id, 4);
  215. char len = (char)t->getName().getLength();
  216. client->zForegroundWriter()->schreibe((char*)&len, 1);
  217. client->zForegroundWriter()->schreibe(t->getName().getText(), len);
  218. short tlen = (short)t->getTooltipUIML().getLength();
  219. client->zForegroundWriter()->schreibe((char*)&tlen, 2);
  220. client->zForegroundWriter()->schreibe(
  221. t->getTooltipUIML().getText(), tlen);
  222. if (t->zModel())
  223. {
  224. t->zModel()->writeTo(client->zForegroundWriter());
  225. }
  226. else
  227. {
  228. ModelInfo("", Framework::RCArray<Framework::Text>(), false, 1.f)
  229. .writeTo(client->zForegroundWriter());
  230. }
  231. }
  232. }
  233. count = 0;
  234. for (int i = 0; i < Game::INSTANCE->getEntityTypeCount(); i++)
  235. {
  236. if (Game::INSTANCE->zEntityType(i)) count++;
  237. }
  238. client->zForegroundWriter()->schreibe((char*)&count, 4);
  239. for (int i = 0; i < count; i++)
  240. {
  241. const EntityType* t = Game::INSTANCE->zEntityType(i);
  242. int id = t->getId();
  243. client->zForegroundWriter()->schreibe((char*)&id, 4);
  244. if (t->zModel())
  245. {
  246. t->zModel()->writeTo(client->zForegroundWriter());
  247. }
  248. else
  249. {
  250. ModelInfo("", Framework::RCArray<Framework::Text>(), false, 1.f)
  251. .writeTo(client->zForegroundWriter());
  252. }
  253. }
  254. foreground.unlock();
  255. }