jsb_socketio.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /****************************************************************************
  2. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  3. http://www.cocos.com
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated engine source code (the "Software"), a limited,
  6. worldwide, royalty-free, non-assignable, revocable and non-exclusive license
  7. to use Cocos Creator solely to develop games on your target platforms. You shall
  8. not use Cocos Creator software for developing other software or tools that's
  9. used for developing games. You are not granted to publish, distribute,
  10. sublicense, and/or sell copies of Cocos Creator.
  11. The software or tools in this License Agreement are licensed, not sold.
  12. Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "jsb_socketio.hpp"
  22. #include "cocos/scripting/js-bindings/jswrapper/SeApi.h"
  23. #include "cocos/scripting/js-bindings/manual/jsb_conversions.hpp"
  24. #include "cocos/scripting/js-bindings/manual/jsb_global.h"
  25. #include "cocos/network/SocketIO.h"
  26. #include "base/ccUTF8.h"
  27. #include "platform/CCApplication.h"
  28. using namespace cocos2d;
  29. using namespace cocos2d::network;
  30. se::Class* __jsb_SocketIO_class = nullptr;
  31. class JSB_SocketIODelegate : public Ref, public SocketIO::SIODelegate
  32. {
  33. public:
  34. //c++11 map to callbacks
  35. typedef std::unordered_map<std::string/* eventName */, se::ValueArray/* 0:callbackFunc, 1:target */> JSB_SIOCallbackRegistry;
  36. JSB_SocketIODelegate()
  37. {
  38. }
  39. virtual ~JSB_SocketIODelegate()
  40. {
  41. CCLOGINFO("In the destructor of JSB_SocketIODelegate(%p)", this);
  42. }
  43. virtual void onConnect(SIOClient* client) override
  44. {
  45. }
  46. virtual void onMessage(SIOClient* client, const std::string& data) override
  47. {
  48. }
  49. virtual void onClose(SIOClient* client) override
  50. {
  51. CCLOG("JSB SocketIO::SIODelegate->onClose method called from native");
  52. this->fireEventToScript(client, "disconnect", "");
  53. auto iter = se::NativePtrToObjectMap::find(client);
  54. if (iter != se::NativePtrToObjectMap::end())
  55. {
  56. iter->second->unroot();
  57. }
  58. if (getReferenceCount() == 1)
  59. {
  60. autorelease();
  61. }
  62. else
  63. {
  64. release();
  65. }
  66. }
  67. virtual void onError(SIOClient* client, const std::string& data) override
  68. {
  69. CCLOG("JSB SocketIO::SIODelegate->onError method called from native with data: %s", data.c_str());
  70. this->fireEventToScript(client, "error", data);
  71. auto iter = se::NativePtrToObjectMap::find(client);
  72. if (iter != se::NativePtrToObjectMap::end())
  73. {
  74. iter->second->unroot();
  75. }
  76. }
  77. virtual void fireEventToScript(SIOClient* client, const std::string& eventName, const std::string& data) override
  78. {
  79. CCLOG("JSB SocketIO::SIODelegate->fireEventToScript method called from native with name '%s' data: %s", eventName.c_str(), data.c_str());
  80. se::ScriptEngine::getInstance()->clearException();
  81. se::AutoHandleScope hs;
  82. if (cocos2d::Application::getInstance() == nullptr)
  83. return;
  84. auto iter = se::NativePtrToObjectMap::find(client); //IDEA: client probably be a new value with the same address as the old one, it may cause undefined result.
  85. if (iter == se::NativePtrToObjectMap::end())
  86. return;
  87. se::Value dataVal;
  88. if (data.empty())
  89. {
  90. dataVal.setNull();
  91. }
  92. else
  93. {
  94. dataVal.setString(data);
  95. }
  96. JSB_SIOCallbackRegistry::iterator it = _eventRegistry.find(eventName);
  97. if (it != _eventRegistry.end())
  98. {
  99. const se::ValueArray& cbStruct = it->second;
  100. assert(cbStruct.size() == 2);
  101. const se::Value& callback = cbStruct[0];
  102. const se::Value& target = cbStruct[1];
  103. if (callback.isObject() && callback.toObject()->isFunction() && target.isObject())
  104. {
  105. se::ValueArray args;
  106. args.push_back(dataVal);
  107. callback.toObject()->call(args, target.toObject());
  108. }
  109. }
  110. if (eventName == "disconnect")
  111. {
  112. cocos2d::log("disconnect ... "); //IDEA:
  113. }
  114. }
  115. void addEvent(const std::string& eventName, const se::Value& callback, const se::Value& target)
  116. {
  117. assert(callback.isObject() && callback.toObject()->isFunction());
  118. assert(target.isObject());
  119. _eventRegistry[eventName].clear();
  120. _eventRegistry[eventName].push_back(callback);
  121. _eventRegistry[eventName].push_back(target);
  122. target.toObject()->attachObject(callback.toObject());
  123. }
  124. private:
  125. JSB_SIOCallbackRegistry _eventRegistry;
  126. };
  127. static bool SocketIO_finalize(se::State& s)
  128. {
  129. SIOClient* cobj = (SIOClient*)s.nativeThisObject();
  130. CCLOGINFO("jsbindings: finalizing JS object %p (SocketIO)", cobj);
  131. cobj->disconnect();
  132. JSB_SocketIODelegate* delegate = static_cast<JSB_SocketIODelegate*>(cobj->getDelegate());
  133. if (delegate->getReferenceCount() == 1)
  134. {
  135. delegate->autorelease();
  136. }
  137. else
  138. {
  139. delegate->release();
  140. }
  141. cobj->release();
  142. return true;
  143. }
  144. SE_BIND_FINALIZE_FUNC(SocketIO_finalize)
  145. static bool SocketIO_prop_getTag(se::State& s)
  146. {
  147. SIOClient* cobj = (SIOClient*)s.nativeThisObject();
  148. s.rval().setString(cobj->getTag());
  149. return true;
  150. }
  151. SE_BIND_PROP_GET(SocketIO_prop_getTag)
  152. static bool SocketIO_prop_setTag(se::State& s)
  153. {
  154. SIOClient* cobj = (SIOClient*)s.nativeThisObject();
  155. cobj->setTag(s.args()[0].toString().c_str());
  156. return true;
  157. }
  158. SE_BIND_PROP_SET(SocketIO_prop_setTag)
  159. static bool SocketIO_send(se::State& s)
  160. {
  161. const auto& args = s.args();
  162. int argc = (int)args.size();
  163. SIOClient* cobj = (SIOClient*)s.nativeThisObject();
  164. if (argc == 1)
  165. {
  166. std::string payload;
  167. bool ok = seval_to_std_string(args[0], &payload);
  168. SE_PRECONDITION2(ok, false, "Converting payload failed!");
  169. cobj->send(payload);
  170. return true;
  171. }
  172. SE_REPORT_ERROR("Wrong number of arguments: %d, expected: %d", argc, 1);
  173. return false;
  174. }
  175. SE_BIND_FUNC(SocketIO_send)
  176. static bool SocketIO_emit(se::State& s)
  177. {
  178. const auto& args = s.args();
  179. int argc = (int)args.size();
  180. SIOClient* cobj = (SIOClient*)s.nativeThisObject();
  181. if (argc >= 1)
  182. {
  183. bool ok = false;
  184. std::string eventName;
  185. ok = seval_to_std_string(args[0], &eventName);
  186. SE_PRECONDITION2(ok, false, "Converting eventName failed!");
  187. std::string payload;
  188. if (argc >= 2)
  189. {
  190. const auto& arg1 = args[1];
  191. // Add this check to make it compatible with old version.
  192. // jsval_to_std_string in v1.6 returns empty string if arg1 is null or undefined
  193. // while seval_to_std_string since 1.7.2 follows JS standard to return "null" or "undefined".
  194. // Therefore, we need a workaround to make it be compatible with versions lower than v1.7.
  195. if (!arg1.isNullOrUndefined())
  196. {
  197. ok = seval_to_std_string(arg1, &payload);
  198. SE_PRECONDITION2(ok, false, "Converting payload failed!");
  199. }
  200. }
  201. cobj->emit(eventName, payload);
  202. return true;
  203. }
  204. SE_REPORT_ERROR("Wrong number of arguments: %d, expected: %d", argc, 2);
  205. return false;
  206. }
  207. SE_BIND_FUNC(SocketIO_emit)
  208. static bool SocketIO_disconnect(se::State& s)
  209. {
  210. const auto& args = s.args();
  211. int argc = (int)args.size();
  212. SIOClient* cobj = (SIOClient*)s.nativeThisObject();
  213. if (argc == 0)
  214. {
  215. cobj->disconnect();
  216. return true;
  217. }
  218. SE_REPORT_ERROR("Wrong number of arguments: %d, expected: %d", argc, 0);
  219. return false;
  220. }
  221. SE_BIND_FUNC(SocketIO_disconnect)
  222. static bool SocketIO_on(se::State& s)
  223. {
  224. const auto& args = s.args();
  225. int argc = (int)args.size();
  226. SIOClient* cobj = (SIOClient*)s.nativeThisObject();
  227. if (argc == 2)
  228. {
  229. bool ok = false;
  230. std::string eventName;
  231. ok = seval_to_std_string(args[0], &eventName);
  232. SE_PRECONDITION2(ok, false, "Converting eventName failed!");
  233. CCLOG("JSB SocketIO eventName to: '%s'", eventName.c_str());
  234. ((JSB_SocketIODelegate *)cobj->getDelegate())->addEvent(eventName, args[1], se::Value(s.thisObject()));
  235. return true;
  236. }
  237. SE_REPORT_ERROR("Wrong number of arguments: %d, expected: %d", argc, 2);
  238. return false;
  239. }
  240. SE_BIND_FUNC(SocketIO_on)
  241. // static
  242. static bool SocketIO_connect(se::State& s)
  243. {
  244. const auto& args = s.args();
  245. int argc = (int)args.size();
  246. CCLOG("JSB SocketIO.connect method called");
  247. if (argc >= 1 && argc <= 3)
  248. {
  249. std::string url;
  250. std::string caFilePath;
  251. bool ok = false;
  252. ok = seval_to_std_string(args[0], &url);
  253. SE_PRECONDITION2(ok, false, "Error processing arguments");
  254. if (argc == 2)
  255. {
  256. if (args[1].isObject())
  257. {
  258. // Just ignore the option argument
  259. }
  260. else if (args[1].isString())
  261. {
  262. // Assume it's CA root file path
  263. ok = seval_to_std_string(args[1], &caFilePath);
  264. SE_PRECONDITION2( ok, false, "Error processing arguments");
  265. }
  266. }
  267. if (argc == 3)
  268. {
  269. // Just ignore the option argument
  270. if (args[2].isString())
  271. {
  272. // Assume it's CA root file path
  273. ok = seval_to_std_string(args[2], &caFilePath);
  274. SE_PRECONDITION2( ok, false, "Error processing arguments");
  275. }
  276. }
  277. JSB_SocketIODelegate* siodelegate = new (std::nothrow) JSB_SocketIODelegate();
  278. CCLOG("Calling native SocketIO.connect method");
  279. SIOClient* ret = SocketIO::connect(url, *siodelegate, caFilePath);
  280. if (ret != nullptr)
  281. {
  282. ret->retain();
  283. siodelegate->retain();
  284. se::Object* obj = se::Object::createObjectWithClass(__jsb_SocketIO_class);
  285. obj->setPrivateData(ret);
  286. s.rval().setObject(obj);
  287. obj->root();
  288. return true;
  289. }
  290. else
  291. {
  292. siodelegate->release();
  293. SE_REPORT_ERROR("SocketIO.connect return nullptr!");
  294. return false;
  295. }
  296. }
  297. SE_REPORT_ERROR("JSB SocketIO.connect: Wrong number of arguments");
  298. return false;
  299. }
  300. SE_BIND_FUNC(SocketIO_connect)
  301. // static
  302. static bool SocketIO_close(se::State& s)
  303. {
  304. const auto& args = s.args();
  305. int argc = (int)args.size();
  306. if (argc == 0)
  307. {
  308. return true;
  309. }
  310. SE_REPORT_ERROR("Wrong number of arguments: %d, expected: %d", argc, 0);
  311. return false;
  312. }
  313. SE_BIND_FUNC(SocketIO_close)
  314. bool register_all_socketio(se::Object* obj)
  315. {
  316. se::Class* cls = se::Class::create("SocketIO", obj, nullptr, nullptr);
  317. cls->defineFinalizeFunction(_SE(SocketIO_finalize));
  318. cls->defineProperty("tag", _SE(SocketIO_prop_getTag), _SE(SocketIO_prop_setTag));
  319. cls->defineFunction("send", _SE(SocketIO_send));
  320. cls->defineFunction("emit", _SE(SocketIO_emit));
  321. cls->defineFunction("disconnect", _SE(SocketIO_disconnect));
  322. cls->defineFunction("on", _SE(SocketIO_on));
  323. cls->install();
  324. JSBClassType::registerClass<SocketIO>(cls);
  325. se::Value ctorVal;
  326. obj->getProperty("SocketIO", &ctorVal);
  327. ctorVal.toObject()->defineFunction("connect", _SE(SocketIO_connect));
  328. ctorVal.toObject()->defineFunction("close", _SE(SocketIO_close));
  329. __jsb_SocketIO_class = cls;
  330. se::ScriptEngine::getInstance()->clearException();
  331. return true;
  332. }