editor_file_server.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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. #include "io/marshalls.h"
  34. //#define DEBUG_PRINT(m_p) print_line(m_p)
  35. #define DEBUG_TIME(m_what) printf("MS: %s - %lu\n", m_what, OS::get_singleton()->get_ticks_usec());
  36. //#define DEBUG_TIME(m_what)
  37. void EditorFileServer::_close_client(ClientData *cd) {
  38. cd->connection->disconnect_from_host();
  39. cd->efs->wait_mutex->lock();
  40. cd->efs->to_wait.insert(cd->thread);
  41. cd->efs->wait_mutex->unlock();
  42. while (cd->files.size()) {
  43. memdelete(cd->files.front()->get());
  44. cd->files.erase(cd->files.front());
  45. }
  46. memdelete(cd);
  47. }
  48. void EditorFileServer::_subthread_start(void *s) {
  49. ClientData *cd = (ClientData *)s;
  50. cd->connection->set_nodelay(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[passlen] = 0;
  70. String s;
  71. s.parse_utf8(passutf8.ptr());
  72. if (s != 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 != "") {
  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. //#define DEBUG_PRINT(m_p) print_line(m_p)
  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[namelen] = 0;
  128. String s;
  129. s.parse_utf8(fileutf8.ptr());
  130. if (cmd == FileAccessNetwork::COMMAND_FILE_EXISTS) {
  131. print_line("FILE EXISTS: " + s);
  132. }
  133. if (cmd == FileAccessNetwork::COMMAND_GET_MODTIME) {
  134. print_line("MOD TIME: " + s);
  135. }
  136. if (cmd == FileAccessNetwork::COMMAND_OPEN_FILE) {
  137. print_line("OPEN: " + s);
  138. }
  139. if (!s.begins_with("res://")) {
  140. _close_client(cd);
  141. ERR_FAIL_COND(!s.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(s), 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(s), buf4);
  160. cd->connection->put_data(buf4, 8);
  161. DEBUG_TIME("open_file_end")
  162. break;
  163. }
  164. FileAccess *fa = FileAccess::open(s, 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. int read = cd->files[id]->get_buffer(buf.ptr(), blocklen);
  206. ERR_CONTINUE(read < 0);
  207. print_line("GET BLOCK - offset: " + itos(offset) + ", blocklen: " + itos(blocklen));
  208. //not found, continue
  209. encode_uint32(id, buf4);
  210. cd->connection->put_data(buf4, 4);
  211. encode_uint32(FileAccessNetwork::RESPONSE_DATA, buf4);
  212. cd->connection->put_data(buf4, 4);
  213. encode_uint64(offset, buf4);
  214. cd->connection->put_data(buf4, 8);
  215. encode_uint32(read, buf4);
  216. cd->connection->put_data(buf4, 4);
  217. cd->connection->put_data(buf.ptr(), read);
  218. } break;
  219. case FileAccessNetwork::COMMAND_CLOSE: {
  220. print_line("CLOSED");
  221. ERR_CONTINUE(!cd->files.has(id));
  222. memdelete(cd->files[id]);
  223. cd->files.erase(id);
  224. } break;
  225. }
  226. }
  227. _close_client(cd);
  228. }
  229. void EditorFileServer::_thread_start(void *s) {
  230. EditorFileServer *self = (EditorFileServer *)s;
  231. while (!self->quit) {
  232. if (self->cmd == CMD_ACTIVATE) {
  233. self->server->listen(self->port);
  234. self->active = true;
  235. self->cmd = CMD_NONE;
  236. } else if (self->cmd == CMD_STOP) {
  237. self->server->stop();
  238. self->active = false;
  239. self->cmd = CMD_NONE;
  240. }
  241. if (self->active) {
  242. if (self->server->is_connection_available()) {
  243. ClientData *cd = memnew(ClientData);
  244. cd->connection = self->server->take_connection();
  245. cd->efs = self;
  246. cd->quit = false;
  247. cd->thread = Thread::create(_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. Thread::wait_to_finish(w);
  256. memdelete(w);
  257. self->wait_mutex->lock();
  258. }
  259. self->wait_mutex->unlock();
  260. OS::get_singleton()->delay_usec(100000);
  261. }
  262. }
  263. void EditorFileServer::start() {
  264. stop();
  265. port = EDITOR_DEF("filesystem/file_server/port", 6010);
  266. password = EDITOR_DEF("filesystem/file_server/password", "");
  267. cmd = CMD_ACTIVATE;
  268. }
  269. bool EditorFileServer::is_active() const {
  270. return active;
  271. }
  272. void EditorFileServer::stop() {
  273. cmd = CMD_STOP;
  274. }
  275. EditorFileServer::EditorFileServer() {
  276. server = TCP_Server::create_ref();
  277. wait_mutex = Mutex::create();
  278. quit = false;
  279. active = false;
  280. cmd = CMD_NONE;
  281. thread = Thread::create(_thread_start, this);
  282. EDITOR_DEF("filesystem/file_server/port", 6010);
  283. EDITOR_DEF("filesystem/file_server/password", "");
  284. }
  285. EditorFileServer::~EditorFileServer() {
  286. quit = true;
  287. Thread::wait_to_finish(thread);
  288. memdelete(thread);
  289. memdelete(wait_mutex);
  290. }