Skip to content

Commit df8b229

Browse files
committed
fix connect timeoute introduced by pull request#11
1 parent 035ad01 commit df8b229

File tree

3 files changed

+55
-48
lines changed

3 files changed

+55
-48
lines changed

src/internal/sio_client_impl.cpp

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#endif
2020

2121
using boost::posix_time::milliseconds;
22+
using namespace std;
2223

2324
namespace sio
2425
{
@@ -60,7 +61,7 @@ namespace sio
6061
sync_close();
6162
}
6263

63-
void client_impl::connect(const std::string& uri, const std::map<string,string>& query)
64+
void client_impl::connect(const string& uri, const map<string,string>& query)
6465
{
6566
if(m_reconn_timer)
6667
{
@@ -87,25 +88,25 @@ namespace sio
8788
m_base_url = uri;
8889
m_reconn_made = 0;
8990

90-
std::string queryString;
91-
for(std::map<std::string,std::string>::const_iterator it=query.begin();it!=query.end();++it){
92-
queryString.append("&");
93-
queryString.append(it->first);
94-
queryString.append("=");
95-
queryString.append(it->second);
91+
string query_str;
92+
for(map<string,string>::const_iterator it=query.begin();it!=query.end();++it){
93+
query_str.append("&");
94+
query_str.append(it->first);
95+
query_str.append("=");
96+
query_str.append(it->second);
9697
}
97-
m_query_string=queryString;
98+
m_query_string=move(query_str);
9899

99100
this->reset_states();
100-
m_client.get_io_service().dispatch(lib::bind(&client_impl::connect_impl,this,uri,queryString));
101-
m_network_thread.reset(new std::thread(lib::bind(&client_impl::run_loop,this)));//uri lifecycle?
101+
m_client.get_io_service().dispatch(lib::bind(&client_impl::connect_impl,this,uri,m_query_string));
102+
m_network_thread.reset(new thread(lib::bind(&client_impl::run_loop,this)));//uri lifecycle?
102103

103104
}
104105

105-
socket::ptr const& client_impl::socket(std::string const& nsp)
106+
socket::ptr const& client_impl::socket(string const& nsp)
106107
{
107-
std::lock_guard<std::mutex> guard(m_socket_mutex);
108-
std::string aux;
108+
lock_guard<mutex> guard(m_socket_mutex);
109+
string aux;
109110
if(nsp == "")
110111
{
111112
aux = "/";
@@ -127,7 +128,7 @@ namespace sio
127128
}
128129
else
129130
{
130-
std::pair<const std::string, socket::ptr> p(aux,std::shared_ptr<sio::socket>(new sio::socket(this,aux)));
131+
pair<const string, socket::ptr> p(aux,shared_ptr<sio::socket>(new sio::socket(this,aux)));
131132
return (m_sockets.insert(p).first)->second;
132133
}
133134
}
@@ -157,9 +158,9 @@ namespace sio
157158
m_packet_mgr.encode(p);
158159
}
159160

160-
void client_impl::remove_socket(std::string const& nsp)
161+
void client_impl::remove_socket(string const& nsp)
161162
{
162-
std::lock_guard<std::mutex> guard(m_socket_mutex);
163+
lock_guard<mutex> guard(m_socket_mutex);
163164
auto it = m_sockets.find(nsp);
164165
if(it!= m_sockets.end())
165166
{
@@ -172,12 +173,12 @@ namespace sio
172173
return m_client.get_io_service();
173174
}
174175

175-
void client_impl::on_socket_closed(std::string const& nsp)
176+
void client_impl::on_socket_closed(string const& nsp)
176177
{
177178
if(m_socket_close_listener)m_socket_close_listener(nsp);
178179
}
179180

180-
void client_impl::on_socket_opened(std::string const& nsp)
181+
void client_impl::on_socket_opened(string const& nsp)
181182
{
182183
if(m_socket_open_listener)m_socket_open_listener(nsp);
183184
}
@@ -192,11 +193,11 @@ namespace sio
192193
"run loop end");
193194
}
194195

