Client.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include "Client.h"
  2. #include "../lib/Connection.h"
  3. #include "../CThreadHelper.h"
  4. #include "../lib/CGameState.h"
  5. #include "../lib/BattleAction.h"
  6. #include "../lib/CGameInterface.h"
  7. #include "CheckTime.h"
  8. #define NOT_LIB
  9. #include "../lib/RegisterTypes.cpp"
  10. template <typename T> class CApplyOnCL;
  11. class CBaseForCLApply
  12. {
  13. public:
  14. virtual void applyOnClAfter(CClient *cl, void *pack) const =0;
  15. virtual void applyOnClBefore(CClient *cl, void *pack) const =0;
  16. virtual ~CBaseForCLApply(){}
  17. template<typename U> static CBaseForCLApply *getApplier(const U * t=NULL)
  18. {
  19. return new CApplyOnCL<U>;
  20. }
  21. };
  22. template <typename T> class CApplyOnCL : public CBaseForCLApply
  23. {
  24. public:
  25. void applyOnClAfter(CClient *cl, void *pack) const
  26. {
  27. T *ptr = static_cast<T*>(pack);
  28. ptr->applyCl(cl);
  29. }
  30. void applyOnClBefore(CClient *cl, void *pack) const
  31. {
  32. T *ptr = static_cast<T*>(pack);
  33. ptr->applyFirstCl(cl);
  34. }
  35. };
  36. static CApplier<CBaseForCLApply> *applier = NULL;
  37. void CClient::run()
  38. {
  39. setThreadName(-1, "CClient::run");
  40. try
  41. {
  42. CPack *pack = NULL;
  43. while(!terminate)
  44. {
  45. pack = serv->retreivePack(); //get the package from the server
  46. if (terminate)
  47. {
  48. delete pack;
  49. pack = NULL;
  50. break;
  51. }
  52. handlePack(pack);
  53. pack = NULL;
  54. }
  55. }
  56. catch (const std::exception& e)
  57. {
  58. tlog3 << "Lost connection to server, ending listening thread!\n";
  59. tlog1 << e.what() << std::endl;
  60. if(!terminate) //rethrow (-> boom!) only if closing connection was unexpected
  61. {
  62. tlog1 << "Something wrong, lost connection while game is still ongoing...\n";
  63. throw;
  64. }
  65. }
  66. }
  67. void CClient::handlePack( CPack * pack )
  68. {
  69. CBaseForCLApply *apply = applier->apps[typeList.getTypeID(pack)]; //find the applier
  70. if(apply)
  71. {
  72. apply->applyOnClBefore(this,pack);
  73. tlog5 << "\tMade first apply on cl\n";
  74. gs->apply(pack);
  75. tlog5 << "\tApplied on gs\n";
  76. apply->applyOnClAfter(this,pack);
  77. tlog5 << "\tMade second apply on cl\n";
  78. }
  79. else
  80. {
  81. tlog1 << "Message cannot be applied, cannot find applier! TypeID " << typeList.getTypeID(pack) << std::endl;
  82. }
  83. delete pack;
  84. }
  85. void CClient::requestMoveFromAI(const CStack *s)
  86. {
  87. boost::thread(&CClient::requestMoveFromAIWorker, this, s);
  88. }
  89. void CClient::requestMoveFromAIWorker(const CStack *s)
  90. {
  91. BattleAction ba;
  92. try
  93. {
  94. CheckTime timer("AI was thinking for ");
  95. ba = ai->activeStack(s);
  96. MakeAction temp_action(ba);
  97. *serv << &temp_action;
  98. }
  99. catch(...)
  100. {
  101. tlog0 << "AI thrown an exception!\n";
  102. }
  103. }
  104. CClient::CClient()
  105. {
  106. gs = NULL;
  107. serv = NULL;
  108. ai = NULL;
  109. curbaction = NULL;
  110. terminate = false;
  111. applier = new CApplier<CBaseForCLApply>;
  112. registerTypes2(*applier);
  113. }