mercredi 20 juillet 2022

Compiler shows error, when i trying to binding handler with a socket (boost::asio)

When I trying to write this, like:

this->_io_service.post(
    std::bind(this->_connection_handler, std::move(peer))
);

and compiler well generate lot of messages that i hard to understand.

Error messages:

n file included from D:\utils\MinGW\include/boost/asio/impl/execution_context.hpp:18,
                 from D:\utils\MinGW\include/boost/asio/execution_context.hpp:409,
                 ...
D:\utils\MinGW\include/boost/asio/impl/io_context.hpp: In instantiation of 'void boost::asio::io_context::initiate_post::operator()(LegacyCompletionHandler&&, boost::asio::io_context*) const [with LegacyCompletionHandler = std::_Bind<std::function<void(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>]':
D:\utils\MinGW\include/boost/asio/async_result.hpp:476:49:   required from 'static boost::asio::detail::completion_handler_async_result<CompletionToken, Signatures>::return_type boost::asio::detail::completion_handler_async_result<CompletionToken, Signatures>::initiate(Initiation&&, RawCompletionToken&&, Args&& ...) [with Initiation = boost::asio::io_context::initiate_post; RawCompletionToken = std::_Bind<std::function<void(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>; Args = {boost::asio::io_context*}; CompletionToken = std::_Bind<std::function<void(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>; Signatures = {void()}; boost::asio::detail::completion_handler_async_result<CompletionToken, Signatures>::return_type = void]'
D:\utils\MinGW\include/boost/asio/async_result.hpp:856:29:   required from 'typename boost::asio::constraint<boost::asio::detail::async_result_has_initiate_memfn<CompletionToken, Signatures>::value, decltype (boost::asio::async_result<typename std::decay<_Tp>::type, Signatures ...>::initiate(declval<Initiation&&>(), declval<CompletionToken&&>(), (declval<Args&&>)()...))>::type boost::asio::async_initiate(Initiation&&, CompletionToken&, Args&& ...) [with CompletionToken = std::_Bind<std::function<void(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>; Signatures = {void()}; Initiation = boost::asio::io_context::initiate_post; Args = {boost::asio::io_context*}; typename boost::asio::constraint<boost::asio::detail::async_result_has_initiate_memfn<CompletionToken, Signatures>::value, decltype (boost::asio::async_result<typename std::decay<_Tp>::type, Signatures ...>::initiate(declval<Initiation&&>(), declval<CompletionToken&&>(), (declval<Args&&>)()...))>::type = void; decltype (boost::asio::async_result<typename std::decay<_Tp>::type, Signatures ...>::initiate(declval<Initiation&&>(), declval<CompletionToken&&>(), (declval<Args&&>)()...)) = void; typename std::decay<_Tp>::type = std::decay<std::_Bind<std::function<void(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)> >::type]'
D:\utils\MinGW\include/boost/asio/impl/io_context.hpp:200:58:   required from 'auto boost::asio::io_context::post(LegacyCompletionHandler&&) [with LegacyCompletionHandler = std::_Bind<std::function<void(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>]'
.\acceptor.cpp:10:27:   required from here
D:\utils\MinGW\include/boost/asio/impl/io_context.hpp:173:5: error: use of deleted function 'std::_Bind<_Functor(_Bound_args ...)>::_Bind(const std::_Bind<_Functor(_Bound_args ...)>&) [with _Functor = std::function<void(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>; _Bound_args = {boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::any_io_executor>}]'
  173 |     BOOST_ASIO_LEGACY_COMPLETION_HANDLER_CHECK(
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from D:\utils\MinGW\include/boost/system/detail/error_category.hpp:17,
                 from D:\utils\MinGW\include/boost/system/detail/error_code.hpp:14,
                 from D:\utils\MinGW\include/boost/system/error_code.hpp:13,
                 from D:\utils\MinGW\include/boost/asio/detail/throw_error.hpp:19,
                 from D:\utils\MinGW\include/boost/asio/detail/impl/win_tss_ptr.ipp:22,
                 from D:\utils\MinGW\include/boost/asio/detail/win_tss_ptr.hpp:76,
                 from D:\utils\MinGW\include/boost/asio/detail/tss_ptr.hpp:25,
                 from D:\utils\MinGW\include/boost/asio/detail/call_stack.hpp:20,
                 from D:\utils\MinGW\include/boost/asio/detail/thread_context.hpp:20,
                 from D:\utils\MinGW\include/boost/asio/detail/recycling_allocator.hpp:20,
                 from D:\utils\MinGW\include/boost/asio/detail/handler_alloc_helpers.hpp:21,
                 from D:\utils\MinGW\include/boost/asio/detail/executor_function.hpp:19,
                 from D:\utils\MinGW\include/boost/asio/execution/any_executor.hpp:23,
                 from D:\utils\MinGW\include/boost/asio/execution.hpp:19,
                 from D:\utils\MinGW\include/boost/asio/any_io_executor.hpp:22,
                 from D:\utils\MinGW\include/boost/asio/basic_socket_acceptor.hpp:19,
                 from D:\utils\MinGW\include/boost/asio/ip/tcp.hpp:19,
                 from .\acceptor.h:4,
                 from .\acceptor.cpp:3:
d:\utils\mingw\include\c++\11.2.0\functional:493:7: note: 'std::_Bind<_Functor(_Bound_args ...)>::_Bind(const std::_Bind<_Functor(_Bound_args ...)>&) [with _Functor = std::function<void(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>; _Bound_args = {boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::any_io_executor>}]' is implicitly deleted because the default definition would be ill-formed:
  493 |       _Bind(const _Bind&) = default;
      |       ^~~~~
d:\utils\mingw\include\c++\11.2.0\functional:493:7: error: use of deleted function 'constexpr std::tuple<_Elements>::tuple(const std::tuple<_Elements>&) [with _Elements = {boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::any_io_executor>}]'
In file included from d:\utils\mingw\include\c++\11.2.0\bits\unique_ptr.h:37,
                 from d:\utils\mingw\include\c++\11.2.0\memory:76,
                 from D:\utils\MinGW\include/boost/asio/detail/memory.hpp:21,
                 from D:\utils\MinGW\include/boost/asio/execution/detail/as_invocable.hpp:20,
                 from D:\utils\MinGW\include/boost/asio/execution/execute.hpp:20,
                 from D:\utils\MinGW\include/boost/asio/execution/executor.hpp:20,
                 from D:\utils\MinGW\include/boost/asio/execution/allocator.hpp:20,
                 from D:\utils\MinGW\include/boost/asio/execution.hpp:18,
                 from D:\utils\MinGW\include/boost/asio/any_io_executor.hpp:22,
                 from D:\utils\MinGW\include/boost/asio/basic_socket_acceptor.hpp:19,
                 from D:\utils\MinGW\include/boost/asio/ip/tcp.hpp:19,
                 from .\acceptor.h:4,
                 from .\acceptor.cpp:3:
d:\utils\mingw\include\c++\11.2.0\tuple:744:17: note: 'constexpr std::tuple<_Elements>::tuple(const std::tuple<_Elements>&) [with _Elements = {boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::any_io_executor>}]' is implicitly deleted because the default definition would be ill-formed:
  744 |       constexpr tuple(const tuple&) = default;
      |                 ^~~~~
d:\utils\mingw\include\c++\11.2.0\tuple:744:17: error: use of deleted function 'constexpr std::_Tuple_impl<_Idx, _Head>::_Tuple_impl(const std::_Tuple_impl<_Idx, _Head>&) [with long long unsigned int _Idx = 0; _Head = boost::asio::basic_stream_socket<boost::asio::ip::tcp>]'
d:\utils\mingw\include\c++\11.2.0\tuple:435:17: note: 'constexpr std::_Tuple_impl<_Idx, _Head>::_Tuple_impl(const std::_Tuple_impl<_Idx, _Head>&) [with long long unsigned int _Idx = 0; _Head = boost::asio::basic_stream_socket<boost::asio::ip::tcp>]' is implicitly deleted because the default definition would be ill-formed:
  435 |       constexpr _Tuple_impl(const _Tuple_impl&) = default;
      |                 ^~~~~~~~~~~
d:\utils\mingw\include\c++\11.2.0\tuple:435:17: error: use of deleted function 'constexpr std::_Head_base<_Idx, _Head, false>::_Head_base(const std::_Head_base<_Idx, _Head, false>&) [with long long unsigned int _Idx = 0; _Head = boost::asio::basic_stream_socket<boost::asio::ip::tcp>]'
d:\utils\mingw\include\c++\11.2.0\tuple:185:17: note: 'constexpr std::_Head_base<_Idx, _Head, false>::_Head_base(const std::_Head_base<_Idx, _Head, false>&) [with long long unsigned int _Idx = 0; _Head = boost::asio::basic_stream_socket<boost::asio::ip::tcp>]' is implicitly deleted because the default definition would be ill-formed:
  185 |       constexpr _Head_base(const _Head_base&) = default;
      |                 ^~~~~~~~~~
d:\utils\mingw\include\c++\11.2.0\tuple:185:17: error: use of deleted function 'boost::asio::basic_stream_socket<Protocol, Executor>::basic_stream_socket(const boost::asio::basic_stream_socket<Protocol, Executor>&) [with Protocol = boost::asio::ip::tcp; Executor = boost::asio::any_io_executor]'
In file included from D:\utils\MinGW\include/boost/asio/basic_socket_streambuf.hpp:25,
                 from D:\utils\MinGW\include/boost/asio/basic_socket_iostream.hpp:24,
                 from D:\utils\MinGW\include/boost/asio/ip/tcp.hpp:20,
                 from .\acceptor.h:4,
                 from .\acceptor.cpp:3:
D:\utils\MinGW\include/boost/asio/basic_stream_socket.hpp:1045:3: note: declared here
 1045 |   basic_stream_socket(const basic_stream_socket&) BOOST_ASIO_DELETED;
      |   ^~~~~~~~~~~~~~~~~~~
In file included from D:\utils\MinGW\include/boost/asio/impl/execution_context.hpp:18,
                 ...
D:\utils\MinGW\include/boost/asio/detail/handler_type_requirements.hpp:72:39: note:   initializing argument 1 of 'char (& boost::asio::detail::zero_arg_copyable_handler_test(Handler, ...))[2] [with Handler = std::_Bind<std::function<void(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>]'
   72 | char (&zero_arg_copyable_handler_test(Handler, ...))[2];
      |                                       ^~~~~~~
D:\utils\MinGW\include/boost/asio/impl/io_context.hpp:129:5: error: use of deleted function 'std::_Bind<_Functor(_Bound_args ...)>::_Bind(const std::_Bind<_Functor(_Bound_args ...)>&) [with _Functor = std::function<void(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>; _Bound_args = {boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::any_io_executor>}]'
  129 |     BOOST_ASIO_LEGACY_COMPLETION_HANDLER_CHECK(
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
D:\utils\MinGW\include/boost/asio/detail/handler_type_requirements.hpp:127:35: note:   initializing argument 1 of 'char boost::asio::detail::argbyv(T) [with T = std::_Bind<std::function<void(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>]'
  127 | template <typename T> char argbyv(T);
      |                                   ^
D:\utils\MinGW\include/boost/asio/impl/io_context.hpp:129:5: error: no match for call to '(std::_Bind<std::function<void(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>) ()'
  129 |     BOOST_ASIO_LEGACY_COMPLETION_HANDLER_CHECK(
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
D:\utils\MinGW\include/boost/asio/impl/io_context.hpp: In instantiation of 'void boost::asio::io_context::initiate_post::operator()(LegacyCompletionHandler&&, boost::asio::io_context*) const [with LegacyCompletionHandler = std::_Bind<std::function<void(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>]':
D:\utils\MinGW\include/boost/asio/async_result.hpp:476:49:   required from 'static boost::asio::detail::completion_handler_async_result<CompletionToken, Signatures>::return_type boost::asio::detail::completion_handler_async_result<CompletionToken, Signatures>::initiate(Initiation&&, RawCompletionToken&&, Args&& ...) [with Initiation = boost::asio::io_context::initiate_post; RawCompletionToken = std::_Bind<std::function<void(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>; Args = {boost::asio::io_context*}; CompletionToken = std::_Bind<std::function<void(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>; Signatures = {void()}; boost::asio::detail::completion_handler_async_result<CompletionToken, Signatures>::return_type = void]'
D:\utils\MinGW\include/boost/asio/async_result.hpp:856:29:   required from 'typename boost::asio::constraint<boost::asio::detail::async_result_has_initiate_memfn<CompletionToken, Signatures>::value, decltype (boost::asio::async_result<typename std::decay<_Tp>::type, Signatures ...>::initiate(declval<Initiation&&>(), declval<CompletionToken&&>(), (declval<Args&&>)()...))>::type boost::asio::async_initiate(Initiation&&, CompletionToken&, Args&& ...) [with CompletionToken = std::_Bind<std::function<void(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>; Signatures = {void()}; Initiation = boost::asio::io_context::initiate_post; Args = {boost::asio::io_context*}; typename boost::asio::constraint<boost::asio::detail::async_result_has_initiate_memfn<CompletionToken, Signatures>::value, decltype (boost::asio::async_result<typename std::decay<_Tp>::type, Signatures ...>::initiate(declval<Initiation&&>(), declval<CompletionToken&&>(), (declval<Args&&>)()...))>::type = void; decltype (boost::asio::async_result<typename std::decay<_Tp>::type, Signatures ...>::initiate(declval<Initiation&&>(), declval<CompletionToken&&>(), (declval<Args&&>)()...)) = void; typename std::decay<_Tp>::type = std::decay<std::_Bind<std::function<void(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)> >::type]'
D:\utils\MinGW\include/boost/asio/impl/io_context.hpp:200:58:   required from 'auto boost::asio::io_context::post(LegacyCompletionHandler&&) [with LegacyCompletionHandler = std::_Bind<std::function<void(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>]'
.\acceptor.cpp:10:27:   required from here
d:\utils\mingw\include\c++\11.2.0\functional:543:9: note: candidate: 'template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) const volatile [with _Args = {_Args ...}; _Result = _Result; _Functor = std::function<void(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>; _Bound_args = {boost::asio::basic_stream_socket<boost::asio::ip::tcp, 
boost::asio::any_io_executor>}]'
  543 |         operator()(_Args&&... __args) const volatile
      |         ^~~~~~~~
d:\utils\mingw\include\c++\11.2.0\functional:543:9: note:   template argument deduction/substitution failed:
d:\utils\mingw\include\c++\11.2.0\functional: In substitution of 'template<class _Functor, class ... _Bound_args> template<class _Fn, class _CallArgs, class ... _BArgs> using _Res_type_impl = typename std::result_of<_Fn&(decltype (std::_Mu<typename std::remove_cv<_BArgs>::type, std::is_bind_expression<typename std::remove_cv<_BArgs>::type>::value, (std::is_placeholder<typename std::remove_cv<_BArgs>::type>::value > 0)>()(declval<_BArgs&>(), declval<_CallArgs&>()))&& ...)>::type [with _Fn = const volatile std::function<void(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>; _CallArgs = std::tuple<>; _BArgs = {const volatile boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::any_io_executor>}; _Functor = std::function<void(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>; _Bound_args = {boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::any_io_executor>}]':
d:\utils\mingw\include\c++\11.2.0\functional:475:8:   required by substitution of 'template<class _Functor, class ... _Bound_args> template<class _CallArgs, template<class _CallArgs, template<class> class __cv_quals> template<class _Functor, class ... _Bound_args> template<class> class __cv_quals> using _Res_type_cv = std::_Bind<_Functor(_Bound_args ...)>::_Res_type_impl<typename __cv_quals<typename std::enable_if<(bool)((std::tuple_size<_Tuple>::value + 1)), _Functor>::type>::type, _CallArgs, typename __cv_quals<_Bound_args>::type ...> [with _CallArgs = std::tuple<>; __cv_quals = std::add_cv; _Functor = std::function<void(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>; _Bound_args = {boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::any_io_executor>}]' = {boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::any_io_executor>}]'                                                                                                                                                                                                                >)>(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>]'
d:\utils\mingw\include\c++\11.2.0\functional:540:9:   required from 'void boost::asio::io_context::initiate_post::operator()(LegacyCompletionHandler&&, boost::asio::io_context*) const [with LegacyCompletionHandler = std::_Bind<std::function<void(boost::asio::basic_stream_socket<boost::asio::ip::tcpken&&, Args&& ...) [with Initiation = boost::asio::io_context::initiate_post; RawCompletionToken = std>)>(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>]'                                                                                                                                                                                                                                             c_stream_socket<boost::asio::ip::tcp>)>; Signatures = {void()}; boost::asio::detail::completion_handle
D:\utils\MinGW\include/boost/asio/async_result.hpp:476:49:   required from 'static boost::asio::detail::completion_handler_async_result<CompletionToken, Signatures>::return_type boost::asio::detail::completion_handler_async_result<CompletionToken, Signatures>::initiate(Initiation&&, RawCompletionToken&&, Args&& ...) [with Initiation = boost::asio::io_context::initiate_post; RawCompletionToken = std::_Bind<std::function<void(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>; Args = {boost::asio::io_context*}; CompletionToken = stl<Initiation&&>(), declval<CompletionToken&&>(), (declval<Args&&>)()...))>::type boost::asio::async_ind::_Bind<std::function<void(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>; Signatures = {void()}; boost::asio::detail::completion_handler_async_result<CompletionToken, Signatures>::return_type = void]'                              s = {boost::asio::io_context*}; typename boost::asio::constraint<boost::asio::detail::async_result_has
D:\utils\MinGW\include/boost/asio/async_result.hpp:856:29:   required from 'typename boost::asio::constraint<boost::asio::detail::async_result_has_initiate_memfn<CompletionToken, Signatures>::value, decltype (boost::asio::async_result<typename std::decay<_Tp>::type, Signatures ...>::initiate(declvaTp>::type, Signatures ...>::initiate(declval<Initiation&&>(), declval<CompletionToken&&>(), (declval<Al<Initiation&&>(), declval<CompletionToken&&>(), (declval<Args&&>)()...))>::type boost::asio::async_initiate(Initiation&&, CompletionToken&, Args&& ...) [with CompletionToken = std::_Bind<std::function<void(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>; Signatures = {void()}; Initiation = boost::asio::io_context::initiate_post; Args = {boost::asio::io_context*}; typename boost::asio::constraint<boost::asio::detail::async_result_has_initiate_memfn<CompletionToken, Signatures>::value, decltype (boost::asio::async_result<typena:asio::ip::tcp>)>]'me std::decay<_Tp>::type, Signatures ...>::initiate(declval<Initiation&&>(), declval<CompletionToken&&>(), (declval<Args&&>)()...))>::type = void; decltype (boost::asio::async_result<typename std::decay<_Tp>::type, Signatures ...>::initiate(declval<Initiation&&>(), declval<CompletionToken&&>(), (declval<Args&&>)()...)) = void; typename std::decay<_Tp>::type = std::decay<std::_Bind<std::function<void(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)> >::type]'
D:\utils\MinGW\include/boost/asio/impl/io_context.hpp:200:58:   required from 'auto boost::asio::io_context::post(LegacyCompletionHandler&&) [with LegacyCompletionHandler = std::_Bind<std::function<void(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>]'
.\acceptor.cpp:10:27:   required from here
d:\utils\mingw\include\c++\11.2.0\functional:464:15: error: no type named 'type' in 'struct std::result_of<const volatile std::function<void(boost::asio::basic_stream_socket<boost::asio::ip::tcp>)>&(const volatile boost::asio::basic_stream_socket<boost::asio::ip::tcp>&)>'
  464 |         using _Res_type_impl
      |               ^~~~~~~~~~~~~~

