NetworkHandler.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include <InitDatei.h>
  2. #include "NetworkHandler.h"
  3. #include "Globals.h"
  4. NetworkHandler::NetworkHandler()
  5. : ReferenceCounter()
  6. {
  7. HINSTANCE dll = dlls->ladeDLL("KSGClient", "data/bin/KSGNetwork.dll");
  8. if (!dll)
  9. throw "DLL not found: 'data/bin/KSGNetwork.dll'";
  10. KSGClient::KSGNetworkCreateMain getMainClient = (KSGClient::KSGNetworkCreateMain)GetProcAddress(dll, KSGNETWORK_CREATE_MAIN);
  11. if (!getMainClient)
  12. throw "Entry point '" KSGNETWORK_CREATE_MAIN "' not found in DLL 'data/bin/KSGNetwork.dll'";
  13. msc = getMainClient();
  14. if (!msc)
  15. throw "Could not create Main Server Client from DLL 'data/bin/KSGNetwork.dll'";
  16. InitDatei* iDat = new InitDatei("data/optionen.ini");
  17. iDat->laden();
  18. if (!iDat->zWert("ServerIP"))
  19. iDat->addWert("ServerIP", "127.0.0.1");
  20. if (!iDat->zWert("ServerPort"))
  21. iDat->addWert("ServerPort", "4225");
  22. iDat->speichern();
  23. Text* ipT = iDat->zWert("ServerIP");
  24. unsigned short port = (unsigned short)TextZuInt(iDat->zWert("ServerPort")->getText(), 10);
  25. esc = 0;
  26. if (msc->registerSSL(ipT->getText(), port))
  27. {
  28. //esc = msc->createErhaltungServerClient();
  29. //esc->verbinde();
  30. }
  31. iDat->release();
  32. fc = 0;
  33. lsc = 0;
  34. gsc = 0;
  35. }
  36. NetworkHandler::~NetworkHandler()
  37. {
  38. if (gsc)
  39. {
  40. gsc->release();
  41. gsc = 0;
  42. }
  43. if (fc)
  44. fc->release();
  45. if (esc)
  46. {
  47. esc->abmelden();
  48. esc->release();
  49. }
  50. if (lsc)
  51. {
  52. lsc->verbinde();
  53. lsc->logout();
  54. lsc->trenne(1);
  55. lsc->release();
  56. }
  57. if (msc)
  58. {
  59. msc->unregister();
  60. msc->release();
  61. }
  62. }
  63. bool NetworkHandler::login(Framework::Text name, Framework::Text password)
  64. {
  65. if (!lsc)
  66. lsc = msc->createLoginServerClient();
  67. else
  68. {
  69. if (gsc)
  70. {
  71. gsc->release();
  72. gsc = 0;
  73. }
  74. lsc->logout();
  75. }
  76. if (!lsc)
  77. return 0;
  78. lsc->verbinde();
  79. bool ok = lsc->login(name, password);
  80. lsc->trenne(ok);
  81. if (ok)
  82. {
  83. accountId = lsc->getAccountId();
  84. lsc->release();
  85. lsc = 0;
  86. }
  87. return ok;
  88. }
  89. bool NetworkHandler::connect(Text ip, short port)
  90. {
  91. if (!gsc)
  92. gsc = msc->createMinigameServerClient();
  93. if (!gsc)
  94. return 0;
  95. Text* secret = gsc->getSecret();
  96. if (!secret)
  97. return 0;
  98. if (!fc)
  99. fc = new FactoryClient();
  100. bool ok = fc->connect(ip, port, accountId, *secret);
  101. secret->release();
  102. return ok;
  103. }
  104. FactoryClient* NetworkHandler::zFactoryClient() const
  105. {
  106. return fc;
  107. }
  108. bool NetworkHandler::leaveGame()
  109. {
  110. if (fc)
  111. {
  112. fc->release();
  113. fc = 0;
  114. }
  115. return 1;
  116. }