main.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include "../global.h"
  2. #include "../lib/Connection.h"
  3. #include <boost/lexical_cast.hpp>
  4. #include <boost/thread.hpp>
  5. #include <fstream>
  6. #include "../StartInfo.h"
  7. #ifdef _WIN32
  8. #include <Windows.h>
  9. #else
  10. #include <unistd.h>
  11. #endif
  12. #include "../lib/CGameState.h"
  13. #include "../CCallback.h"
  14. #include "../lib/CGameInterface.h"
  15. #include <boost/format.hpp>
  16. using namespace std;
  17. using namespace boost;
  18. std::string NAME = NAME_VER + std::string(" DLL runner");
  19. int main(int argc, char** argv)
  20. {
  21. int pid = -1;
  22. #ifdef _WIN32
  23. pid = GetCurrentProcessId();
  24. #else
  25. pid = getpid();
  26. #endif
  27. logfile = new std::ofstream(("VCMI_Server_log_" + boost::lexical_cast<std::string>(pid) + ".txt").c_str());
  28. try
  29. {
  30. string host = "127.0.0.1";
  31. string port = "3030";
  32. CConnection *serv = NULL;
  33. while(!serv)
  34. {
  35. try
  36. {
  37. tlog0 << "Establishing connection...\n";
  38. serv = new CConnection(host, port, "DLL host");
  39. }
  40. catch(...)
  41. {
  42. tlog1 << "\nCannot establish connection! Retrying within 2 seconds" << std::endl;
  43. boost::this_thread::sleep(boost::posix_time::seconds(2));
  44. }
  45. }
  46. ui8 color;
  47. StartInfo si;
  48. string battleAIName;
  49. *serv >> si >> battleAIName >> color;
  50. assert(si.mode == StartInfo::DUEL);
  51. tlog0 << format("Server wants us to run %s in battle %s as side %d") % battleAIName % si.mapname % color;
  52. CGameState *gs = new CGameState();
  53. gs->init(&si, 0, 0);
  54. CBattleCallback * cbc = new CBattleCallback(gs, color, this);
  55. CBattleGameInterface *ai = CDynLibHandler::getNewBattleAI(battleAIName);
  56. ai->init(cbc);
  57. }
  58. catch(std::exception &e)
  59. {
  60. tlog1 << "Encountered exception: " << e.what() << std::endl;
  61. }
  62. catch(...)
  63. {
  64. tlog1 << "Encountered unknown exception!" << std::endl;
  65. }
  66. return EXIT_SUCCESS;
  67. }