From df8b2291c87776093917ecebf26d1ef61289e000 Mon Sep 17 00:00:00 2001 From: melode11 Date: Wed, 6 May 2015 23:30:35 +0800 Subject: [PATCH] fix connect timeoute introduced by pull request#11 --- src/internal/sio_client_impl.cpp | 92 ++++++++++++++++---------------- src/internal/sio_client_impl.h | 2 +- src/sio_socket.cpp | 9 +++- 3 files changed, 55 insertions(+), 48 deletions(-) diff --git a/src/internal/sio_client_impl.cpp b/src/internal/sio_client_impl.cpp index c737054..92ce0af 100644 --- a/src/internal/sio_client_impl.cpp +++ b/src/internal/sio_client_impl.cpp @@ -19,6 +19,7 @@ #endif using boost::posix_time::milliseconds; +using namespace std; namespace sio { @@ -60,7 +61,7 @@ namespace sio sync_close(); } - void client_impl::connect(const std::string& uri, const std::map& query) + void client_impl::connect(const string& uri, const map& query) { if(m_reconn_timer) { @@ -87,25 +88,25 @@ namespace sio m_base_url = uri; m_reconn_made = 0; - std::string queryString; - for(std::map::const_iterator it=query.begin();it!=query.end();++it){ - queryString.append("&"); - queryString.append(it->first); - queryString.append("="); - queryString.append(it->second); + string query_str; + for(map::const_iterator it=query.begin();it!=query.end();++it){ + query_str.append("&"); + query_str.append(it->first); + query_str.append("="); + query_str.append(it->second); } - m_query_string=queryString; + m_query_string=move(query_str); this->reset_states(); - m_client.get_io_service().dispatch(lib::bind(&client_impl::connect_impl,this,uri,queryString)); - m_network_thread.reset(new std::thread(lib::bind(&client_impl::run_loop,this)));//uri lifecycle? + m_client.get_io_service().dispatch(lib::bind(&client_impl::connect_impl,this,uri,m_query_string)); + m_network_thread.reset(new thread(lib::bind(&client_impl::run_loop,this)));//uri lifecycle? } - socket::ptr const& client_impl::socket(std::string const& nsp) + socket::ptr const& client_impl::socket(string const& nsp) { - std::lock_guard guard(m_socket_mutex); - std::string aux; + lock_guard guard(m_socket_mutex); + string aux; if(nsp == "") { aux = "/"; @@ -127,7 +128,7 @@ namespace sio } else { - std::pair p(aux,std::shared_ptr(new sio::socket(this,aux))); + pair p(aux,shared_ptr(new sio::socket(this,aux))); return (m_sockets.insert(p).first)->second; } } @@ -157,9 +158,9 @@ namespace sio m_packet_mgr.encode(p); } - void client_impl::remove_socket(std::string const& nsp) + void client_impl::remove_socket(string const& nsp) { - std::lock_guard guard(m_socket_mutex); + lock_guard guard(m_socket_mutex); auto it = m_sockets.find(nsp); if(it!= m_sockets.end()) { @@ -172,12 +173,12 @@ namespace sio return m_client.get_io_service(); } - void client_impl::on_socket_closed(std::string const& nsp) + void client_impl::on_socket_closed(string const& nsp) { if(m_socket_close_listener)m_socket_close_listener(nsp); } - void client_impl::on_socket_opened(std::string const& nsp) + void client_impl::on_socket_opened(string const& nsp) { if(m_socket_open_listener)m_socket_open_listener(nsp); } @@ -192,11 +193,11 @@ namespace sio "run loop end"); } - void client_impl::connect_impl(const std::string& uri, const std::string& queryString) + void client_impl::connect_impl(const string& uri, const string& queryString) { do{ websocketpp::uri uo(uri); - std::ostringstream ss; + ostringstream ss; if (m_sid.size()==0) { ss<<"ws://"<cancel(); @@ -233,7 +234,7 @@ namespace sio } if (m_con.expired()) { - std::cerr << "Error: No active session" << std::endl; + cerr << "Error: No active session" << endl; } else { @@ -242,7 +243,7 @@ namespace sio } } - void client_impl::send_impl(std::shared_ptr const& payload_ptr,frame::opcode::value opcode) + void client_impl::send_impl(shared_ptr const& payload_ptr,frame::opcode::value opcode) { if(m_con_state == con_opened) { @@ -257,7 +258,7 @@ namespace sio m_client.send(m_con,*payload_ptr,opcode,ec); if(ec) { - std::cerr<<"Send failed,reason:"<< ec.message()<reset_states(); - LOG("Reconnecting..."<(static_cast(m_reconn_delay * pow(1.5,m_reconn_made)),m_reconn_delay_max); } - socket::ptr client_impl::get_socket_locked(std::string const& nsp) + socket::ptr client_impl::get_socket_locked(string const& nsp) { - std::lock_guard guard(m_socket_mutex); + lock_guard guard(m_socket_mutex); auto it = m_sockets.find(nsp); if(it != m_sockets.end()) { @@ -340,9 +341,9 @@ namespace sio void client_impl::sockets_invoke_void(void (sio::socket::*fn)(void)) { - std::map socks; + map socks; { - std::lock_guard guard(m_socket_mutex); + lock_guard guard(m_socket_mutex); socks.insert(m_sockets.begin(),m_sockets.end()); } for (auto it = socks.begin(); it!=socks.end(); ++it) { @@ -355,10 +356,10 @@ namespace sio m_con.reset(); m_con_state = con_closed; this->sockets_invoke_void(&sio::socket::on_disconnect); - LOG("Connection failed." << std::endl); + LOG("Connection failed." << endl); if(m_reconn_madenext_delay(); if(m_reconnect_listener) m_reconnect_listener(m_reconn_made,delay); m_reconn_timer.reset(new boost::asio::deadline_timer(m_client.get_io_service())); @@ -374,23 +375,24 @@ namespace sio void client_impl::on_open(connection_hdl con) { - LOG("Connected." << std::endl); + LOG("Connected." << endl); m_con_state = con_opened; m_con = con; m_reconn_made = 0; this->sockets_invoke_void(&sio::socket::on_open); + this->socket(""); if(m_open_listener)m_open_listener(); } void client_impl::on_close(connection_hdl con) { - LOG("Client Disconnected." << std::endl); + LOG("Client Disconnected." << endl); m_con_state = con_closed; lib::error_code ec; close::status::value code = close::status::normal; client_type::connection_ptr conn_ptr = m_client.get_con_from_hdl(con, ec); if (ec) { - LOG("OnClose get conn failed"<sockets_invoke_void(&sio::socket::on_disconnect); if(m_reconn_madenext_delay(); if(m_reconnect_listener) m_reconnect_listener(m_reconn_made,delay); m_reconn_timer.reset(new boost::asio::deadline_timer(m_client.get_io_service())); @@ -447,7 +449,7 @@ namespace sio const map* values = &(obj_ptr->get_map()); auto it = values->find("sid"); if (it!= values->end()) { - m_sid = std::static_pointer_cast(it->second)->get_string(); + m_sid = static_pointer_cast(it->second)->get_string(); } else { @@ -455,7 +457,7 @@ namespace sio } it = values->find("pingInterval"); if (it!= values->end()&&it->second->get_flag() == message::flag_integer) { - m_ping_interval = (unsigned)std::static_pointer_cast(it->second)->get_int(); + m_ping_interval = (unsigned)static_pointer_cast(it->second)->get_int(); } else { @@ -464,7 +466,7 @@ namespace sio it = values->find("pingTimeout"); if (it!=values->end()&&it->second->get_flag() == message::flag_integer) { - m_ping_timeout = (unsigned) std::static_pointer_cast(it->second)->get_int(); + m_ping_timeout = (unsigned) static_pointer_cast(it->second)->get_int(); } else { @@ -474,9 +476,9 @@ namespace sio m_ping_timer.reset(new boost::asio::deadline_timer(m_client.get_io_service())); boost::system::error_code ec; m_ping_timer->expires_from_now(milliseconds(m_ping_interval), ec); - if(ec)LOG("ec:"<async_wait(lib::bind(&client_impl::ping,this,lib::placeholders::_1)); - LOG("On handshake,sid:"< const& payload) { - LOG("encoded payload length:"<length()<length()<& queryString); + void connect(const std::string& uri, const std::map& queryString); sio::socket::ptr const& socket(const std::string& nsp); diff --git a/src/sio_socket.cpp b/src/sio_socket.cpp index 63a460d..754299e 100644 --- a/src/sio_socket.cpp +++ b/src/sio_socket.cpp @@ -265,6 +265,10 @@ namespace sio void socket::impl::send_connect() { NULL_GUARD(m_client); + if(m_nsp == "/") + { + return; + } packet p(packet::type_connect,m_nsp); m_client->send(p); m_connection_timer.reset(new boost::asio::deadline_timer(m_client->get_io_service())); @@ -469,8 +473,9 @@ namespace sio return; } m_connection_timer.reset(); - LOG("Connection timeout"<on_disconnect(); + LOG("Connection timeout,close socket."<on_close(); } void socket::impl::send_packet(sio::packet &p)