98.28% Lines (57/58) 100.00% Functions (13/13)
TLA Baseline Branch
Line Hits Code Line Hits Code
1   // 1   //
2   // Copyright (c) 2026 Michael Vandeberg 2   // Copyright (c) 2026 Michael Vandeberg
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   #include <boost/corosio/detail/platform.hpp> 10   #include <boost/corosio/detail/platform.hpp>
11   11  
12   #if BOOST_COROSIO_POSIX || BOOST_COROSIO_HAS_IOCP 12   #if BOOST_COROSIO_POSIX || BOOST_COROSIO_HAS_IOCP
13   13  
14   #include <boost/corosio/local_stream_socket.hpp> 14   #include <boost/corosio/local_stream_socket.hpp>
15   #include <boost/corosio/detail/except.hpp> 15   #include <boost/corosio/detail/except.hpp>
16   #include <boost/corosio/detail/local_stream_service.hpp> 16   #include <boost/corosio/detail/local_stream_service.hpp>
17   #include <boost/corosio/native/detail/make_err.hpp> 17   #include <boost/corosio/native/detail/make_err.hpp>
18   18  
19   #if BOOST_COROSIO_POSIX 19   #if BOOST_COROSIO_POSIX
20   #include <sys/ioctl.h> 20   #include <sys/ioctl.h>
21   #include <sys/socket.h> 21   #include <sys/socket.h>
22   #elif BOOST_COROSIO_HAS_IOCP 22   #elif BOOST_COROSIO_HAS_IOCP
23   #include <boost/corosio/native/detail/iocp/win_windows.hpp> 23   #include <boost/corosio/native/detail/iocp/win_windows.hpp>
24   #ifndef AF_UNIX 24   #ifndef AF_UNIX
25   #define AF_UNIX 1 25   #define AF_UNIX 1
26   #endif 26   #endif
27   #endif 27   #endif
28   28  
29   namespace boost::corosio { 29   namespace boost::corosio {
30   30  
HITCBC 31   342 local_stream_socket::~local_stream_socket() 31   342 local_stream_socket::~local_stream_socket()
32   { 32   {
HITCBC 33   342 close(); 33   342 close();
HITCBC 34   342 } 34   342 }
35   35  
HITCBC 36   284 local_stream_socket::local_stream_socket(capy::execution_context& ctx) 36   284 local_stream_socket::local_stream_socket(capy::execution_context& ctx)
HITCBC 37   284 : io_object(create_handle<detail::local_stream_service>(ctx)) 37   284 : io_object(create_handle<detail::local_stream_service>(ctx))
38   { 38   {
HITCBC 39   284 } 39   284 }
40   40  
41   std::error_code 41   std::error_code
HITCBC 42   51 local_stream_socket::open() noexcept 42   51 local_stream_socket::open() noexcept
43   { 43   {
HITCBC 44   51 if (is_open()) 44   51 if (is_open())
MISUBC 45   ✗ return {}; 45   ✗ return {};
HITCBC 46   51 return open_for_family(AF_UNIX, SOCK_STREAM, 0); 46   51 return open_for_family(AF_UNIX, SOCK_STREAM, 0);
47   } 47   }
48   48  
49   std::error_code 49   std::error_code
HITCBC 50   51 local_stream_socket::open_for_family( 50   51 local_stream_socket::open_for_family(
51   int family, int type, int protocol) noexcept 51   int family, int type, int protocol) noexcept
52   { 52   {
HITCBC 53   51 auto& svc = static_cast<detail::local_stream_service&>(h_.service()); 53   51 auto& svc = static_cast<detail::local_stream_service&>(h_.service());
HITCBC 54   51 std::error_code ec = svc.open_socket( 54   51 std::error_code ec = svc.open_socket(
HITCBC 55   51 static_cast<local_stream_socket::implementation&>(*h_.get()), family, 55   51 static_cast<local_stream_socket::implementation&>(*h_.get()), family,
56   type, protocol); 56   type, protocol);
HITCBC 57   51 return ec; 57   51 return ec;
58   } 58   }
59   59  
60   void 60   void
HITCBC 61   355 local_stream_socket::close() noexcept 61   355 local_stream_socket::close() noexcept
62   { 62   {
HITCBC 63   355 if (!is_open()) 63   355 if (!is_open())
HITCBC 64   133 return; 64   133 return;
HITCBC 65   222 h_.service().close(h_); 65   222 h_.service().close(h_);
66   } 66   }
67   67  
68   void 68   void
HITCBC 69   8 local_stream_socket::cancel() noexcept 69   8 local_stream_socket::cancel() noexcept
70   { 70   {
HITCBC 71   8 if (!is_open()) 71   8 if (!is_open())
HITCBC 72   2 return; 72   2 return;
HITCBC 73   6 get().cancel(); 73   6 get().cancel();
74   } 74   }
75   75  
76   std::error_code 76   std::error_code
HITCBC 77   6 local_stream_socket::shutdown(shutdown_type what) noexcept 77   6 local_stream_socket::shutdown(shutdown_type what) noexcept
78   { 78   {
HITCBC 79   6 if (!is_open()) 79   6 if (!is_open())
HITCBC 80   2 return make_error_code(std::errc::bad_file_descriptor); 80   2 return make_error_code(std::errc::bad_file_descriptor);
HITCBC 81   4 return get().shutdown(what); 81   4 return get().shutdown(what);
82   } 82   }
83   83  
84   std::error_code 84   std::error_code
HITCBC 85   173 local_stream_socket::assign(native_handle_type fd) noexcept 85   173 local_stream_socket::assign(native_handle_type fd) noexcept
86   { 86   {
HITCBC 87   173 auto& svc = static_cast<detail::local_stream_service&>(h_.service()); 87   173 auto& svc = static_cast<detail::local_stream_service&>(h_.service());
HITCBC 88   173 std::error_code ec = svc.assign_socket( 88   173 std::error_code ec = svc.assign_socket(
HITCBC 89   173 static_cast<local_stream_socket::implementation&>(*h_.get()), fd); 89   173 static_cast<local_stream_socket::implementation&>(*h_.get()), fd);
HITCBC 90   173 return ec; 90   173 return ec;
91   } 91   }
92   92  
93   native_handle_type 93   native_handle_type
HITCBC 94   25 local_stream_socket::native_handle() const noexcept 94   25 local_stream_socket::native_handle() const noexcept
95   { 95   {
HITCBC 96   25 if (!is_open()) 96   25 if (!is_open())
97   #if BOOST_COROSIO_HAS_IOCP 97   #if BOOST_COROSIO_HAS_IOCP
98   return ~native_handle_type(0); 98   return ~native_handle_type(0);
99   #else 99   #else
HITCBC 100   2 return -1; 100   2 return -1;
101   #endif 101   #endif
HITCBC 102   23 return get().native_handle(); 102   23 return get().native_handle();
103   } 103   }
104   104  
105   native_handle_type 105   native_handle_type
HITCBC 106   6 local_stream_socket::release() 106   6 local_stream_socket::release()
107   { 107   {
HITCBC 108   6 if (!is_open()) 108   6 if (!is_open())
HITCBC 109   2 detail::throw_system_error( 109   2 detail::throw_system_error(
HITCBC 110   2 make_error_code(std::errc::bad_file_descriptor), 110   2 make_error_code(std::errc::bad_file_descriptor),
111   "local_stream_socket::release"); 111   "local_stream_socket::release");
HITCBC 112   4 return get().release_socket(); 112   4 return get().release_socket();
113   } 113   }
114   114  
115   std::size_t 115   std::size_t
HITCBC 116   11 local_stream_socket::available() const 116   11 local_stream_socket::available() const
117   { 117   {
HITCBC 118   11 if (!is_open()) 118   11 if (!is_open())
HITCBC 119   2 detail::throw_system_error( 119   2 detail::throw_system_error(
HITCBC 120   4 make_error_code(std::errc::bad_file_descriptor), 120   4 make_error_code(std::errc::bad_file_descriptor),
121   "local_stream_socket::available"); 121   "local_stream_socket::available");
122   #if BOOST_COROSIO_HAS_IOCP 122   #if BOOST_COROSIO_HAS_IOCP
123   u_long value = 0; 123   u_long value = 0;
124   if (::ioctlsocket(static_cast<SOCKET>(native_handle()), FIONREAD, &value) != 124   if (::ioctlsocket(static_cast<SOCKET>(native_handle()), FIONREAD, &value) !=
125   0) 125   0)
126   detail::throw_system_error( 126   detail::throw_system_error(
127   detail::make_err(static_cast<unsigned long>(::WSAGetLastError())), 127   detail::make_err(static_cast<unsigned long>(::WSAGetLastError())),
128   "local_stream_socket::available"); 128   "local_stream_socket::available");
129   return static_cast<std::size_t>(value); 129   return static_cast<std::size_t>(value);
130   #else 130   #else
HITCBC 131   9 int value = 0; 131   9 int value = 0;
HITCBC 132   9 if (::ioctl(native_handle(), FIONREAD, &value) < 0) 132   9 if (::ioctl(native_handle(), FIONREAD, &value) < 0)
HITCBC 133   5 detail::throw_system_error( 133   5 detail::throw_system_error(
HITCBC 134   10 detail::make_err(errno), "local_stream_socket::available"); 134   10 detail::make_err(errno), "local_stream_socket::available");
HITCBC 135   4 return static_cast<std::size_t>(value); 135   4 return static_cast<std::size_t>(value);
136   #endif 136   #endif
137   } 137   }
138   138  
139   local_endpoint 139   local_endpoint
HITCBC 140   6 local_stream_socket::local_endpoint() const noexcept 140   6 local_stream_socket::local_endpoint() const noexcept
141   { 141   {
HITCBC 142   6 if (!is_open()) 142   6 if (!is_open())
HITCBC 143   2 return corosio::local_endpoint{}; 143   2 return corosio::local_endpoint{};
HITCBC 144   4 return get().local_endpoint(); 144   4 return get().local_endpoint();
145   } 145   }
146   146  
147   local_endpoint 147   local_endpoint
HITCBC 148   6 local_stream_socket::remote_endpoint() const noexcept 148   6 local_stream_socket::remote_endpoint() const noexcept
149   { 149   {
HITCBC 150   6 if (!is_open()) 150   6 if (!is_open())
HITCBC 151   2 return corosio::local_endpoint{}; 151   2 return corosio::local_endpoint{};
HITCBC 152   4 return get().remote_endpoint(); 152   4 return get().remote_endpoint();
153   } 153   }
154   154  
155   } // namespace boost::corosio 155   } // namespace boost::corosio
156   156  
157   #endif // BOOST_COROSIO_POSIX || BOOST_COROSIO_HAS_IOCP 157   #endif // BOOST_COROSIO_POSIX || BOOST_COROSIO_HAS_IOCP