In the line 173 of "boost/asio/impl/io_context.hpp", there is a comment says:

// If you get an error on the following line it means that your handler does
// not meet the documented type requirements for a LegacyCompletionHandler.

Here is my codes:

acceptor.h

#ifndef ACCEPTOR_H
#define ACCEPTOR_H

#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/io_service.hpp>

typedef 
    std::function<void(boost::asio::ip::tcp::socket)>
        connection_handler;

class acceptor {
private:
    boost::asio::io_service &_io_service;
    boost::asio::ip::tcp::acceptor _acceptor;
    connection_handler _connection_handler;
    std::function<void (
        const boost::system::error_code&,
        boost::asio::ip::tcp::socket
    )> _accept_handler;
private:
    void accept_handler(
        const boost::system::error_code& error,
        boost::asio::ip::tcp::socket peer
    );
public:
    acceptor(
        std::string ipv4_addr,
        int port, 
        boost::asio::io_service &io_service, 
        connection_handler handler
    );
    void start_accept();
};

#endif /* ACCEPTOR_Hs */

acceptor.cpp

#include <iostream>

#include "acceptor.h"

void acceptor::accept_handler(
    const boost::system::error_code& error,
    boost::asio::ip::tcp::socket peer
){
    this->_acceptor.async_accept(this->_accept_handler);
    this->_io_service.post(
        std::bind(this->_connection_handler, std::move(peer))
    );
}

