Branch data Line data Source code
1 : : /*
2 : : * Copyright 2025 Jinwoo Sung
3 : : *
4 : : * Licensed under the Apache License, Version 2.0 (the "License");
5 : : * you may not use this file except in compliance with the License.
6 : : * You may obtain a copy of the License at
7 : : *
8 : : * http://www.apache.org/licenses/LICENSE-2.0
9 : : *
10 : : * Unless required by applicable law or agreed to in writing, software
11 : : * distributed under the License is distributed on an "AS IS" BASIS,
12 : : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 : : * See the License for the specific language governing permissions and
14 : : * limitations under the License.
15 : : */
16 : :
17 : : #pragma once
18 : :
19 : : #include <boost/asio/ip/udp.hpp>
20 : : #include <functional>
21 : : #include <memory>
22 : : #include <optional>
23 : : #include <vector>
24 : :
25 : : #include "wirestead/base/visibility.hpp"
26 : : #include "wirestead/config/udp_config.hpp"
27 : : #include "wirestead/diagnostics/error_types.hpp"
28 : : #include "wirestead/interface/channel.hpp"
29 : :
30 : : namespace boost {
31 : : namespace asio {
32 : : class io_context;
33 : : }
34 : : } // namespace boost
35 : :
36 : : namespace wirestead {
37 : : namespace transport {
38 : :
39 : : /**
40 : : * @brief UDP Transport implementation with 1:N support
41 : : */
42 : : class WIRESTEAD_API UdpChannel : public interface::Channel, public std::enable_shared_from_this<UdpChannel> {
43 : : public:
44 : : using OnBytesFrom = std::function<void(memory::ConstByteSpan, const boost::asio::ip::udp::endpoint&)>;
45 : :
46 : : static std::shared_ptr<UdpChannel> create(const config::UdpConfig& cfg);
47 : : static std::shared_ptr<UdpChannel> create(const config::UdpConfig& cfg, boost::asio::io_context& ioc);
48 : : ~UdpChannel() override;
49 : :
50 : : // Move semantics
51 : : UdpChannel(UdpChannel&&) noexcept;
52 : : UdpChannel& operator=(UdpChannel&&) noexcept;
53 : :
54 : : // Non-copyable
55 : : UdpChannel(const UdpChannel&) = delete;
56 : : UdpChannel& operator=(const UdpChannel&) = delete;
57 : :
58 : : // Channel implementation
59 : : void start() override;
60 : : void stop() override;
61 : : bool is_connected() const override;
62 : : bool is_backpressure_active() const override;
63 : : wrapper::RuntimeStats stats() const override;
64 : : void reset_stats() override;
65 : : std::optional<diagnostics::ErrorInfo> last_error_info() const override;
66 : :
67 : : // 1:1 writes (using configured remote_endpoint_)
68 : : bool async_write_copy(memory::ConstByteSpan data) override;
69 : : bool async_write_move(std::vector<uint8_t>&& data) override;
70 : : bool async_write_shared(std::shared_ptr<const std::vector<uint8_t>> data) override;
71 : : bool async_try_write_copy(memory::ConstByteSpan data) override;
72 : : bool async_try_write_move(std::vector<uint8_t>&& data) override;
73 : : bool async_try_write_shared(std::shared_ptr<const std::vector<uint8_t>> data) override;
74 : :
75 : : // 1:N writes (explicit destination)
76 : : virtual bool async_write_to(memory::ConstByteSpan data, const boost::asio::ip::udp::endpoint& destination);
77 : : virtual bool async_try_write_to(memory::ConstByteSpan data, const boost::asio::ip::udp::endpoint& destination);
78 : :
79 : : // Callbacks
80 : : void on_bytes(OnBytes cb) override;
81 : : virtual void on_bytes_from(OnBytesFrom cb);
82 : : void on_state(OnState cb) override;
83 : : void on_backpressure(OnBackpressure cb) override;
84 : :
85 : : void set_backpressure_strategy(base::constants::BackpressureStrategy strategy);
86 : :
87 : : /**
88 : : * @brief Get the local endpoint the socket is bound to.
89 : : */
90 : : boost::asio::ip::udp::endpoint local_endpoint() const;
91 : :
92 : : /**
93 : : * @brief Get the ASIO executor for this channel.
94 : : */
95 : : boost::asio::any_io_executor get_executor() override;
96 : :
97 : : private:
98 : : explicit UdpChannel(const config::UdpConfig& cfg);
99 : : UdpChannel(const config::UdpConfig& cfg, boost::asio::io_context& ioc);
100 : :
101 : : struct Impl;
102 : 169 : const Impl* get_impl() const { return impl_.get(); }
103 : 829248 : Impl* get_impl() { return impl_.get(); }
104 : : std::unique_ptr<Impl> impl_;
105 : : };
106 : :
107 : : } // namespace transport
108 : : } // namespace wirestead
|