cppcoro/include/cppcoro/net/socket_recv_operation.hpp
Lewis Baker b830b08324 Refactor async I/O operation classes to remove some duplication.
Factors out implementation of calls to OS that was duplicated in
both cancellable and non-cancellable operation objects into an
'impl' class that can be used by both.

Also, fixed potential data race in some try_start() methods when
trying to read the m_skipCompletionOnSuccess member of a socket
after starting the I/O operation. Now ensure that this flag is
read before starting the operation.

Simplified logic for file_[read/write]_operation slightly by
passing out-parameter to receive number of bytes read/written
if operation completes synchronously. This avoids an extra call
to GetOverlappedResult() to retrieve this information.
2018-01-12 06:35:30 +10:30

95 lines
2.2 KiB
C++

///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Lewis Baker
// Licenced under MIT license. See LICENSE.txt for details.
///////////////////////////////////////////////////////////////////////////////
#ifndef CPPCORO_NET_SOCKET_RECV_OPERATION_HPP_INCLUDED
#define CPPCORO_NET_SOCKET_RECV_OPERATION_HPP_INCLUDED
#include <cppcoro/config.hpp>
#include <cppcoro/cancellation_token.hpp>
#include <cstdint>
#if CPPCORO_OS_WINNT
# include <cppcoro/detail/win32.hpp>
# include <cppcoro/detail/win32_overlapped_operation.hpp>
namespace cppcoro::net
{
class socket;
class socket_recv_operation_impl
{
public:
socket_recv_operation_impl(
socket& s,
void* buffer,
std::size_t byteCount) noexcept
: m_socket(s)
, m_buffer(buffer, byteCount)
{}
bool try_start(cppcoro::detail::win32_overlapped_operation_base& operation) noexcept;
void cancel(cppcoro::detail::win32_overlapped_operation_base& operation) noexcept;
private:
socket& m_socket;
cppcoro::detail::win32::wsabuf m_buffer;
};
class socket_recv_operation
: public cppcoro::detail::win32_overlapped_operation<socket_recv_operation>
{
public:
socket_recv_operation(
socket& s,
void* buffer,
std::size_t byteCount) noexcept
: m_impl(s, buffer, byteCount)
{}
private:
friend class cppcoro::detail::win32_overlapped_operation<socket_recv_operation>;
bool try_start() noexcept { return m_impl.try_start(*this); }
socket_recv_operation_impl m_impl;
};
class socket_recv_operation_cancellable
: public cppcoro::detail::win32_overlapped_operation_cancellable<socket_recv_operation_cancellable>
{
public:
socket_recv_operation_cancellable(
socket& s,
void* buffer,
std::size_t byteCount,
cancellation_token&& ct) noexcept
: cppcoro::detail::win32_overlapped_operation_cancellable<socket_recv_operation_cancellable>(std::move(ct))
, m_impl(s, buffer, byteCount)
{}
private:
friend class cppcoro::detail::win32_overlapped_operation_cancellable<socket_recv_operation_cancellable>;
bool try_start() noexcept { return m_impl.try_start(*this); }
void cancel() noexcept { m_impl.cancel(*this); }
socket_recv_operation_impl m_impl;
};
}
#endif // CPPCORO_OS_WINNT
#endif