c++ - Boost shared_ptr object having problems with getting values -
ok... i've spent time 1 this.
what doing making list of connections, "strangers" unconfirmed, or denied connection waits ack make sure client got message. host has vector of these strangers boost::shared_ptr, , when stranger accepted, , acked, becomes client, thats not problem.
when connection received, new stranger made, runtime error when calling endpoint "(*it)->getendpoint()" inside of sending packet, that's example, same other values, "timed_out".
down here stranger class, , usage of sending
class stranger : public boost::enable_shared_from_this<stranger> //a short lived object sends reply if connection accepted / denied until ack received or timed out. { private: udp::endpoint end_p_; std::string data_; std::string name_; deadline_timer timeout; public: bool accepted = false; //public check if stranger accepted or not. bool timed_out = false; //we assume packet sending accepting connection because have name parameter. stranger(udp::endpoint end_p, std::string name, boost::asio::io_service &io_s) :timeout(io_s) ,end_p_(end_p) { std::stringstream ss; //formatting packet ss<<(char)(con_rep_accepted)<<0<<0; //packettype, 2 bytes pak num(its 0 because connection) data_ = ss.str(); name = name_; accepted = true; timed_out = false; timeout.expires_from_now(boost::posix_time::seconds(5)); timeout.async_wait(boost::bind(&stranger::dotimeout, this)); } //we assume packet sending denying connection because there no name. stranger(udp::endpoint end_p , boost::asio::io_service& io_s) :timeout(io_s) { end_p_ = end_p; std::stringstream ss; //formatting packet ss<<(char)(con_rep_denied)<<0<<0; //1 byte packet type, 2 bytes pak num(its 0 because connection) data_ = ss.str(); accepted = false; timed_out = false; timeout.expires_from_now(boost::posix_time::seconds(5)); timeout.async_wait(boost::bind(&stranger::dotimeout, this)); } void dotimeout() { std::cout<<"timed.\n"; timed_out = true; } udp::endpoint getendpoint(){return end_p_;} //to compare endpoints std::string getdata(){return data_;} std::string getname(){return name_;} ~stranger(){timeout.cancel();} };
where debugger points when runtime. client same stranger right now, no game data send. vector of shared_ptrs.
//everything server receives void host_state::receive() { socket_.async_receive_from( boost::asio::buffer(in_data,1024), sender_endpoint_, [this](boost::system::error_code ec, std::size_t bytes) { if (!ec && bytes >= 3 ) //the header 3 bytes, , smallest usable packet 3 bytes. { int pak_type = (int)(in_data[0]); unsigned short packet_num = (in_data[1] << 8) | in_data[2]; //the number in order for(auto = clients.begin(); it!=clients.end(); it++) //here!------------------------ if((*it)->getendpoint() == sender_endpoint_) //<-debugger points here //------------------------ {
using windows, gcc 4.8.1, boost 1.57.0. debug dump:
#0 00435e5c boost::shared_ptr<client>::operator->(this=0x6e6f6974) (c:/boost_1_57_0/boost/smart_ptr/shared_ptr.hpp:648) #1 00406394 host_state::__lambda1::operator() (__closure=0x28fc00, ec=..., bytes=67) (c:\users\poteto\desktop\pixely_trenches\sources\server.cpp:26) #2 00408eb1 boost::asio::detail::binder2<host_state::receive()::__lambda1, boost::system::error_code, unsigned int>::operator()(void)(this=0x28fc00) (c:/boost_1_57_0/boost/asio/detail/bind_handler.hpp:127) #3 00408e61 boost::asio::asio_handler_invoke<boost::asio::detail::binder2<host_state::receive()::__lambda1, boost::system::error_code, unsigned int> >(boost::asio::detail::binder2<host_state::receive()::__lambda1, boost::system::error_code, unsigned int> &, ...)(function=...) (c:/boost_1_57_0/boost/asio/handler_invoke_hook.hpp:69) #4 00408d55 boost_asio_handler_invoke_helpers::invoke<boost::asio::detail::binder2<host_state::receive()::__lambda1, boost::system::error_code, unsigned int>, host_state::receive()::__lambda1>(boost::asio::detail::binder2<host_state::receive()::__lambda1, boost::system::error_code, unsigned int> &, host_state::__lambda1 &)(function=..., context=...) (c:/boost_1_57_0/boost/asio/detail/handler_invoke_helpers.hpp:37) #5 00408993 boost::asio::detail::win_iocp_socket_recvfrom_op<boost::asio::mutable_buffers_1, boost::asio::ip::basic_endpoint<boost::asio::ip::udp>, host_state::receive()::__lambda1>::do_complete(boost::asio::detail::io_service_impl *, boost::asio::detail::operation *, const boost::system::error_code &, std::size_t)(owner=0x9cff48, base=0x9c3510, result_ec=..., bytes_transferred=67) (c:/boost_1_57_0/boost/asio/detail/win_iocp_socket_recvfrom_op.hpp:104) #6 00423cd0 boost::asio::detail::win_iocp_operation::complete(this=0x9c3510, owner=..., ec=..., bytes_transferred=67) (c:/boost_1_57_0/boost/asio/detail/win_iocp_operation.hpp:46) #7 00424df5 boost::asio::detail::win_iocp_io_service::do_one(this=0x9cff48, block=true, ec=...) (c:/boost_1_57_0/boost/asio/detail/impl/win_iocp_io_service.ipp:405) #8 00424aa1 boost::asio::detail::win_iocp_io_service::run(this=0x9cff48, ec=...) (c:/boost_1_57_0/boost/asio/detail/impl/win_iocp_io_service.ipp:164) #9 0041e65a boost::asio::io_service::run(this=0x511391c) (c:/boost_1_57_0/boost/asio/impl/io_service.ipp:59) #10 0041645e _fu15___zst4cout() (c:/users/poteto/desktop/pixely_trenches/sources/host.h:232) #11 00405a87 changestate() (c:\users\poteto\desktop\pixely_trenches\sources\main.cpp:44) #12 00405c90 sdl_main(argc=argc@entry=1, argv=argv@entry=0x3b0008) (c:\users\poteto\desktop\pixely_trenches\sources\main.cpp:98) #13 0040c11c console_main(argc=argc@entry=1, argv=argv@entry=0x3b0008) (../src/main/windows/sdl_windows_main.c:140) #14 0040c2dd winmain@16(hinst=0x400000, hprev=0x0, szcmdline=0x9d3ab4 "", sw=10) (../src/main/windows/sdl_windows_main.c:177) #15 00448c1b main () (??:??)
when queue async activity, want use shared_ptr in bind object's life extended long there outstanding activities.
your problem in usage of boost::bind.
for example:
timeout.async_wait(boost::bind(&stranger::dotimeout, shared_from_this()));
is proper pattern.
Comments
Post a Comment