3535#include " network/HttpClient.h"
3636#include " network/Uri.h"
3737#include " network/WebSocket.h"
38+
39+ #include < memory>
3840#include < algorithm>
3941#include < iterator>
4042#include < sstream>
@@ -80,8 +82,8 @@ class SocketIOPacket
8082 std::vector<std::string> getData ()const { return _args; };
8183 virtual std::string stringify ()const ;
8284
83- static SocketIOPacket * createPacketWithType (const std::string& type, SocketIOVersion version);
84- static SocketIOPacket * createPacketWithTypeIndex (int type, SocketIOVersion version);
85+ static std::shared_ptr< SocketIOPacket> createPacketWithType (const std::string& type, SocketIOVersion version);
86+ static std::shared_ptr< SocketIOPacket> createPacketWithTypeIndex (int type, SocketIOVersion version);
8587protected:
8688 std::string _pId;// id message
8789 std::string _ack;//
@@ -306,46 +308,47 @@ SocketIOPacketV10x::~SocketIOPacketV10x()
306308 _endpoint = " " ;
307309}
308310
309- SocketIOPacket * SocketIOPacket::createPacketWithType (const std::string& type, SocketIOPacket::SocketIOVersion version)
311+ std::shared_ptr< SocketIOPacket> SocketIOPacket::createPacketWithType (const std::string& type, SocketIOPacket::SocketIOVersion version)
310312{
311- SocketIOPacket *ret;
312- switch (version)
313+ if (version == SocketIOPacket::SocketIOVersion::V09x)
313314 {
314- case SocketIOPacket::SocketIOVersion::V09x:
315- ret = new (std::nothrow) SocketIOPacket;
316- break ;
317- case SocketIOPacket::SocketIOVersion::V10x:
318- ret = new (std::nothrow) SocketIOPacketV10x;
319- break ;
315+ auto ret = std::make_shared<SocketIOPacket>();
316+ ret->initWithType (type);
317+ return ret;
318+ }
319+ else if (version == SocketIOPacket::SocketIOVersion::V10x)
320+ {
321+ auto ret = std::make_shared<SocketIOPacketV10x>();
322+ ret->initWithType (type);
323+ return ret;
320324 }
321- ret->initWithType (type);
322- return ret;
325+ return nullptr ;
323326}
324327
325-
326- SocketIOPacket * SocketIOPacket::createPacketWithTypeIndex (int type, SocketIOPacket::SocketIOVersion version)
328+ std::shared_ptr<SocketIOPacket> SocketIOPacket::createPacketWithTypeIndex (int type, SocketIOPacket::SocketIOVersion version)
327329{
328- SocketIOPacket *ret;
329- switch (version)
330+ if (version == SocketIOPacket::SocketIOVersion::V09x)
330331 {
331- case SocketIOPacket::SocketIOVersion::V09x:
332- ret = new (std::nothrow) SocketIOPacket;
333- break ;
334- case SocketIOPacket::SocketIOVersion::V10x:
335- return new (std::nothrow) SocketIOPacketV10x;
336- break ;
332+ auto ret = std::make_shared<SocketIOPacket>();
333+ ret->initWithTypeIndex (type);
334+ return ret;
337335 }
338- ret->initWithTypeIndex (type);
339- return ret;
336+ else if (version == SocketIOPacket::SocketIOVersion::V10x)
337+ {
338+ auto ret = std::make_shared<SocketIOPacketV10x>();
339+ ret->initWithTypeIndex (type);
340+ return ret;
341+ }
342+ return nullptr ;
340343}
341344
342345/* *
343346 * @brief The implementation of the socket.io connection
344347 * Clients/endpoints may share the same impl to accomplish multiplexing on the same websocket
345348 */
346349class SIOClientImpl :
347- public cocos2d::Ref ,
348- public WebSocket::Delegate
350+ public WebSocket::Delegate ,
351+ public std::enable_shared_from_this<SIOClientImpl>
349352{
350353private:
351354 int _heartbeat, _timeout;
@@ -363,7 +366,7 @@ class SIOClientImpl :
363366 SIOClientImpl (const Uri& uri, const std::string& caFilePath);
364367 virtual ~SIOClientImpl ();
365368
366- static SIOClientImpl* create (const Uri& uri, const std::string& caFilePath);
369+ static std::shared_ptr< SIOClientImpl> create (const Uri& uri, const std::string& caFilePath);
367370
368371 virtual void onOpen (WebSocket* ws);
369372 virtual void onMessage (WebSocket* ws, const WebSocket::Data& data);
@@ -386,7 +389,7 @@ class SIOClientImpl :
386389
387390 void send (const std::string& endpoint, const std::string& s);
388391 void send (const std::string& endpoint, const std::vector<std::string>& s);
389- void send (SocketIOPacket * packet);
392+ void send (std::shared_ptr< SocketIOPacket>& packet);
390393 void emit (const std::string& endpoint, const std::string& eventname, const std::string& args);
391394 void emit (const std::string& endpoint, const std::string& eventname, const std::vector<std::string>& args);
392395
@@ -430,7 +433,14 @@ void SIOClientImpl::handshake()
430433 request->setUrl (pre .str ());
431434 request->setRequestType (HttpRequest::Type::GET);
432435
433- request->setResponseCallback (CC_CALLBACK_2 (SIOClientImpl::handshakeResponse, this ));
436+ std::weak_ptr<SIOClientImpl> self = shared_from_this ();
437+ auto callback = [self](HttpClient* client, HttpResponse *resp) {
438+ auto conn = self.lock ();
439+ if (conn) {
440+ conn->handshakeResponse (client, resp);
441+ }
442+ };
443+ request->setResponseCallback (callback);
434444 request->setTag (" handshake" );
435445
436446 CCLOGINFO (" SIOClientImpl::handshake() waiting" );
@@ -627,13 +637,13 @@ void SIOClientImpl::disconnect()
627637 _ws->close ();
628638}
629639
630- SIOClientImpl* SIOClientImpl::create (const Uri& uri, const std::string& caFilePath)
640+ std::shared_ptr< SIOClientImpl> SIOClientImpl::create (const Uri& uri, const std::string& caFilePath)
631641{
632642 SIOClientImpl *s = new (std::nothrow) SIOClientImpl (uri, caFilePath);
633643
634644 if (s && s->init ())
635645 {
636- return s ;
646+ return std::shared_ptr<SIOClientImpl>(s) ;
637647 }
638648
639649 return nullptr ;
@@ -651,7 +661,7 @@ void SIOClientImpl::addClient(const std::string& endpoint, SIOClient* client)
651661
652662void SIOClientImpl::connectToEndpoint (const std::string& endpoint)
653663{
654- SocketIOPacket * packet = SocketIOPacket::createPacketWithType (" connect" , _version);
664+ auto packet = SocketIOPacket::createPacketWithType (" connect" , _version);
655665 packet->setEndpoint (endpoint);
656666 this ->send (packet);
657667}
@@ -679,7 +689,7 @@ void SIOClientImpl::disconnectFromEndpoint(const std::string& endpoint)
679689
680690void SIOClientImpl::heartbeat (float /* dt*/ )
681691{
682- SocketIOPacket * packet = SocketIOPacket::createPacketWithType (" heartbeat" , _version);
692+ auto packet = SocketIOPacket::createPacketWithType (" heartbeat" , _version);
683693
684694 this ->send (packet);
685695
@@ -692,7 +702,7 @@ void SIOClientImpl::send(const std::string& endpoint, const std::vector<std::str
692702 switch (_version) {
693703 case SocketIOPacket::SocketIOVersion::V09x:
694704 {
695- SocketIOPacket * packet = SocketIOPacket::createPacketWithType (" message" , _version);
705+ auto packet = SocketIOPacket::createPacketWithType (" message" , _version);
696706 packet->setEndpoint (endpoint);
697707 for (auto &i : s)
698708 {
@@ -715,7 +725,7 @@ void SIOClientImpl::send(const std::string& endpoint, const std::string& s)
715725 send (endpoint, t);
716726}
717727
718- void SIOClientImpl::send (SocketIOPacket * packet)
728+ void SIOClientImpl::send (std::shared_ptr< SocketIOPacket>& packet)
719729{
720730 std::string req = packet->toString ();
721731 if (_connected)
@@ -730,7 +740,7 @@ void SIOClientImpl::send(SocketIOPacket *packet)
730740void SIOClientImpl::emit (const std::string& endpoint, const std::string& eventname, const std::string& args)
731741{
732742 CCLOGINFO (" Emitting event \" %s\" " , eventname.c_str ());
733- SocketIOPacket * packet = SocketIOPacket::createPacketWithType (" event" , _version);
743+ auto packet = SocketIOPacket::createPacketWithType (" event" , _version);
734744 packet->setEndpoint (endpoint == " /" ? " " : endpoint);
735745 packet->setEvent (eventname);
736746 packet->addData (args);
@@ -740,7 +750,7 @@ void SIOClientImpl::emit(const std::string& endpoint, const std::string& eventna
740750void SIOClientImpl::emit (const std::string& endpoint, const std::string& eventname, const std::vector<std::string>& args)
741751{
742752 CCLOGINFO (" Emitting event \" %s\" " , eventname.c_str ());
743- SocketIOPacket * packet = SocketIOPacket::createPacketWithType (" event" , _version);
753+ auto packet = SocketIOPacket::createPacketWithType (" event" , _version);
744754 packet->setEndpoint (endpoint == " /" ? " " : endpoint);
745755 packet->setEvent (eventname);
746756 for (auto &arg : args) {
@@ -753,22 +763,30 @@ void SIOClientImpl::onOpen(WebSocket* /*ws*/)
753763{
754764 _connected = true ;
755765
756- SocketIO::getInstance ()->addSocket (_uri.getAuthority (), this );
766+ auto self = shared_from_this ();
767+
768+ SocketIO::getInstance ()->addSocket (_uri.getAuthority (), self);
757769
758770 if (_version == SocketIOPacket::SocketIOVersion::V10x)
759771 {
760772 std::string s = " 5" ;// That's a ping https://github.com/Automattic/engine.io-parser/blob/1b8e077b2218f4947a69f5ad18be2a512ed54e93/lib/index.js#L21
761773 _ws->send (s);
762774 }
763775
764- Director::getInstance ()->getScheduler ()->schedule (CC_SCHEDULE_SELECTOR (SIOClientImpl::heartbeat), this , (_heartbeat * .9f ), false );
776+ std::weak_ptr<SIOClientImpl> selfWeak = shared_from_this ();
777+ auto f = [selfWeak](float dt) {
778+ auto conn = selfWeak.lock ();
779+ if (conn)
780+ conn->heartbeat (dt);
781+ };
782+
783+ Director::getInstance ()->getScheduler ()->schedule (f, this , (_heartbeat * .9f ), false , " heart_beat" );
765784
766785 for (auto & client : _clients)
767786 {
768787 client.second ->onOpen ();
769788 }
770789
771- CCLOGINFO (" SIOClientImpl::onOpen socket connected!" );
772790}
773791
774792void SIOClientImpl::onMessage (WebSocket* /* ws*/ , const WebSocket::Data& data)
@@ -1012,11 +1030,9 @@ void SIOClientImpl::onClose(WebSocket* /*ws*/)
10121030 _connected = false ;
10131031 if (Director::getInstance ())
10141032 Director::getInstance ()->getScheduler ()->unscheduleAllForTarget (this );
1015-
1033+
10161034 SocketIO::getInstance ()->removeSocket (_uri.getAuthority ());
10171035 }
1018-
1019- this ->release ();
10201036}
10211037
10221038void SIOClientImpl::onError (WebSocket* /* ws*/ , const WebSocket::ErrorCode& error)
@@ -1025,13 +1041,12 @@ void SIOClientImpl::onError(WebSocket* /*ws*/, const WebSocket::ErrorCode& error
10251041}
10261042
10271043// begin SIOClient methods
1028- SIOClient::SIOClient (const std::string& path, SIOClientImpl* impl, SocketIO::SIODelegate& delegate)
1044+ SIOClient::SIOClient (const std::string& path, std::shared_ptr< SIOClientImpl>& impl, SocketIO::SIODelegate& delegate)
10291045 : _path(path)
10301046 , _connected(false )
10311047 , _socket(impl)
10321048 , _delegate(&delegate)
10331049{
1034- CC_SAFE_RETAIN (_socket);
10351050}
10361051
10371052SIOClient::~SIOClient ()
@@ -1040,7 +1055,6 @@ SIOClient::~SIOClient()
10401055 {
10411056 _socket->disconnectFromEndpoint (_path);
10421057 }
1043- CC_SAFE_RELEASE (_socket);
10441058}
10451059
10461060void SIOClient::onOpen ()
@@ -1108,15 +1122,13 @@ void SIOClient::disconnect()
11081122{
11091123 setConnected (false );
11101124 _socket->disconnectFromEndpoint (_path);
1111-
11121125 this ->release ();
11131126}
11141127
11151128void SIOClient::socketClosed ()
11161129{
11171130 setConnected (false );
11181131 _delegate->onClose (this );
1119-
11201132 this ->release ();
11211133}
11221134
@@ -1125,7 +1137,7 @@ bool SIOClient::isConnected() const
11251137 return _connected && _socket && _socket->_connected ;
11261138}
11271139
1128- void SIOClient::setConnected (bool connected)
1140+ void SIOClient::setConnected (bool connected)
11291141{
11301142 _connected = connected;
11311143}
@@ -1196,8 +1208,8 @@ SIOClient* SocketIO::connect(const std::string& uri, SocketIO::SIODelegate& dele
11961208{
11971209 Uri uriObj = Uri::parse (uri);
11981210
1199- SIOClientImpl * socket = SocketIO::getInstance ()->getSocket (uriObj.getAuthority ());
1200- SIOClient *c = nullptr ;
1211+ std::shared_ptr< SIOClientImpl> socket = SocketIO::getInstance ()->getSocket (uriObj.getAuthority ());
1212+ SIOClient * c = nullptr ;
12011213
12021214 std::string path = uriObj.getPath ();
12031215 if (path.empty ())
@@ -1233,7 +1245,7 @@ SIOClient* SocketIO::connect(const std::string& uri, SocketIO::SIODelegate& dele
12331245 c->disconnect ();
12341246
12351247 CCLOG (" SocketIO: recreate a new socket, new client, connect" );
1236- SIOClientImpl* newSocket = SIOClientImpl::create (uriObj, caFilePath);
1248+ std::shared_ptr< SIOClientImpl> newSocket = SIOClientImpl::create (uriObj, caFilePath);
12371249 SIOClient *newC = new (std::nothrow) SIOClient (path, newSocket, delegate);
12381250
12391251 newSocket->addClient (path, newC);
@@ -1246,14 +1258,16 @@ SIOClient* SocketIO::connect(const std::string& uri, SocketIO::SIODelegate& dele
12461258 return c;
12471259}
12481260
1249- SIOClientImpl* SocketIO::getSocket (const std::string& uri)
1261+ std::shared_ptr< SIOClientImpl> SocketIO::getSocket (const std::string& uri)
12501262{
1251- return _sockets.at (uri);
1263+ auto p = _sockets.find (uri);
1264+ if (p == _sockets.end ()) return nullptr ;
1265+ return p->second .lock ();
12521266}
12531267
1254- void SocketIO::addSocket (const std::string& uri, SIOClientImpl* socket)
1268+ void SocketIO::addSocket (const std::string& uri, std::shared_ptr< SIOClientImpl>& socket)
12551269{
1256- _sockets.insert (uri, socket);
1270+ _sockets.emplace (uri, socket);
12571271}
12581272
12591273void SocketIO::removeSocket (const std::string& uri)
0 commit comments