editor_file_server.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. #include "editor_file_server.h"
  2. #include "io/marshalls.h"
  3. #include "io/marshalls.h"
  4. #include "../editor_settings.h"
  5. //#define DEBUG_PRINT(m_p) print_line(m_p)
  6. #define DEBUG_TIME(m_what) printf("MS: %s - %lli\n",m_what,OS::get_singleton()->get_ticks_usec());
  7. //#define DEBUG_TIME(m_what)
  8. void EditorFileServer::_close_client(ClientData *cd) {
  9. cd->connection->disconnect();
  10. cd->efs->wait_mutex->lock();
  11. cd->efs->to_wait.insert(cd->thread);
  12. cd->efs->wait_mutex->unlock();
  13. while(cd->files.size()) {
  14. memdelete(cd->files.front()->get());
  15. cd->files.erase(cd->files.front());
  16. }
  17. memdelete(cd);
  18. }
  19. void EditorFileServer::_subthread_start(void*s) {
  20. ClientData *cd = (ClientData*)s;
  21. cd->connection->set_nodelay(true);
  22. uint8_t buf4[8];
  23. Error err = cd->connection->get_data(buf4,4);
  24. if (err!=OK) {
  25. _close_client(cd);
  26. ERR_FAIL_COND(err!=OK);
  27. }
  28. int passlen=decode_uint32(buf4);
  29. if (passlen>512) {
  30. _close_client(cd);
  31. ERR_FAIL_COND(passlen>512);
  32. } else if (passlen>0) {
  33. Vector<char> passutf8;
  34. passutf8.resize(passlen+1);
  35. err = cd->connection->get_data((uint8_t*)passutf8.ptr(),passlen);
  36. if (err!=OK) {
  37. _close_client(cd);
  38. ERR_FAIL_COND(err!=OK);
  39. }
  40. passutf8[passlen]=0;
  41. String s;
  42. s.parse_utf8(passutf8.ptr());
  43. if (s!=cd->efs->password) {
  44. encode_uint32(ERR_INVALID_DATA,buf4);
  45. cd->connection->put_data(buf4,4);
  46. OS::get_singleton()->delay_usec(1000000);
  47. _close_client(cd);
  48. ERR_PRINT("CLIENT PASSWORD MISMATCH");
  49. ERR_FAIL();
  50. }
  51. } else {
  52. if (cd->efs->password!="") {
  53. encode_uint32(ERR_INVALID_DATA,buf4);
  54. cd->connection->put_data(buf4,4);
  55. OS::get_singleton()->delay_usec(1000000);
  56. _close_client(cd);
  57. ERR_PRINT("CLIENT PASSWORD MISMATCH (should be empty!)");
  58. ERR_FAIL();
  59. }
  60. }
  61. encode_uint32(OK,buf4);
  62. cd->connection->put_data(buf4,4);
  63. while(!cd->quit) {
  64. //wait for ID
  65. err = cd->connection->get_data(buf4,4);
  66. //#define DEBUG_PRINT(m_p) print_line(m_p)
  67. DEBUG_TIME("get_data")
  68. if (err!=OK) {
  69. _close_client(cd);
  70. ERR_FAIL_COND(err!=OK);
  71. }
  72. int id=decode_uint32(buf4);
  73. //wait for command
  74. err = cd->connection->get_data(buf4,4);
  75. if (err!=OK) {
  76. _close_client(cd);
  77. ERR_FAIL_COND(err!=OK);
  78. }
  79. int cmd=decode_uint32(buf4);
  80. switch(cmd) {
  81. case FileAccessNetwork::COMMAND_FILE_EXISTS:
  82. case FileAccessNetwork::COMMAND_GET_MODTIME:
  83. case FileAccessNetwork::COMMAND_OPEN_FILE: {
  84. DEBUG_TIME("open_file")
  85. err = cd->connection->get_data(buf4,4);
  86. if (err!=OK) {
  87. _close_client(cd);
  88. ERR_FAIL_COND(err!=OK);
  89. }
  90. int namelen=decode_uint32(buf4);
  91. Vector<char> fileutf8;
  92. fileutf8.resize(namelen+1);
  93. err = cd->connection->get_data((uint8_t*)fileutf8.ptr(),namelen);
  94. if (err!=OK) {
  95. _close_client(cd);
  96. ERR_FAIL_COND(err!=OK);
  97. }
  98. fileutf8[namelen]=0;
  99. String s;
  100. s.parse_utf8(fileutf8.ptr());
  101. if (cmd==FileAccessNetwork::COMMAND_FILE_EXISTS) {
  102. print_line("FILE EXISTS: "+s);
  103. }
  104. if (cmd==FileAccessNetwork::COMMAND_GET_MODTIME) {
  105. print_line("MOD TIME: "+s);
  106. }
  107. if (cmd==FileAccessNetwork::COMMAND_OPEN_FILE) {
  108. print_line("OPEN: "+s);
  109. }
  110. if ( !s.begins_with("res://")) {
  111. _close_client(cd);
  112. ERR_FAIL_COND(!s.begins_with("res://"));
  113. }
  114. ERR_CONTINUE(cd->files.has(id));
  115. if (cmd==FileAccessNetwork::COMMAND_FILE_EXISTS) {
  116. encode_uint32(id,buf4);
  117. cd->connection->put_data(buf4,4);
  118. encode_uint32(FileAccessNetwork::RESPONSE_FILE_EXISTS,buf4);
  119. cd->connection->put_data(buf4,4);
  120. encode_uint32(FileAccess::exists(s),buf4);
  121. cd->connection->put_data(buf4,4);
  122. DEBUG_TIME("open_file_end")
  123. break;
  124. }
  125. if (cmd==FileAccessNetwork::COMMAND_GET_MODTIME) {
  126. encode_uint32(id,buf4);
  127. cd->connection->put_data(buf4,4);
  128. encode_uint32(FileAccessNetwork::RESPONSE_GET_MODTIME,buf4);
  129. cd->connection->put_data(buf4,4);
  130. encode_uint64(FileAccess::get_modified_time(s),buf4);
  131. cd->connection->put_data(buf4,8);
  132. DEBUG_TIME("open_file_end")
  133. break;
  134. }
  135. FileAccess *fa = FileAccess::open(s,FileAccess::READ);
  136. if (!fa) {
  137. //not found, continue
  138. encode_uint32(id,buf4);
  139. cd->connection->put_data(buf4,4);
  140. encode_uint32(FileAccessNetwork::RESPONSE_OPEN,buf4);
  141. cd->connection->put_data(buf4,4);
  142. encode_uint32(ERR_FILE_NOT_FOUND,buf4);
  143. cd->connection->put_data(buf4,4);
  144. DEBUG_TIME("open_file_end")
  145. break;
  146. }
  147. encode_uint32(id,buf4);
  148. cd->connection->put_data(buf4,4);
  149. encode_uint32(FileAccessNetwork::RESPONSE_OPEN,buf4);
  150. cd->connection->put_data(buf4,4);
  151. encode_uint32(OK,buf4);
  152. cd->connection->put_data(buf4,4);
  153. encode_uint64(fa->get_len(),buf4);
  154. cd->connection->put_data(buf4,8);
  155. cd->files[id]=fa;
  156. DEBUG_TIME("open_file_end")
  157. } break;
  158. case FileAccessNetwork::COMMAND_READ_BLOCK: {
  159. err = cd->connection->get_data(buf4,8);
  160. if (err!=OK) {
  161. _close_client(cd);
  162. ERR_FAIL_COND(err!=OK);
  163. }
  164. ERR_CONTINUE(!cd->files.has(id));
  165. uint64_t offset = decode_uint64(buf4);
  166. err = cd->connection->get_data(buf4,4);
  167. if (err!=OK) {
  168. _close_client(cd);
  169. ERR_FAIL_COND(err!=OK);
  170. }
  171. int blocklen=decode_uint32(buf4);
  172. ERR_CONTINUE(blocklen > (16*1024*1024));
  173. cd->files[id]->seek(offset);
  174. Vector<uint8_t> buf;
  175. buf.resize(blocklen);
  176. int read = cd->files[id]->get_buffer(buf.ptr(),blocklen);
  177. ERR_CONTINUE(read<0);
  178. print_line("GET BLOCK - offset: "+itos(offset)+", blocklen: "+itos(blocklen));
  179. //not found, continue
  180. encode_uint32(id,buf4);
  181. cd->connection->put_data(buf4,4);
  182. encode_uint32(FileAccessNetwork::RESPONSE_DATA,buf4);
  183. cd->connection->put_data(buf4,4);
  184. encode_uint64(offset,buf4);
  185. cd->connection->put_data(buf4,8);
  186. encode_uint32(read,buf4);
  187. cd->connection->put_data(buf4,4);
  188. cd->connection->put_data(buf.ptr(),read);
  189. } break;
  190. case FileAccessNetwork::COMMAND_CLOSE: {
  191. print_line("CLOSED");
  192. ERR_CONTINUE(!cd->files.has(id));
  193. memdelete(cd->files[id]);
  194. cd->files.erase(id);
  195. } break;
  196. }
  197. }
  198. _close_client(cd);
  199. }
  200. void EditorFileServer::_thread_start(void*s) {
  201. EditorFileServer *self=(EditorFileServer*)s;
  202. while(!self->quit) {
  203. if (self->cmd==CMD_ACTIVATE) {
  204. self->server->listen(self->port);
  205. self->active=true;
  206. self->cmd=CMD_NONE;
  207. } else if (self->cmd==CMD_STOP) {
  208. self->server->stop();
  209. self->active=false;
  210. self->cmd=CMD_NONE;
  211. }
  212. if (self->active) {
  213. if (self->server->is_connection_available()) {
  214. ClientData *cd = memnew( ClientData);
  215. cd->connection=self->server->take_connection();
  216. cd->efs=self;
  217. cd->quit=false;
  218. cd->thread=Thread::create(_subthread_start,cd);
  219. }
  220. }
  221. self->wait_mutex->lock();
  222. while (self->to_wait.size()) {
  223. Thread *w = self->to_wait.front()->get();
  224. self->to_wait.erase(w);
  225. self->wait_mutex->unlock();
  226. Thread::wait_to_finish(w);
  227. self->wait_mutex->lock();
  228. }
  229. self->wait_mutex->unlock();
  230. OS::get_singleton()->delay_usec(100000);
  231. }
  232. }
  233. void EditorFileServer::start() {
  234. stop();
  235. port=EDITOR_DEF("file_server/port",6010);
  236. password=EDITOR_DEF("file_server/password","");
  237. cmd=CMD_ACTIVATE;
  238. }
  239. bool EditorFileServer::is_active() const {
  240. return active;
  241. }
  242. void EditorFileServer::stop(){
  243. cmd=CMD_STOP;
  244. }
  245. EditorFileServer::EditorFileServer() {
  246. server = TCP_Server::create_ref();
  247. wait_mutex = Mutex::create();
  248. quit=false;
  249. active=false;
  250. cmd=CMD_NONE;
  251. thread=Thread::create(_thread_start,this);
  252. List<IP_Address> local_ip;
  253. IP::get_singleton()->get_local_addresses(&local_ip);
  254. EDITOR_DEF("file_server/port",6010);
  255. String lip;
  256. String hint;
  257. for(List<IP_Address>::Element *E=local_ip.front();E;E=E->next()) {
  258. String ip = E->get();
  259. if (ip=="127.0.0.1")
  260. continue;
  261. if (lip!="")
  262. lip=ip;
  263. if (hint!="")
  264. hint+=",";
  265. hint+=ip;
  266. }
  267. EDITOR_DEF("file_server/host",lip);
  268. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING,"file_server/host",PROPERTY_HINT_ENUM,hint));
  269. EDITOR_DEF("file_server/password","");
  270. }
  271. EditorFileServer::~EditorFileServer() {
  272. quit=true;
  273. Thread::wait_to_finish(thread);
  274. memdelete(wait_mutex);
  275. }