195-
void client_impl::connect_impl(const std::string& uri, const std::string& queryString)
196+
void client_impl::connect_impl(const string& uri, const string& queryString)
196197
{
197198
do{
198199
websocketpp::uri uo(uri);
199-
std::ostringstream ss;
200+
ostringstream ss;
200201

201202
if (m_sid.size()==0) {
202203
ss<<"ws://"<<uo.get_host()<<":"<<uo.get_port()<<"/socket.io/?EIO=4&transport=websocket&t="<<time(NULL)<<queryString;
@@ -223,17 +224,17 @@ namespace sio
223224
}
224225
}
225226

226-
void client_impl::close_impl(close::status::value const& code,std::string const& reason)
227+
void client_impl::close_impl(close::status::value const& code,string const& reason)
227228
{
228-
LOG("Close by reason:"<<reason << std::endl);
229+
LOG("Close by reason:"<<reason << endl);
229230
if(m_reconn_timer)
230231
{
231232
m_reconn_timer->cancel();
232233
m_reconn_timer.reset();
233234
}
234235
if (m_con.expired())
235236
{
236-
std::cerr << "Error: No active session" << std::endl;
237+
cerr << "Error: No active session" << endl;
237238
}
238239
else
239240
{
@@ -242,7 +243,7 @@ namespace sio
242243
}
243244
}
244245

245-
void client_impl::send_impl(std::shared_ptr<const std::string> const& payload_ptr,frame::opcode::value opcode)
246+
void client_impl::send_impl(shared_ptr<const string> const& payload_ptr,frame::opcode::value opcode)
246247
{
247248
if(m_con_state == con_opened)
248249
{
@@ -257,7 +258,7 @@ namespace sio
257258
m_client.send(m_con,*payload_ptr,opcode,ec);
258259
if(ec)
259260
{
260-
std::cerr<<"Send failed,reason:"<< ec.message()<<std::endl;
261+
cerr<<"Send failed,reason:"<< ec.message()<<endl;
261262
}
262263
}
263264
}
@@ -266,7 +267,7 @@ namespace sio
266267
{
267268
if(ec || m_con.expired())
268269
{
269-
LOG("ping exit,con is expired?"<<m_con.expired()<<",ec:"<<ec.message()<<std::endl);
270+
LOG("ping exit,con is expired?"<<m_con.expired()<<",ec:"<<ec.message()<<endl);
270271
return;
271272
}
272273
packet p(packet::frame_ping);
@@ -297,7 +298,7 @@ namespace sio
297298
{
298299
return;
299300
}
300-
LOG("Pong timeout"<<std::endl);
301+
LOG("Pong timeout"<<endl);
301302
m_client.get_io_service().dispatch(lib::bind(&client_impl::close_impl, this,close::status::policy_violation,"Pong timeout"));
302303
}
303304

@@ -312,7 +313,7 @@ namespace sio
312313
m_con_state = con_opening;
313314
m_reconn_made++;
314315
this->reset_states();
315-
LOG("Reconnecting..."<<std::endl);
316+
LOG("Reconnecting..."<<endl);
316317
if(m_reconnecting_listener) m_reconnecting_listener();
317318
m_client.get_io_service().dispatch(lib::bind(&client_impl::connect_impl,this,m_base_url,m_query_string));
318319
}
@@ -324,9 +325,9 @@ namespace sio
324325
return min<unsigned>(static_cast<unsigned>(m_reconn_delay * pow(1.5,m_reconn_made)),m_reconn_delay_max);
325326
}
326327

