Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:03:46

0001 ///////////////////////////////////////////////////////////////////////////////
0002 // Copyright (c) Lewis Baker
0003 // Licenced under MIT license. See LICENSE.txt for details.
0004 ///////////////////////////////////////////////////////////////////////////////
0005 #ifndef CPPCORO_NET_SOCKET_RECV_FROM_OPERATION_HPP_INCLUDED
0006 #define CPPCORO_NET_SOCKET_RECV_FROM_OPERATION_HPP_INCLUDED
0007 
0008 #include <cppcoro/config.hpp>
0009 #include <cppcoro/cancellation_token.hpp>
0010 #include <cppcoro/net/ip_endpoint.hpp>
0011 
0012 #include <cstdint>
0013 #include <tuple>
0014 
0015 #if CPPCORO_OS_WINNT
0016 # include <cppcoro/detail/win32.hpp>
0017 # include <cppcoro/detail/win32_overlapped_operation.hpp>
0018 
0019 namespace cppcoro::net
0020 {
0021     class socket;
0022 
0023     class socket_recv_from_operation_impl
0024     {
0025     public:
0026 
0027         socket_recv_from_operation_impl(
0028             socket& socket,
0029             void* buffer,
0030             std::size_t byteCount) noexcept
0031             : m_socket(socket)
0032             , m_buffer(buffer, byteCount)
0033         {}
0034 
0035         bool try_start(cppcoro::detail::win32_overlapped_operation_base& operation) noexcept;
0036         void cancel(cppcoro::detail::win32_overlapped_operation_base& operation) noexcept;
0037         std::tuple<std::size_t, ip_endpoint> get_result(
0038             cppcoro::detail::win32_overlapped_operation_base& operation);
0039 
0040     private:
0041 
0042         socket& m_socket;
0043         cppcoro::detail::win32::wsabuf m_buffer;
0044 
0045         static constexpr std::size_t sockaddrStorageAlignment = 4;
0046 
0047         // Storage suitable for either SOCKADDR_IN or SOCKADDR_IN6
0048         alignas(sockaddrStorageAlignment) std::uint8_t m_sourceSockaddrStorage[28];
0049         int m_sourceSockaddrLength;
0050 
0051     };
0052 
0053     class socket_recv_from_operation
0054         : public cppcoro::detail::win32_overlapped_operation<socket_recv_from_operation>
0055     {
0056     public:
0057 
0058         socket_recv_from_operation(
0059             socket& socket,
0060             void* buffer,
0061             std::size_t byteCount) noexcept
0062             : m_impl(socket, buffer, byteCount)
0063         {}
0064 
0065     private:
0066 
0067         friend class cppcoro::detail::win32_overlapped_operation<socket_recv_from_operation>;
0068 
0069         bool try_start() noexcept { return m_impl.try_start(*this); }
0070         decltype(auto) get_result() { return m_impl.get_result(*this); }
0071 
0072         socket_recv_from_operation_impl m_impl;
0073 
0074     };
0075 
0076     class socket_recv_from_operation_cancellable
0077         : public cppcoro::detail::win32_overlapped_operation_cancellable<socket_recv_from_operation_cancellable>
0078     {
0079     public:
0080 
0081         socket_recv_from_operation_cancellable(
0082             socket& socket,
0083             void* buffer,
0084             std::size_t byteCount,
0085             cancellation_token&& ct) noexcept
0086             : cppcoro::detail::win32_overlapped_operation_cancellable<socket_recv_from_operation_cancellable>(std::move(ct))
0087             , m_impl(socket, buffer, byteCount)
0088         {}
0089 
0090     private:
0091 
0092         friend class cppcoro::detail::win32_overlapped_operation_cancellable<socket_recv_from_operation_cancellable>;
0093 
0094         bool try_start() noexcept { return m_impl.try_start(*this); }
0095         void cancel() noexcept { m_impl.cancel(*this); }
0096         decltype(auto) get_result() { return m_impl.get_result(*this); }
0097 
0098         socket_recv_from_operation_impl m_impl;
0099 
0100     };
0101 
0102 }
0103 
0104 #endif // CPPCORO_OS_WINNT
0105 
0106 #endif