unilink  0.4.3
A simple C++ library for unified async communication
udp.hpp
Go to the documentation of this file.
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 <memory>
20 #include <vector>
21 
25 
26 namespace boost {
27 namespace asio {
28 class io_context;
29 }
30 } // namespace boost
31 
32 namespace unilink {
33 namespace transport {
34 
38 class UNILINK_API UdpChannel : public interface::Channel, public std::enable_shared_from_this<UdpChannel> {
39  public:
40  static std::shared_ptr<UdpChannel> create(const config::UdpConfig& cfg);
41  static std::shared_ptr<UdpChannel> create(const config::UdpConfig& cfg, boost::asio::io_context& ioc);
42  ~UdpChannel() override;
43 
44  // Move semantics
45  UdpChannel(UdpChannel&&) noexcept;
46  UdpChannel& operator=(UdpChannel&&) noexcept;
47 
48  // Non-copyable
49  UdpChannel(const UdpChannel&) = delete;
50  UdpChannel& operator=(const UdpChannel&) = delete;
51 
52  // Channel implementation
53  void start() override;
54  void stop() override;
55  bool is_connected() const override;
56 
57  void async_write_copy(memory::ConstByteSpan data) override;
58  void async_write_move(std::vector<uint8_t>&& data) override;
59  void async_write_shared(std::shared_ptr<const std::vector<uint8_t>> data) override;
60 
61  void on_bytes(OnBytes cb) override;
62  void on_state(OnState cb) override;
63  void on_backpressure(OnBackpressure cb) override;
64 
65  private:
66  explicit UdpChannel(const config::UdpConfig& cfg);
67  UdpChannel(const config::UdpConfig& cfg, boost::asio::io_context& ioc);
68 
69  struct Impl;
70  const Impl* get_impl() const { return impl_.get(); }
71  Impl* get_impl() { return impl_.get(); }
72  std::unique_ptr<Impl> impl_;
73 };
74 
75 } // namespace transport
76 } // namespace unilink
Definition: serial.hpp:27
#define UNILINK_API
Definition: visibility.hpp:37