unilink  0.4.3
A simple C++ library for unified async communication
udp_builder.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 <cstdint>
20 #include <string>
21 
26 
27 namespace unilink {
28 namespace builder {
29 
33 #ifdef _MSC_VER
34 #pragma warning(push)
35 #pragma warning(disable : 4251)
36 #endif
37 class UNILINK_API UdpBuilder : public BuilderInterface<wrapper::Udp, UdpBuilder> {
38  public:
39  UdpBuilder();
40 
41  // Delete copy, allow move
42  UdpBuilder(const UdpBuilder&) = delete;
43  UdpBuilder& operator=(const UdpBuilder&) = delete;
44  UdpBuilder(UdpBuilder&&) = default;
46 
47  std::unique_ptr<wrapper::Udp> build() override;
48 
49  UdpBuilder& auto_manage(bool auto_manage = true) override;
50 
51  // Modernized event handlers
52  UdpBuilder& on_data(std::function<void(const wrapper::MessageContext&)> handler) override;
53  UdpBuilder& on_connect(std::function<void(const wrapper::ConnectionContext&)> handler) override;
54  UdpBuilder& on_disconnect(std::function<void(const wrapper::ConnectionContext&)> handler) override;
55  UdpBuilder& on_error(std::function<void(const wrapper::ErrorContext&)> handler) override;
56 
60  UdpBuilder& set_local_port(uint16_t port);
61 
65  UdpBuilder& set_remote(const std::string& address, uint16_t port);
66 
70  UdpBuilder& use_independent_context(bool use_independent = true);
71 
72  private:
73  config::UdpConfig cfg_;
74  bool auto_manage_;
75  bool use_independent_context_;
76 
77  // Callbacks
78  std::function<void(const wrapper::MessageContext&)> on_data_;
79  std::function<void(const wrapper::ConnectionContext&)> on_connect_;
80  std::function<void(const wrapper::ConnectionContext&)> on_disconnect_;
81  std::function<void(const wrapper::ErrorContext&)> on_error_;
82 };
83 
84 #ifdef _MSC_VER
85 #pragma warning(pop)
86 #endif
87 
88 } // namespace builder
89 } // namespace unilink
#define UNILINK_API
Definition: visibility.hpp:37