chriskohlhoff / asio

Asio C++ Library
http://think-async.com/Asio
4.96k stars 1.22k forks source link

Abstraction between local::stream_protocol and windows::stream_handle #629

Open ghost opened 3 years ago

ghost commented 3 years ago

@fabiencastan commented on Jul 15, 2018, 9:24 AM UTC:

In Boost ASIO, we have access to boost::asio::local::stream_protocol on unix and boost::asio::windows::stream_handle on windows. This is great to have access to low level feature of each one. But is it possible to have a multi-platform abstraction layer with the lowest-common-denominator? Or is there already one that I missed?

For instance:

This issue was moved by chriskohlhoff from boostorg/asio#130.

ghost commented 3 years ago

@mehrdadn commented on Mar 7, 2020, 10:55 PM UTC:

Hi, I was wondering if there has been any progress on this? Is there any common abstraction between Win32 named pipes and UNIX-domain sockets?

ghost commented 3 years ago

@djarek commented on Mar 8, 2020, 12:30 AM UTC:

You could use generic stream protocol sockets, you'll just need an ifdef in a factory function.

template <class Executor>
net::generic::stream_protocol::socket
make_local_socket(Executor const& e,/*parameters*/)
{
#ifdef USE_WINDOWS_PIPE
  return net::windows::stream_handle{e};
#elif USE_UNIX_DOMAIN_SOCKET
  return net::local::stream_protocol{e};
#else
  #error Unknown IPC method
#endif
}
ghost commented 3 years ago

@mehrdadn commented on Mar 8, 2020, 12:33 AM UTC:

Oh wow I didn't realize this was possible. Will look into it, thank you!

ghost commented 3 years ago

@mehrdadn commented on Mar 8, 2020, 4:36 AM UTC:

Sorry to bother you again, but are these both compatible with generic::stream_protocol::socket? I'm getting an error when compiling this code... I'm not sure if I'm using them incorrectly or not:

#include <boost/asio.hpp>

boost::asio::generic::stream_protocol::socket
    make_local_socket(boost::asio::io_service &io_service)
{
#ifdef BOOST_ASIO_HAS_LOCAL_SOCKETS
    return boost::asio::local::stream_protocol(io_service);
#endif
#ifdef BOOST_ASIO_HAS_WINDOWS_STREAM_HANDLE
    return boost::asio::windows::stream_handle(io_service);
#endif
}

error C2440: return: cannot convert from boost::asio::windows::stream_handle to boost::asio::basic_stream_socket<boost::asio::generic::stream_protocol,boost::asio::executor> note: Constructor for class boost::asio::basic_stream_socket<boost::asio::generic::stream_protocol,boost::asio::executor> is declared explicit

Using explicit constructors doesn't seem to help either:

#include <boost/asio.hpp>

boost::asio::generic::stream_protocol::socket
    make_local_socket(boost::asio::io_service &io_service)
{
#ifdef BOOST_ASIO_HAS_LOCAL_SOCKETS
    boost::asio::local::stream_protocol unix(io_service);
    boost::asio::generic::stream_protocol::socket result(std::move(unix));
#endif
#ifdef BOOST_ASIO_HAS_WINDOWS_STREAM_HANDLE
    boost::asio::windows::stream_handle windows(io_service);
    boost::asio::generic::stream_protocol::socket result(std::move(windows));
#endif
    return result;
}

boost/asio/impl/executor.hpp(218): error C2280: boost::asio::windows::basic_stream_handle<boost::asio::executor>::basic_stream_handle(const boost::asio::windows::basic_stream_handle<boost::asio::executor> &): attempting to reference a deleted function boost/asio/windows/basic_stream_handle.hpp(465): note: compiler has generated boost::asio::windows::basic_stream_handle<boost::asio::executor>::basic_stream_handle here boost/asio/impl/executor.hpp(213): note: while compiling class template member function bool boost::asio::executor::impl<Executor,std::allocator<void>>::equals(const boost::asio::executor::impl_base *) noexcept const with Executor=boost::asio::windows::basic_stream_handleboost::asio::executor boost/asio/impl/executor.hpp(333): note: see reference to class template instantiation boost::asio::executor::impl<Executor,std::allocator<void>> being compiled with Executor=boost::asio::windows::basic_stream_handleboost::asio::executor temp.cpp(12): note: see reference to function template instantiation boost::asio::executor::executor<boost::asio::windows::basic_stream_handle<boost::asio::executor>>(Executor) being compiled with Executor=boost::asio::windows::basic_stream_handleboost::asio::executor boost/asio/use_future.hpp(139): note: see reference to class template instantiation boost::asio::use_future_t<std::allocator<void>>::std_allocator_void being compiled boost/asio/use_future.hpp(147): note: see reference to class template instantiation boost::asio::use_future_t<std::allocator<void>> being compiled

Am I using these incorrectly by any chance?

Thank you!

ghost commented 3 years ago

@djarek commented on Mar 8, 2020, 2:47 PM UTC:

Hmm, I only use generic streams to store either local domain or tcp sockets. Looks like it's trying to use the windows stream handle as an executor, so it may be a bug in ASIO.