definition.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #pragma once
  2. #include <array>
  3. #include <cstddef>
  4. #include <cstdint>
  5. namespace sunrise::core::settings::server::gameplay {
  6. /** Process that owns the gameplay UDP endpoint for this run. */
  7. enum class Topology : std::uint8_t {
  8. /** No endpoint is bound and no method-0 descriptor is advertised. */
  9. disabled,
  10. /** Sunrise binds the endpoint and hosts the peer protocol itself. */
  11. embedded,
  12. /** A configured process owns the endpoint. Sunrise binds nothing. */
  13. external,
  14. };
  15. /** Octets in one IPv4 address. */
  16. inline constexpr std::size_t kAddressOctets = 4;
  17. /** The join descriptor carries an even UDP port, so an odd port is refused at load. */
  18. inline constexpr std::uint16_t kPortAlignment = 2;
  19. /** Even, and clear of both discovery ports. */
  20. inline constexpr std::uint16_t kDefaultPort = 30976;
  21. /** Discovery owns 3074 and 3075, so the gameplay endpoint may take neither. */
  22. inline constexpr std::uint16_t kDiscoveryPortLow = 3074;
  23. /** Upper discovery port. See kDiscoveryPortLow. */
  24. inline constexpr std::uint16_t kDiscoveryPortHigh = 3075;
  25. /** One carrier, the live wave, and removal headroom fit in this many reserved slots. */
  26. inline constexpr std::uint16_t kDefaultServerReserve = 256;
  27. /** A smaller reserve cannot hold one carrier plus its removal and quarantine headroom. */
  28. inline constexpr std::uint16_t kMinimumServerReserve = 8;
  29. /** The client keeps at least this many lease bits after the reserve is subtracted. */
  30. inline constexpr std::uint16_t kClientLeaseMinimum = 4096;
  31. /**
  32. * Gameplay endpoint topology and the entity-slot split it implies.
  33. * The advertised address is the one message 12 publishes. The transport address is where the
  34. * datagram lands after the Client's egress rewrite. Both carry the same port.
  35. */
  36. struct Settings {
  37. /** Embedded by default. Public activities reach each other over the peer protocol. */
  38. Topology topology{Topology::embedded};
  39. /** Local interface the embedded endpoint binds. */
  40. std::array<unsigned char, kAddressOctets> bindAddress{127, 0, 0, 1};
  41. /** Address written into the published descriptor. */
  42. std::array<unsigned char, kAddressOctets> advertisedAddress{127, 0, 0, 1};
  43. /** Address the Client's egress rewrite produces. Must reach the bound endpoint. */
  44. std::array<unsigned char, kAddressOctets> transportAddress{127, 0, 0, 1};
  45. /** Even UDP port, shared by all three addresses because the egress hook keeps the port. */
  46. std::uint16_t port{kDefaultPort};
  47. /** Entity indices held back from the client lease for server-authored entities. */
  48. std::uint16_t serverReserveCount{kDefaultServerReserve};
  49. };
  50. /**
  51. * Checks one gameplay block for internal consistency.
  52. * @param settings Parsed or default gameplay settings.
  53. * @return True when the topology, port, and reserve can be bound and advertised together.
  54. */
  55. [[nodiscard]] bool valid(const Settings& settings) noexcept;
  56. /**
  57. * Reports the slots actually held back from the client lease.
  58. * A disabled channel authors no entities, so it reserves nothing and the client keeps the
  59. * whole slot space.
  60. * @param settings Active gameplay settings.
  61. * @return Configured reserve, or zero while the channel is off.
  62. */
  63. [[nodiscard]] std::uint16_t effective_reserve(const Settings& settings) noexcept;
  64. } // namespace sunrise::core::settings::server::gameplay