FileServer.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. /****************************************************************************
  2. Copyright (c) 2013 cocos2d-x.org
  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 "FileServer.h"
  21. #include "Runtime.h"
  22. #include "zlib.h"
  23. #include "ConfigParser.h"
  24. // header files for directory operation
  25. #ifdef _WIN32
  26. #include <direct.h>
  27. #else
  28. #include <sys/stat.h>
  29. #endif
  30. USING_NS_CC;
  31. //1M size
  32. #define MAXPROTOLENGTH 1048576
  33. #define PROTO_START "RuntimeSend:"
  34. FileServer* FileServer::s_sharedFileServer = nullptr;
  35. FileServer* FileServer::getShareInstance()
  36. {
  37. if (s_sharedFileServer == nullptr)
  38. {
  39. s_sharedFileServer = new FileServer;
  40. }
  41. return s_sharedFileServer;
  42. }
  43. void FileServer::purge()
  44. {
  45. CC_SAFE_DELETE(s_sharedFileServer);
  46. }
  47. void FileServer::readResFileFinfo()
  48. {
  49. std::string filecfg = _writePath + "/fileinfo_debug.json";
  50. FILE * pFile = fopen (filecfg.c_str() , "r");
  51. if(pFile)
  52. {
  53. char buffer[65536];
  54. rapidjson::FileReadStream inputStream(pFile, buffer, sizeof(buffer));
  55. _filecfgjson.ParseStream<0>(inputStream);
  56. fclose(pFile);
  57. }
  58. if(! _filecfgjson.IsObject()){
  59. _filecfgjson.SetObject();
  60. }
  61. //save file info to disk every five second
  62. Director::getInstance()->getScheduler()->schedule([&](float){
  63. rapidjson::StringBuffer buffer;
  64. rapidjson::Writer< rapidjson::StringBuffer > writer(buffer);
  65. _filecfgjson.Accept(writer);
  66. const char* str = buffer.GetString();
  67. std::string filecfg = _writePath + "/fileinfo_debug.json";
  68. FILE * pFile = fopen(filecfg.c_str(), "w");
  69. if (!pFile) return ;
  70. fwrite(str, sizeof(char), strlen(str), pFile);
  71. fclose(pFile);
  72. },this, 5.0f, false, "fileinfo");
  73. }
  74. void FileServer::addResFileInfo(const char* filename, uint64_t u64)
  75. {
  76. if(_filecfgjson.HasMember(filename)){
  77. _filecfgjson.RemoveMember(filename);
  78. }
  79. char filetime[512]= {0};
  80. sprintf(filetime, "%llu", u64);
  81. rapidjson::Value filetimeValue(rapidjson::kStringType);
  82. filetimeValue.SetString(filetime, _filecfgjson.GetAllocator());
  83. rapidjson::Value filenameValue(rapidjson::kStringType);
  84. filenameValue.SetString(filename,_filecfgjson.GetAllocator());
  85. _filecfgjson.AddMember(filenameValue, filetimeValue, _filecfgjson.GetAllocator());
  86. }
  87. void FileServer::removeResFileInfo(const char *filename)
  88. {
  89. if (_filecfgjson.HasMember(filename)) {
  90. _filecfgjson.RemoveMember(filename);
  91. }
  92. }
  93. std::string FileServer::getTransingFileName()
  94. {
  95. _fileNameMutex.lock();
  96. std::string filename = _strFileName;
  97. _fileNameMutex.unlock();
  98. return filename;
  99. }
  100. void FileServer::setTransingFileName(const std::string &filename)
  101. {
  102. _fileNameMutex.lock();
  103. _strFileName = filename;
  104. _fileNameMutex.unlock();
  105. }
  106. bool FileServer::listenOnTCP(int port)
  107. {
  108. int listenfd, n;
  109. const int on = 1;
  110. struct addrinfo hints, *res, *ressave;
  111. char serv[30];
  112. snprintf(serv, sizeof(serv)-1, "%d", port );
  113. serv[sizeof(serv)-1]=0;
  114. bzero(&hints, sizeof(struct addrinfo));
  115. hints.ai_flags = AI_PASSIVE;
  116. hints.ai_family = AF_INET; // AF_UNSPEC: Do we need IPv6 ?
  117. hints.ai_socktype = SOCK_STREAM;
  118. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
  119. WSADATA wsaData;
  120. n = WSAStartup(MAKEWORD(2, 2),&wsaData);
  121. #endif
  122. if ( (n = getaddrinfo(NULL, serv, &hints, &res)) != 0) {
  123. fprintf(stderr,"net_listen error for %s: %s", serv, gai_strerror(n));
  124. return false;
  125. }
  126. ressave = res;
  127. do {
  128. listenfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  129. if (listenfd < 0)
  130. continue; /* error, try next one */
  131. setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on));
  132. //setsockopt(listenfd, IPPROTO_TCP, TCP_NODELAY, (const char*)&on, sizeof(on));
  133. auto address = ConfigParser::getInstance()->getBindAddress();
  134. // bind address
  135. if (address.length() > 0)
  136. {
  137. if (res->ai_family == AF_INET)
  138. {
  139. struct sockaddr_in *sin = (struct sockaddr_in*) res->ai_addr;
  140. inet_pton(res->ai_family, address.c_str(), (void*)&sin->sin_addr);
  141. }
  142. else if (res->ai_family == AF_INET6)
  143. {
  144. struct sockaddr_in6 *sin = (struct sockaddr_in6*) res->ai_addr;
  145. inet_pton(res->ai_family, address.c_str(), (void*)&sin->sin6_addr);
  146. }
  147. }
  148. if (::bind(listenfd, res->ai_addr, res->ai_addrlen) == 0)
  149. break; /* success */
  150. /* bind error, close and try next one */
  151. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
  152. closesocket(listenfd);
  153. #else
  154. close(listenfd);
  155. #endif
  156. } while ((res = res->ai_next) != NULL);
  157. if (res == NULL)
  158. {
  159. perror("net_listen:");
  160. freeaddrinfo(ressave);
  161. return false;
  162. }
  163. listen(listenfd, 1);
  164. if (res->ai_family == AF_INET)
  165. {
  166. char buf[INET_ADDRSTRLEN] = "";
  167. struct sockaddr_in *sin = (struct sockaddr_in*) res->ai_addr;
  168. if( inet_ntop(res->ai_family, &sin->sin_addr, buf, sizeof(buf)) != NULL )
  169. cocos2d::log("Console: listening on %s : %d", buf, ntohs(sin->sin_port));
  170. else
  171. perror("inet_ntop");
  172. } else if (res->ai_family == AF_INET6)
  173. {
  174. char buf[INET6_ADDRSTRLEN] = "";
  175. struct sockaddr_in6 *sin = (struct sockaddr_in6*) res->ai_addr;
  176. if( inet_ntop(res->ai_family, &sin->sin6_addr, buf, sizeof(buf)) != NULL )
  177. cocos2d::log("Console: listening on %s : %d", buf, ntohs(sin->sin6_port));
  178. else
  179. perror("inet_ntop");
  180. }
  181. freeaddrinfo(ressave);
  182. _listenfd = listenfd;
  183. _receiveThread = std::thread(std::bind( &FileServer::loopReceiveFile, this));
  184. _writeThread = std::thread(std::bind(&FileServer::loopWriteFile, this));
  185. _responseThread = std::thread(std::bind(&FileServer::loopResponse, this));
  186. return true;
  187. }
  188. void FileServer::stop()
  189. {
  190. _receiveEndThread = true;
  191. _writeEndThread = true;
  192. _responseEndThread = true;
  193. if (_receiveRunning && _receiveThread.joinable())
  194. {
  195. _receiveThread.join();
  196. }
  197. if (_writeRunning && _writeThread.joinable())
  198. {
  199. _writeThread.join();
  200. }
  201. if (_responseRunning && _responseThread.joinable())
  202. {
  203. _responseThread.join();
  204. }
  205. }
  206. FileServer::FileServer() :
  207. _listenfd(-1),
  208. _receiveRunning(false),
  209. _receiveEndThread(false),
  210. _writeRunning(false),
  211. _writeEndThread(false),
  212. _responseRunning(false),
  213. _responseEndThread(false)
  214. {
  215. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  216. // need to be opened by Code IDE
  217. _isUsingWritePath = false;
  218. #else
  219. _isUsingWritePath = true;
  220. #endif
  221. _writePath = FileUtils::getInstance()->getWritablePath();
  222. #if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
  223. #include "Widget_mac.h"
  224. _writePath += getCurAppName();
  225. _writePath += "/";
  226. #endif
  227. _writePath += "debugruntime/";
  228. _writePath = replaceAll(_writePath, "\\", "/");
  229. if (_writePath.at(_writePath.length() - 1) != '/'){
  230. _writePath.append("/");
  231. }
  232. }
  233. FileServer::~FileServer()
  234. {
  235. stop();
  236. }
  237. void FileServer::loopReceiveFile()
  238. {
  239. struct sockaddr client;
  240. socklen_t client_len;
  241. /* new client */
  242. client_len = sizeof(client);
  243. int fd = accept(_listenfd, (struct sockaddr *)&client, &client_len );
  244. char *protoBuf = new char[MAXPROTOLENGTH];
  245. while(!_receiveEndThread) {
  246. // recv start flag
  247. char startflag[13] = {0};
  248. recvBuf(fd, startflag, sizeof(startflag) - 1);
  249. if (strcmp(startflag, PROTO_START) != 0)
  250. {
  251. continue;
  252. }
  253. // recv proto num
  254. union
  255. {
  256. char char_type[3];
  257. unsigned short uint16_type;
  258. }protonum;
  259. recvBuf(fd, protonum.char_type, sizeof(protonum.char_type) - 1);
  260. //recv protobuf length
  261. union
  262. {
  263. char char_type[3];
  264. unsigned short uint16_type;
  265. }protolength;
  266. recvBuf(fd, protolength.char_type, sizeof(protolength.char_type) - 1);
  267. //recv variable length
  268. memset(protoBuf, 0, MAXPROTOLENGTH);
  269. recvBuf(fd, protoBuf, protolength.uint16_type);
  270. RecvBufStruct recvDataBuf;
  271. recvDataBuf.fd = fd;
  272. recvDataBuf.fileProto.ParseFromString(protoBuf);
  273. if (1 == recvDataBuf.fileProto.package_seq())
  274. {
  275. _recvErrorFile = "";
  276. } else
  277. {
  278. // recv error
  279. if (_recvErrorFile == recvDataBuf.fileProto.file_name())
  280. {
  281. continue;
  282. }
  283. }
  284. unsigned long contentSize = recvDataBuf.fileProto.content_size();
  285. if (contentSize == 0)
  286. {
  287. recvDataBuf.contentBuf="";
  288. _recvBufListMutex.lock();
  289. _recvBufList.push_back(recvDataBuf);
  290. _recvBufListMutex.unlock();
  291. }else if(contentSize > 0)
  292. {
  293. //recv body data
  294. Bytef *contentbuf = new Bytef[contentSize+1];
  295. memset(contentbuf, 0, contentSize+1);
  296. unsigned long recvTotalLen = contentSize;
  297. while (recvTotalLen != 0){
  298. unsigned long recvLen = MAXPROTOLENGTH;
  299. if(recvTotalLen < MAXPROTOLENGTH)
  300. recvLen = recvTotalLen;
  301. memset(protoBuf, 0, MAXPROTOLENGTH);
  302. unsigned long result = recv(fd, protoBuf, recvLen,0);
  303. if (result <= 0)
  304. {
  305. usleep(1);
  306. continue;
  307. }
  308. memcpy(contentbuf + contentSize - recvTotalLen, protoBuf, result);
  309. recvTotalLen -= result;
  310. }
  311. if (recvDataBuf.fileProto.compress_type() == runtime::FileSendProtos_CompressType::FileSendProtos_CompressType_ZIP){
  312. unsigned long uncompressSize = recvDataBuf.fileProto.uncompress_size();
  313. Bytef *buff = new Bytef[uncompressSize * sizeof(Bytef)];
  314. memset(buff, 0, uncompressSize * sizeof(Bytef));
  315. int err = ::uncompress(buff, &uncompressSize,contentbuf, contentSize * sizeof(Bytef));
  316. if (err != Z_OK){
  317. CC_SAFE_DELETE_ARRAY(buff);
  318. CC_SAFE_DELETE_ARRAY(contentbuf);
  319. addResponse(recvDataBuf.fd, recvDataBuf.fileProto.file_name(), runtime::FileSendComplete::RESULTTYPE::FileSendComplete_RESULTTYPE_UNCOMPRESS_ERROR, err);
  320. continue;
  321. }
  322. CC_SAFE_DELETE_ARRAY(contentbuf);
  323. contentbuf = buff;
  324. contentSize = uncompressSize;
  325. }
  326. recvDataBuf.contentBuf.assign((const char*)contentbuf, contentSize);
  327. CC_SAFE_DELETE_ARRAY(contentbuf);
  328. _recvBufListMutex.lock();
  329. _recvBufList.push_back(recvDataBuf);
  330. _recvBufListMutex.unlock();
  331. }
  332. }
  333. _receiveRunning = false;
  334. CC_SAFE_DELETE_ARRAY(protoBuf);
  335. }
  336. void FileServer::loopWriteFile()
  337. {
  338. _writeRunning = true;
  339. while(!_writeEndThread)
  340. {
  341. _recvBufListMutex.lock();
  342. size_t recvSize = _recvBufList.size();
  343. _recvBufListMutex.unlock();
  344. if(0 == recvSize)
  345. {
  346. usleep(500);
  347. continue;
  348. }
  349. _recvBufListMutex.lock();
  350. RecvBufStruct recvDataBuf = _recvBufList.front();
  351. _recvBufList.pop_front();
  352. _recvBufListMutex.unlock();
  353. std::string filename = recvDataBuf.fileProto.file_name();
  354. std::string fullfilename = _writePath;
  355. fullfilename += filename;
  356. _fileNameMutex.lock();
  357. _strFileName = filename;
  358. _fileNameMutex.unlock();
  359. //cocos2d::log("WriteFile:: fullfilename = %s",filename.c_str());
  360. createDir(fullfilename.substr(0, fullfilename.find_last_of("/")).c_str());
  361. FILE *fp= nullptr;
  362. if (1 == recvDataBuf.fileProto.package_seq())
  363. {
  364. _writeErrorFile ="";
  365. fp = fopen(fullfilename.c_str(), "wb");
  366. } else
  367. {
  368. if (_writeErrorFile == filename)
  369. {
  370. continue;
  371. }
  372. fp=fopen(fullfilename.c_str(), "ab");
  373. }
  374. if (nullptr == fp)
  375. {
  376. addResponse(recvDataBuf.fd, filename, runtime::FileSendComplete::RESULTTYPE::FileSendComplete_RESULTTYPE_FOPEN_ERROR, errno);
  377. continue;
  378. }
  379. if (fp)
  380. {
  381. if (recvDataBuf.contentBuf.size() > 0 && 0 == fwrite(recvDataBuf.contentBuf.c_str(), sizeof(char), recvDataBuf.contentBuf.size(), fp))
  382. {
  383. addResponse(recvDataBuf.fd, filename, runtime::FileSendComplete::RESULTTYPE::FileSendComplete_RESULTTYPE_FWRITE_ERROR, errno);
  384. fclose(fp);
  385. continue;
  386. }
  387. fclose(fp);
  388. }
  389. if (1 == recvDataBuf.fileProto.package_seq())
  390. {
  391. //record new file modify
  392. addResFileInfo(filename.c_str(), recvDataBuf.fileProto.modified_time());
  393. addResponse(recvDataBuf.fd, filename, runtime::FileSendComplete::RESULTTYPE::FileSendComplete_RESULTTYPE_SUCCESS, 0);
  394. }
  395. }
  396. _writeRunning = false;
  397. }
  398. void FileServer::addResponse(int fd, std::string filename, int errortype, int errornum)
  399. {
  400. switch (errortype)
  401. {
  402. case runtime::FileSendComplete::RESULTTYPE::FileSendComplete_RESULTTYPE_UNCOMPRESS_ERROR:
  403. case runtime::FileSendComplete::RESULTTYPE::FileSendComplete_RESULTTYPE_RECV_ERROR:
  404. _recvErrorFile = filename;
  405. break;
  406. case runtime::FileSendComplete::RESULTTYPE::FileSendComplete_RESULTTYPE_FOPEN_ERROR:
  407. case runtime::FileSendComplete::RESULTTYPE::FileSendComplete_RESULTTYPE_FWRITE_ERROR:
  408. _writeErrorFile = filename;
  409. break;
  410. default:
  411. break;
  412. }
  413. ResponseStruct responseBuf;
  414. responseBuf.fd = fd;
  415. responseBuf.fileResponseProto.set_file_name(filename.c_str());
  416. responseBuf.fileResponseProto.set_result((::runtime::FileSendComplete_RESULTTYPE)errortype);
  417. responseBuf.fileResponseProto.set_error_num(errornum);
  418. // push Response struct
  419. _responseBufListMutex.lock();
  420. _responseBufList.push_back(responseBuf);
  421. _responseBufListMutex.unlock();
  422. }
  423. void FileServer::loopResponse()
  424. {
  425. _responseRunning = true;
  426. while(!_responseEndThread) {
  427. _responseBufListMutex.lock();
  428. size_t responseSize = _responseBufList.size();
  429. _responseBufListMutex.unlock();
  430. if(0 == responseSize)
  431. {
  432. usleep(500);
  433. /* error */
  434. continue;
  435. }
  436. _responseBufListMutex.lock();
  437. ResponseStruct responseBuf = _responseBufList.front();
  438. _responseBufList.pop_front();
  439. _responseBufListMutex.unlock();
  440. //send response
  441. std::string responseString;
  442. runtime::FileSendComplete fileSendProtoComplete;
  443. fileSendProtoComplete.set_file_name(responseBuf.fileResponseProto.file_name());
  444. fileSendProtoComplete.set_result(responseBuf.fileResponseProto.result());
  445. fileSendProtoComplete.set_error_num(responseBuf.fileResponseProto.error_num());
  446. fileSendProtoComplete.SerializeToString(&responseString);
  447. char dataBuf[1024] = {0};
  448. struct ResponseHeaderStruct
  449. {
  450. char startFlag[13]; // needs to store PROTO_START, which is 12+NULL long
  451. unsigned short protoNum;
  452. unsigned short protoBufLen;
  453. };
  454. ResponseHeaderStruct responseHeader;
  455. strcpy(responseHeader.startFlag, PROTO_START);
  456. responseHeader.protoNum = PROTONUM::FILESENDCOMPLETE;
  457. responseHeader.protoBufLen = (unsigned short) responseString.size();
  458. memcpy(dataBuf, &responseHeader, sizeof(responseHeader));
  459. memcpy(dataBuf + sizeof(responseHeader), responseString.c_str(), responseString.size());
  460. sendBuf(responseBuf.fd, dataBuf, sizeof(responseHeader) + responseString.size());
  461. cocos2d::log("responseFile:%s,result:%d", fileSendProtoComplete.file_name().c_str(), fileSendProtoComplete.result());
  462. }
  463. _responseRunning = false;
  464. }
  465. bool createDir(const char *sPathName)
  466. {
  467. char DirName[256]={0};
  468. strcpy(DirName, sPathName);
  469. size_t i, len = strlen(DirName);
  470. if(DirName[len - 1] != '/')
  471. {
  472. strcat(DirName, "/");
  473. }
  474. len = strlen(DirName);
  475. for(i = 1; i < len; i++)
  476. {
  477. if(DirName[i] == '/')
  478. {
  479. DirName[i] = 0;
  480. #ifdef _WIN32
  481. if(_access(DirName, 0) != 0)
  482. {
  483. if(_mkdir(DirName/*, 0755*/) == -1)
  484. #else
  485. if (access(DirName, 0) != 0)
  486. {
  487. if(mkdir(DirName, 0755) == -1)
  488. #endif
  489. {
  490. perror("mkdir error");
  491. return false;
  492. }
  493. }
  494. DirName[i] = '/';
  495. }
  496. }
  497. return true;
  498. }