1
0

CCConsole.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  1. /****************************************************************************
  2. Copyright (c) 2013-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "base/CCConsole.h"
  21. #include <thread>
  22. #include <algorithm>
  23. #include <functional>
  24. #include <cctype>
  25. #include <locale>
  26. #include <sstream>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <time.h>
  30. #include <fcntl.h>
  31. #if defined(_MSC_VER) || defined(__MINGW32__)
  32. #include <io.h>
  33. #include <WS2tcpip.h>
  34. #include <Winsock2.h>
  35. #if defined(__MINGW32__)
  36. #include "platform/win32/inet_pton_mingw.h"
  37. #endif
  38. #define bzero(a, b) memset(a, 0, b);
  39. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  40. #include "platform/winrt/inet_ntop_winrt.h"
  41. #include "platform/winrt/inet_pton_winrt.h"
  42. #include "platform/winrt/CCWinRTUtils.h"
  43. #endif
  44. #else
  45. #include <netdb.h>
  46. #include <unistd.h>
  47. #include <arpa/inet.h>
  48. #include <netinet/in.h>
  49. #include <sys/socket.h>
  50. #include <sys/un.h>
  51. #include <sys/ioctl.h>
  52. #endif
  53. #include "base/CCDirector.h"
  54. #include "base/CCScheduler.h"
  55. #include "platform/CCPlatformConfig.h"
  56. #include "base/CCConfiguration.h"
  57. #include "2d/CCScene.h"
  58. #include "platform/CCFileUtils.h"
  59. #include "renderer/CCTextureCache.h"
  60. #include "base/base64.h"
  61. #include "base/ccUtils.h"
  62. #include "base/allocator/CCAllocatorDiagnostics.h"
  63. NS_CC_BEGIN
  64. extern const char* cocos2dVersion(void);
  65. #define PROMPT "> "
  66. static const size_t SEND_BUFSIZ = 512;
  67. /** private functions */
  68. namespace {
  69. #if defined(__MINGW32__)
  70. // inet
  71. const char* inet_ntop(int af, const void* src, char* dst, int cnt)
  72. {
  73. struct sockaddr_in srcaddr;
  74. memset(&srcaddr, 0, sizeof(struct sockaddr_in));
  75. memcpy(&(srcaddr.sin_addr), src, sizeof(srcaddr.sin_addr));
  76. srcaddr.sin_family = af;
  77. if (WSAAddressToStringA((struct sockaddr*) &srcaddr, sizeof(struct sockaddr_in), 0, dst, (LPDWORD) &cnt) != 0)
  78. {
  79. return nullptr;
  80. }
  81. return dst;
  82. }
  83. #endif
  84. //
  85. // Free functions to log
  86. //
  87. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
  88. void SendLogToWindow(const char *log)
  89. {
  90. static const int CCLOG_STRING_TAG = 1;
  91. // Send data as a message
  92. COPYDATASTRUCT myCDS;
  93. myCDS.dwData = CCLOG_STRING_TAG;
  94. myCDS.cbData = (DWORD)strlen(log) + 1;
  95. myCDS.lpData = (PVOID)log;
  96. if (Director::getInstance()->getOpenGLView())
  97. {
  98. HWND hwnd = Director::getInstance()->getOpenGLView()->getWin32Window();
  99. SendMessage(hwnd,
  100. WM_COPYDATA,
  101. (WPARAM)(HWND)hwnd,
  102. (LPARAM)(LPVOID)&myCDS);
  103. }
  104. }
  105. #elif CC_TARGET_PLATFORM == CC_PLATFORM_WINRT
  106. void SendLogToWindow(const char *log)
  107. {
  108. }
  109. #endif
  110. void _log(const char *format, va_list args)
  111. {
  112. int bufferSize = MAX_LOG_LENGTH;
  113. char* buf = nullptr;
  114. do
  115. {
  116. buf = new (std::nothrow) char[bufferSize];
  117. if (buf == nullptr)
  118. return; // not enough memory
  119. int ret = vsnprintf(buf, bufferSize - 3, format, args);
  120. if (ret < 0)
  121. {
  122. bufferSize *= 2;
  123. delete [] buf;
  124. }
  125. else
  126. break;
  127. } while (true);
  128. strcat(buf, "\n");
  129. #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
  130. __android_log_print(ANDROID_LOG_DEBUG, "cocos2d-x debug info", "%s", buf);
  131. #elif CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT
  132. int pos = 0;
  133. int len = strlen(buf);
  134. char tempBuf[MAX_LOG_LENGTH + 1] = { 0 };
  135. WCHAR wszBuf[MAX_LOG_LENGTH + 1] = { 0 };
  136. do
  137. {
  138. std::copy(buf + pos, buf + pos + MAX_LOG_LENGTH, tempBuf);
  139. tempBuf[MAX_LOG_LENGTH] = 0;
  140. MultiByteToWideChar(CP_UTF8, 0, tempBuf, -1, wszBuf, sizeof(wszBuf));
  141. OutputDebugStringW(wszBuf);
  142. WideCharToMultiByte(CP_ACP, 0, wszBuf, -1, tempBuf, sizeof(tempBuf), nullptr, FALSE);
  143. printf("%s", tempBuf);
  144. pos += MAX_LOG_LENGTH;
  145. } while (pos < len);
  146. SendLogToWindow(buf);
  147. fflush(stdout);
  148. #else
  149. // Linux, Mac, iOS, etc
  150. fprintf(stdout, "%s", buf);
  151. fflush(stdout);
  152. #endif
  153. Director::getInstance()->getConsole()->log(buf);
  154. delete [] buf;
  155. }
  156. }
  157. // FIXME: Deprecated
  158. void CCLog(const char * format, ...)
  159. {
  160. va_list args;
  161. va_start(args, format);
  162. _log(format, args);
  163. va_end(args);
  164. }
  165. void log(const char * format, ...)
  166. {
  167. va_list args;
  168. va_start(args, format);
  169. _log(format, args);
  170. va_end(args);
  171. }
  172. //
  173. // Utility code
  174. //
  175. std::string Console::Utility::_prompt(PROMPT);
  176. //TODO: these general utils should be in a separate class
  177. //
  178. // Trimming functions were taken from: http://stackoverflow.com/a/217605
  179. //
  180. // trim from start
  181. std::string& Console::Utility::ltrim(std::string& s) {
  182. s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
  183. return s;
  184. }
  185. // trim from end
  186. std::string& Console::Utility::rtrim(std::string& s) {
  187. s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
  188. return s;
  189. }
  190. // trim from both ends
  191. std::string& Console::Utility::trim(std::string& s) {
  192. return Console::Utility::ltrim(Console::Utility::rtrim(s));
  193. }
  194. std::vector<std::string>& Console::Utility::split(const std::string& s, char delim, std::vector<std::string>& elems) {
  195. std::stringstream ss(s);
  196. std::string item;
  197. while (std::getline(ss, item, delim)) {
  198. elems.push_back(item);
  199. }
  200. return elems;
  201. }
  202. std::vector<std::string> Console::Utility::split(const std::string& s, char delim) {
  203. std::vector<std::string> elems;
  204. Console::Utility::split(s, delim, elems);
  205. return elems;
  206. }
  207. //isFloat taken from http://stackoverflow.com/questions/447206/c-isfloat-function
  208. bool Console::Utility::isFloat(const std::string& myString) {
  209. std::istringstream iss(myString);
  210. float f;
  211. iss >> std::noskipws >> f; // noskipws considers leading whitespace invalid
  212. // Check the entire string was consumed and if either failbit or badbit is set
  213. return iss.eof() && !iss.fail();
  214. }
  215. ssize_t Console::Utility::sendToConsole(int fd, const void* buffer, size_t length, int flags)
  216. {
  217. if (_prompt.length() == length) {
  218. if (strncmp(_prompt.c_str(), static_cast<const char*>(buffer), length) == 0) {
  219. fprintf(stderr,"bad parameter error: a buffer is the prompt string.\n");
  220. return 0;
  221. }
  222. }
  223. const char* buf = static_cast<const char*>(buffer);
  224. ssize_t retLen = 0;
  225. for (size_t i = 0; i < length; ) {
  226. size_t len = length - i;
  227. if (SEND_BUFSIZ < len) len = SEND_BUFSIZ;
  228. retLen += send(fd, buf + i, len, flags);
  229. i += len;
  230. }
  231. return retLen;
  232. }
  233. // dprintf() is not defined in Android
  234. // so we add our own 'dpritnf'
  235. ssize_t Console::Utility::mydprintf(int sock, const char *format, ...)
  236. {
  237. va_list args;
  238. char buf[16386];
  239. va_start(args, format);
  240. vsnprintf(buf, sizeof(buf), format, args);
  241. va_end(args);
  242. return sendToConsole(sock, buf, strlen(buf));
  243. }
  244. void Console::Utility::sendPrompt(int fd)
  245. {
  246. const char* prompt = _prompt.c_str();
  247. send(fd, prompt, strlen(prompt), 0);
  248. }
  249. void Console::Utility::setPrompt(const std::string &prompt)
  250. {
  251. _prompt = prompt;
  252. }
  253. const std::string& Console::Utility::getPrompt()
  254. {
  255. return _prompt;
  256. }
  257. //
  258. // Command code
  259. //
  260. void Console::Command::addCallback(const Callback& callback_)
  261. {
  262. callback = callback_;
  263. }
  264. void Console::Command::addSubCommand(const Command& subCmd)
  265. {
  266. subCommands[subCmd.name] = subCmd;
  267. }
  268. const Console::Command* Console::Command::getSubCommand(const std::string& subCmdName) const
  269. {
  270. auto it = subCommands.find(subCmdName);
  271. if(it != subCommands.end()) {
  272. auto& subCmd = it->second;
  273. return &subCmd;
  274. }
  275. return nullptr;
  276. }
  277. void Console::Command::delSubCommand(const std::string& subCmdName)
  278. {
  279. auto it = subCommands.find(subCmdName);
  280. if(it != subCommands.end()) {
  281. subCommands.erase(it);
  282. }
  283. }
  284. void Console::Command::commandHelp(int fd, const std::string& /*args*/)
  285. {
  286. if (! help.empty()) {
  287. Console::Utility::mydprintf(fd, "%s\n", help.c_str());
  288. }
  289. if (! subCommands.empty()) {
  290. sendHelp(fd, subCommands, "");
  291. }
  292. }
  293. void Console::Command::commandGeneric(int fd, const std::string& args)
  294. {
  295. // The first argument (including the empty)
  296. std::string key(args);
  297. auto pos = args.find(" ");
  298. if ((pos != std::string::npos) && (0 < pos)) {
  299. key = args.substr(0, pos);
  300. }
  301. // help
  302. if (key == "help" || key == "-h") {
  303. commandHelp(fd, args);
  304. return;
  305. }
  306. // find sub command
  307. auto it = subCommands.find(key);
  308. if (it != subCommands.end()) {
  309. auto subCmd = it->second;
  310. if (subCmd.callback) {
  311. subCmd.callback(fd, args);
  312. }
  313. return;
  314. }
  315. // can not find
  316. if (callback) {
  317. callback(fd, args);
  318. }
  319. }
  320. //
  321. // Console code
  322. //
  323. Console::Console()
  324. : _listenfd(-1)
  325. , _running(false)
  326. , _endThread(false)
  327. , _isIpv6Server(false)
  328. , _sendDebugStrings(false)
  329. , _bindAddress("")
  330. {
  331. createCommandAllocator();
  332. createCommandConfig();
  333. createCommandDebugMsg();
  334. createCommandDirector();
  335. createCommandExit();
  336. createCommandFileUtils();
  337. createCommandFps();
  338. createCommandHelp();
  339. createCommandProjection();
  340. createCommandResolution();
  341. createCommandSceneGraph();
  342. createCommandTexture();
  343. createCommandTouch();
  344. createCommandUpload();
  345. createCommandVersion();
  346. }
  347. Console::~Console()
  348. {
  349. stop();
  350. }
  351. bool Console::listenOnTCP(int port)
  352. {
  353. int listenfd = -1, n;
  354. const int on = 1;
  355. struct addrinfo hints, *res, *ressave;
  356. char serv[30];
  357. snprintf(serv, sizeof(serv)-1, "%d", port );
  358. bzero(&hints, sizeof(struct addrinfo));
  359. hints.ai_flags = AI_PASSIVE;
  360. hints.ai_family = AF_UNSPEC;
  361. hints.ai_socktype = SOCK_STREAM;
  362. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  363. WSADATA wsaData;
  364. n = WSAStartup(MAKEWORD(2, 2),&wsaData);
  365. #endif
  366. if ( (n = getaddrinfo(nullptr, serv, &hints, &res)) != 0) {
  367. fprintf(stderr,"net_listen error for %s: %s", serv, gai_strerror(n));
  368. return false;
  369. }
  370. ressave = res;
  371. do {
  372. listenfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  373. if (listenfd < 0)
  374. continue; /* error, try next one */
  375. setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on));
  376. // bind address
  377. if (!_bindAddress.empty())
  378. {
  379. if (res->ai_family == AF_INET)
  380. {
  381. struct sockaddr_in *sin = (struct sockaddr_in*) res->ai_addr;
  382. inet_pton(res->ai_family, _bindAddress.c_str(), (void*)&sin->sin_addr);
  383. }
  384. else if (res->ai_family == AF_INET6)
  385. {
  386. struct sockaddr_in6 *sin = (struct sockaddr_in6*) res->ai_addr;
  387. inet_pton(res->ai_family, _bindAddress.c_str(), (void*)&sin->sin6_addr);
  388. }
  389. }
  390. if (bind(listenfd, res->ai_addr, res->ai_addrlen) == 0)
  391. break; /* success */
  392. /* bind error, close and try next one */
  393. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  394. closesocket(listenfd);
  395. #else
  396. close(listenfd);
  397. #endif
  398. } while ( (res = res->ai_next) != nullptr);
  399. if (res == nullptr) {
  400. perror("net_listen:");
  401. freeaddrinfo(ressave);
  402. return false;
  403. }
  404. listen(listenfd, 50);
  405. if (res->ai_family == AF_INET) {
  406. _isIpv6Server = false;
  407. char buf[INET_ADDRSTRLEN] = {0};
  408. struct sockaddr_in *sin = (struct sockaddr_in*) res->ai_addr;
  409. if( inet_ntop(res->ai_family, &sin->sin_addr, buf, sizeof(buf)) != nullptr )
  410. cocos2d::log("Console: IPV4 server is listening on %s:%d", buf, ntohs(sin->sin_port));
  411. else
  412. perror("inet_ntop");
  413. } else if (res->ai_family == AF_INET6) {
  414. _isIpv6Server = true;
  415. char buf[INET6_ADDRSTRLEN] = {0};
  416. struct sockaddr_in6 *sin = (struct sockaddr_in6*) res->ai_addr;
  417. if( inet_ntop(res->ai_family, &sin->sin6_addr, buf, sizeof(buf)) != nullptr )
  418. cocos2d::log("Console: IPV6 server is listening on [%s]:%d", buf, ntohs(sin->sin6_port));
  419. else
  420. perror("inet_ntop");
  421. }
  422. freeaddrinfo(ressave);
  423. return listenOnFileDescriptor(listenfd);
  424. }
  425. bool Console::listenOnFileDescriptor(int fd)
  426. {
  427. if(_running) {
  428. cocos2d::log("Console already started. 'stop' it before calling 'listen' again");
  429. return false;
  430. }
  431. _listenfd = fd;
  432. _thread = std::thread( std::bind( &Console::loop, this) );
  433. return true;
  434. }
  435. void Console::stop()
  436. {
  437. if( _running ) {
  438. _endThread = true;
  439. if (_thread.joinable())
  440. {
  441. _thread.join();
  442. }
  443. }
  444. }
  445. void Console::addCommand(const Command& cmd)
  446. {
  447. _commands[cmd.name] = cmd;
  448. }
  449. void Console::addSubCommand(const std::string& cmdName, const Command& subCmd)
  450. {
  451. auto it = _commands.find(cmdName);
  452. if(it != _commands.end()) {
  453. auto& cmd = it->second;
  454. addSubCommand(cmd, subCmd);
  455. }
  456. }
  457. void Console::addSubCommand(Command& cmd, const Command& subCmd)
  458. {
  459. cmd.subCommands[subCmd.name] = subCmd;
  460. }
  461. const Console::Command* Console::getCommand(const std::string& cmdName)
  462. {
  463. auto it = _commands.find(cmdName);
  464. if(it != _commands.end()) {
  465. auto& cmd = it->second;
  466. return &cmd;
  467. }
  468. return nullptr;
  469. }
  470. const Console::Command* Console::getSubCommand(const std::string& cmdName, const std::string& subCmdName)
  471. {
  472. auto it = _commands.find(cmdName);
  473. if(it != _commands.end()) {
  474. auto& cmd = it->second;
  475. return getSubCommand(cmd, subCmdName);
  476. }
  477. return nullptr;
  478. }
  479. const Console::Command* Console::getSubCommand(const Command& cmd, const std::string& subCmdName)
  480. {
  481. return cmd.getSubCommand(subCmdName);
  482. }
  483. void Console::delCommand(const std::string& cmdName)
  484. {
  485. auto it = _commands.find(cmdName);
  486. if(it != _commands.end()) {
  487. _commands.erase(it);
  488. }
  489. }
  490. void Console::delSubCommand(const std::string& cmdName, const std::string& subCmdName)
  491. {
  492. auto it = _commands.find(cmdName);
  493. if(it != _commands.end()) {
  494. auto& cmd = it->second;
  495. delSubCommand(cmd, subCmdName);
  496. }
  497. }
  498. void Console::delSubCommand(Command& cmd, const std::string& subCmdName)
  499. {
  500. cmd.delSubCommand(subCmdName);
  501. }
  502. void Console::log(const char* buf)
  503. {
  504. if( _sendDebugStrings ) {
  505. _DebugStringsMutex.lock();
  506. _DebugStrings.push_back(buf);
  507. _DebugStringsMutex.unlock();
  508. }
  509. }
  510. void Console::setBindAddress(const std::string &address)
  511. {
  512. _bindAddress = address;
  513. }
  514. bool Console::isIpv6Server() const
  515. {
  516. return _isIpv6Server;
  517. }
  518. //
  519. // Main Loop
  520. //
  521. void Console::loop()
  522. {
  523. fd_set copy_set;
  524. struct timeval timeout, timeout_copy;
  525. _running = true;
  526. FD_ZERO(&_read_set);
  527. FD_SET(_listenfd, &_read_set);
  528. _maxfd = _listenfd;
  529. timeout.tv_sec = 0;
  530. /* 0.016 seconds. Wake up once per frame at 60PFS */
  531. timeout.tv_usec = 16000;
  532. while(!_endThread) {
  533. copy_set = _read_set;
  534. timeout_copy = timeout;
  535. int nready = select(_maxfd+1, &copy_set, nullptr, nullptr, &timeout_copy);
  536. if( nready == -1 )
  537. {
  538. /* error */
  539. if(errno != EINTR)
  540. cocos2d::log("Abnormal error in select()\n");
  541. continue;
  542. }
  543. else if( nready == 0 )
  544. {
  545. /* timeout. do something ? */
  546. }
  547. else
  548. {
  549. /* new client */
  550. if(FD_ISSET(_listenfd, &copy_set)) {
  551. addClient();
  552. if(--nready <= 0)
  553. continue;
  554. }
  555. /* data from client */
  556. std::vector<int> to_remove;
  557. for(const auto &fd: _fds) {
  558. if(FD_ISSET(fd,&copy_set))
  559. {
  560. //fix Bug #4302 Test case ConsoleTest--ConsoleUploadFile crashed on Linux
  561. //On linux, if you send data to a closed socket, the sending process will
  562. //receive a SIGPIPE, which will cause linux system shutdown the sending process.
  563. //Add this ioctl code to check if the socket has been closed by peer.
  564. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  565. u_long n = 0;
  566. ioctlsocket(fd, FIONREAD, &n);
  567. #else
  568. int n = 0;
  569. ioctl(fd, FIONREAD, &n);
  570. #endif
  571. if(n == 0)
  572. {
  573. //no data received, or fd is closed
  574. continue;
  575. }
  576. if( ! parseCommand(fd) )
  577. {
  578. to_remove.push_back(fd);
  579. }
  580. if(--nready <= 0)
  581. break;
  582. }
  583. }
  584. /* remove closed connections */
  585. for(int fd: to_remove) {
  586. FD_CLR(fd, &_read_set);
  587. _fds.erase(std::remove(_fds.begin(), _fds.end(), fd), _fds.end());
  588. }
  589. }
  590. /* Any message for the remote console ? send it! */
  591. if( !_DebugStrings.empty() ) {
  592. if (_DebugStringsMutex.try_lock())
  593. {
  594. for (const auto &str : _DebugStrings) {
  595. for (auto fd : _fds) {
  596. Console::Utility::sendToConsole(fd, str.c_str(), str.length());
  597. }
  598. }
  599. _DebugStrings.clear();
  600. _DebugStringsMutex.unlock();
  601. }
  602. }
  603. }
  604. // clean up: ignore stdin, stdout and stderr
  605. for(const auto &fd: _fds )
  606. {
  607. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  608. closesocket(fd);
  609. #else
  610. close(fd);
  611. #endif
  612. }
  613. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  614. closesocket(_listenfd);
  615. WSACleanup();
  616. #else
  617. close(_listenfd);
  618. #endif
  619. _running = false;
  620. }
  621. //
  622. // Helpers
  623. //
  624. ssize_t Console::readline(int fd, char* ptr, size_t maxlen)
  625. {
  626. size_t n, rc;
  627. char c;
  628. for( n = 0; n < maxlen - 1; n++ ) {
  629. if( (rc = recv(fd, &c, 1, 0)) ==1 ) {
  630. *ptr++ = c;
  631. if(c == '\n') {
  632. break;
  633. }
  634. } else if( rc == 0 ) {
  635. return 0;
  636. } else if( errno == EINTR ) {
  637. continue;
  638. } else {
  639. return -1;
  640. }
  641. }
  642. *ptr = 0;
  643. return n;
  644. }
  645. ssize_t Console::readBytes(int fd, char* buffer, size_t maxlen, bool* more)
  646. {
  647. size_t n, rc;
  648. char c, *ptr = buffer;
  649. *more = false;
  650. for( n = 0; n < maxlen; n++ ) {
  651. if( (rc = recv(fd, &c, 1, 0)) ==1 ) {
  652. *ptr++ = c;
  653. if(c == '\n') {
  654. return n;
  655. }
  656. } else if( rc == 0 ) {
  657. return 0;
  658. } else if( errno == EINTR ) {
  659. continue;
  660. } else {
  661. return -1;
  662. }
  663. }
  664. *more = true;
  665. return n;
  666. }
  667. bool Console::parseCommand(int fd)
  668. {
  669. char buf[512];
  670. bool more_data;
  671. auto h = readBytes(fd, buf, 6, &more_data);
  672. if( h < 0)
  673. {
  674. return false;
  675. }
  676. if(strncmp(buf, "upload", 6) == 0)
  677. {
  678. char c = '\0';
  679. recv(fd, &c, 1, 0);
  680. if(c == ' ')
  681. {
  682. commandUpload(fd);
  683. Console::Utility::sendPrompt(fd);
  684. return true;
  685. }
  686. else
  687. {
  688. const char err[] = "upload: invalid args! Type 'help' for options\n";
  689. Console::Utility::sendToConsole(fd, err, strlen(err));
  690. Console::Utility::sendPrompt(fd);
  691. return true;
  692. }
  693. }
  694. if(!more_data)
  695. {
  696. buf[h] = 0;
  697. }
  698. else
  699. {
  700. char *pb = buf + 6;
  701. auto r = readline(fd, pb, sizeof(buf)-6);
  702. if(r < 0)
  703. {
  704. const char err[] = "Unknown error!\n";
  705. Console::Utility::sendPrompt(fd);
  706. Console::Utility::sendToConsole(fd, err, strlen(err));
  707. return false;
  708. }
  709. }
  710. std::string cmdLine;
  711. std::vector<std::string> args;
  712. cmdLine = std::string(buf);
  713. args = Console::Utility::split(cmdLine, ' ');
  714. if(args.empty())
  715. {
  716. const char err[] = "Unknown command. Type 'help' for options\n";
  717. Console::Utility::sendToConsole(fd, err, strlen(err));
  718. Console::Utility::sendPrompt(fd);
  719. return true;
  720. }
  721. auto it = _commands.find(Console::Utility::trim(args[0]));
  722. if(it != _commands.end())
  723. {
  724. std::string args2;
  725. for(size_t i = 1; i < args.size(); ++i)
  726. {
  727. if(i > 1)
  728. {
  729. args2 += ' ';
  730. }
  731. args2 += Console::Utility::trim(args[i]);
  732. }
  733. auto cmd = it->second;
  734. cmd.commandGeneric(fd, args2);
  735. }else if(strcmp(buf, "\r\n") != 0) {
  736. const char err[] = "Unknown command. Type 'help' for options\n";
  737. Console::Utility::sendToConsole(fd, err, strlen(err));
  738. }
  739. Console::Utility::sendPrompt(fd);
  740. return true;
  741. }
  742. void Console::addClient()
  743. {
  744. struct sockaddr_in6 ipv6Addr;
  745. struct sockaddr_in ipv4Addr;
  746. struct sockaddr* addr = _isIpv6Server ? (struct sockaddr*)&ipv6Addr : (struct sockaddr*)&ipv4Addr;
  747. socklen_t addrLen = _isIpv6Server ? sizeof(ipv6Addr) : sizeof(ipv4Addr);
  748. /* new client */
  749. int fd = accept(_listenfd, addr, &addrLen);
  750. // add fd to list of FD
  751. if( fd != -1 ) {
  752. FD_SET(fd, &_read_set);
  753. _fds.push_back(fd);
  754. _maxfd = std::max(_maxfd,fd);
  755. Console::Utility::sendPrompt(fd);
  756. /**
  757. * A SIGPIPE is sent to a process if it tried to write to socket that had been shutdown for
  758. * writing or isn't connected (anymore) on iOS.
  759. *
  760. * The default behaviour for this signal is to end the process.So we make the process ignore SIGPIPE.
  761. */
  762. #if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
  763. int set = 1;
  764. setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, (void*)&set, sizeof(int));
  765. #endif
  766. }
  767. }
  768. //
  769. // create commands
  770. //
  771. void Console::createCommandAllocator()
  772. {
  773. addCommand({"allocator", "Display allocator diagnostics for all allocators. Args: [-h | help | ]",
  774. CC_CALLBACK_2(Console::commandAllocator, this)});
  775. }
  776. void Console::createCommandConfig()
  777. {
  778. addCommand({"config", "Print the Configuration object. Args: [-h | help | ]",
  779. CC_CALLBACK_2(Console::commandConfig, this)});
  780. }
  781. void Console::createCommandDebugMsg()
  782. {
  783. addCommand({"debugmsg", "Whether or not to forward the debug messages on the console. Args: [-h | help | on | off | ]",
  784. CC_CALLBACK_2(Console::commandDebugMsg, this)});
  785. addSubCommand("debugmsg", {"on", "enable debug logging", CC_CALLBACK_2(Console::commandDebugMsgSubCommandOnOff, this)});
  786. addSubCommand("debugmsg", {"off", "disable debug logging", CC_CALLBACK_2(Console::commandDebugMsgSubCommandOnOff, this)});
  787. }
  788. void Console::createCommandDirector()
  789. {
  790. addCommand({"director", "director commands, type -h or [director help] to list supported directives"});
  791. addSubCommand("director", {"pause", "pause all scheduled timers, the draw rate will be 4 FPS to reduce CPU consumption",
  792. CC_CALLBACK_2(Console::commandDirectorSubCommandPause, this)});
  793. addSubCommand("director", {"resume", "resume all scheduled timers",
  794. CC_CALLBACK_2(Console::commandDirectorSubCommandResume, this)});
  795. addSubCommand("director", {"stop", "Stops the animation. Nothing will be drawn.",
  796. CC_CALLBACK_2(Console::commandDirectorSubCommandStop, this)});
  797. addSubCommand("director", {"start", "Restart the animation again, Call this function only if [director stop] was called earlier",
  798. CC_CALLBACK_2(Console::commandDirectorSubCommandStart, this)});
  799. addSubCommand("director", {"end", "exit this app.",
  800. CC_CALLBACK_2(Console::commandDirectorSubCommandEnd, this)});
  801. }
  802. void Console::createCommandExit()
  803. {
  804. addCommand({"exit", "Close connection to the console. Args: [-h | help | ]", CC_CALLBACK_2(Console::commandExit, this)});
  805. }
  806. void Console::createCommandFileUtils()
  807. {
  808. addCommand({"fileutils", "Flush or print the FileUtils info. Args: [-h | help | flush | ]",
  809. CC_CALLBACK_2(Console::commandFileUtils, this)});
  810. addSubCommand("fileutils", {"flush", "Purges the file searching cache.",
  811. CC_CALLBACK_2(Console::commandFileUtilsSubCommandFlush, this)});
  812. }
  813. void Console::createCommandFps()
  814. {
  815. addCommand({"fps", "Turn on / off the FPS. Args: [-h | help | on | off | ]", CC_CALLBACK_2(Console::commandFps, this)});
  816. addSubCommand("fps", {"on", "Display the FPS on the bottom-left corner.", CC_CALLBACK_2(Console::commandFpsSubCommandOnOff, this)});
  817. addSubCommand("fps", {"off", "Hide the FPS on the bottom-left corner.", CC_CALLBACK_2(Console::commandFpsSubCommandOnOff, this)});
  818. }
  819. void Console::createCommandHelp()
  820. {
  821. addCommand({"help", "Print this message. Args: [ ]", CC_CALLBACK_2(Console::commandHelp, this)});
  822. }
  823. void Console::createCommandProjection()
  824. {
  825. addCommand({"projection", "Change or print the current projection. Args: [-h | help | 2d | 3d | ]",
  826. CC_CALLBACK_2(Console::commandProjection, this)});
  827. addSubCommand("projection", {"2d", "sets a 2D projection (orthogonal projection).",
  828. CC_CALLBACK_2(Console::commandProjectionSubCommand2d, this)});
  829. addSubCommand("projection", {"3d", "sets a 3D projection with a fovy=60, znear=0.5f and zfar=1500.",
  830. CC_CALLBACK_2(Console::commandProjectionSubCommand3d, this)});
  831. }
  832. void Console::createCommandResolution()
  833. {
  834. addCommand({"resolution", "Change or print the window resolution. Args: [-h | help | width height resolution_policy | ]",
  835. CC_CALLBACK_2(Console::commandResolution, this)});
  836. addSubCommand("resolution", {"", "", CC_CALLBACK_2(Console::commandResolutionSubCommandEmpty, this)});
  837. }
  838. void Console::createCommandSceneGraph()
  839. {
  840. addCommand({"scenegraph", "Print the scene graph", CC_CALLBACK_2(Console::commandSceneGraph, this)});
  841. }
  842. void Console::createCommandTexture()
  843. {
  844. addCommand({"texture", "Flush or print the TextureCache info. Args: [-h | help | flush | ] ",
  845. CC_CALLBACK_2(Console::commandTextures, this)});
  846. addSubCommand("texture", {"flush", "Purges the dictionary of loaded textures.",
  847. CC_CALLBACK_2(Console::commandTexturesSubCommandFlush, this)});
  848. }
  849. void Console::createCommandTouch()
  850. {
  851. addCommand({"touch", "simulate touch event via console, type -h or [touch help] to list supported directives"});
  852. addSubCommand("touch", {"tap", "touch tap x y: simulate touch tap at (x,y).",
  853. CC_CALLBACK_2(Console::commandTouchSubCommandTap, this)});
  854. addSubCommand("touch", {"swipe", "touch swipe x1 y1 x2 y2: simulate touch swipe from (x1,y1) to (x2,y2).",
  855. CC_CALLBACK_2(Console::commandTouchSubCommandSwipe, this)});
  856. }
  857. void Console::createCommandUpload()
  858. {
  859. addCommand({"upload", "upload file. Args: [filename base64_encoded_data]", CC_CALLBACK_1(Console::commandUpload, this)});
  860. }
  861. void Console::createCommandVersion()
  862. {
  863. addCommand({"version", "print version string ", CC_CALLBACK_2(Console::commandVersion, this)});
  864. }
  865. //
  866. // commands
  867. //
  868. void Console::commandAllocator(int fd, const std::string& /*args*/)
  869. {
  870. #if CC_ENABLE_ALLOCATOR_DIAGNOSTICS
  871. auto info = allocator::AllocatorDiagnostics::instance()->diagnostics();
  872. Console::Utility::mydprintf(fd, info.c_str());
  873. #else
  874. Console::Utility::mydprintf(fd, "allocator diagnostics not available. CC_ENABLE_ALLOCATOR_DIAGNOSTICS must be set to 1 in ccConfig.h\n");
  875. #endif
  876. }
  877. void Console::commandConfig(int fd, const std::string& /*args*/)
  878. {
  879. Scheduler *sched = Director::getInstance()->getScheduler();
  880. sched->performFunctionInCocosThread( [=](){
  881. Console::Utility::mydprintf(fd, "%s", Configuration::getInstance()->getInfo().c_str());
  882. Console::Utility::sendPrompt(fd);
  883. });
  884. }
  885. void Console::commandDebugMsg(int fd, const std::string& /*args*/)
  886. {
  887. Console::Utility::mydprintf(fd, "Debug message is: %s\n", _sendDebugStrings ? "on" : "off");
  888. }
  889. void Console::commandDebugMsgSubCommandOnOff(int /*fd*/, const std::string& args)
  890. {
  891. _sendDebugStrings = (args.compare("on") == 0);
  892. }
  893. void Console::commandDirectorSubCommandPause(int /*fd*/, const std::string& /*args*/)
  894. {
  895. auto director = Director::getInstance();
  896. Scheduler *sched = director->getScheduler();
  897. sched->performFunctionInCocosThread( [](){
  898. Director::getInstance()->pause();
  899. });
  900. }
  901. void Console::commandDirectorSubCommandResume(int /*fd*/, const std::string& /*args*/)
  902. {
  903. auto director = Director::getInstance();
  904. director->resume();
  905. }
  906. void Console::commandDirectorSubCommandStop(int /*fd*/, const std::string& /*args*/)
  907. {
  908. auto director = Director::getInstance();
  909. Scheduler *sched = director->getScheduler();
  910. sched->performFunctionInCocosThread( [](){
  911. Director::getInstance()->stopAnimation();
  912. });
  913. }
  914. void Console::commandDirectorSubCommandStart(int /*fd*/, const std::string& /*args*/)
  915. {
  916. auto director = Director::getInstance();
  917. director->startAnimation();
  918. }
  919. void Console::commandDirectorSubCommandEnd(int /*fd*/, const std::string& /*args*/)
  920. {
  921. auto director = Director::getInstance();
  922. director->end();
  923. }
  924. void Console::commandExit(int fd, const std::string& /*args*/)
  925. {
  926. FD_CLR(fd, &_read_set);
  927. _fds.erase(std::remove(_fds.begin(), _fds.end(), fd), _fds.end());
  928. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  929. closesocket(fd);
  930. #else
  931. close(fd);
  932. #endif
  933. }
  934. void Console::commandFileUtils(int fd, const std::string& /*args*/)
  935. {
  936. Scheduler *sched = Director::getInstance()->getScheduler();
  937. sched->performFunctionInCocosThread( std::bind(&Console::printFileUtils, this, fd) );
  938. }
  939. void Console::commandFileUtilsSubCommandFlush(int /*fd*/, const std::string& /*args*/)
  940. {
  941. FileUtils::getInstance()->purgeCachedEntries();
  942. }
  943. void Console::commandFps(int fd, const std::string& /*args*/)
  944. {
  945. Console::Utility::mydprintf(fd, "FPS is: %s\n", Director::getInstance()->isDisplayStats() ? "on" : "off");
  946. }
  947. void Console::commandFpsSubCommandOnOff(int /*fd*/, const std::string& args)
  948. {
  949. bool state = (args.compare("on") == 0);
  950. Director *dir = Director::getInstance();
  951. Scheduler *sched = dir->getScheduler();
  952. sched->performFunctionInCocosThread( std::bind(&Director::setDisplayStats, dir, state));
  953. }
  954. void Console::commandHelp(int fd, const std::string& /*args*/)
  955. {
  956. sendHelp(fd, _commands, "\nAvailable commands:\n");
  957. }
  958. void Console::commandProjection(int fd, const std::string& /*args*/)
  959. {
  960. auto director = Director::getInstance();
  961. char buf[20];
  962. auto proj = director->getProjection();
  963. switch (proj) {
  964. case cocos2d::Director::Projection::_2D:
  965. sprintf(buf,"2d");
  966. break;
  967. case cocos2d::Director::Projection::_3D:
  968. sprintf(buf,"3d");
  969. break;
  970. case cocos2d::Director::Projection::CUSTOM:
  971. sprintf(buf,"custom");
  972. break;
  973. default:
  974. sprintf(buf,"unknown");
  975. break;
  976. }
  977. Console::Utility::mydprintf(fd, "Current projection: %s\n", buf);
  978. }
  979. void Console::commandProjectionSubCommand2d(int /*fd*/, const std::string& /*args*/)
  980. {
  981. auto director = Director::getInstance();
  982. Scheduler *sched = director->getScheduler();
  983. sched->performFunctionInCocosThread( [=](){
  984. director->setProjection(Director::Projection::_2D);
  985. } );
  986. }
  987. void Console::commandProjectionSubCommand3d(int /*fd*/, const std::string& /*args*/)
  988. {
  989. auto director = Director::getInstance();
  990. Scheduler *sched = director->getScheduler();
  991. sched->performFunctionInCocosThread( [=](){
  992. director->setProjection(Director::Projection::_3D);
  993. } );
  994. }
  995. void Console::commandResolution(int /*fd*/, const std::string& args)
  996. {
  997. int width, height, policy;
  998. std::istringstream stream( args );
  999. stream >> width >> height>> policy;
  1000. Scheduler *sched = Director::getInstance()->getScheduler();
  1001. sched->performFunctionInCocosThread( [=](){
  1002. Director::getInstance()->getOpenGLView()->setDesignResolutionSize(width, height, static_cast<ResolutionPolicy>(policy));
  1003. } );
  1004. }
  1005. void Console::commandResolutionSubCommandEmpty(int fd, const std::string& /*args*/)
  1006. {
  1007. auto director = Director::getInstance();
  1008. Size points = director->getWinSize();
  1009. Size pixels = director->getWinSizeInPixels();
  1010. auto glview = director->getOpenGLView();
  1011. Size design = glview->getDesignResolutionSize();
  1012. ResolutionPolicy res = glview->getResolutionPolicy();
  1013. Rect visibleRect = glview->getVisibleRect();
  1014. Console::Utility::mydprintf(fd, "Window Size:\n"
  1015. "\t%d x %d (points)\n"
  1016. "\t%d x %d (pixels)\n"
  1017. "\t%d x %d (design resolution)\n"
  1018. "Resolution Policy: %d\n"
  1019. "Visible Rect:\n"
  1020. "\torigin: %d x %d\n"
  1021. "\tsize: %d x %d\n",
  1022. (int)points.width, (int)points.height,
  1023. (int)pixels.width, (int)pixels.height,
  1024. (int)design.width, (int)design.height,
  1025. (int)res,
  1026. (int)visibleRect.origin.x, (int)visibleRect.origin.y,
  1027. (int)visibleRect.size.width, (int)visibleRect.size.height
  1028. );
  1029. }
  1030. void Console::commandSceneGraph(int fd, const std::string& /*args*/)
  1031. {
  1032. Scheduler *sched = Director::getInstance()->getScheduler();
  1033. sched->performFunctionInCocosThread( std::bind(&Console::printSceneGraphBoot, this, fd) );
  1034. }
  1035. void Console::commandTextures(int fd, const std::string& /*args*/)
  1036. {
  1037. Scheduler *sched = Director::getInstance()->getScheduler();
  1038. sched->performFunctionInCocosThread( [=](){
  1039. Console::Utility::mydprintf(fd, "%s", Director::getInstance()->getTextureCache()->getCachedTextureInfo().c_str());
  1040. Console::Utility::sendPrompt(fd);
  1041. });
  1042. }
  1043. void Console::commandTexturesSubCommandFlush(int /*fd*/, const std::string& /*args*/)
  1044. {
  1045. Scheduler *sched = Director::getInstance()->getScheduler();
  1046. sched->performFunctionInCocosThread( [](){
  1047. Director::getInstance()->getTextureCache()->removeAllTextures();
  1048. });
  1049. }
  1050. void Console::commandTouchSubCommandTap(int fd, const std::string& args)
  1051. {
  1052. auto argv = Console::Utility::split(args,' ');
  1053. if((argv.size() == 3 ) && (Console::Utility::isFloat(argv[1]) && Console::Utility::isFloat(argv[2])))
  1054. {
  1055. float x = utils::atof(argv[1].c_str());
  1056. float y = utils::atof(argv[2].c_str());
  1057. std::srand ((unsigned)time(nullptr));
  1058. _touchId = rand();
  1059. Scheduler *sched = Director::getInstance()->getScheduler();
  1060. sched->performFunctionInCocosThread( [&](){
  1061. Director::getInstance()->getOpenGLView()->handleTouchesBegin(1, &_touchId, &x, &y);
  1062. Director::getInstance()->getOpenGLView()->handleTouchesEnd(1, &_touchId, &x, &y);
  1063. });
  1064. }
  1065. else
  1066. {
  1067. const char msg[] = "touch: invalid arguments.\n";
  1068. Console::Utility::sendToConsole(fd, msg, strlen(msg));
  1069. }
  1070. }
  1071. void Console::commandTouchSubCommandSwipe(int fd, const std::string& args)
  1072. {
  1073. auto argv = Console::Utility::split(args,' ');
  1074. if((argv.size() == 5)
  1075. && (Console::Utility::isFloat(argv[1])) && (Console::Utility::isFloat(argv[2]))
  1076. && (Console::Utility::isFloat(argv[3])) && (Console::Utility::isFloat(argv[4])))
  1077. {
  1078. float x1 = utils::atof(argv[1].c_str());
  1079. float y1 = utils::atof(argv[2].c_str());
  1080. float x2 = utils::atof(argv[3].c_str());
  1081. float y2 = utils::atof(argv[4].c_str());
  1082. std::srand ((unsigned)time(nullptr));
  1083. _touchId = rand();
  1084. Scheduler *sched = Director::getInstance()->getScheduler();
  1085. sched->performFunctionInCocosThread( [=](){
  1086. float tempx = x1, tempy = y1;
  1087. Director::getInstance()->getOpenGLView()->handleTouchesBegin(1, &_touchId, &tempx, &tempy);
  1088. });
  1089. float dx = std::abs(x1 - x2);
  1090. float dy = std::abs(y1 - y2);
  1091. float _x_ = x1, _y_ = y1;
  1092. if(dx > dy)
  1093. {
  1094. while(dx > 1)
  1095. {
  1096. if(x1 < x2)
  1097. {
  1098. _x_ += 1;
  1099. }
  1100. if(x1 > x2)
  1101. {
  1102. _x_ -= 1;
  1103. }
  1104. if(y1 < y2)
  1105. {
  1106. _y_ += dy/dx;
  1107. }
  1108. if(y1 > y2)
  1109. {
  1110. _y_ -= dy/dx;
  1111. }
  1112. sched->performFunctionInCocosThread( [=](){
  1113. float tempx = _x_, tempy = _y_;
  1114. Director::getInstance()->getOpenGLView()->handleTouchesMove(1, &_touchId, &tempx, &tempy);
  1115. });
  1116. dx -= 1;
  1117. }
  1118. }
  1119. else
  1120. {
  1121. while(dy > 1)
  1122. {
  1123. if(x1 < x2)
  1124. {
  1125. _x_ += dx/dy;
  1126. }
  1127. if(x1 > x2)
  1128. {
  1129. _x_ -= dx/dy;
  1130. }
  1131. if(y1 < y2)
  1132. {
  1133. _y_ += 1;
  1134. }
  1135. if(y1 > y2)
  1136. {
  1137. _y_ -= 1;
  1138. }
  1139. sched->performFunctionInCocosThread( [=](){
  1140. float tempx = _x_, tempy = _y_;
  1141. Director::getInstance()->getOpenGLView()->handleTouchesMove(1, &_touchId, &tempx, &tempy);
  1142. });
  1143. dy -= 1;
  1144. }
  1145. }
  1146. sched->performFunctionInCocosThread( [=](){
  1147. float tempx = x2, tempy = y2;
  1148. Director::getInstance()->getOpenGLView()->handleTouchesEnd(1, &_touchId, &tempx, &tempy);
  1149. });
  1150. }
  1151. else
  1152. {
  1153. const char msg[] = "touch: invalid arguments.\n";
  1154. Console::Utility::sendToConsole(fd, msg, strlen(msg));
  1155. }
  1156. }
  1157. static char invalid_filename_char[] = {':', '/', '\\', '?', '%', '*', '<', '>', '"', '|', '\r', '\n', '\t'};
  1158. void Console::commandUpload(int fd)
  1159. {
  1160. ssize_t n, rc;
  1161. char buf[512] = {0};
  1162. char c = 0;
  1163. char *ptr = buf;
  1164. //read file name
  1165. for( n = 0; n < sizeof(buf) - 1; n++ )
  1166. {
  1167. if( (rc = recv(fd, &c, 1, 0)) == 1 )
  1168. {
  1169. for(char x : invalid_filename_char)
  1170. {
  1171. if (c == x)
  1172. {
  1173. const char err[] = "upload: invalid file name!\n";
  1174. Console::Utility::sendToConsole(fd, err, strlen(err));
  1175. return;
  1176. }
  1177. }
  1178. if (c == ' ')
  1179. {
  1180. break;
  1181. }
  1182. *ptr++ = c;
  1183. }
  1184. else if( rc == 0 )
  1185. {
  1186. break;
  1187. }
  1188. else if( errno == EINTR )
  1189. {
  1190. continue;
  1191. }
  1192. else
  1193. {
  1194. break;
  1195. }
  1196. }
  1197. *ptr = 0;
  1198. static std::string writablePath = FileUtils::getInstance()->getWritablePath();
  1199. std::string filepath = writablePath + std::string(buf);
  1200. FILE* fp = fopen(FileUtils::getInstance()->getSuitableFOpen(filepath).c_str(), "wb");
  1201. if(!fp)
  1202. {
  1203. const char err[] = "can't create file!\n";
  1204. Console::Utility::sendToConsole(fd, err, strlen(err));
  1205. return;
  1206. }
  1207. while (true)
  1208. {
  1209. char data[4];
  1210. for(int i = 0; i < 4; i++)
  1211. {
  1212. data[i] = '=';
  1213. }
  1214. bool more_data;
  1215. readBytes(fd, data, 4, &more_data);
  1216. if(!more_data)
  1217. {
  1218. break;
  1219. }
  1220. unsigned char *decode;
  1221. unsigned char *in = (unsigned char *)data;
  1222. int dt = base64Decode(in, 4, &decode);
  1223. if (dt > 0)
  1224. {
  1225. fwrite(decode, dt, 1, fp);
  1226. }
  1227. free(decode);
  1228. }
  1229. fclose(fp);
  1230. }
  1231. void Console::commandVersion(int fd, const std::string& /*args*/)
  1232. {
  1233. Console::Utility::mydprintf(fd, "%s\n", cocos2dVersion());
  1234. }
  1235. // helper free functions
  1236. int Console::printSceneGraph(int fd, Node* node, int level)
  1237. {
  1238. int total = 1;
  1239. for(int i=0; i<level; ++i)
  1240. Console::Utility::sendToConsole(fd, "-", 1);
  1241. Console::Utility::mydprintf(fd, " %s\n", node->getDescription().c_str());
  1242. for(const auto& child: node->getChildren())
  1243. total += printSceneGraph(fd, child, level+1);
  1244. return total;
  1245. }
  1246. void Console::printSceneGraphBoot(int fd)
  1247. {
  1248. Console::Utility::sendToConsole(fd,"\n",1);
  1249. auto scene = Director::getInstance()->getRunningScene();
  1250. int total = printSceneGraph(fd, scene, 0);
  1251. Console::Utility::mydprintf(fd, "Total Nodes: %d\n", total);
  1252. Console::Utility::sendPrompt(fd);
  1253. }
  1254. void Console::printFileUtils(int fd)
  1255. {
  1256. FileUtils* fu = FileUtils::getInstance();
  1257. Console::Utility::mydprintf(fd, "\nSearch Paths:\n");
  1258. auto& list = fu->getSearchPaths();
  1259. for( const auto &item : list) {
  1260. Console::Utility::mydprintf(fd, "%s\n", item.c_str());
  1261. }
  1262. Console::Utility::mydprintf(fd, "\nResolution Order:\n");
  1263. auto& list1 = fu->getSearchResolutionsOrder();
  1264. for( const auto &item : list1) {
  1265. Console::Utility::mydprintf(fd, "%s\n", item.c_str());
  1266. }
  1267. Console::Utility::mydprintf(fd, "\nWritable Path:\n");
  1268. Console::Utility::mydprintf(fd, "%s\n", fu->getWritablePath().c_str());
  1269. Console::Utility::mydprintf(fd, "\nFull Path Cache:\n");
  1270. auto& cache = fu->getFullPathCache();
  1271. for( const auto &item : cache) {
  1272. Console::Utility::mydprintf(fd, "%s -> %s\n", item.first.c_str(), item.second.c_str());
  1273. }
  1274. Console::Utility::sendPrompt(fd);
  1275. }
  1276. void Console::sendHelp(int fd, const std::map<std::string, Command>& commands, const char* msg)
  1277. {
  1278. Console::Utility::sendToConsole(fd, msg, strlen(msg));
  1279. for(auto& it : commands)
  1280. {
  1281. auto command = it.second;
  1282. if (command.help.empty()) continue;
  1283. Console::Utility::mydprintf(fd, "\t%s", command.name.c_str());
  1284. ssize_t tabs = strlen(command.name.c_str()) / 8;
  1285. tabs = 3 - tabs;
  1286. for(int j=0;j<tabs;j++){
  1287. Console::Utility::mydprintf(fd, "\t");
  1288. }
  1289. Console::Utility::mydprintf(fd,"%s\n", command.help.c_str());
  1290. }
  1291. }
  1292. NS_CC_END