FileServer.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #ifndef _FILE_SERVER__H_
  21. #define _FILE_SERVER__H_
  22. #include "cocos2d.h"
  23. #include "json/document-wrapper.h"
  24. #include "json/filereadstream.h"
  25. #include "json/stringbuffer.h"
  26. #include "json/writer.h"
  27. #include "Protos.pb.h"
  28. #include <string>
  29. #include "SimulatorExport.h"
  30. // header files for socket
  31. #ifdef _WIN32
  32. #include <io.h>
  33. #include <WS2tcpip.h>
  34. #define bzero(a, b) memset(a, 0, b);
  35. #else
  36. #include <netdb.h>
  37. #include <arpa/inet.h>
  38. #include <netinet/in.h>
  39. #include <sys/socket.h>
  40. #endif
  41. #ifdef _WIN32
  42. #define usleep(t) Sleep(t)
  43. #else
  44. #include <unistd.h>
  45. #endif
  46. class CC_LIBSIM_DLL FileServer
  47. {
  48. static FileServer *s_sharedFileServer;
  49. public:
  50. static FileServer* getShareInstance();
  51. static void purge();
  52. bool listenOnTCP(int port);
  53. void stop();
  54. void readResFileFinfo();
  55. void addResFileInfo(const char* filename,uint64_t u64);
  56. void removeResFileInfo(const char *filename);
  57. rapidjson::Document* getFileCfgJson() { return &_filecfgjson; }
  58. bool getIsUsingWritePath() { return _isUsingWritePath; }
  59. void setIsUsingWritePath(bool use) { _isUsingWritePath = use; }
  60. std::string getWritePath() { return _writePath; }
  61. std::string getTransingFileName();
  62. void setTransingFileName(const std::string& filename);
  63. protected:
  64. FileServer();
  65. ~FileServer();
  66. private:
  67. void loopReceiveFile();
  68. void loopWriteFile();
  69. void loopResponse();
  70. void addResponse(int fd, std::string filename,int errortype,int errornum);
  71. enum PROTONUM
  72. {
  73. FILEPROTO = 1,
  74. FILESENDCOMPLETE = 2,
  75. DIRPROTO = 3,
  76. DIRSENDCOMPLETE = 4
  77. };
  78. struct RecvBufStruct
  79. {
  80. runtime::FileSendProtos fileProto;
  81. std::string contentBuf;
  82. int fd;
  83. };
  84. struct ResponseStruct
  85. {
  86. runtime::FileSendComplete fileResponseProto;
  87. int fd;
  88. };
  89. // file descriptor: socket, console, etc.
  90. int _listenfd;
  91. std::thread _responseThread;
  92. std::thread _receiveThread;
  93. std::thread _writeThread;
  94. bool _receiveRunning;
  95. bool _receiveEndThread;
  96. bool _writeRunning;
  97. bool _writeEndThread;
  98. bool _responseRunning;
  99. bool _responseEndThread;
  100. std::list<RecvBufStruct> _recvBufList;
  101. std::list<ResponseStruct> _responseBufList;
  102. std::mutex _recvBufListMutex;
  103. std::mutex _responseBufListMutex;
  104. rapidjson::Document _filecfgjson;
  105. std::string _strFileName;
  106. std::mutex _fileNameMutex;
  107. std::string _recvErrorFile;
  108. std::string _writeErrorFile;
  109. bool _isUsingWritePath;
  110. std::string _writePath;
  111. };
  112. bool createDir(const char *sPathName);
  113. #endif // _FILE_SERVER__H_