From a94d95ac59d44c6b5ee0e83fa5345ac18cea2960 Mon Sep 17 00:00:00 2001 From: melode11 Date: Thu, 2 Apr 2015 19:20:39 +0800 Subject: [PATCH] fix some special case of packet parsing,fully support namespace --- src/sio_client.cpp | 27 ++++++++++++++++++++---- src/sio_packet.cpp | 52 ++++++++++++++++++++++++++++------------------ src/sio_packet.h | 2 +- 3 files changed, 56 insertions(+), 25 deletions(-) diff --git a/src/sio_client.cpp b/src/sio_client.cpp index 00183e0..4631b37 100755 --- a/src/sio_client.cpp +++ b/src/sio_client.cpp @@ -472,7 +472,16 @@ void set_##__FIELD__(__TYPE__ const& l) \ boost::system::error_code ec; m_connection_timer->expires_from_now(milliseconds(60000), ec); m_connection_timer->async_wait(lib::bind(&client::impl::__timeout_connection,this,lib::placeholders::_1)); - + if(m_nsp.length()>0)//send connect only if we got nsp, otherwise wait for default one. + { + packet p(packet::type_connect, m_nsp); + m_packet_mgr.encode(p, + [&](bool isBin,shared_ptr payload) + { + lib::error_code ec; + this->m_client.send(this->m_con, *payload, frame::opcode::text, ec); + }); + } if(m_open_listener)m_open_listener(); } @@ -537,7 +546,10 @@ void set_##__FIELD__(__TYPE__ const& l) \ case packet::type_connect: { LOG("Received Message type (Connect)"<on_connected(); + if(p.get_nsp() == m_nsp) + { + this->on_connected(); + } break; } case packet::type_disconnect: @@ -731,7 +743,7 @@ void set_##__FIELD__(__TYPE__ const& l) \ else { std::string payload; - packet pack(packet::type_disconnect); + packet pack(packet::type_disconnect,m_nsp); m_packet_mgr.encode(pack, [&](bool isBin,shared_ptr payload) { @@ -804,7 +816,14 @@ void set_##__FIELD__(__TYPE__ const& l) \ { ss<<"ws://"<=0))); } - packet::packet(type type,message::ptr const& msg): + packet::packet(type type,string const& nsp, message::ptr const& msg): _frame(frame_message), _type(type), + _nsp(nsp), _message(msg), _pack_id(-1), _pending_buffers(0) @@ -260,36 +261,47 @@ namespace sio } } - size_t comma_pos = payload_ptr.find_first_of("{[,"); - if( comma_pos!= string::npos && payload_ptr[comma_pos] == ',') + size_t nsp_json_pos = payload_ptr.find_first_of("{[\"/",pos,4); + if(nsp_json_pos==string::npos)//no namespace and no message,the end. { - _nsp = payload_ptr.substr(pos,comma_pos - pos); - pos = comma_pos+1; - } - if (pos >= payload_ptr.length()) { - //message only have type, maybe with namespace. return false; } - size_t data_pos = payload_ptr.find_first_of("[{", pos, 2); - if (data_pos == string::npos) { - //we have pack id, no message. - _pack_id = stoi(payload_ptr.substr(pos)); - return false; - } - else if(data_pos>pos) + size_t json_pos = nsp_json_pos; + if(payload_ptr[nsp_json_pos] == '/')//nsp_json_pos is start of nsp { - //we have pack id and messages. - _pack_id = stoi(payload_ptr.substr(pos,data_pos - pos)); + size_t comma_pos = payload_ptr.find_first_of(",");//end of nsp + if(comma_pos == string::npos)//packet end with nsp + { + _nsp = payload_ptr.substr(nsp_json_pos); + return false; + } + else//we have a message, maybe the message have an id. + { + _nsp = payload_ptr.substr(nsp_json_pos,comma_pos - nsp_json_pos); + pos = comma_pos+1;//start of the message + json_pos = payload_ptr.find_first_of("\"[{", pos, 3);//start of the json part of message + if(json_pos == string::npos) + { + //no message,the end + //assume if there's no message, there's no message id. + return false; + } + } + } + + if(pos(payload_ptr.data() + data_pos, payload_ptr.length() - data_pos)); + _buffers.push_back(make_shared(payload_ptr.data() + json_pos, payload_ptr.length() - json_pos)); return true; } else { Document doc; - doc.Parse<0>(payload_ptr.substr(data_pos).data()); + doc.Parse<0>(payload_ptr.data()+json_pos); _message = from_json(doc, vector >()); return false; } @@ -465,4 +477,4 @@ namespace sio m_decode_callback(*p); } } -} \ No newline at end of file +} diff --git a/src/sio_packet.h b/src/sio_packet.h index d6b5652..45e431e 100755 --- a/src/sio_packet.h +++ b/src/sio_packet.h @@ -54,7 +54,7 @@ namespace sio packet(frame_type frame); - packet(type type,message::ptr const& msg = message::ptr());//other message types constructor. + packet(type type,string const& nsp= string(),message::ptr const& msg = message::ptr());//other message types constructor. //empty constructor for parse. packet();