327-
socket::ptr client_impl::get_socket_locked(std::string const& nsp)
328+
socket::ptr client_impl::get_socket_locked(string const& nsp)
328329
{
329-
std::lock_guard<std::mutex> guard(m_socket_mutex);
330+
lock_guard<mutex> guard(m_socket_mutex);
330331
auto it = m_sockets.find(nsp);
331332
if(it != m_sockets.end())
332333
{
@@ -340,9 +341,9 @@ namespace sio
340341

341342
void client_impl::sockets_invoke_void(void (sio::socket::*fn)(void))
342343
{
343-
std::map<const std::string,socket::ptr> socks;
344+
map<const string,socket::ptr> socks;
344345
{
345-
std::lock_guard<std::mutex> guard(m_socket_mutex);
346+
lock_guard<mutex> guard(m_socket_mutex);
346347
socks.insert(m_sockets.begin(),m_sockets.end());
347348
}
348349
for (auto it = socks.begin(); it!=socks.end(); ++it) {
@@ -355,10 +356,10 @@ namespace sio
355356
m_con.reset();
356357
m_con_state = con_closed;
357358
this->sockets_invoke_void(&sio::socket::on_disconnect);
358-
LOG("Connection failed." << std::endl);
359+
LOG("Connection failed." << endl);
359360
if(m_reconn_made<m_reconn_attempts)
360361
{
361-
LOG("Reconnect for attempt:"<<m_reconn_made<<std::endl);
362+
LOG("Reconnect for attempt:"<<m_reconn_made<<endl);
362363
unsigned delay = this->next_delay();
363364
if(m_reconnect_listener) m_reconnect_listener(m_reconn_made,delay);
364365
m_reconn_timer.reset(new boost::asio::deadline_timer(m_client.get_io_service()));
@@ -374,23 +375,24 @@ namespace sio
374375

375376
void client_impl::on_open(connection_hdl con)
376377
{
377-
LOG("Connected." << std::endl);
378+
LOG("Connected." << endl);
378379
m_con_state = con_opened;
379380
m_con = con;
380381
m_reconn_made = 0;
381382
this->sockets_invoke_void(&sio::socket::on_open);
383+
this->socket("");
382384
if(m_open_listener)m_open_listener();
383385
}
384386

385387
void client_impl::on_close(connection_hdl con)
386388
{
387-
LOG("Client Disconnected." << std::endl);
389+
LOG("Client Disconnected." << endl);
388390
m_con_state = con_closed;
389391
lib::error_code ec;
390392
close::status::value code = close::status::normal;
391393
client_type::connection_ptr conn_ptr = m_client.get_con_from_hdl(con, ec);
392394
if (ec) {
393-
LOG("OnClose get conn failed"<<ec<<std::endl);
395+
LOG("OnClose get conn failed"<<ec<<endl);
394396
}
395397
else
396398
{
@@ -410,7 +412,7 @@ namespace sio
410412
this->sockets_invoke_void(&sio::socket::on_disconnect);
411413
if(m_reconn_made<m_reconn_attempts)
412414
{
413-
LOG("Reconnect for attempt:"<<m_reconn_made<<std::endl);
415+
LOG("Reconnect for attempt:"<<m_reconn_made<<endl);
414416
unsigned delay = this->next_delay();
415417
if(m_reconnect_listener) m_reconnect_listener(m_reconn_made,delay);
416418
m_reconn_timer.reset(new boost::asio::deadline_timer(m_client.get_io_service()));
@@ -447,15 +449,15 @@ namespace sio
447449
const map<string,message::ptr>* values = &(obj_ptr->get_map());
448450
auto it = values->find("sid");
449451
if (it!= values->end()) {
450-
m_sid = std::static_pointer_cast<string_message>(it->second)->get_string();
452+
m_sid = static_pointer_cast<string_message>(it->second)->get_string();
451453
}
452454
else
453455
{
454456
goto failed;
455457
}
456458
it = values->find("pingInterval");
457459
if (it!= values->end()&&it->second->get_flag() == message::flag_integer) {
458-
m_ping_interval = (unsigned)std::static_pointer_cast<int_message>(it->second)->get_int();
460+
m_ping_interval = (unsigned)static_pointer_cast<int_message>(it->second)->get_int();
459461
}
460462
else
461463
{
@@ -464,7 +466,7 @@ namespace sio
464466
it = values->find("pingTimeout");
465467

466468
if (it!=values->end()&&it->second->get_flag() == message::flag_integer) {
467-
m_ping_timeout = (unsigned) std::static_pointer_cast<int_message>(it->second)->get_int();
469+
m_ping_timeout = (unsigned) static_pointer_cast<int_message>(it->second)->get_int();
468470
}
469471
else
470472
{
@@ -474,9 +476,9 @@ namespace sio
474476
m_ping_timer.reset(new boost::asio::deadline_timer(m_client.get_io_service()));
475477
boost::system::error_code ec;
476478
m_ping_timer->expires_from_now(milliseconds(m_ping_interval), ec);
477-
if(ec)LOG("ec:"<<ec.message()<<std::endl);
479+
if(ec)LOG("ec:"<<ec.message()<<endl);
478480
m_ping_timer->async_wait(lib::bind(&client_impl::ping,this,lib::placeholders::_1));
479-
LOG("On handshake,sid:"<<m_sid<<",ping interval:"<<m_ping_interval<<",ping timeout"<<m_ping_timeout<<std::endl);
481+
LOG("On handshake,sid:"<<m_sid<<",ping interval:"<<m_ping_interval<<",ping timeout"<<m_ping_timeout<<endl);
480482
return;
481483
}
482484
failed:
@@ -521,13 +523,13 @@ namespace sio
521523

522524
void client_impl::on_encode(bool isBinary,shared_ptr<const string> const& payload)
523525
{
524-
LOG("encoded payload length:"<<payload->length()<<std::endl);
526+
LOG("encoded payload length:"<<payload->length()<<endl);
525527
m_client.get_io_service().dispatch(lib::bind(&client_impl::send_impl,this,payload,isBinary?frame::opcode::binary:frame::opcode::text));
526528
}
527529

528530
void client_impl::clear_timers()
529531
{
530-
LOG("clear timers"<<std::endl);
532+
LOG("clear timers"<<endl);
531533
boost::system::error_code ec;
532534
if(m_ping_timeout_timer)
533535
{

src/internal/sio_client_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ namespace sio
8787
}
8888

8989
// Client Functions - such as send, etc.
90-
void connect(const std::string& uri, const std::map<string, string>& queryString);
90+
void connect(const std::string& uri, const std::map<std::string, std::string>& queryString);
9191

9292
sio::socket::ptr const& socket(const std::string& nsp);
9393

src/sio_socket.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,10 @@ namespace sio
265265
void socket::impl::send_connect()
266266
{
267267
NULL_GUARD(m_client);
268+
if(m_nsp == "/")
269+
{
270+
return;
271+
}
268272
packet p(packet::type_connect,m_nsp);
269273
m_client->send(p);
270274
m_connection_timer.reset(new boost::asio::deadline_timer(m_client->get_io_service()));
@@ -469,8 +473,9 @@ namespace sio
469473
return;
470474
}
471475
m_connection_timer.reset();
472-
LOG("Connection timeout"<<std::endl);
473-
this->on_disconnect();
476+
LOG("Connection timeout,close socket."<<std::endl);
477+
//Should close socket if no connected message arrive.Otherwise we'll never ask for open again.
478+
this->on_close();
474479
}
475480

476481
void socket::impl::send_packet(sio::packet &p)

0 commit comments

Comments
 (0)