|
| 1 | +// |
| 2 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | +// |
| 5 | + |
| 6 | +#ifndef OCVSMD_COMMON_IPC_CHANNEL_HPP_INCLUDED |
| 7 | +#define OCVSMD_COMMON_IPC_CHANNEL_HPP_INCLUDED |
| 8 | + |
| 9 | +#include "dsdl_helpers.hpp" |
| 10 | +#include "gateway.hpp" |
| 11 | + |
| 12 | +#include <cetl/pf17/cetlpf.hpp> |
| 13 | + |
| 14 | +#include <cstddef> |
| 15 | +#include <functional> |
| 16 | +#include <utility> |
| 17 | + |
| 18 | +namespace ocvsmd |
| 19 | +{ |
| 20 | +namespace common |
| 21 | +{ |
| 22 | +namespace ipc |
| 23 | +{ |
| 24 | + |
| 25 | +class AnyChannel |
| 26 | +{ |
| 27 | +public: |
| 28 | + struct Connected |
| 29 | + {}; |
| 30 | + |
| 31 | + struct Disconnected |
| 32 | + {}; |
| 33 | + |
| 34 | + template <typename Input> |
| 35 | + using EventVar = cetl::variant<Input, Connected, Disconnected>; |
| 36 | + |
| 37 | + template <typename Input> |
| 38 | + using EventHandler = std::function<void(const EventVar<Input>&)>; |
| 39 | + |
| 40 | +protected: |
| 41 | + AnyChannel() = default; |
| 42 | + |
| 43 | +}; // AnyChannel |
| 44 | + |
| 45 | +template <typename Input, typename Output> |
| 46 | +class Channel final : public AnyChannel |
| 47 | +{ |
| 48 | +public: |
| 49 | + Channel(Channel&& other) noexcept |
| 50 | + : gateway_{std::move(other.gateway_)} |
| 51 | + , event_handler_{std::move(other.event_handler_)} |
| 52 | + { |
| 53 | + setupEventHandler(); |
| 54 | + } |
| 55 | + |
| 56 | + Channel& operator=(Channel&& other) noexcept |
| 57 | + { |
| 58 | + if (this != &other) |
| 59 | + { |
| 60 | + gateway_ = std::move(other.gateway_); |
| 61 | + event_handler_ = std::move(other.event_handler_); |
| 62 | + setupEventHandler(); |
| 63 | + } |
| 64 | + return *this; |
| 65 | + } |
| 66 | + |
| 67 | + Channel(const Channel&) = delete; |
| 68 | + Channel& operator=(const Channel&) = delete; |
| 69 | + |
| 70 | + ~Channel() |
| 71 | + { |
| 72 | + gateway_->setEventHandler(nullptr); |
| 73 | + event_handler_ = nullptr; |
| 74 | + } |
| 75 | + |
| 76 | + void send(const Output& output) |
| 77 | + { |
| 78 | + constexpr std::size_t BufferSize = Output::; |
| 79 | + constexpr bool IsOnStack = BufferSize <= MsgSmallPayloadSize; |
| 80 | + |
| 81 | + return tryPerformOnSerialized<Output, Result, BufferSize, IsOnStack>( // |
| 82 | + output, |
| 83 | + [this](const auto payload) { |
| 84 | + // |
| 85 | + gateway_->send(payload); |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | +private: |
| 90 | + friend class ClientRouter; |
| 91 | + |
| 92 | + Channel(detail::Gateway::Ptr gateway, EventHandler<Input> event_handler) |
| 93 | + : gateway_{std::move(gateway)} |
| 94 | + , event_handler_{std::move(event_handler)} |
| 95 | + { |
| 96 | + CETL_DEBUG_ASSERT(gateway_, ""); |
| 97 | + CETL_DEBUG_ASSERT(event_handler_, ""); |
| 98 | + |
| 99 | + setupEventHandler(); |
| 100 | + } |
| 101 | + |
| 102 | + void setupEventHandler() |
| 103 | + { |
| 104 | + gateway_->setEventHandler([this](const auto& event) { |
| 105 | + // |
| 106 | + event_handler_(event); |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + static constexpr std::size_t MsgSmallPayloadSize = 256; |
| 111 | + |
| 112 | + detail::Gateway::Ptr gateway_; |
| 113 | + EventHandler<Input> event_handler_; |
| 114 | + |
| 115 | +}; // Channel |
| 116 | + |
| 117 | +} // namespace ipc |
| 118 | +} // namespace common |
| 119 | +} // namespace ocvsmd |
| 120 | + |
| 121 | +#endif // OCVSMD_COMMON_IPC_CHANNEL_HPP_INCLUDED |
0 commit comments