FactoryClient.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. #include <Bild.h>
  2. #include <Textur.h>
  3. #include "FactoryClient.h"
  4. #include "Globals.h"
  5. using namespace Network;
  6. using namespace Framework;
  7. FactoryClient::FactoryClient()
  8. {
  9. client = 0;
  10. background = 0;
  11. foreground = 0;
  12. backgroundReader = 0;
  13. foregroundReader = 0;
  14. bgReaderUsage = 0;
  15. fgReaderUsage = 0;
  16. }
  17. FactoryClient::~FactoryClient()
  18. {
  19. if (client)
  20. disconnect();
  21. }
  22. void FactoryClient::loadServerInfo()
  23. {
  24. // receive type information
  25. for (int i = 0; i < blockTypeCount; i++)
  26. blockTypes[i]->release();
  27. delete[] blockTypes;
  28. for (int i = 0; i < itemTypeCount; i++)
  29. itemTypes[i]->release();
  30. delete[] itemTypes;
  31. for (int i = 0; i < entityTypeCount; i++)
  32. entityTypes[i]->release();
  33. delete[] entityTypes;
  34. foregroundReader->lese((char*)&blockTypeCount, 4);
  35. blockTypes = new BlockType * [blockTypeCount];
  36. for (int i = 0; i < blockTypeCount; i++)
  37. {
  38. int id;
  39. foregroundReader->lese((char*)&id, 4);
  40. bool needsInstance;
  41. foregroundReader->lese((char*)&needsInstance, 1);
  42. int maxHp;
  43. foregroundReader->lese((char*)&maxHp, 4);
  44. blockTypes[i] = new BlockType(id, needsInstance, ModelInfo(foregroundReader), maxHp);
  45. }
  46. foregroundReader->lese((char*)&itemTypeCount, 4);
  47. itemTypes = new ItemType * [itemTypeCount];
  48. for (int i = 0; i < itemTypeCount; i++)
  49. {
  50. int id;
  51. foregroundReader->lese((char*)&id, 4);
  52. itemTypes[i] = new ItemType(id, ModelInfo(foregroundReader));
  53. }
  54. foregroundReader->lese((char*)&entityTypeCount, 4);
  55. entityTypes = new EntityType * [entityTypeCount];
  56. for (int i = 0; i < entityTypeCount; i++)
  57. {
  58. int id;
  59. foregroundReader->lese((char*)&id, 4);
  60. entityTypes[i] = new EntityType(id, ModelInfo(foregroundReader));
  61. }
  62. // pre rendering item models
  63. Kam3D* kam = new Kam3D();
  64. Welt3D* w = new Welt3D();
  65. w->addDiffuseLight(DiffuseLight{ Vec3<float>(0.5f, 0.5f, -1.f), Vec3<float>(1.f, 1.f, 1.f) });
  66. kam->setWelt(w);
  67. kam->setBildschirmPosition(0, 0);
  68. kam->setBildschirmSize(50, 50);
  69. kam->setPosition(Vec3<float>(0, 0, 0));
  70. kam->setRotation({ 2.f, 0, -1.f });
  71. Bild* b = new Bild();
  72. b->neuBild(50, 50, 0);
  73. for (int i = 0; i < itemTypeCount; i++)
  74. {
  75. Model3D* mdl = new Model3D();
  76. mdl->setModelDaten(itemTypes[i]->getItemModel());
  77. mdl->setModelTextur(itemTypes[i]->getItemTextur());
  78. mdl->setPosition(Vec3<float>(-0.8f, -0.45f, -0.4f));
  79. mdl->setSize(2.f);
  80. w->addZeichnung(mdl);
  81. w->tick(0);
  82. window->zBildschirm()->lock();
  83. DX11Textur* t = (DX11Textur*)window->zBildschirm()->zGraphicsApi()->createOrGetTextur(Text("rendered/items/") + i, dynamic_cast<Bild*>(b->getThis()));
  84. window->zBildschirm()->zGraphicsApi()->renderKamera(kam, t);
  85. Bild* result = new Bild();
  86. t->copyToImage(result);
  87. itemIcons->add(result);
  88. t->release();
  89. window->zBildschirm()->unlock();
  90. w->removeZeichnung(mdl);
  91. }
  92. b->release();
  93. kam->release();
  94. }
  95. bool FactoryClient::connect(Text ip, unsigned short sslPort)
  96. {
  97. if (client)
  98. disconnect();
  99. client = new SSLKlient();
  100. if (!client->verbinde(sslPort, ip))
  101. return false;
  102. this->ip = ip;
  103. return 1;
  104. }
  105. int FactoryClient::ping()
  106. {
  107. ZeitMesser zm;
  108. zm.messungStart();
  109. if (!client->sende("\3", 1))
  110. return -1;
  111. char c;
  112. client->getNachricht(&c, 1);
  113. zm.messungEnde();
  114. return (int)(zm.getSekunden() * 1000);
  115. }
  116. int FactoryClient::status(Framework::Text name, Framework::Text secret)
  117. {
  118. if (!client->sende("\4", 1))
  119. return 404;
  120. char c;
  121. client->getNachricht(&c, 1);
  122. if (c == 1)
  123. {
  124. char len = (char)name.getLength();
  125. client->sende(&len, 1);
  126. client->sende(name, len);
  127. short sLen = (short)secret.getLength();
  128. client->sende((char*)&sLen, 2);
  129. client->sende(secret, sLen);
  130. char res;
  131. client->getNachricht(&res, 1);
  132. if (res == 1)
  133. return 200;
  134. if (res == 0)
  135. return 403;
  136. }
  137. return 404;
  138. }
  139. int FactoryClient::join(Framework::Text name, Framework::Text &secret, unsigned short port)
  140. {
  141. client->sende("\1", 1);
  142. char len = (char)name.getLength();
  143. client->sende(&len, 1);
  144. client->sende(name, len);
  145. short sLen = (short)secret.getLength();
  146. client->sende((char*)&sLen, 2);
  147. client->sende(secret, sLen);
  148. char res;
  149. client->getNachricht(&res, 1);
  150. if (res == 1 || res == 2)
  151. {
  152. if (res == 2)
  153. {
  154. client->getNachricht((char*)&sLen, 2);
  155. char* buffer = new char[sLen + 1];
  156. client->getNachricht(buffer, sLen);
  157. buffer[sLen] = 0;
  158. secret = buffer;
  159. delete[] buffer;
  160. }
  161. int keyLen;
  162. client->getNachricht((char*)&keyLen, 4);
  163. char* key = new char[keyLen];
  164. client->getNachricht(key, keyLen);
  165. foreground = new Klient();
  166. if (!foreground->verbinde(port, ip))
  167. {
  168. delete[] key;
  169. return false;
  170. }
  171. if (!foreground->sende((char*)&keyLen, 2))
  172. {
  173. delete[] key;
  174. return false;
  175. }
  176. if (!foreground->sende(key, keyLen))
  177. {
  178. delete[] key;
  179. return false;
  180. }
  181. background = new Klient();
  182. if (!background->verbinde(port, ip))
  183. {
  184. delete[] key;
  185. foreground->release();
  186. foreground = 0;
  187. background->release();
  188. background = 0;
  189. return false;
  190. }
  191. if (!background->sende((char*)&keyLen, 2))
  192. {
  193. delete[] key;
  194. foreground->release();
  195. foreground = 0;
  196. background->release();
  197. background = 0;
  198. return false;
  199. }
  200. if (!background->sende(key, keyLen))
  201. {
  202. delete[] key;
  203. foreground->release();
  204. foreground = 0;
  205. background->release();
  206. background = 0;
  207. return false;
  208. }
  209. delete[] key;
  210. bool bg = 0;
  211. if (!foreground->sende((char*)&bg, 1))
  212. {
  213. delete[] key;
  214. return 201;
  215. }
  216. foregroundReader = new NetworkReader(foreground);
  217. bg = 1;
  218. if (!background->sende((char*)&bg, 1))
  219. return 201;
  220. backgroundReader = new NetworkReader(background);
  221. client->trenne();
  222. loadServerInfo();
  223. return 200;
  224. }
  225. if (res == 0)
  226. return 403;
  227. return 500;
  228. }
  229. void FactoryClient::disconnect()
  230. {
  231. if (client)
  232. {
  233. NetworkReader* fgReader = foregroundReader;
  234. NetworkReader* bgReader = backgroundReader;
  235. backgroundReader = 0;
  236. foregroundReader = 0;
  237. if (foreground)
  238. foreground->trenne();
  239. if (background)
  240. background->trenne();
  241. while (fgReaderUsage > 0 || bgReaderUsage > 0)
  242. Sleep(100);
  243. delete fgReader;
  244. delete bgReader;
  245. client->release();
  246. client = 0;
  247. if (foreground)
  248. foreground->release();
  249. foreground = 0;
  250. if (background)
  251. background->release();
  252. background = 0;
  253. }
  254. }
  255. NetworkReader* FactoryClient::getNextForegroundMessage()
  256. {
  257. fgReaderUsage++;
  258. if (!foreground)
  259. return 0;
  260. if (!foreground->hatNachricht(0))
  261. return 0;
  262. return foregroundReader;
  263. }
  264. NetworkReader* FactoryClient::getNextBackgroundMessage()
  265. {
  266. bgReaderUsage++;
  267. if (!background)
  268. return 0;
  269. if (!background->hatNachricht(0))
  270. return 0;
  271. return backgroundReader;
  272. }
  273. void FactoryClient::endMessageReading(bool bg)
  274. {
  275. if (bg)
  276. bgReaderUsage--;
  277. else
  278. fgReaderUsage--;
  279. }
  280. void FactoryClient::sendPlayerAction(char* data, unsigned short length)
  281. {
  282. if (!foreground)
  283. return;
  284. cs.lock();
  285. length += 1;
  286. foreground->sende((char*)&length, 2);
  287. char msgId = 2;
  288. foreground->sende(&msgId, 1);
  289. foreground->sende((char*)data, length - 1);
  290. cs.unlock();
  291. }
  292. void FactoryClient::sendPlayerMovement(MovementFrame& frame)
  293. {
  294. if (!foreground)
  295. return;
  296. cs.lock();
  297. short length = 38;
  298. foreground->sende((char*)&length, 2);
  299. char msgId = 2; // player message
  300. foreground->sende(&msgId, 1);
  301. foreground->sende(&msgId, 1); // set movement
  302. foreground->sende((char*)&frame.direction.x, 4);
  303. foreground->sende((char*)&frame.direction.y, 4);
  304. foreground->sende((char*)&frame.direction.z, 4);
  305. foreground->sende((char*)&frame.targetPosition.x, 4);
  306. foreground->sende((char*)&frame.targetPosition.y, 4);
  307. foreground->sende((char*)&frame.targetPosition.z, 4);
  308. foreground->sende((char*)&frame.movementFlags, 4);
  309. foreground->sende((char*)&frame.duration, 8);
  310. cs.unlock();
  311. }
  312. void FactoryClient::entityAPIRequest(int entityId, char* message, unsigned short length)
  313. {
  314. if (!foreground)
  315. return;
  316. cs.lock();
  317. length += 5;
  318. foreground->sende((char*)&length, 2);
  319. char msgId = 3;
  320. foreground->sende(&msgId, 1);
  321. foreground->sende((char*)&entityId, 4);
  322. foreground->sende(message, length - 5);
  323. cs.unlock();
  324. }
  325. void FactoryClient::chunkAPIRequest(Punkt center, char* message, unsigned short length)
  326. {
  327. length += 10;
  328. cs.lock();
  329. foreground->sende((char*)&length, 2);
  330. char type = 1;
  331. foreground->sende(&type, 1);
  332. type = 0;
  333. foreground->sende(&type, 1);
  334. foreground->sende((char*)&center.x, 4);
  335. foreground->sende((char*)&center.y, 4);
  336. foreground->sende(message, length - 10);
  337. cs.unlock();
  338. }
  339. void FactoryClient::inventoryAPIRequest(Framework::Either<int, Framework::VecN<int, 4>> target, char* message, unsigned short length)
  340. {
  341. if (!foreground)
  342. return;
  343. cs.lock();
  344. length += target.isA() ? 6 : 18;
  345. foreground->sende((char*)&length, 2);
  346. char msgId = 4;
  347. foreground->sende(&msgId, 1);
  348. bool isEntity = target.isA();
  349. foreground->sende((char*)&isEntity, 1);
  350. if (target.isA())
  351. {
  352. int id = target.getA();
  353. foreground->sende((char*)&id, 4);
  354. }
  355. else
  356. {
  357. for (int i = 0; i < 4; i++)
  358. {
  359. int v = target.getB()[i];
  360. foreground->sende((char*)&v, 4);
  361. }
  362. }
  363. foreground->sende(message, length - (target.isA() ? 6 : 18));
  364. cs.unlock();
  365. }