LCOV - code coverage report
Current view: top level - wirestead/builder - udp_builder.hpp (source / functions) Coverage Total Hit
Test: Wirestead Coverage Report Lines: 100.0 % 2 2
Test Date: 2026-08-30 10:35:09 Functions: 100.0 % 1 1
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             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 <chrono>
      20                 :             : #include <cstddef>
      21                 :             : #include <cstdint>
      22                 :             : #include <optional>
      23                 :             : #include <string>
      24                 :             : #include <vector>
      25                 :             : 
      26                 :             : #include "wirestead/base/visibility.hpp"
      27                 :             : #include "wirestead/builder/ibuilder.hpp"
      28                 :             : #include "wirestead/wrapper/udp/udp.hpp"
      29                 :             : #include "wirestead/wrapper/udp/udp_server.hpp"
      30                 :             : 
      31                 :             : namespace wirestead {
      32                 :             : namespace builder {
      33                 :             : 
      34                 :             : #ifdef _MSC_VER
      35                 :             : #pragma warning(push)
      36                 :             : #pragma warning(disable : 4251)
      37                 :             : #endif
      38                 :             : 
      39                 :             : /**
      40                 :             :  * @brief Modernized Builder for UdpClient
      41                 :             :  */
      42                 :             : class WIRESTEAD_API UdpClientBuilder : public BuilderInterface<wrapper::UdpClient, UdpClientBuilder> {
      43                 :             :  public:
      44                 :             :   UdpClientBuilder();
      45                 :             :   explicit UdpClientBuilder(uint16_t local_port);
      46                 :             : 
      47                 :             :   // Delete copy
      48                 :             :   UdpClientBuilder(const UdpClientBuilder&) = delete;
      49                 :             :   UdpClientBuilder& operator=(const UdpClientBuilder&) = delete;
      50                 :             : 
      51                 :             :   std::unique_ptr<wrapper::UdpClient> build() override;
      52                 :             : 
      53                 :             :   UdpClientBuilder& auto_start(bool auto_start = true) override;
      54                 :             :   UdpClientBuilder& local_port(uint16_t port);
      55                 :             :   UdpClientBuilder& bind_address(const std::string& address);
      56                 :             :   [[deprecated("Use bind_address instead")]]
      57                 :             :   UdpClientBuilder& local_address(const std::string& address) {
      58                 :             :     return bind_address(address);
      59                 :             :   }
      60                 :             :   UdpClientBuilder& remote_endpoint(const std::string& host, uint16_t port);
      61                 :           3 :   UdpClientBuilder& remote(const std::string& host, uint16_t port) { return remote_endpoint(host, port); }
      62                 :             :   UdpClientBuilder& broadcast(bool enable = true);
      63                 :             :   UdpClientBuilder& reuse_address(bool enable = true);
      64                 :             :   // Join a multicast group after binding, so this socket receives datagrams
      65                 :             :   // addressed to it. `interface_address` picks the NIC by its local IPv4
      66                 :             :   // address; leave it empty to let the kernel choose, which is only reliable
      67                 :             :   // on a host with one interface. See UdpConfig::multicast_group.
      68                 :             :   //
      69                 :             :   // Multicast receivers usually want reuse_address(true) as well, so more than
      70                 :             :   // one process on the host can listen to the same group and port.
      71                 :             :   UdpClientBuilder& multicast_group(const std::string& group, const std::string& interface_address = "");
      72                 :             : 
      73                 :             :   UdpClientBuilder& independent_context(bool use_independent = true);
      74                 :             :   UdpClientBuilder& send_buffer_size(size_t bytes);
      75                 :             :   UdpClientBuilder& receive_buffer_size(size_t bytes);
      76                 :             : 
      77                 :             :  private:
      78                 :             :   uint16_t local_port_;
      79                 :             :   std::string bind_address_;
      80                 :             :   std::string remote_host_;
      81                 :             :   uint16_t remote_port_;
      82                 :             :   bool auto_start_;
      83                 :             :   bool independent_context_;
      84                 :             :   bool enable_broadcast_;
      85                 :             :   bool reuse_address_;
      86                 :             :   std::optional<std::string> multicast_group_;
      87                 :             :   std::optional<std::string> multicast_interface_;
      88                 :             : 
      89                 :             :   size_t send_buffer_size_;
      90                 :             :   size_t receive_buffer_size_;
      91                 :             : };
      92                 :             : 
      93                 :             : using UdpClientBuilderDefault = UdpClientBuilder;
      94                 :             : 
      95                 :             : /**
      96                 :             :  * @brief Modernized Builder for UdpServer
      97                 :             :  */
      98                 :             : class WIRESTEAD_API UdpServerBuilder : public BuilderInterface<wrapper::UdpServer, UdpServerBuilder> {
      99                 :             :  public:
     100                 :             :   UdpServerBuilder();
     101                 :             :   explicit UdpServerBuilder(uint16_t local_port);
     102                 :             : 
     103                 :             :   // Delete copy
     104                 :             :   UdpServerBuilder(const UdpServerBuilder&) = delete;
     105                 :             :   UdpServerBuilder& operator=(const UdpServerBuilder&) = delete;
     106                 :             : 
     107                 :             :   std::unique_ptr<wrapper::UdpServer> build() override;
     108                 :             : 
     109                 :             :   UdpServerBuilder& auto_start(bool auto_start = true) override;
     110                 :             :   UdpServerBuilder& local_port(uint16_t port);
     111                 :             :   UdpServerBuilder& bind_address(const std::string& address);
     112                 :             :   [[deprecated("Use bind_address instead")]]
     113                 :             :   UdpServerBuilder& local_address(const std::string& address) {
     114                 :             :     return bind_address(address);
     115                 :             :   }
     116                 :             :   UdpServerBuilder& max_clients(uint32_t max);
     117                 :             :   UdpServerBuilder& broadcast(bool enable = true);
     118                 :             :   UdpServerBuilder& reuse_address(bool enable = true);
     119                 :             :   // Join a multicast group after binding, so this socket receives datagrams
     120                 :             :   // addressed to it. `interface_address` picks the NIC by its local IPv4
     121                 :             :   // address; leave it empty to let the kernel choose, which is only reliable
     122                 :             :   // on a host with one interface. See UdpConfig::multicast_group.
     123                 :             :   //
     124                 :             :   // Multicast receivers usually want reuse_address(true) as well, so more than
     125                 :             :   // one process on the host can listen to the same group and port.
     126                 :             :   UdpServerBuilder& multicast_group(const std::string& group, const std::string& interface_address = "");
     127                 :             : 
     128                 :             :   UdpServerBuilder& independent_context(bool use_independent = true);
     129                 :             :   /**
     130                 :             :    * @brief Configure application-level idle timeout for virtual sessions.
     131                 :             :    *
     132                 :             :    * A value of 0ms disables idle timeout. When enabled, stale UDP virtual
     133                 :             :    * sessions are removed and a later datagram from the same endpoint creates a
     134                 :             :    * new virtual session.
     135                 :             :    */
     136                 :             :   UdpServerBuilder& idle_timeout(std::chrono::milliseconds timeout);
     137                 :             :   UdpServerBuilder& send_buffer_size(size_t bytes);
     138                 :             :   UdpServerBuilder& receive_buffer_size(size_t bytes);
     139                 :             : 
     140                 :             :  private:
     141                 :             :   uint16_t local_port_;
     142                 :             :   std::string bind_address_;
     143                 :             :   bool auto_start_;
     144                 :             :   bool independent_context_;
     145                 :             :   bool enable_broadcast_;
     146                 :             :   bool reuse_address_;
     147                 :             :   std::optional<std::string> multicast_group_;
     148                 :             :   std::optional<std::string> multicast_interface_;
     149                 :             : 
     150                 :             :   uint32_t max_clients_ = 0;
     151                 :             :   bool client_limit_enabled_ = false;
     152                 :           5 :   std::chrono::milliseconds idle_timeout_{0};
     153                 :             :   bool idle_timeout_set_ = false;
     154                 :             :   size_t send_buffer_size_;
     155                 :             :   size_t receive_buffer_size_;
     156                 :             : };
     157                 :             : 
     158                 :             : using UdpServerBuilderDefault = UdpServerBuilder;
     159                 :             : 
     160                 :             : #ifdef _MSC_VER
     161                 :             : #pragma warning(pop)
     162                 :             : #endif
     163                 :             : 
     164                 :             : }  // namespace builder
     165                 :             : }  // namespace wirestead
        

Generated by: LCOV version 2.0-1