TLA Line data Source code
1 : //
2 : // Copyright (c) 2026 Steve Gerbino
3 : //
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)
6 : //
7 : // Official repository: https://github.com/cppalliance/corosio
8 : //
9 :
10 : #ifndef BOOST_COROSIO_DETAIL_UDP_SERVICE_HPP
11 : #define BOOST_COROSIO_DETAIL_UDP_SERVICE_HPP
12 :
13 : #include <boost/corosio/detail/config.hpp>
14 : #include <boost/corosio/udp_socket.hpp>
15 : #include <boost/corosio/endpoint.hpp>
16 : #include <boost/capy/ex/execution_context.hpp>
17 : #include <system_error>
18 :
19 : namespace boost::corosio::detail {
20 :
21 : /** Abstract UDP service base class.
22 :
23 : Concrete implementations (epoll_udp_service,
24 : select_udp_service, etc.) inherit from this class and
25 : provide platform-specific datagram socket operations. The
26 : context constructor installs whichever backend via
27 : `make_service`, and `udp_socket.cpp` retrieves it via
28 : `use_service<udp_service>()`.
29 : */
30 : class BOOST_COROSIO_DECL udp_service
31 : : public capy::execution_context::service
32 : , public io_object::io_service
33 : {
34 : public:
35 : /// Identifies this service for `execution_context` lookup.
36 : using key_type = udp_service;
37 :
38 : /** Open a datagram socket.
39 :
40 : Creates a socket and associates it with the platform reactor.
41 :
42 : @param impl The socket implementation to open.
43 : @param family Address family (e.g. `AF_INET`, `AF_INET6`).
44 : @param type Socket type (`SOCK_DGRAM`).
45 : @param protocol Protocol number (`IPPROTO_UDP`).
46 : @return Error code on failure, empty on success.
47 : */
48 : virtual std::error_code open_datagram_socket(
49 : udp_socket::implementation& impl,
50 : int family,
51 : int type,
52 : int protocol) = 0;
53 :
54 : /** Assign an existing native socket handle to a socket.
55 :
56 : Adopts a pre-created socket handle. On success the impl
57 : takes ownership and will close the handle. On failure the
58 : caller retains ownership and must close it. If the impl is
59 : already open, its pending operations are cancelled and the
60 : held socket is closed before the new one is adopted.
61 :
62 : @param impl The socket implementation to assign to.
63 : @param fd The native socket handle to adopt.
64 : @return Error code on failure, empty on success.
65 : */
66 : virtual std::error_code assign_socket(
67 : udp_socket::implementation& impl,
68 : native_handle_type fd) = 0;
69 :
70 : /** Bind a datagram socket to a local endpoint.
71 :
72 : @param impl The socket implementation to bind.
73 : @param ep The local endpoint to bind to.
74 : @return Error code on failure, empty on success.
75 : */
76 : virtual std::error_code
77 : bind_datagram(udp_socket::implementation& impl, endpoint ep) = 0;
78 :
79 : protected:
80 : /// Construct the UDP service.
81 HIT 1603 : udp_service() = default;
82 :
83 : /// Destroy the UDP service.
84 1603 : ~udp_service() override = default;
85 : };
86 :
87 : } // namespace boost::corosio::detail
88 :
89 : #endif // BOOST_COROSIO_DETAIL_UDP_SERVICE_HPP
|