fix asan reported errors (#20332)

This commit is contained in:
Arnold 2019-11-15 13:39:23 +08:00 committed by minggo
parent 49bca674cc
commit 587bcabf44
4 changed files with 83 additions and 67 deletions

View File

@ -694,7 +694,7 @@ protected:
PolygonInfo _polyInfo; PolygonInfo _polyInfo;
// opacity and RGB protocol // opacity and RGB protocol
bool _opacityModifyRGB; bool _opacityModifyRGB = false;
// image is flipped // image is flipped
bool _flippedX = false; /// Whether the sprite is flipped horizontally or not bool _flippedX = false; /// Whether the sprite is flipped horizontally or not

View File

@ -32,6 +32,7 @@
#include "network/Uri.h" #include "network/Uri.h"
#include <algorithm> #include <algorithm>
#include <sstream> #include <sstream>
#include <memory>
#include <iterator> #include <iterator>
#include "base/ccUTF8.h" #include "base/ccUTF8.h"
#include "base/CCDirector.h" #include "base/CCDirector.h"
@ -79,8 +80,8 @@ public:
std::vector<std::string> getData()const{ return _args; }; std::vector<std::string> getData()const{ return _args; };
virtual std::string stringify()const; virtual std::string stringify()const;
static SocketIOPacket * createPacketWithType(const std::string& type, SocketIOVersion version); static std::shared_ptr<SocketIOPacket> createPacketWithType(const std::string& type, SocketIOVersion version);
static SocketIOPacket * createPacketWithTypeIndex(int type, SocketIOVersion version); static std::shared_ptr<SocketIOPacket> createPacketWithTypeIndex(int type, SocketIOVersion version);
protected: protected:
std::string _pId;//id message std::string _pId;//id message
std::string _ack;// std::string _ack;//
@ -305,37 +306,38 @@ SocketIOPacketV10x::~SocketIOPacketV10x()
_endpoint = ""; _endpoint = "";
} }
SocketIOPacket * SocketIOPacket::createPacketWithType(const std::string& type, SocketIOPacket::SocketIOVersion version) std::shared_ptr<SocketIOPacket> SocketIOPacket::createPacketWithType(const std::string& type, SocketIOPacket::SocketIOVersion version)
{ {
SocketIOPacket *ret; if(version == SocketIOPacket::SocketIOVersion::V09x)
switch (version)
{ {
case SocketIOPacket::SocketIOVersion::V09x: auto ret = std::make_shared<SocketIOPacket>();
ret = new (std::nothrow) SocketIOPacket;
break;
case SocketIOPacket::SocketIOVersion::V10x:
ret = new (std::nothrow) SocketIOPacketV10x;
break;
}
ret->initWithType(type); ret->initWithType(type);
return ret; return ret;
}
else if(version == SocketIOPacket::SocketIOVersion::V10x)
{
auto ret = std::make_shared<SocketIOPacketV10x>();
ret->initWithType(type);
return ret;
}
return nullptr;
} }
std::shared_ptr<SocketIOPacket> SocketIOPacket::createPacketWithTypeIndex(int type, SocketIOPacket::SocketIOVersion version)
SocketIOPacket * SocketIOPacket::createPacketWithTypeIndex(int type, SocketIOPacket::SocketIOVersion version)
{ {
SocketIOPacket *ret; if(version == SocketIOPacket::SocketIOVersion::V09x)
switch (version)
{ {
case SocketIOPacket::SocketIOVersion::V09x: auto ret = std::make_shared<SocketIOPacket>();
ret = new (std::nothrow) SocketIOPacket;
break;
case SocketIOPacket::SocketIOVersion::V10x:
return new (std::nothrow) SocketIOPacketV10x;
break;
}
ret->initWithTypeIndex(type); ret->initWithTypeIndex(type);
return ret; return ret;
}
else if(version == SocketIOPacket::SocketIOVersion::V10x)
{
auto ret = std::make_shared<SocketIOPacketV10x>();
ret->initWithTypeIndex(type);
return ret;
}
return nullptr;
} }
/** /**
@ -343,8 +345,8 @@ SocketIOPacket * SocketIOPacket::createPacketWithTypeIndex(int type, SocketIOPac
* Clients/endpoints may share the same impl to accomplish multiplexing on the same websocket * Clients/endpoints may share the same impl to accomplish multiplexing on the same websocket
*/ */
class SIOClientImpl : class SIOClientImpl :
public cocos2d::Ref, public WebSocket::Delegate,
public WebSocket::Delegate public std::enable_shared_from_this<SIOClientImpl>
{ {
private: private:
int _heartbeat, _timeout; int _heartbeat, _timeout;
@ -362,7 +364,7 @@ public:
SIOClientImpl(const Uri& uri, const std::string& caFilePath); SIOClientImpl(const Uri& uri, const std::string& caFilePath);
virtual ~SIOClientImpl(); virtual ~SIOClientImpl();
static SIOClientImpl* create(const Uri& uri, const std::string& caFilePath); static std::shared_ptr<SIOClientImpl> create(const Uri& uri, const std::string& caFilePath);
virtual void onOpen(WebSocket* ws); virtual void onOpen(WebSocket* ws);
virtual void onMessage(WebSocket* ws, const WebSocket::Data& data); virtual void onMessage(WebSocket* ws, const WebSocket::Data& data);
@ -385,7 +387,7 @@ public:
void send(const std::string& endpoint, const std::string& s); void send(const std::string& endpoint, const std::string& s);
void send(const std::string& endpoint, const std::vector<std::string>& s); void send(const std::string& endpoint, const std::vector<std::string>& s);
void send(SocketIOPacket *packet); void send(std::shared_ptr<SocketIOPacket>& packet);
void emit(const std::string& endpoint, const std::string& eventname, const std::string& args); void emit(const std::string& endpoint, const std::string& eventname, const std::string& args);
void emit(const std::string& endpoint, const std::string& eventname, const std::vector<std::string>& args); void emit(const std::string& endpoint, const std::string& eventname, const std::vector<std::string>& args);
@ -429,7 +431,14 @@ void SIOClientImpl::handshake()
request->setUrl(pre.str()); request->setUrl(pre.str());
request->setRequestType(HttpRequest::Type::GET); request->setRequestType(HttpRequest::Type::GET);
request->setResponseCallback(CC_CALLBACK_2(SIOClientImpl::handshakeResponse, this)); std::weak_ptr<SIOClientImpl> self = shared_from_this();
auto callback = [self](HttpClient* client, HttpResponse *resp) {
auto conn = self.lock();
if (conn) {
conn->handshakeResponse(client, resp);
}
};
request->setResponseCallback(callback);
request->setTag("handshake"); request->setTag("handshake");
CCLOGINFO("SIOClientImpl::handshake() waiting"); CCLOGINFO("SIOClientImpl::handshake() waiting");
@ -633,13 +642,13 @@ void SIOClientImpl::disconnect()
_ws->close(); _ws->close();
} }
SIOClientImpl* SIOClientImpl::create(const Uri& uri, const std::string& caFilePath) std::shared_ptr<SIOClientImpl> SIOClientImpl::create(const Uri& uri, const std::string& caFilePath)
{ {
SIOClientImpl *s = new (std::nothrow) SIOClientImpl(uri, caFilePath); SIOClientImpl *s = new (std::nothrow) SIOClientImpl(uri, caFilePath);
if (s && s->init()) if (s && s->init())
{ {
return s; return std::shared_ptr<SIOClientImpl>(s);
} }
return nullptr; return nullptr;
@ -657,7 +666,7 @@ void SIOClientImpl::addClient(const std::string& endpoint, SIOClient* client)
void SIOClientImpl::connectToEndpoint(const std::string& endpoint) void SIOClientImpl::connectToEndpoint(const std::string& endpoint)
{ {
SocketIOPacket *packet = SocketIOPacket::createPacketWithType("connect", _version); auto packet = SocketIOPacket::createPacketWithType("connect", _version);
packet->setEndpoint(endpoint); packet->setEndpoint(endpoint);
this->send(packet); this->send(packet);
} }
@ -685,7 +694,7 @@ void SIOClientImpl::disconnectFromEndpoint(const std::string& endpoint)
void SIOClientImpl::heartbeat(float /*dt*/) void SIOClientImpl::heartbeat(float /*dt*/)
{ {
SocketIOPacket *packet = SocketIOPacket::createPacketWithType("heartbeat", _version); auto packet = SocketIOPacket::createPacketWithType("heartbeat", _version);
this->send(packet); this->send(packet);
@ -698,7 +707,7 @@ void SIOClientImpl::send(const std::string& endpoint, const std::vector<std::str
switch (_version) { switch (_version) {
case SocketIOPacket::SocketIOVersion::V09x: case SocketIOPacket::SocketIOVersion::V09x:
{ {
SocketIOPacket *packet = SocketIOPacket::createPacketWithType("message", _version); auto packet = SocketIOPacket::createPacketWithType("message", _version);
packet->setEndpoint(endpoint); packet->setEndpoint(endpoint);
for(auto &i : s) for(auto &i : s)
{ {
@ -721,7 +730,7 @@ void SIOClientImpl::send(const std::string& endpoint, const std::string& s)
send(endpoint, t); send(endpoint, t);
} }
void SIOClientImpl::send(SocketIOPacket *packet) void SIOClientImpl::send(std::shared_ptr<SocketIOPacket>& packet)
{ {
std::string req = packet->toString(); std::string req = packet->toString();
if (_connected) if (_connected)
@ -736,7 +745,7 @@ void SIOClientImpl::send(SocketIOPacket *packet)
void SIOClientImpl::emit(const std::string& endpoint, const std::string& eventname, const std::string& args) void SIOClientImpl::emit(const std::string& endpoint, const std::string& eventname, const std::string& args)
{ {
CCLOGINFO("Emitting event \"%s\"", eventname.c_str()); CCLOGINFO("Emitting event \"%s\"", eventname.c_str());
SocketIOPacket *packet = SocketIOPacket::createPacketWithType("event", _version); auto packet = SocketIOPacket::createPacketWithType("event", _version);
packet->setEndpoint(endpoint == "/" ? "" : endpoint); packet->setEndpoint(endpoint == "/" ? "" : endpoint);
packet->setEvent(eventname); packet->setEvent(eventname);
packet->addData(args); packet->addData(args);
@ -746,7 +755,7 @@ void SIOClientImpl::emit(const std::string& endpoint, const std::string& eventna
void SIOClientImpl::emit(const std::string& endpoint, const std::string& eventname, const std::vector<std::string>& args) void SIOClientImpl::emit(const std::string& endpoint, const std::string& eventname, const std::vector<std::string>& args)
{ {
CCLOGINFO("Emitting event \"%s\"", eventname.c_str()); CCLOGINFO("Emitting event \"%s\"", eventname.c_str());
SocketIOPacket *packet = SocketIOPacket::createPacketWithType("event", _version); auto packet = SocketIOPacket::createPacketWithType("event", _version);
packet->setEndpoint(endpoint == "/" ? "" : endpoint); packet->setEndpoint(endpoint == "/" ? "" : endpoint);
packet->setEvent(eventname); packet->setEvent(eventname);
for (auto &arg : args) { for (auto &arg : args) {
@ -759,7 +768,9 @@ void SIOClientImpl::onOpen(WebSocket* /*ws*/)
{ {
_connected = true; _connected = true;
SocketIO::getInstance()->addSocket(_uri.getAuthority(), this); auto self = shared_from_this();
SocketIO::getInstance()->addSocket(_uri.getAuthority(), self);
if (_version == SocketIOPacket::SocketIOVersion::V10x) if (_version == SocketIOPacket::SocketIOVersion::V10x)
{ {
@ -767,14 +778,20 @@ void SIOClientImpl::onOpen(WebSocket* /*ws*/)
_ws->send(s.data()); _ws->send(s.data());
} }
Director::getInstance()->getScheduler()->schedule(CC_SCHEDULE_SELECTOR(SIOClientImpl::heartbeat), this, (_heartbeat * .9f), false); std::weak_ptr<SIOClientImpl> selfWeak = shared_from_this();
auto f = [selfWeak](float dt) {
auto conn = selfWeak.lock();
if(conn)
conn->heartbeat(dt);
};
Director::getInstance()->getScheduler()->schedule(f, this, (_heartbeat * .9f), false, "heart_beat");
for (auto& client : _clients) for (auto& client : _clients)
{ {
client.second->onOpen(); client.second->onOpen();
} }
CCLOGINFO("SIOClientImpl::onOpen socket connected!");
} }
void SIOClientImpl::onMessage(WebSocket* /*ws*/, const WebSocket::Data& data) void SIOClientImpl::onMessage(WebSocket* /*ws*/, const WebSocket::Data& data)
@ -1023,8 +1040,6 @@ void SIOClientImpl::onClose(WebSocket* /*ws*/)
SocketIO::getInstance()->removeSocket(_uri.getAuthority()); SocketIO::getInstance()->removeSocket(_uri.getAuthority());
} }
this->release();
} }
void SIOClientImpl::onError(WebSocket* /*ws*/, const WebSocket::ErrorCode& error) void SIOClientImpl::onError(WebSocket* /*ws*/, const WebSocket::ErrorCode& error)
@ -1033,7 +1048,7 @@ void SIOClientImpl::onError(WebSocket* /*ws*/, const WebSocket::ErrorCode& error
} }
//begin SIOClient methods //begin SIOClient methods
SIOClient::SIOClient(const std::string& path, SIOClientImpl* impl, SocketIO::SIODelegate& delegate) SIOClient::SIOClient(const std::string& path, std::shared_ptr<SIOClientImpl>& impl, SocketIO::SIODelegate& delegate)
: _path(path) : _path(path)
, _connected(false) , _connected(false)
, _socket(impl) , _socket(impl)
@ -1115,7 +1130,6 @@ void SIOClient::disconnect()
{ {
setConnected(false); setConnected(false);
_socket->disconnectFromEndpoint(_path); _socket->disconnectFromEndpoint(_path);
this->release(); this->release();
} }
@ -1123,7 +1137,6 @@ void SIOClient::socketClosed()
{ {
setConnected(false); setConnected(false);
_delegate->onClose(this); _delegate->onClose(this);
this->release(); this->release();
} }
@ -1198,8 +1211,8 @@ SIOClient* SocketIO::connect(const std::string& uri, SocketIO::SIODelegate& dele
{ {
Uri uriObj = Uri::parse(uri); Uri uriObj = Uri::parse(uri);
SIOClientImpl *socket = SocketIO::getInstance()->getSocket(uriObj.getAuthority()); std::shared_ptr<SIOClientImpl> socket = SocketIO::getInstance()->getSocket(uriObj.getAuthority());
SIOClient *c = nullptr; SIOClient * c = nullptr;
std::string path = uriObj.getPath(); std::string path = uriObj.getPath();
if (path == "") if (path == "")
@ -1235,7 +1248,7 @@ SIOClient* SocketIO::connect(const std::string& uri, SocketIO::SIODelegate& dele
c->disconnect(); c->disconnect();
CCLOG("SocketIO: recreate a new socket, new client, connect"); CCLOG("SocketIO: recreate a new socket, new client, connect");
SIOClientImpl* newSocket = SIOClientImpl::create(uriObj, caFilePath); std::shared_ptr<SIOClientImpl> newSocket = SIOClientImpl::create(uriObj, caFilePath);
SIOClient *newC = new (std::nothrow) SIOClient(path, newSocket, delegate); SIOClient *newC = new (std::nothrow) SIOClient(path, newSocket, delegate);
newSocket->addClient(path, newC); newSocket->addClient(path, newC);
@ -1248,14 +1261,16 @@ SIOClient* SocketIO::connect(const std::string& uri, SocketIO::SIODelegate& dele
return c; return c;
} }
SIOClientImpl* SocketIO::getSocket(const std::string& uri) std::shared_ptr<SIOClientImpl> SocketIO::getSocket(const std::string& uri)
{ {
return _sockets.at(uri); auto p = _sockets.find(uri);
if(p == _sockets.end()) return nullptr;
return p->second.lock();
} }
void SocketIO::addSocket(const std::string& uri, SIOClientImpl* socket) void SocketIO::addSocket(const std::string& uri, std::shared_ptr<SIOClientImpl>& socket)
{ {
_sockets.insert(uri, socket); _sockets.emplace(uri, socket);
} }
void SocketIO::removeSocket(const std::string& uri) void SocketIO::removeSocket(const std::string& uri)

View File

@ -173,10 +173,11 @@ private:
static SocketIO *_inst; static SocketIO *_inst;
cocos2d::Map<std::string, SIOClientImpl*> _sockets; std::unordered_map<std::string, std::weak_ptr<SIOClientImpl
>> _sockets;
SIOClientImpl* getSocket(const std::string& uri); std::shared_ptr<SIOClientImpl> getSocket(const std::string& uri);
void addSocket(const std::string& uri, SIOClientImpl* socket); void addSocket(const std::string& uri, std::shared_ptr<SIOClientImpl>& socket);
void removeSocket(const std::string& uri); void removeSocket(const std::string& uri);
friend class SIOClientImpl; friend class SIOClientImpl;
@ -202,9 +203,9 @@ private:
std::string _path, _tag; std::string _path, _tag;
bool _connected; bool _connected;
SIOClientImpl* _socket; std::shared_ptr<SIOClientImpl> _socket;
SocketIO::SIODelegate* _delegate; SocketIO::SIODelegate* _delegate = nullptr;
EventRegistry _eventRegistry; EventRegistry _eventRegistry;
@ -228,7 +229,7 @@ private:
* @param impl the SIOClientImpl object. * @param impl the SIOClientImpl object.
* @param delegate the SIODelegate object. * @param delegate the SIODelegate object.
*/ */
SIOClient(const std::string& path, SIOClientImpl* impl, SocketIO::SIODelegate& delegate); SIOClient(const std::string& path, std::shared_ptr<SIOClientImpl>& impl, SocketIO::SIODelegate& delegate);
/** /**
* Destructor of SIOClient class. * Destructor of SIOClient class.
*/ */

View File

@ -467,11 +467,11 @@ public:
return false; return false;
} }
_data.reserve(LWS_PRE + len); _data.resize(LWS_PRE + len);
_data.resize(LWS_PRE, 0x00);
if (len > 0) if (len > 0)
{ {
_data.insert(_data.end(), buf, buf + len); std::copy(buf, buf + len, _data.begin() + LWS_PRE);
} }
_payload = _data.data() + LWS_PRE; _payload = _data.data() + LWS_PRE;