editor_file_server.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*************************************************************************/
  2. /* editor_file_server.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "editor_file_server.h"
  31. #include "../editor_settings.h"
  32. #include "core/io/marshalls.h"
  33. //#define DEBUG_PRINT(m_p) print_line(m_p)
  34. //#define DEBUG_TIME(m_what) printf("MS: %s - %lu\n", m_what, OS::get_singleton()->get_ticks_usec());
  35. #define DEBUG_PRINT(m_what)
  36. #define DEBUG_TIME(m_what)
  37. void EditorFileServer::_close_client(ClientData *cd) {
  38. cd->connection->disconnect_from_host();
  39. {
  40. MutexLock lock(cd->efs->wait_mutex);
  41. cd->efs->to_wait.insert(cd->thread);
  42. }
  43. while (cd->files.size()) {
  44. memdelete(cd->files.front()->get());
  45. cd->files.erase(cd->files.front());
  46. }
  47. memdelete(cd);
  48. }
  49. void EditorFileServer::_subthread_start(void *s) {
  50. ClientData *cd = (ClientData *)s;
  51. cd->connection->set_no_delay(true);
  52. uint8_t buf4[8];
  53. Error err = cd->connection->get_data(buf4, 4);
  54. if (err != OK) {
  55. _close_client(cd);
  56. ERR_FAIL_COND(err != OK);
  57. }
  58. int passlen = decode_uint32(buf4);
  59. if (passlen > 512) {
  60. _close_client(cd);
  61. ERR_FAIL_COND(passlen > 512);
  62. } else if (passlen > 0) {
  63. Vector<char> passutf8;
  64. passutf8.resize(passlen + 1);
  65. err = cd->connection->get_data((uint8_t *)passutf8.ptr(), passlen);
  66. if (err != OK) {
  67. _close_client(cd);
  68. ERR_FAIL_COND(err != OK);
  69. }
  70. passutf8.write[passlen] = 0;
  71. String s2;
  72. s2.parse_utf8(passutf8.ptr());
  73. if (s2 != cd->efs->password) {
  74. encode_uint32(ERR_INVALID_DATA, buf4);
  75. cd->connection->put_data(buf4, 4);
  76. OS::get_singleton()->delay_usec(1000000);
  77. _close_client(cd);
  78. ERR_PRINT("CLIENT PASSWORD MISMATCH");
  79. ERR_FAIL();
  80. }
  81. } else {
  82. if (cd->efs->password != "") {
  83. encode_uint32(ERR_INVALID_DATA, buf4);
  84. cd->connection->put_data(buf4, 4);
  85. OS::get_singleton()->delay_usec(1000000);
  86. _close_client(cd);
  87. ERR_PRINT("CLIENT PASSWORD MISMATCH (should be empty!)");
  88. ERR_FAIL();
  89. }
  90. }
  91. encode_uint32(OK, buf4);
  92. cd->connection->put_data(buf4, 4);
  93. while (!cd->quit) {
  94. //wait for ID
  95. err = cd->connection->get_data(buf4, 4);
  96. DEBUG_TIME("get_data")
  97. if (err != OK) {
  98. _close_client(cd);
  99. ERR_FAIL_COND(err != OK);
  100. }
  101. int id = decode_uint32(buf4);
  102. //wait for command
  103. err = cd->connection->get_data(buf4, 4);
  104. if (err != OK) {
  105. _close_client(cd);
  106. ERR_FAIL_COND(err != OK);
  107. }
  108. int cmd = decode_uint32(buf4);
  109. switch (cmd) {
  110. case FileAccessNetwork::COMMAND_FILE_EXISTS:
  111. case FileAccessNetwork::COMMAND_GET_MODTIME:
  112. case FileAccessNetwork::COMMAND_OPEN_FILE: {
  113. DEBUG_TIME("open_file")
  114. err = cd->connection->get_data(buf4, 4);
  115. if (err != OK) {
  116. _close_client(cd);
  117. ERR_FAIL_COND(err != OK);
  118. }
  119. int namelen = decode_uint32(buf4);
  120. Vector<char> fileutf8;
  121. fileutf8.resize(namelen + 1);
  122. err = cd->connection->get_data((uint8_t *)fileutf8.ptr(), namelen);
  123. if (err != OK) {
  124. _close_client(cd);
  125. ERR_FAIL_COND(err != OK);
  126. }
  127. fileutf8.write[namelen] = 0;
  128. String s2;
  129. s2.parse_utf8(fileutf8.ptr());
  130. if (cmd == FileAccessNetwork::COMMAND_FILE_EXISTS) {
  131. print_verbose("FILE EXISTS: " + s2);
  132. }
  133. if (cmd == FileAccessNetwork::COMMAND_GET_MODTIME) {
  134. print_verbose("MOD TIME: " + s2);
  135. }
  136. if (cmd == FileAccessNetwork::COMMAND_OPEN_FILE) {
  137. print_verbose("OPEN: " + s2);
  138. }
  139. if (!s2.begins_with("res://")) {
  140. _close_client(cd);
  141. ERR_FAIL_COND(!s2.begins_with("res://"));
  142. }
  143. ERR_CONTINUE(cd->files.has(id));
  144. if (cmd == FileAccessNetwork::COMMAND_FILE_EXISTS) {
  145. encode_uint32(id, buf4);
  146. cd->connection->put_data(buf4, 4);
  147. encode_uint32(FileAccessNetwork::RESPONSE_FILE_EXISTS, buf4);
  148. cd->connection->put_data(buf4, 4);
  149. encode_uint32(FileAccess::exists(s2), buf4);
  150. cd->connection->put_data(buf4, 4);
  151. DEBUG_TIME("open_file_end")
  152. break;
  153. }
  154. if (cmd == FileAccessNetwork::COMMAND_GET_MODTIME) {
  155. encode_uint32(id, buf4);
  156. cd->connection->put_data(buf4, 4);
  157. encode_uint32(FileAccessNetwork::RESPONSE_GET_MODTIME, buf4);
  158. cd->connection->put_data(buf4, 4);
  159. encode_uint64(FileAccess::get_modified_time(s2), buf4);
  160. cd->connection->put_data(buf4, 8);
  161. DEBUG_TIME("open_file_end")
  162. break;
  163. }
  164. FileAccess *fa = FileAccess::open(s2, FileAccess::READ);
  165. if (!fa) {
  166. //not found, continue
  167. encode_uint32(id, buf4);
  168. cd->connection->put_data(buf4, 4);
  169. encode_uint32(FileAccessNetwork::RESPONSE_OPEN, buf4);
  170. cd->connection->put_data(buf4, 4);
  171. encode_uint32(ERR_FILE_NOT_FOUND, buf4);
  172. cd->connection->put_data(buf4, 4);
  173. DEBUG_TIME("open_file_end")
  174. break;
  175. }
  176. encode_uint32(id, buf4);
  177. cd->connection->put_data(buf4, 4);
  178. encode_uint32(FileAccessNetwork::RESPONSE_OPEN, buf4);
  179. cd->connection->put_data(buf4, 4);
  180. encode_uint32(OK, buf4);
  181. cd->connection->put_data(buf4, 4);
  182. encode_uint64(fa->get_len(), buf4);
  183. cd->connection->put_data(buf4, 8);
  184. cd->files[id] = fa;
  185. DEBUG_TIME("open_file_end")
  186. } break;
  187. case FileAccessNetwork::COMMAND_READ_BLOCK: {
  188. err = cd->connection->get_data(buf4, 8);
  189. if (err != OK) {
  190. _close_client(cd);
  191. ERR_FAIL_COND(err != OK);
  192. }
  193. ERR_CONTINUE(!cd->files.has(id));
  194. uint64_t offset = decode_uint64(buf4);
  195. err = cd->connection->get_data(buf4, 4);
  196. if (err != OK) {
  197. _close_client(cd);
  198. ERR_FAIL_COND(err != OK);
  199. }
  200. int blocklen = decode_uint32(buf4);
  201. ERR_CONTINUE(blocklen > (16 * 1024 * 1024));
  202. cd->files[id]->seek(offset);
  203. Vector<uint8_t> buf;
  204. buf.resize(blocklen);
  205. uint32_t read = cd->files[id]->get_buffer(buf.ptrw(), blocklen);
  206. print_verbose("GET BLOCK - offset: " + itos(offset) + ", blocklen: " + itos(blocklen));
  207. //not found, continue
  208. encode_uint32(id, buf4);
  209. cd->connection->put_data(buf4, 4);
  210. encode_uint32(FileAccessNetwork::RESPONSE_DATA, buf4);
  211. cd->connection->put_data(buf4, 4);
  212. encode_uint64(offset, buf4);
  213. cd->connection->put_data(buf4, 8);
  214. encode_uint32(read, buf4);
  215. cd->connection->put_data(buf4, 4);
  216. cd->connection->put_data(buf.ptr(), read);
  217. } break;
  218. case FileAccessNetwork::COMMAND_CLOSE: {
  219. print_verbose("CLOSED");
  220. ERR_CONTINUE(!cd->files.has(id));
  221. memdelete(cd->files[id]);
  222. cd->files.erase(id);
  223. } break;
  224. }
  225. }
  226. _close_client(cd);
  227. }
  228. void EditorFileServer::_thread_start(void *s) {
  229. EditorFileServer *self = (EditorFileServer *)s;
  230. while (!self->quit) {
  231. if (self->cmd == CMD_ACTIVATE) {
  232. self->server->listen(self->port);
  233. self->active = true;
  234. self->cmd = CMD_NONE;
  235. } else if (self->cmd == CMD_STOP) {
  236. self->server->stop();
  237. self->active = false;
  238. self->cmd = CMD_NONE;
  239. }
  240. if (self->active) {
  241. if (self->server->is_connection_available()) {
  242. ClientData *cd = memnew(ClientData);
  243. cd->connection = self->server->take_connection();
  244. cd->efs = self;
  245. cd->quit = false;
  246. cd->thread = memnew(Thread);
  247. cd->thread->start(_subthread_start, cd);
  248. }
  249. }
  250. self->wait_mutex.lock();
  251. while (self->to_wait.size()) {
  252. Thread *w = self->to_wait.front()->get();
  253. self->to_wait.erase(w);
  254. self->wait_mutex.unlock();
  255. w->wait_to_finish();
  256. self->wait_mutex.lock();
  257. }
  258. self->wait_mutex.unlock();
  259. OS::get_singleton()->delay_usec(100000);
  260. }
  261. }
  262. void EditorFileServer::start() {
  263. stop();
  264. port = EDITOR_DEF("filesystem/file_server/port", 6010);
  265. password = EDITOR_DEF("filesystem/file_server/password", "");
  266. cmd = CMD_ACTIVATE;
  267. }
  268. bool EditorFileServer::is_active() const {
  269. return active;
  270. }
  271. void EditorFileServer::stop() {
  272. cmd = CMD_STOP;
  273. }
  274. EditorFileServer::EditorFileServer() {
  275. server.instance();
  276. quit = false;
  277. active = false;
  278. cmd = CMD_NONE;
  279. thread.start(_thread_start, this);
  280. EDITOR_DEF("filesystem/file_server/port", 6010);
  281. EDITOR_DEF("filesystem/file_server/password", "");
  282. }
  283. EditorFileServer::~EditorFileServer() {
  284. quit = true;
  285. thread.wait_to_finish();
  286. }