NetPacksBase.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * NetPacksBase.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "../constants/EntityIdentifiers.h"
  12. #include "../serializer/Serializeable.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. class CGameState;
  15. class CConnection;
  16. class ICPackVisitor;
  17. struct DLL_LINKAGE CPack : public Serializeable
  18. {
  19. /// Pointer to connection that pack received from
  20. /// Only set & used on server
  21. std::shared_ptr<CConnection> c;
  22. CPack() = default;
  23. virtual ~CPack() = default;
  24. template <typename Handler> void serialize(Handler &h)
  25. {
  26. logNetwork->error("CPack serialized... this should not happen!");
  27. throw std::runtime_error("CPack serialized... this should not happen!");
  28. }
  29. void visit(ICPackVisitor & cpackVisitor);
  30. protected:
  31. /// <summary>
  32. /// For basic types of netpacks hierarchy like CPackForClient. Called first.
  33. /// </summary>
  34. virtual void visitBasic(ICPackVisitor & cpackVisitor);
  35. /// <summary>
  36. /// For leaf types of netpacks hierarchy. Called after visitBasic.
  37. /// </summary>
  38. virtual void visitTyped(ICPackVisitor & cpackVisitor);
  39. };
  40. struct DLL_LINKAGE CPackForClient : public CPack
  41. {
  42. virtual void applyGs(CGameState * gs) = 0;
  43. protected:
  44. void visitBasic(ICPackVisitor & cpackVisitor) override;
  45. };
  46. struct DLL_LINKAGE Query : public CPackForClient
  47. {
  48. QueryID queryID; // equals to -1 if it is not an actual query (and should not be answered)
  49. };
  50. struct DLL_LINKAGE CPackForServer : public CPack
  51. {
  52. mutable PlayerColor player = PlayerColor::NEUTRAL;
  53. mutable uint32_t requestID = 0;
  54. template <typename Handler> void serialize(Handler &h)
  55. {
  56. h & player;
  57. h & requestID;
  58. }
  59. protected:
  60. void visitBasic(ICPackVisitor & cpackVisitor) override;
  61. };
  62. struct DLL_LINKAGE CPackForLobby : public CPack
  63. {
  64. virtual bool isForServer() const;
  65. protected:
  66. void visitBasic(ICPackVisitor & cpackVisitor) override;
  67. };
  68. VCMI_LIB_NAMESPACE_END