editor_file_server.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. cd->files.erase(cd->files.front());
  45. }
  46. memdelete(cd);
  47. }
  48. void EditorFileServer::_subthread_start(void *s) {
  49. ClientData *cd = static_cast<ClientData *>(s);
  50. cd->connection->set_no_delay(true);
  51. uint8_t buf4[8];
  52. Error err = cd->connection->get_data(buf4, 4);
  53. if (err != OK) {
  54. _close_client(cd);
  55. ERR_FAIL_COND(err != OK);
  56. }
  57. int passlen = decode_uint32(buf4);
  58. if (passlen > 512) {
  59. _close_client(cd);
  60. ERR_FAIL_COND(passlen > 512);
  61. } else if (passlen > 0) {
  62. Vector<char> passutf8;
  63. passutf8.resize(passlen + 1);
  64. err = cd->connection->get_data((uint8_t *)passutf8.ptr(), passlen);
  65. if (err != OK) {
  66. _close_client(cd);
  67. ERR_FAIL_COND(err != OK);
  68. }
  69. passutf8.write[passlen] = 0;
  70. String s2;
  71. s2.parse_utf8(passutf8.ptr());
  72. if (s2 != cd->efs->password) {
  73. encode_uint32(ERR_INVALID_DATA, buf4);
  74. cd->connection->put_data(buf4, 4);
  75. OS::get_singleton()->delay_usec(1000000);
  76. _close_client(cd);
  77. ERR_PRINT("CLIENT PASSWORD MISMATCH");
  78. ERR_FAIL();
  79. }
  80. } else {
  81. if (!cd->efs->password.is_empty()) {
  82. encode_uint32(ERR_INVALID_DATA, buf4);
  83. cd->connection->put_data(buf4, 4);
  84. OS::get_singleton()->delay_usec(1000000);
  85. _close_client(cd);
  86. ERR_PRINT("CLIENT PASSWORD MISMATCH (should be empty!)");
  87. ERR_FAIL();
  88. }
  89. }
  90. encode_uint32(OK, buf4);
  91. cd->connection->put_data(buf4, 4);
  92. while (!cd->quit) {
  93. //wait for ID
  94. err = cd->connection->get_data(buf4, 4);
  95. DEBUG_TIME("get_data")
  96. if (err != OK) {
  97. _close_client(cd);
  98. ERR_FAIL_COND(err != OK);
  99. }
  100. int id = decode_uint32(buf4);
  101. //wait for command
  102. err = cd->connection->get_data(buf4, 4);
  103. if (err != OK) {
  104. _close_client(cd);
  105. ERR_FAIL_COND(err != OK);
  106. }
  107. int cmd = decode_uint32(buf4);
  108. switch (cmd) {
  109. case FileAccessNetwork::COMMAND_FILE_EXISTS:
  110. case FileAccessNetwork::COMMAND_GET_MODTIME:
  111. case FileAccessNetwork::COMMAND_OPEN_FILE: {
  112. DEBUG_TIME("open_file")
  113. err = cd->connection->get_data(buf4, 4);
  114. if (err != OK) {
  115. _close_client(cd);
  116. ERR_FAIL_COND(err != OK);
  117. }
  118. int namelen = decode_uint32(buf4);
  119. Vector<char> fileutf8;
  120. fileutf8.resize(namelen + 1);
  121. err = cd->connection->get_data((uint8_t *)fileutf8.ptr(), namelen);
  122. if (err != OK) {
  123. _close_client(cd);
  124. ERR_FAIL_COND(err != OK);
  125. }
  126. fileutf8.write[namelen] = 0;
  127. String s2;
  128. s2.parse_utf8(fileutf8.ptr());
  129. if (cmd == FileAccessNetwork::COMMAND_FILE_EXISTS) {
  130. print_verbose("FILE EXISTS: " + s2);
  131. }
  132. if (cmd == FileAccessNetwork::COMMAND_GET_MODTIME) {
  133. print_verbose("MOD TIME: " + s2);
  134. }
  135. if (cmd == FileAccessNetwork::COMMAND_OPEN_FILE) {
  136. print_verbose("OPEN: " + s2);
  137. }
  138. if (!s2.begins_with("res://")) {
  139. _close_client(cd);
  140. ERR_FAIL_COND(!s2.begins_with("res://"));
  141. }
  142. ERR_CONTINUE(cd->files.has(id));
  143. if (cmd == FileAccessNetwork::COMMAND_FILE_EXISTS) {
  144. encode_uint32(id, buf4);
  145. cd->connection->put_data(buf4, 4);
  146. encode_uint32(FileAccessNetwork::RESPONSE_FILE_EXISTS, buf4);
  147. cd->connection->put_data(buf4, 4);
  148. encode_uint32(FileAccess::exists(s2), buf4);
  149. cd->connection->put_data(buf4, 4);
  150. DEBUG_TIME("open_file_end")
  151. break;
  152. }
  153. if (cmd == FileAccessNetwork::COMMAND_GET_MODTIME) {
  154. encode_uint32(id, buf4);
  155. cd->connection->put_data(buf4, 4);
  156. encode_uint32(FileAccessNetwork::RESPONSE_GET_MODTIME, buf4);
  157. cd->connection->put_data(buf4, 4);
  158. encode_uint64(FileAccess::get_modified_time(s2), buf4);
  159. cd->connection->put_data(buf4, 8);
  160. DEBUG_TIME("open_file_end")
  161. break;
  162. }
  163. Ref<FileAccess> fa = FileAccess::open(s2, FileAccess::READ);
  164. if (fa.is_null()) {
  165. //not found, continue
  166. encode_uint32(id, buf4);
  167. cd->connection->put_data(buf4, 4);
  168. encode_uint32(FileAccessNetwork::RESPONSE_OPEN, buf4);
  169. cd->connection->put_data(buf4, 4);
  170. encode_uint32(ERR_FILE_NOT_FOUND, buf4);
  171. cd->connection->put_data(buf4, 4);
  172. DEBUG_TIME("open_file_end")
  173. break;
  174. }
  175. encode_uint32(id, buf4);
  176. cd->connection->put_data(buf4, 4);
  177. encode_uint32(FileAccessNetwork::RESPONSE_OPEN, buf4);
  178. cd->connection->put_data(buf4, 4);
  179. encode_uint32(OK, buf4);
  180. cd->connection->put_data(buf4, 4);
  181. encode_uint64(fa->get_length(), buf4);
  182. cd->connection->put_data(buf4, 8);
  183. cd->files[id] = fa;
  184. DEBUG_TIME("open_file_end")
  185. } break;
  186. case FileAccessNetwork::COMMAND_READ_BLOCK: {
  187. err = cd->connection->get_data(buf4, 8);
  188. if (err != OK) {
  189. _close_client(cd);
  190. ERR_FAIL_COND(err != OK);
  191. }
  192. ERR_CONTINUE(!cd->files.has(id));
  193. uint64_t offset = decode_uint64(buf4);
  194. err = cd->connection->get_data(buf4, 4);
  195. if (err != OK) {
  196. _close_client(cd);
  197. ERR_FAIL_COND(err != OK);
  198. }
  199. int blocklen = decode_uint32(buf4);
  200. ERR_CONTINUE(blocklen > (16 * 1024 * 1024));
  201. cd->files[id]->seek(offset);
  202. Vector<uint8_t> buf;
  203. buf.resize(blocklen);
  204. uint32_t read = cd->files[id]->get_buffer(buf.ptrw(), blocklen);
  205. print_verbose("GET BLOCK - offset: " + itos(offset) + ", blocklen: " + itos(blocklen));
  206. //not found, continue
  207. encode_uint32(id, buf4);
  208. cd->connection->put_data(buf4, 4);
  209. encode_uint32(FileAccessNetwork::RESPONSE_DATA, buf4);
  210. cd->connection->put_data(buf4, 4);
  211. encode_uint64(offset, buf4);
  212. cd->connection->put_data(buf4, 8);
  213. encode_uint32(read, buf4);
  214. cd->connection->put_data(buf4, 4);
  215. cd->connection->put_data(buf.ptr(), read);
  216. } break;
  217. case FileAccessNetwork::COMMAND_CLOSE: {
  218. print_verbose("CLOSED");
  219. ERR_CONTINUE(!cd->files.has(id));
  220. cd->files.erase(id);
  221. } break;
  222. }
  223. }
  224. _close_client(cd);
  225. }
  226. void EditorFileServer::_thread_start(void *s) {
  227. EditorFileServer *self = static_cast<EditorFileServer *>(s);
  228. while (!self->quit) {
  229. if (self->cmd == CMD_ACTIVATE) {
  230. self->server->listen(self->port);
  231. self->active = true;
  232. self->cmd = CMD_NONE;
  233. } else if (self->cmd == CMD_STOP) {
  234. self->server->stop();
  235. self->active = false;
  236. self->cmd = CMD_NONE;
  237. }
  238. if (self->active) {
  239. if (self->server->is_connection_available()) {
  240. ClientData *cd = memnew(ClientData);
  241. cd->connection = self->server->take_connection();
  242. cd->efs = self;
  243. cd->quit = false;
  244. cd->thread = memnew(Thread);
  245. cd->thread->start(_subthread_start, cd);
  246. }
  247. }
  248. self->wait_mutex.lock();
  249. while (self->to_wait.size()) {
  250. Thread *w = self->to_wait.front()->get();
  251. self->to_wait.erase(w);
  252. self->wait_mutex.unlock();
  253. w->wait_to_finish();
  254. self->wait_mutex.lock();
  255. }
  256. self->wait_mutex.unlock();
  257. OS::get_singleton()->delay_usec(100000);
  258. }
  259. }
  260. void EditorFileServer::start() {
  261. stop();
  262. port = EDITOR_GET("filesystem/file_server/port");
  263. password = EDITOR_GET("filesystem/file_server/password");
  264. cmd = CMD_ACTIVATE;
  265. }
  266. bool EditorFileServer::is_active() const {
  267. return active;
  268. }
  269. void EditorFileServer::stop() {
  270. cmd = CMD_STOP;
  271. }
  272. EditorFileServer::EditorFileServer() {
  273. server.instantiate();
  274. thread.start(_thread_start, this);
  275. EDITOR_DEF("filesystem/file_server/port", 6010);
  276. EDITOR_DEF("filesystem/file_server/password", "");
  277. }
  278. EditorFileServer::~EditorFileServer() {
  279. quit = true;
  280. thread.wait_to_finish();
  281. }