1  
//
1  
//
2  
// Copyright (c) 2026 Steve Gerbino
2  
// Copyright (c) 2026 Steve Gerbino
3  
//
3  
//
4  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
4  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6  
//
6  
//
7  
// Official repository: https://github.com/cppalliance/corosio
7  
// Official repository: https://github.com/cppalliance/corosio
8  
//
8  
//
9  

9  

10  
#ifndef BOOST_COROSIO_DETAIL_UDP_SERVICE_HPP
10  
#ifndef BOOST_COROSIO_DETAIL_UDP_SERVICE_HPP
11  
#define BOOST_COROSIO_DETAIL_UDP_SERVICE_HPP
11  
#define BOOST_COROSIO_DETAIL_UDP_SERVICE_HPP
12  

12  

13  
#include <boost/corosio/detail/config.hpp>
13  
#include <boost/corosio/detail/config.hpp>
14  
#include <boost/corosio/udp_socket.hpp>
14  
#include <boost/corosio/udp_socket.hpp>
15  
#include <boost/corosio/endpoint.hpp>
15  
#include <boost/corosio/endpoint.hpp>
16  
#include <boost/capy/ex/execution_context.hpp>
16  
#include <boost/capy/ex/execution_context.hpp>
17  
#include <system_error>
17  
#include <system_error>
18  

18  

19  
namespace boost::corosio::detail {
19  
namespace boost::corosio::detail {
20  

20  

21  
/** Abstract UDP service base class.
21  
/** Abstract UDP service base class.
22  

22  

23  
    Concrete implementations (epoll_udp_service,
23  
    Concrete implementations (epoll_udp_service,
24  
    select_udp_service, etc.) inherit from this class and
24  
    select_udp_service, etc.) inherit from this class and
25  
    provide platform-specific datagram socket operations. The
25  
    provide platform-specific datagram socket operations. The
26  
    context constructor installs whichever backend via
26  
    context constructor installs whichever backend via
27  
    `make_service`, and `udp_socket.cpp` retrieves it via
27  
    `make_service`, and `udp_socket.cpp` retrieves it via
28  
    `use_service<udp_service>()`.
28  
    `use_service<udp_service>()`.
29  
*/
29  
*/
30  
class BOOST_COROSIO_DECL udp_service
30  
class BOOST_COROSIO_DECL udp_service
31  
    : public capy::execution_context::service
31  
    : public capy::execution_context::service
32  
    , public io_object::io_service
32  
    , public io_object::io_service
33  
{
33  
{
34  
public:
34  
public:
35  
    /// Identifies this service for `execution_context` lookup.
35  
    /// Identifies this service for `execution_context` lookup.
36  
    using key_type = udp_service;
36  
    using key_type = udp_service;
37  

37  

38  
    /** Open a datagram socket.
38  
    /** Open a datagram socket.
39  

39  

40  
        Creates a socket and associates it with the platform reactor.
40  
        Creates a socket and associates it with the platform reactor.
41  

41  

42  
        @param impl The socket implementation to open.
42  
        @param impl The socket implementation to open.
43  
        @param family Address family (e.g. `AF_INET`, `AF_INET6`).
43  
        @param family Address family (e.g. `AF_INET`, `AF_INET6`).
44  
        @param type Socket type (`SOCK_DGRAM`).
44  
        @param type Socket type (`SOCK_DGRAM`).
45  
        @param protocol Protocol number (`IPPROTO_UDP`).
45  
        @param protocol Protocol number (`IPPROTO_UDP`).
46  
        @return Error code on failure, empty on success.
46  
        @return Error code on failure, empty on success.
47  
    */
47  
    */
48  
    virtual std::error_code open_datagram_socket(
48  
    virtual std::error_code open_datagram_socket(
49  
        udp_socket::implementation& impl,
49  
        udp_socket::implementation& impl,
50  
        int family,
50  
        int family,
51  
        int type,
51  
        int type,
52  
        int protocol) = 0;
52  
        int protocol) = 0;
53  

53  

54  
    /** Bind a datagram socket to a local endpoint.
54  
    /** Bind a datagram socket to a local endpoint.
55  

55  

56  
        @param impl The socket implementation to bind.
56  
        @param impl The socket implementation to bind.
57  
        @param ep The local endpoint to bind to.
57  
        @param ep The local endpoint to bind to.
58  
        @return Error code on failure, empty on success.
58  
        @return Error code on failure, empty on success.
59  
    */
59  
    */
60  
    virtual std::error_code
60  
    virtual std::error_code
61  
    bind_datagram(udp_socket::implementation& impl, endpoint ep) = 0;
61  
    bind_datagram(udp_socket::implementation& impl, endpoint ep) = 0;
62  

62  

63  
protected:
63  
protected:
64  
    /// Construct the UDP service.
64  
    /// Construct the UDP service.
65  
    udp_service() = default;
65  
    udp_service() = default;
66  

66  

67  
    /// Destroy the UDP service.
67  
    /// Destroy the UDP service.
68  
    ~udp_service() override = default;
68  
    ~udp_service() override = default;
69  
};
69  
};
70  

70  

71  
} // namespace boost::corosio::detail
71  
} // namespace boost::corosio::detail
72  

72  

73  
#endif // BOOST_COROSIO_DETAIL_UDP_SERVICE_HPP
73  
#endif // BOOST_COROSIO_DETAIL_UDP_SERVICE_HPP