NetPacksBase.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 applyGs(CGameState * gs)
  30. {}
  31. void visit(ICPackVisitor & cpackVisitor);
  32. protected:
  33. /// <summary>
  34. /// For basic types of netpacks hierarchy like CPackForClient. Called first.
  35. /// </summary>
  36. virtual void visitBasic(ICPackVisitor & cpackVisitor);
  37. /// <summary>
  38. /// For leaf types of netpacks hierarchy. Called after visitBasic.
  39. /// </summary>
  40. virtual void visitTyped(ICPackVisitor & cpackVisitor);
  41. };
  42. struct DLL_LINKAGE CPackForClient : public CPack
  43. {
  44. protected:
  45. void visitBasic(ICPackVisitor & cpackVisitor) override;
  46. };
  47. struct DLL_LINKAGE Query : public CPackForClient
  48. {
  49. QueryID queryID; // equals to -1 if it is not an actual query (and should not be answered)
  50. };
  51. struct DLL_LINKAGE CPackForServer : public CPack
  52. {
  53. mutable PlayerColor player = PlayerColor::NEUTRAL;
  54. mutable uint32_t requestID = 0;
  55. template <typename Handler> void serialize(Handler &h)
  56. {
  57. h & player;
  58. h & requestID;
  59. }
  60. protected:
  61. void visitBasic(ICPackVisitor & cpackVisitor) override;
  62. };
  63. struct DLL_LINKAGE CPackForLobby : public CPack
  64. {
  65. virtual bool isForServer() const;
  66. protected:
  67. void visitBasic(ICPackVisitor & cpackVisitor) override;
  68. };
  69. VCMI_LIB_NAMESPACE_END