35
35
#include " network/HttpClient.h"
36
36
#include " network/Uri.h"
37
37
#include " network/WebSocket.h"
38
+
39
+ #include < memory>
38
40
#include < algorithm>
39
41
#include < iterator>
40
42
#include < sstream>
@@ -80,8 +82,8 @@ class SocketIOPacket
80
82
std::vector<std::string> getData ()const { return _args; };
81
83
virtual std::string stringify ()const ;
82
84
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);
85
87
protected:
86
88
std::string _pId;// id message
87
89
std::string _ack;//
@@ -306,46 +308,47 @@ SocketIOPacketV10x::~SocketIOPacketV10x()
306
308
_endpoint = " " ;
307
309
}
308
310
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)
310
312
{
311
- SocketIOPacket *ret;
312
- switch (version)
313
+ if (version == SocketIOPacket::SocketIOVersion::V09x)
313
314
{
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;
320
324
}
321
- ret->initWithType (type);
322
- return ret;
325
+ return nullptr ;
323
326
}
324
327
325
-
326
- SocketIOPacket * SocketIOPacket::createPacketWithTypeIndex (int type, SocketIOPacket::SocketIOVersion version)
328
+ std::shared_ptr<SocketIOPacket> SocketIOPacket::createPacketWithTypeIndex (int type, SocketIOPacket::SocketIOVersion version)
327
329
{
328
- SocketIOPacket *ret;
329
- switch (version)
330
+ if (version == SocketIOPacket::SocketIOVersion::V09x)
330
331
{
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;
337
335
}
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 ;
340
343
}
341
344
342
345
/* *
343
346
* @brief The implementation of the socket.io connection
344
347
* Clients/endpoints may share the same impl to accomplish multiplexing on the same websocket
345
348
*/
346
349
class SIOClientImpl :
347
- public cocos2d::Ref ,
348
- public WebSocket::Delegate
350
+ public WebSocket::Delegate ,
351
+ public std::enable_shared_from_this<SIOClientImpl>
349
352
{
350
353
private:
351
354
int _heartbeat, _timeout;
@@ -363,7 +366,7 @@ class SIOClientImpl :
363
366
SIOClientImpl (const Uri& uri, const std::string& caFilePath);
364
367
virtual ~SIOClientImpl ();
365
368
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);
367
370
368
371
virtual void onOpen (WebSocket* ws);
369
372
virtual void onMessage (WebSocket* ws, const WebSocket::Data& data);
@@ -386,7 +389,7 @@ class SIOClientImpl :
386
389
387
390
void send (const std::string& endpoint, const std::string& s);
388
391
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);
390
393
void emit (const std::string& endpoint, const std::string& eventname, const std::string& args);
391
394
void emit (const std::string& endpoint, const std::string& eventname, const std::vector<std::string>& args);
392
395
@@ -430,7 +433,14 @@ void SIOClientImpl::handshake()
430
433
request->setUrl (pre .str ());
431
434
request->setRequestType (HttpRequest::Type::GET);
432
435
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);
434
444
request->setTag (" handshake" );
435
445
436
446
CCLOGINFO (" SIOClientImpl::handshake() waiting" );
@@ -627,13 +637,13 @@ void SIOClientImpl::disconnect()
627
637
_ws->close ();
628
638
}
629
639
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)
631
641
{
632
642
SIOClientImpl *s = new (std::nothrow) SIOClientImpl (uri, caFilePath);
633
643
634
644
if (s && s->init ())
635
645
{
636
- return s ;
646
+ return std::shared_ptr<SIOClientImpl>(s) ;
637
647
}
638
648
639
649
return nullptr ;
@@ -651,7 +661,7 @@ void SIOClientImpl::addClient(const std::string& endpoint, SIOClient* client)
651
661
652
662
void SIOClientImpl::connectToEndpoint (const std::string& endpoint)
653
663
{
654
- SocketIOPacket * packet = SocketIOPacket::createPacketWithType (" connect" , _version);
664
+ auto packet = SocketIOPacket::createPacketWithType (" connect" , _version);
655
665
packet->setEndpoint (endpoint);
656
666
this ->send (packet);
657
667
}
@@ -679,7 +689,7 @@ void SIOClientImpl::disconnectFromEndpoint(const std::string& endpoint)
679
689
680
690
void SIOClientImpl::heartbeat (float /* dt*/ )
681
691
{
682
- SocketIOPacket * packet = SocketIOPacket::createPacketWithType (" heartbeat" , _version);
692
+ auto packet = SocketIOPacket::createPacketWithType (" heartbeat" , _version);
683
693
684
694
this ->send (packet);
685
695
@@ -692,7 +702,7 @@ void SIOClientImpl::send(const std::string& endpoint, const std::vector<std::str
692
702
switch (_version) {
693
703
case SocketIOPacket::SocketIOVersion::V09x:
694
704
{
695
- SocketIOPacket * packet = SocketIOPacket::createPacketWithType (" message" , _version);
705
+ auto packet = SocketIOPacket::createPacketWithType (" message" , _version);
696
706
packet->setEndpoint (endpoint);
697
707
for (auto &i : s)
698
708
{
@@ -715,7 +725,7 @@ void SIOClientImpl::send(const std::string& endpoint, const std::string& s)
715
725
send (endpoint, t);
716
726
}
717
727
718
- void SIOClientImpl::send (SocketIOPacket * packet)
728
+ void SIOClientImpl::send (std::shared_ptr< SocketIOPacket>& packet)
719
729
{
720
730
std::string req = packet->toString ();
721
731
if (_connected)
@@ -730,7 +740,7 @@ void SIOClientImpl::send(SocketIOPacket *packet)
730
740
void SIOClientImpl::emit (const std::string& endpoint, const std::string& eventname, const std::string& args)
731
741
{
732
742
CCLOGINFO (" Emitting event \" %s\" " , eventname.c_str ());
733
- SocketIOPacket * packet = SocketIOPacket::createPacketWithType (" event" , _version);
743
+ auto packet = SocketIOPacket::createPacketWithType (" event" , _version);
734
744
packet->setEndpoint (endpoint == " /" ? " " : endpoint);
735
745
packet->setEvent (eventname);
736
746
packet->addData (args);
@@ -740,7 +750,7 @@ void SIOClientImpl::emit(const std::string& endpoint, const std::string& eventna
740
750
void SIOClientImpl::emit (const std::string& endpoint, const std::string& eventname, const std::vector<std::string>& args)
741
751
{
742
752
CCLOGINFO (" Emitting event \" %s\" " , eventname.c_str ());
743
- SocketIOPacket * packet = SocketIOPacket::createPacketWithType (" event" , _version);
753
+ auto packet = SocketIOPacket::createPacketWithType (" event" , _version);
744
754
packet->setEndpoint (endpoint == " /" ? " " : endpoint);
745
755
packet->setEvent (eventname);
746
756
for (auto &arg : args) {
@@ -753,22 +763,30 @@ void SIOClientImpl::onOpen(WebSocket* /*ws*/)
753
763
{
754
764
_connected = true ;
755
765
756
- SocketIO::getInstance ()->addSocket (_uri.getAuthority (), this );
766
+ auto self = shared_from_this ();
767
+
768
+ SocketIO::getInstance ()->addSocket (_uri.getAuthority (), self);
757
769
758
770
if (_version == SocketIOPacket::SocketIOVersion::V10x)
759
771
{
760
772
std::string s = " 5" ;// That's a ping https://github.com/Automattic/engine.io-parser/blob/1b8e077b2218f4947a69f5ad18be2a512ed54e93/lib/index.js#L21
761
773
_ws->send (s);
762
774
}
763
775
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" );
765
784
766
785
for (auto & client : _clients)
767
786
{
768
787
client.second ->onOpen ();
769
788
}
770
789
771
- CCLOGINFO (" SIOClientImpl::onOpen socket connected!" );
772
790
}
773
791
774
792
void SIOClientImpl::onMessage (WebSocket* /* ws*/ , const WebSocket::Data& data)
@@ -1012,11 +1030,9 @@ void SIOClientImpl::onClose(WebSocket* /*ws*/)
1012
1030
_connected = false ;
1013
1031
if (Director::getInstance ())
1014
1032
Director::getInstance ()->getScheduler ()->unscheduleAllForTarget (this );
1015
-
1033
+
1016
1034
SocketIO::getInstance ()->removeSocket (_uri.getAuthority ());
1017
1035
}
1018
-
1019
- this ->release ();
1020
1036
}
1021
1037
1022
1038
void SIOClientImpl::onError (WebSocket* /* ws*/ , const WebSocket::ErrorCode& error)
@@ -1025,13 +1041,12 @@ void SIOClientImpl::onError(WebSocket* /*ws*/, const WebSocket::ErrorCode& error
1025
1041
}
1026
1042
1027
1043
// 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)
1029
1045
: _path(path)
1030
1046
, _connected(false )
1031
1047
, _socket(impl)
1032
1048
, _delegate(&delegate)
1033
1049
{
1034
- CC_SAFE_RETAIN (_socket);
1035
1050
}
1036
1051
1037
1052
SIOClient::~SIOClient ()
@@ -1040,7 +1055,6 @@ SIOClient::~SIOClient()
1040
1055
{
1041
1056
_socket->disconnectFromEndpoint (_path);
1042
1057
}
1043
- CC_SAFE_RELEASE (_socket);
1044
1058
}
1045
1059
1046
1060
void SIOClient::onOpen ()
@@ -1108,15 +1122,13 @@ void SIOClient::disconnect()
1108
1122
{
1109
1123
setConnected (false );
1110
1124
_socket->disconnectFromEndpoint (_path);
1111
-
1112
1125
this ->release ();
1113
1126
}
1114
1127
1115
1128
void SIOClient::socketClosed ()
1116
1129
{
1117
1130
setConnected (false );
1118
1131
_delegate->onClose (this );
1119
-
1120
1132
this ->release ();
1121
1133
}
1122
1134
@@ -1125,7 +1137,7 @@ bool SIOClient::isConnected() const
1125
1137
return _connected && _socket && _socket->_connected ;
1126
1138
}
1127
1139
1128
- void SIOClient::setConnected (bool connected)
1140
+ void SIOClient::setConnected (bool connected)
1129
1141
{
1130
1142
_connected = connected;
1131
1143
}
@@ -1196,8 +1208,8 @@ SIOClient* SocketIO::connect(const std::string& uri, SocketIO::SIODelegate& dele
1196
1208
{
1197
1209
Uri uriObj = Uri::parse (uri);
1198
1210
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 ;
1201
1213
1202
1214
std::string path = uriObj.getPath ();
1203
1215
if (path.empty ())
@@ -1233,7 +1245,7 @@ SIOClient* SocketIO::connect(const std::string& uri, SocketIO::SIODelegate& dele
1233
1245
c->disconnect ();
1234
1246
1235
1247
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);
1237
1249
SIOClient *newC = new (std::nothrow) SIOClient (path, newSocket, delegate);
1238
1250
1239
1251
newSocket->addClient (path, newC);
@@ -1246,14 +1258,16 @@ SIOClient* SocketIO::connect(const std::string& uri, SocketIO::SIODelegate& dele
1246
1258
return c;
1247
1259
}
1248
1260
1249
- SIOClientImpl* SocketIO::getSocket (const std::string& uri)
1261
+ std::shared_ptr< SIOClientImpl> SocketIO::getSocket (const std::string& uri)
1250
1262
{
1251
- return _sockets.at (uri);
1263
+ auto p = _sockets.find (uri);
1264
+ if (p == _sockets.end ()) return nullptr ;
1265
+ return p->second .lock ();
1252
1266
}
1253
1267
1254
- void SocketIO::addSocket (const std::string& uri, SIOClientImpl* socket)
1268
+ void SocketIO::addSocket (const std::string& uri, std::shared_ptr< SIOClientImpl>& socket)
1255
1269
{
1256
- _sockets.insert (uri, socket);
1270
+ _sockets.emplace (uri, socket);
1257
1271
}
1258
1272
1259
1273
void SocketIO::removeSocket (const std::string& uri)
0 commit comments