src/corosio/src/tcp_socket.cpp

100.0% Lines (55/0/55) 100.0% List of functions (13/0/13)
tcp_socket.cpp
f(x) Functions (13)
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2025 Vinnie Falco ([email protected])
3 // Copyright (c) 2026 Steve Gerbino
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 //
8 // Official repository: https://github.com/cppalliance/corosio
9 //
10
11 #include <boost/corosio/tcp_socket.hpp>
12 #include <boost/corosio/detail/except.hpp>
13 #include <boost/corosio/detail/platform.hpp>
14
15 #if BOOST_COROSIO_HAS_IOCP
16 #include <boost/corosio/native/detail/iocp/win_tcp_acceptor_service.hpp>
17 #else
18 #include <boost/corosio/detail/tcp_service.hpp>
19 #endif
20
21 namespace boost::corosio {
22
23 24795x tcp_socket::~tcp_socket()
24 {
25 24795x close();
26 24795x }
27
28 20845x tcp_socket::tcp_socket(capy::execution_context& ctx)
29 #if BOOST_COROSIO_HAS_IOCP
30 : io_object(create_handle<detail::win_tcp_service>(ctx))
31 #else
32 20845x : io_object(create_handle<detail::tcp_service>(ctx))
33 #endif
34 {
35 20845x }
36
37 std::error_code
38 10242x tcp_socket::open(tcp proto) noexcept
39 {
40 10242x if (is_open())
41 6x return {};
42 10236x return open_for_family(proto.family(), proto.type(), proto.protocol());
43 }
44
45 std::error_code
46 10236x tcp_socket::open_for_family(int family, int type, int protocol) noexcept
47 {
48 #if BOOST_COROSIO_HAS_IOCP
49 auto& svc = static_cast<detail::win_tcp_service&>(h_.service());
50 auto& wrapper = static_cast<tcp_socket::implementation&>(*h_.get());
51 std::error_code ec = svc.open_socket(
52 *static_cast<detail::win_tcp_socket&>(wrapper).get_internal(), family, type,
53 protocol);
54 #else
55 10236x auto& svc = static_cast<detail::tcp_service&>(h_.service());
56 10236x std::error_code ec = svc.open_socket(
57 10236x static_cast<tcp_socket::implementation&>(*h_.get()), family, type,
58 protocol);
59 #endif
60 10236x return ec;
61 }
62
63 std::error_code
64 27x tcp_socket::assign(native_handle_type fd) noexcept
65 {
66 #if BOOST_COROSIO_HAS_IOCP
67 auto& svc = static_cast<detail::win_tcp_service&>(h_.service());
68 auto& wrapper = static_cast<tcp_socket::implementation&>(*h_.get());
69 std::error_code ec = svc.assign_socket(
70 *static_cast<detail::win_tcp_socket&>(wrapper).get_internal(), fd);
71 #else
72 27x auto& svc = static_cast<detail::tcp_service&>(h_.service());
73 27x std::error_code ec = svc.assign_socket(
74 27x static_cast<tcp_socket::implementation&>(*h_.get()), fd);
75 #endif
76 27x return ec;
77 }
78
79 native_handle_type
80 6x tcp_socket::release()
81 {
82 6x if (!is_open())
83 3x detail::throw_system_error(
84 3x make_error_code(std::errc::bad_file_descriptor),
85 "tcp_socket::release");
86 3x return get().release_socket();
87 }
88
89 std::error_code
90 24x tcp_socket::bind(endpoint ep) noexcept
91 {
92 24x if (!is_open())
93 3x return make_error_code(std::errc::bad_file_descriptor);
94 #if BOOST_COROSIO_HAS_IOCP
95 auto& svc = static_cast<detail::win_tcp_service&>(h_.service());
96 auto& wrapper = static_cast<tcp_socket::implementation&>(*h_.get());
97 return svc.bind_socket(
98 *static_cast<detail::win_tcp_socket&>(wrapper).get_internal(), ep);
99 #else
100 21x auto& svc = static_cast<detail::tcp_service&>(h_.service());
101 21x return svc.bind_socket(
102 42x static_cast<tcp_socket::implementation&>(*h_.get()), ep);
103 #endif
104 }
105
106 void
107 45503x tcp_socket::close() noexcept
108 {
109 45503x if (!is_open())
110 25145x return;
111 20358x h_.service().close(h_);
112 }
113
114 void
115 330x tcp_socket::cancel() noexcept
116 {
117 330x if (!is_open())
118 3x return;
119 327x get().cancel();
120 }
121
122 std::error_code
123 32x tcp_socket::shutdown(shutdown_type what) noexcept
124 {
125 32x if (!is_open())
126 9x return make_error_code(std::errc::bad_file_descriptor);
127 23x return get().shutdown(what);
128 }
129
130 native_handle_type
131 50x tcp_socket::native_handle() const noexcept
132 {
133 50x if (!is_open())
134 {
135 #if BOOST_COROSIO_HAS_IOCP
136 return static_cast<native_handle_type>(~0ull); // INVALID_SOCKET
137 #else
138 3x return -1;
139 #endif
140 }
141 47x return get().native_handle();
142 }
143
144 endpoint
145 102x tcp_socket::local_endpoint() const noexcept
146 {
147 102x if (!is_open())
148 15x return endpoint{};
149 87x return get().local_endpoint();
150 }
151
152 endpoint
153 99x tcp_socket::remote_endpoint() const noexcept
154 {
155 99x if (!is_open())
156 15x return endpoint{};
157 84x return get().remote_endpoint();
158 }
159
160 } // namespace boost::corosio
161