c++ - socket accepted but m_socket.remote_endpoint raise Transport endpoint is not connected -


code:

listener.hpp:

template <typename sockettype> void listener<sockettype>::beginaccept() {     auto worker = selectworker();     auto socket = worker->createsocket();      m_acceptor->async_accept(socket->getasiosocket(),         [this, worker, socket] (const boost::system::error_code &ec)     {         this->onaccept(worker, socket, ec);     }); }  template <typename sockettype> void listener<sockettype>::onaccept(networkthread<sockettype> *worker, std::shared_ptr<sockettype> const& socket, const boost::system::error_code &ec) {     // error has occurred     if (ec)         worker->removesocket(socket.get());     else         socket->open();      beginaccept(); } 

socket.cpp:

bool socket::open() {     try     {         const_cast<std::string &>(m_address) = m_socket.remote_endpoint().address().to_string();         const_cast<std::string &>(m_remoteendpoint) = boost::lexical_cast<std::string>(m_socket.remote_endpoint());     }     catch (boost::system::error_code& error)     {         slog.outinfo("socket::open() failed remote address.  error: %s", error.message().c_str());         return false;     }     catch (const std::exception& error)     {         slog.outinfo("socket::open() failed(with std::exception) remote address.  error: %s", error.what());         return false;     }     catch (...)     {         slog.outerror("socket::open() failed remote address.  other error");         return false;     }      m_outbuffer.reset(new packetbuffer);     m_secondaryoutbuffer.reset(new packetbuffer);     m_inbuffer.reset(new packetbuffer);      startasyncread();      return true; } 

is related tcp attack?

edit: gdb:

(gdb) p m_socket $1 = {<boost::asio::basic_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> >> = {<boost::asio::basic_io_object<boost::asio::stream_socket_service<boost::asio::ip::tcp>, true>> = {       implementation = {<boost::asio::detail::reactive_socket_service_base::base_implementation_type> = {socket_ = 21, state_ = 80 'p', reactor_data_ = 0x7ffff0005120}, protocol_ = {family_ = 2}}, service_ = 0x7f2b30}, <boost::asio::socket_base> = {       static message_peek = 2, static message_out_of_band = 1, static message_do_not_route = 4, static message_end_of_record = 128, static max_connections = 128}, <no data fields>}, <no data fields>} (gdb) p m_socket.is_open() $2 = true (gdb) bt #0  mangos::socket::open (this=0x7ffff0003c40) @ /home/ubuntu/moltencore/moltencore/src/shared/network/socket.cpp:52 #1  0x00000000004fbff1 in mangos::listener<authsocket>::onaccept (this=0x7fffffffe6c0, worker=0x8041e0, socket=std::shared_ptr (count 2, weak 1) 0x7ffff0003c40, ec=...) @ /home/ubuntu/moltencore/moltencore/src/shared/network/listener.hpp:121 #2  0x00000000004fa030 in mangos::listener<authsocket>::beginaccept()::{lambda(boost::system::error_code const&)#1}::operator()(boost::system::error_code const&) const (__closure=0x7ffff4ec8be0, ec=...)     @ /home/ubuntu/moltencore/moltencore/src/shared/network/listener.hpp:110 #3  0x0000000000502a2d in boost::asio::detail::binder1<mangos::listener<authsocket>::beginaccept()::{lambda(boost::system::error_code const&)#1}, boost::system::error_code>::operator()() (this=0x7ffff4ec8be0) @ /usr/include/boost/asio/detail/bind_handler.hpp:47 #4  0x00000000005020c3 in boost::asio::asio_handler_invoke<boost::asio::detail::binder1<mangos::listener<authsocket>::beginaccept()::{lambda(boost::system::error_code const&)#1}, boost::system::error_code> >(boost::asio::detail::binder1<mangos::listener<authsocket>::beginaccept()::{lambda(boost::system::error_code const&)#1}, boost::system::error_code>&, ...) (function=...) @ /usr/include/boost/asio/handler_invoke_hook.hpp:69 #5  0x00000000005014b8 in boost_asio_handler_invoke_helpers::invoke<boost::asio::detail::binder1<mangos::listener<authsocket>::beginaccept()::{lambda(boost::system::error_code const&)#1}, boost::system::error_code>, {lambda(boost::system::error_code const&)#1}>(boost::asio::detail::binder1<mangos::listener<authsocket>::beginaccept()::{lambda(boost::system::error_code const&)#1}, boost::system::error_code>&, {lambda(boost::system::error_code const&)#1}&) (function=..., context=...)     @ /usr/include/boost/asio/detail/handler_invoke_helpers.hpp:37 #6  0x0000000000500886 in boost::asio::detail::reactive_socket_accept_op<boost::asio::basic_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> >, boost::asio::ip::tcp, mangos::listener<authsocket>::beginaccept()::{lambda(boost::system::error_code const&)#1}>::do_complete(boost::asio::detail::task_io_service*, boost::asio::detail::task_io_service_operation*, boost::system::error_code const&, unsigned long) (owner=0x8084d0, base=0x804910) @ /usr/include/boost/asio/detail/reactive_socket_accept_op.hpp:123 #7  0x00000000004f0300 in boost::asio::detail::task_io_service_operation::complete (this=0x804910, owner=..., ec=..., bytes_transferred=0) @ /usr/include/boost/asio/detail/task_io_service_operation.hpp:38 #8  0x00000000004f1d55 in boost::asio::detail::epoll_reactor::descriptor_state::do_complete (owner=0x8084d0, base=0x804140, ec=..., bytes_transferred=1) @ /usr/include/boost/asio/detail/impl/epoll_reactor.ipp:651 #9  0x00000000004f0300 in boost::asio::detail::task_io_service_operation::complete (this=0x804140, owner=..., ec=..., bytes_transferred=1) @ /usr/include/boost/asio/detail/task_io_service_operation.hpp:38 #10 0x00000000004f2823 in boost::asio::detail::task_io_service::do_run_one (this=0x8084d0, lock=..., this_thread=..., ec=...) @ /usr/include/boost/asio/detail/impl/task_io_service.ipp:372 #11 0x00000000004f23a1 in boost::asio::detail::task_io_service::run (this=0x8084d0, ec=...) @ /usr/include/boost/asio/detail/impl/task_io_service.ipp:149 #12 0x00000000004f2abc in boost::asio::io_service::run (this=0x7ef540) @ /usr/include/boost/asio/impl/io_service.ipp:59 #13 0x00000000004f7c89 in mangos::listener<authsocket>::listener(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)::{lambda()#1}::operator()() const () @ /home/ubuntu/moltencore/moltencore/src/shared/network/listener.hpp:84 #14 0x0000000000505624 in std::_bind_simple<mangos::listener<authsocket>::listener(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)::{lambda()#1} ()>::_m_invoke<>(std::_index_tuple<>) (this=0x8049c8)     @ /usr/include/c++/5/functional:1531 #15 0x0000000000504f38 in std::_bind_simple<mangos::listener<authsocket>::listener(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)::{lambda()#1} ()>::operator()() (this=0x8049c8) @ /usr/include/c++/5/functional:1520 #16 0x0000000000504416 in std::thread::_impl<std::_bind_simple<mangos::listener<authsocket>::listener(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)::{lambda()#1} ()> >::_m_run() (this=0x8049b0) @ /usr/include/c++/5/thread:115 #17 0x00007ffff6c98c80 in ?? () /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #18 0x00007ffff7bc16ba in start_thread (arg=0x7ffff4ec9700) @ pthread_create.c:333 #19 0x00007ffff63fe82d in clone () @ ../sysdeps/unix/sysv/linux/x86_64/clone.s:109 

edit: networkthread.hpp:

template <typename sockettype> class networkthread {     private:         boost::asio::io_service m_service;          std::mutex m_socketlock;         std::unordered_set<std::shared_ptr<sockettype>> m_sockets;          // note work member *must* declared after service member work constructor function correctly         std::unique_ptr<boost::asio::io_service::work> m_work;          std::thread m_servicethread;      public:         networkthread() : m_work(new boost::asio::io_service::work(m_service))         {             m_servicethread = std::thread([this] { boost::system::error_code ec; this->m_service.run(ec); });         }          ~networkthread()         {             // allow io_service::run() exit.             m_work.reset();             m_service.stop();             m_servicethread.join();              // attempt gracefully close open connections             (auto = m_sockets.begin(); != m_sockets.end();)             {                 auto const current = i;                 ++i;                  if (!(*current)->isclosed())                     (*current)->close();             }         }          size_t size() const { return m_sockets.size(); }          std::shared_ptr<sockettype> createsocket();          void removesocket(socket *socket)         {             std::lock_guard<std::mutex> guard(m_socketlock);             m_sockets.erase(socket->shared<sockettype>());         } };  template <typename sockettype> std::shared_ptr<sockettype> networkthread<sockettype>::createsocket() {     std::lock_guard<std::mutex> guard(m_socketlock);      auto const = m_sockets.emplace(std::make_shared<sockettype>(m_service, [this] (socket *socket) { this->removesocket(socket); }));      return *i.first; } 

the problem totally different:

in code:

template <typename sockettype> void listener<sockettype>::onaccept(     networkthread<sockettype> *worker,      std::shared_ptr<sockettype> const& socket,      const boost::system::error_code &ec ) {    // @ point, socket has 1 reference count, held lambda     // in beginaccept, reference count cannot increase, since it's     // constant.        // ...          beginaccept(); // creates new, distinct  instance of lambda                     // fuinction object in beginaccept.      // when exit, constrol given calling lambda in beginaccept     // destructor happily deletes socket, since reference count     // still 1. } 

bottom line: onaccept() needs receive socket pointer value, , needs transfer or store shared pointer somehow. done passing shared_ptr beginreceive() by value. beginreceive() must take responsibility keep socket pointer alive until connection dies, this, passing pointer itself, either parameter, or lambda capture, value.

the easiest way fix code move call startasyncread() onaccept(), since there convenient copy of shared_ptr of socket there.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -