cocos2d / cocos2d-x

Cocos2d-x is a suite of open-source, cross-platform, game-development tools utilized by millions of developers across the globe. Its core has evolved to serve as the foundation for Cocos Creator 1.x & 2.x.
https://www.cocos.com/en/cocos2d-x
18.21k stars 7.06k forks source link

Lua: webSockets receive information received from other closed sockets #18991

Open zzyAZU opened 6 years ago

zzyAZU commented 6 years ago

WebSocket 当有多个实例的时候出现接收到其他已经关闭的socket的接收的信息的BUG

Steps to Reproduce: 触发时机如下: 1.lua 中创建一个 websocket: self._conn = cc.WebSocket:create(ADDR) 这里c++的 WebSocket 创建了一个对象实例 指针值为 0xabcdefaa(举个例子),这个socket称之为A 2.执行游戏的重启(因为实现原因,每次重新链接一次需要新创建一个socket),先关闭A,再创建一个websocket B(调用A.close 然后再创建一个 weebsocket B 连接游戏),如果在A 真正的关闭回调执行前(WebSocket onConnectionClosed 被调用) 被 lua gc (delete A), 然后创建的 B 的 WebSocket 也为 0xabcdefaa (跟之前 A的指针一致),这时候 B 的socket会收到 A 后续残余收到的部分协议。

导致这个的原因应该是初始化_wsInstance 设置 绑定 userdata 的时候绑定的是 WebSocket 实例的指针,如果 WebSocket 维护一个 uid 保证每次创建的 WebSocket uid 都保证不一样 用 uid 做为 userdata 在 WebSocketCallbackWrapper 根据 uid 找对应的 websocket 这样应该就不会出现这种情况

ps.这样出现几率很小,影响不会太大

drelaptop commented 6 years ago

Please, don't new a issue which describe the problem only using Chinese.

不要提纯中文描述的 issue,这会让英文开发者很难了解问题,很难一起讨论解决。最好是纯英文的,其次是中文,附带英文翻译的

zzyAZU commented 6 years ago

how i fix it: WebSocket.h image WebSocket.cpp image image image image in websocket.cpp WebSocket::onClientOpenConnectionRequest() image

ps:left is my modification, right is the newst in cocos git version the code is well tested, please help me confirm, i am correct

PatriceJiang commented 6 years ago

I am re-implementing WebSocket, WebSocket.cpp will be rewritten, and this bug will be fixed after that.
This PR will focus on improving thread safety and reducing network data process latency. https://github.com/cocos2d/cocos2d-x/compare/v3...PatriceJiang:new_websocket

zzyAZU commented 6 years ago

that sounds pretty good

zzyAZU commented 6 years ago

did you finish rewritten it yet?

PatriceJiang commented 6 years ago

Third party libraries libwebsocket2.4.2 and libuv are now required. I need to compile and test them first.

zzyAZU commented 6 years ago

hello! i find another bug! websockets have very little chances to receive 2 messages in a single message callback, the code using libwebsockets may have mistakes @PatriceJiang

PatriceJiang commented 6 years ago

Have you tried to confirm it?

zzyAZU commented 6 years ago

yes, i use wireshark to follow websocket meta messages compared to cocos websocket received messages, cocos received 2 packet in a single message callback

PatriceJiang commented 6 years ago

According to the websocket protocol, message data can be split into multiple package during transferring. could you pls provide more information about the error.