consumer.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #pragma once
  2. #include <cstddef>
  3. #include <cstdint>
  4. #include <span>
  5. #include <string_view>
  6. namespace sunrise::client::network {
  7. /** The largest svc8 frame is a 0x7D800-byte activity payload plus 49 wire bytes. */
  8. inline constexpr std::size_t kBapFrameCapacity = 0x7D800 + 49;
  9. /**
  10. * Fixed BAP connection slots shared by the transport and the Server.
  11. * A crossing holds the old and the new activity link at once, so a lobby plus three activity
  12. * links is reachable. Below that the accept is refused and the connect stalls.
  13. */
  14. inline constexpr std::size_t kBapConnectionCount = 8;
  15. /** HTTP request view passed from Client hooks to Server. Every span carries its size. */
  16. struct HttpRequest {
  17. std::string_view url;
  18. std::string_view contentType;
  19. std::span<const std::byte> body;
  20. std::span<std::byte> response;
  21. };
  22. /** HTTP completion fields written by the Server consumer. */
  23. struct HttpResponse {
  24. std::size_t size{};
  25. unsigned statusCode{};
  26. };
  27. /** Lifecycle event raised by the BAP transport. */
  28. enum class BapEvent : std::uint8_t {
  29. open,
  30. frame,
  31. /** Timed service event, with no inbound bytes. */
  32. poll,
  33. close,
  34. };
  35. /** Connection-scoped BAP exchange with caller-owned frame storage. */
  36. struct BapRequest {
  37. BapEvent event{};
  38. std::uint32_t connectionId{};
  39. std::span<const std::byte> frame{};
  40. std::span<std::byte> response{};
  41. };
  42. /** BAP completion fields written by the Server consumer. */
  43. struct BapResponse {
  44. std::size_t size{};
  45. };
  46. /** In-process HTTP route with caller-owned request and response storage. */
  47. using HttpConsumer = bool (*)(const HttpRequest&, HttpResponse&) noexcept;
  48. /** In-process BAP route with explicit connection lifecycle events. */
  49. using BapConsumer = bool (*)(const BapRequest&, BapResponse&) noexcept;
  50. /** Registers the Server consumer used by the HTTP detour. */
  51. [[nodiscard]] bool register_http_consumer(HttpConsumer consumer) noexcept;
  52. /** Removes the Server consumer when it still matches. */
  53. void unregister_http_consumer(HttpConsumer consumer) noexcept;
  54. /** Registers the Server BAP consumer. */
  55. [[nodiscard]] bool register_bap_consumer(BapConsumer consumer) noexcept;
  56. /** Removes the Server BAP consumer when it still matches. */
  57. void unregister_bap_consumer(BapConsumer consumer) noexcept;
  58. } // namespace sunrise::client::network