acceptor::acceptor(
    std::string ipv4_addr, 
    int port, 
    boost::asio::io_service &io_service, 
    connection_handler handler
)
:_connection_handler(handler),
_io_service(io_service),
_acceptor(
    boost::asio::ip::tcp::acceptor(
        io_service,
        boost::asio::ip::tcp::endpoint(
            boost::asio::ip::address_v4::from_string(ipv4_addr),
            port
        )
    )
),
_accept_handler(
    std::bind(
        &acceptor::accept_handler, 
        this, 
        std::placeholders::_1, 
        std::placeholders::_2
    )
){}

void acceptor::start_accept(){
    this->_acceptor.async_accept(this->_accept_handler);
}

myhttpd.cpp

#include <iostream>

#include "acceptor.h"

void handler(boost::asio::ip::tcp::socket socket) {
    std::cout << "connected" << std::endl;
}

int main(int argc, char *argv[]) {
    boost::asio::io_service _io_service;
    boost::asio::io_service::work work(_io_service);
    acceptor ac("127.0.0.1", 10000, _io_service, &handler);
    ac.start_accept();
    _io_service.run();
}

Am i doing something wrong? I'm sorry if my grammar confuses you, my English is not very good. Any help i will appreciated.

Aucun commentaire:

Enregistrer un commentaire