editor_file_server.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "io/marshalls.h"
  33. //#define DEBUG_PRINT(m_p) print_line(m_p)
  34. #define DEBUG_TIME(m_what) printf("MS: %s - %lli\n", m_what, OS::get_singleton()->get_ticks_usec());
  35. //#define DEBUG_TIME(m_what)
  36. void EditorFileServer::_close_client(ClientData *cd) {
  37. cd->connection->disconnect();
  38. cd->efs->wait_mutex->lock();
  39. cd->efs->to_wait.insert(cd->thread);
  40. cd->efs->wait_mutex->unlock();
  41. while (cd->files.size()) {
  42. memdelete(cd->files.front()->get());
  43. cd->files.erase(cd->files.front());
  44. }
  45. memdelete(cd);
  46. }
  47. void EditorFileServer::_subthread_start(void *s) {
  48. ClientData *cd = (ClientData *)s;
  49. cd->connection->set_nodelay(true);
  50. uint8_t buf4[8];
  51. Error err = cd->connection->get_data(buf4, 4);
  52. if (err != OK) {
  53. _close_client(cd);
  54. ERR_FAIL_COND(err != OK);
  55. }
  56. int passlen = decode_uint32(buf4);
  57. if (passlen > 512) {
  58. _close_client(cd);
  59. ERR_FAIL_COND(passlen > 512);
  60. } else if (passlen > 0) {
  61. Vector<char> passutf8;
  62. passutf8.resize(passlen + 1);
  63. err = cd->connection->get_data((uint8_t *)passutf8.ptr(), passlen);
  64. if (err != OK) {
  65. _close_client(cd);
  66. ERR_FAIL_COND(err != OK);
  67. }
  68. passutf8[passlen] = 0;
  69. String s;
  70. s.parse_utf8(passutf8.ptr());
  71. if (s != cd->efs->password) {
  72. encode_uint32(ERR_INVALID_DATA, buf4);
  73. cd->connection->put_data(buf4, 4);
  74. OS::get_singleton()->delay_usec(1000000);
  75. _close_client(cd);
  76. ERR_PRINT("CLIENT PASSWORD MISMATCH");
  77. ERR_FAIL();
  78. }
  79. } else {
  80. if (cd->efs->password != "") {
  81. encode_uint32(ERR_INVALID_DATA, buf4);
  82. cd->connection->put_data(buf4, 4);
  83. OS::get_singleton()->delay_usec(1000000);
  84. _close_client(cd);
  85. ERR_PRINT("CLIENT PASSWORD MISMATCH (should be empty!)");
  86. ERR_FAIL();
  87. }
  88. }
  89. encode_uint32(OK, buf4);
  90. cd->connection->put_data(buf4, 4);
  91. while (!cd->quit) {
  92. //wait for ID
  93. err = cd->connection->get_data(buf4, 4);
  94. //#define DEBUG_PRINT(m_p) print_line(m_p)
  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[namelen] = 0;
  127. String s;
  128. s.parse_utf8(fileutf8.ptr());
  129. if (cmd == FileAccessNetwork::COMMAND_FILE_EXISTS) {
  130. print_line("FILE EXISTS: " + s);
  131. }
  132. if (cmd == FileAccessNetwork::COMMAND_GET_MODTIME) {
  133. print_line("MOD TIME: " + s);
  134. }
  135. if (cmd == FileAccessNetwork::COMMAND_OPEN_FILE) {
  136. print_line("OPEN: " + s);
  137. }
  138. if (!s.begins_with("res://")) {
  139. _close_client(cd);
  140. ERR_FAIL_COND(!s.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(s), 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(s), buf4);
  159. cd->connection->put_data(buf4, 8);
  160. DEBUG_TIME("open_file_end")
  161. break;
  162. }
  163. FileAccess *fa = FileAccess::open(s, FileAccess::READ);
  164. if (!fa) {
  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_len(), 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. int read = cd->files[id]->get_buffer(buf.ptr(), blocklen);
  205. ERR_CONTINUE(read < 0);
  206. print_line("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_line("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 = Thread::create(_subthread_start, cd);
  247. }
  248. }
  249. self->wait_mutex->lock();
  250. while (self->to_wait.size()) {
  251. Thread *w = self->to_wait.front()->get();
  252. self->to_wait.erase(w);
  253. self->wait_mutex->unlock();
  254. Thread::wait_to_finish(w);
  255. memdelete(w);
  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("file_server/port", 6010);
  265. password = EDITOR_DEF("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 = TCP_Server::create_ref();
  276. wait_mutex = Mutex::create();
  277. quit = false;
  278. active = false;
  279. cmd = CMD_NONE;
  280. thread = Thread::create(_thread_start, this);
  281. EDITOR_DEF("file_server/port", 6010);
  282. EDITOR_DEF("file_server/password", "");
  283. }
  284. EditorFileServer::~EditorFileServer() {
  285. quit = true;
  286. Thread::wait_to_finish(thread);
  287. memdelete(thread);
  288. memdelete(wait_mutex);
  289. }