GameClient.cpp 7.6 KB

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