file_access_network.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /**************************************************************************/
  2. /* file_access_network.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "file_access_network.h"
  31. #include "core/io/ip.h"
  32. #include "core/io/marshalls.h"
  33. #include "core/os/os.h"
  34. #include "core/project_settings.h"
  35. //#define DEBUG_PRINT(m_p) print_line(m_p)
  36. //#define DEBUG_TIME(m_what) printf("MS: %s - %lli\n",m_what,OS::get_singleton()->get_ticks_usec());
  37. #define DEBUG_PRINT(m_p)
  38. #define DEBUG_TIME(m_what)
  39. void FileAccessNetworkClient::lock_mutex() {
  40. mutex.lock();
  41. lockcount++;
  42. }
  43. void FileAccessNetworkClient::unlock_mutex() {
  44. lockcount--;
  45. mutex.unlock();
  46. }
  47. void FileAccessNetworkClient::put_32(int p_32) {
  48. uint8_t buf[4];
  49. encode_uint32(p_32, buf);
  50. client->put_data(buf, 4);
  51. DEBUG_PRINT("put32: " + itos(p_32));
  52. }
  53. void FileAccessNetworkClient::put_64(int64_t p_64) {
  54. uint8_t buf[8];
  55. encode_uint64(p_64, buf);
  56. client->put_data(buf, 8);
  57. DEBUG_PRINT("put64: " + itos(p_64));
  58. }
  59. int FileAccessNetworkClient::get_32() {
  60. uint8_t buf[4];
  61. client->get_data(buf, 4);
  62. return decode_uint32(buf);
  63. }
  64. int64_t FileAccessNetworkClient::get_64() {
  65. uint8_t buf[8];
  66. client->get_data(buf, 8);
  67. return decode_uint64(buf);
  68. }
  69. void FileAccessNetworkClient::_thread_func() {
  70. client->set_no_delay(true);
  71. while (!quit) {
  72. DEBUG_PRINT("SEM WAIT - " + itos(sem->get()));
  73. sem.wait();
  74. DEBUG_TIME("sem_unlock");
  75. //DEBUG_PRINT("semwait returned "+itos(werr));
  76. DEBUG_PRINT("MUTEX LOCK " + itos(lockcount));
  77. lock_mutex();
  78. DEBUG_PRINT("MUTEX PASS");
  79. blockrequest_mutex.lock();
  80. while (block_requests.size()) {
  81. put_32(block_requests.front()->get().id);
  82. put_32(FileAccessNetwork::COMMAND_READ_BLOCK);
  83. put_64(block_requests.front()->get().offset);
  84. put_32(block_requests.front()->get().size);
  85. block_requests.pop_front();
  86. }
  87. blockrequest_mutex.unlock();
  88. DEBUG_PRINT("THREAD ITER");
  89. DEBUG_TIME("sem_read");
  90. int id = get_32();
  91. int response = get_32();
  92. DEBUG_PRINT("GET RESPONSE: " + itos(response));
  93. FileAccessNetwork *fa = nullptr;
  94. if (response != FileAccessNetwork::RESPONSE_DATA) {
  95. if (!accesses.has(id)) {
  96. unlock_mutex();
  97. ERR_FAIL_COND(!accesses.has(id));
  98. }
  99. }
  100. if (accesses.has(id)) {
  101. fa = accesses[id];
  102. }
  103. switch (response) {
  104. case FileAccessNetwork::RESPONSE_OPEN: {
  105. DEBUG_TIME("sem_open");
  106. int status = get_32();
  107. if (status != OK) {
  108. fa->_respond(0, Error(status));
  109. } else {
  110. int64_t len = get_64();
  111. fa->_respond(len, Error(status));
  112. }
  113. fa->sem.post();
  114. } break;
  115. case FileAccessNetwork::RESPONSE_DATA: {
  116. int64_t offset = get_64();
  117. int32_t len = get_32();
  118. Vector<uint8_t> block;
  119. block.resize(len);
  120. client->get_data(block.ptrw(), len);
  121. if (fa) { //may have been queued
  122. fa->_set_block(offset, block);
  123. }
  124. } break;
  125. case FileAccessNetwork::RESPONSE_FILE_EXISTS: {
  126. int status = get_32();
  127. fa->exists_modtime = status != 0;
  128. fa->sem.post();
  129. } break;
  130. case FileAccessNetwork::RESPONSE_GET_MODTIME: {
  131. uint64_t status = get_64();
  132. fa->exists_modtime = status;
  133. fa->sem.post();
  134. } break;
  135. }
  136. unlock_mutex();
  137. }
  138. }
  139. void FileAccessNetworkClient::_thread_func(void *s) {
  140. FileAccessNetworkClient *self = (FileAccessNetworkClient *)s;
  141. self->_thread_func();
  142. }
  143. Error FileAccessNetworkClient::connect(const String &p_host, int p_port, const String &p_password) {
  144. IP_Address ip;
  145. if (p_host.is_valid_ip_address()) {
  146. ip = p_host;
  147. } else {
  148. ip = IP::get_singleton()->resolve_hostname(p_host);
  149. }
  150. DEBUG_PRINT("IP: " + String(ip) + " port " + itos(p_port));
  151. Error err = client->connect_to_host(ip, p_port);
  152. ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot connect to host with IP: " + String(ip) + " and port: " + itos(p_port));
  153. while (client->get_status() == StreamPeerTCP::STATUS_CONNECTING) {
  154. //DEBUG_PRINT("trying to connect....");
  155. OS::get_singleton()->delay_usec(1000);
  156. }
  157. if (client->get_status() != StreamPeerTCP::STATUS_CONNECTED) {
  158. return ERR_CANT_CONNECT;
  159. }
  160. CharString cs = p_password.utf8();
  161. put_32(cs.length());
  162. client->put_data((const uint8_t *)cs.ptr(), cs.length());
  163. int e = get_32();
  164. if (e != OK) {
  165. return ERR_INVALID_PARAMETER;
  166. }
  167. thread.start(_thread_func, this);
  168. return OK;
  169. }
  170. FileAccessNetworkClient *FileAccessNetworkClient::singleton = nullptr;
  171. FileAccessNetworkClient::FileAccessNetworkClient() {
  172. quit = false;
  173. singleton = this;
  174. last_id = 0;
  175. client.instance();
  176. lockcount = 0;
  177. }
  178. FileAccessNetworkClient::~FileAccessNetworkClient() {
  179. if (thread.is_started()) {
  180. quit = true;
  181. sem.post();
  182. thread.wait_to_finish();
  183. }
  184. }
  185. void FileAccessNetwork::_set_block(uint64_t p_offset, const Vector<uint8_t> &p_block) {
  186. int32_t page = p_offset / page_size;
  187. ERR_FAIL_INDEX(page, pages.size());
  188. if (page < pages.size() - 1) {
  189. ERR_FAIL_COND(p_block.size() != page_size);
  190. } else {
  191. ERR_FAIL_COND((uint64_t)p_block.size() != total_size % page_size);
  192. }
  193. buffer_mutex.lock();
  194. pages.write[page].buffer = p_block;
  195. pages.write[page].queued = false;
  196. buffer_mutex.unlock();
  197. if (waiting_on_page == page) {
  198. waiting_on_page = -1;
  199. page_sem.post();
  200. }
  201. }
  202. void FileAccessNetwork::_respond(uint64_t p_len, Error p_status) {
  203. DEBUG_PRINT("GOT RESPONSE - len: " + itos(p_len) + " status: " + itos(p_status));
  204. response = p_status;
  205. if (response != OK) {
  206. return;
  207. }
  208. opened = true;
  209. total_size = p_len;
  210. int32_t pc = ((total_size - 1) / page_size) + 1;
  211. pages.resize(pc);
  212. }
  213. Error FileAccessNetwork::_open(const String &p_path, int p_mode_flags) {
  214. ERR_FAIL_COND_V(p_mode_flags != READ, ERR_UNAVAILABLE);
  215. if (opened) {
  216. close();
  217. }
  218. FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton;
  219. DEBUG_PRINT("open: " + p_path);
  220. DEBUG_TIME("open_begin");
  221. nc->lock_mutex();
  222. nc->put_32(id);
  223. nc->accesses[id] = this;
  224. nc->put_32(COMMAND_OPEN_FILE);
  225. CharString cs = p_path.utf8();
  226. nc->put_32(cs.length());
  227. nc->client->put_data((const uint8_t *)cs.ptr(), cs.length());
  228. pos = 0;
  229. eof_flag = false;
  230. last_page = -1;
  231. last_page_buff = nullptr;
  232. //buffers.clear();
  233. nc->unlock_mutex();
  234. DEBUG_PRINT("OPEN POST");
  235. DEBUG_TIME("open_post");
  236. nc->sem.post(); //awaiting answer
  237. DEBUG_PRINT("WAIT...");
  238. sem.wait();
  239. DEBUG_TIME("open_end");
  240. DEBUG_PRINT("WAIT ENDED...");
  241. return response;
  242. }
  243. void FileAccessNetwork::close() {
  244. if (!opened) {
  245. return;
  246. }
  247. FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton;
  248. DEBUG_PRINT("CLOSE");
  249. nc->lock_mutex();
  250. nc->put_32(id);
  251. nc->put_32(COMMAND_CLOSE);
  252. pages.clear();
  253. opened = false;
  254. nc->unlock_mutex();
  255. }
  256. bool FileAccessNetwork::is_open() const {
  257. return opened;
  258. }
  259. void FileAccessNetwork::seek(uint64_t p_position) {
  260. ERR_FAIL_COND_MSG(!opened, "File must be opened before use.");
  261. eof_flag = p_position > total_size;
  262. if (p_position >= total_size) {
  263. p_position = total_size;
  264. }
  265. pos = p_position;
  266. }
  267. void FileAccessNetwork::seek_end(int64_t p_position) {
  268. seek(total_size + p_position);
  269. }
  270. uint64_t FileAccessNetwork::get_position() const {
  271. ERR_FAIL_COND_V_MSG(!opened, 0, "File must be opened before use.");
  272. return pos;
  273. }
  274. uint64_t FileAccessNetwork::get_len() const {
  275. ERR_FAIL_COND_V_MSG(!opened, 0, "File must be opened before use.");
  276. return total_size;
  277. }
  278. bool FileAccessNetwork::eof_reached() const {
  279. ERR_FAIL_COND_V_MSG(!opened, false, "File must be opened before use.");
  280. return eof_flag;
  281. }
  282. uint8_t FileAccessNetwork::get_8() const {
  283. uint8_t v;
  284. get_buffer(&v, 1);
  285. return v;
  286. }
  287. void FileAccessNetwork::_queue_page(int32_t p_page) const {
  288. if (p_page >= pages.size()) {
  289. return;
  290. }
  291. if (pages[p_page].buffer.empty() && !pages[p_page].queued) {
  292. FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton;
  293. nc->blockrequest_mutex.lock();
  294. FileAccessNetworkClient::BlockRequest br;
  295. br.id = id;
  296. br.offset = (uint64_t)p_page * page_size;
  297. br.size = page_size;
  298. nc->block_requests.push_back(br);
  299. pages.write[p_page].queued = true;
  300. nc->blockrequest_mutex.unlock();
  301. DEBUG_PRINT("QUEUE PAGE POST");
  302. nc->sem.post();
  303. DEBUG_PRINT("queued " + itos(p_page));
  304. }
  305. }
  306. uint64_t FileAccessNetwork::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
  307. ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
  308. if (pos + p_length > total_size) {
  309. eof_flag = true;
  310. }
  311. if (pos + p_length >= total_size) {
  312. p_length = total_size - pos;
  313. }
  314. uint8_t *buff = last_page_buff;
  315. for (uint64_t i = 0; i < p_length; i++) {
  316. int32_t page = pos / page_size;
  317. if (page != last_page) {
  318. buffer_mutex.lock();
  319. if (pages[page].buffer.empty()) {
  320. waiting_on_page = page;
  321. for (int32_t j = 0; j < read_ahead; j++) {
  322. _queue_page(page + j);
  323. }
  324. buffer_mutex.unlock();
  325. DEBUG_PRINT("wait");
  326. page_sem.wait();
  327. DEBUG_PRINT("done");
  328. } else {
  329. for (int32_t j = 0; j < read_ahead; j++) {
  330. _queue_page(page + j);
  331. }
  332. buffer_mutex.unlock();
  333. }
  334. buff = pages.write[page].buffer.ptrw();
  335. last_page_buff = buff;
  336. last_page = page;
  337. }
  338. p_dst[i] = buff[pos - uint64_t(page) * page_size];
  339. pos++;
  340. }
  341. return p_length;
  342. }
  343. Error FileAccessNetwork::get_error() const {
  344. return pos == total_size ? ERR_FILE_EOF : OK;
  345. }
  346. void FileAccessNetwork::flush() {
  347. ERR_FAIL();
  348. }
  349. void FileAccessNetwork::store_8(uint8_t p_dest) {
  350. ERR_FAIL();
  351. }
  352. bool FileAccessNetwork::file_exists(const String &p_path) {
  353. FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton;
  354. nc->lock_mutex();
  355. nc->put_32(id);
  356. nc->put_32(COMMAND_FILE_EXISTS);
  357. CharString cs = p_path.utf8();
  358. nc->put_32(cs.length());
  359. nc->client->put_data((const uint8_t *)cs.ptr(), cs.length());
  360. nc->unlock_mutex();
  361. DEBUG_PRINT("FILE EXISTS POST");
  362. nc->sem.post();
  363. sem.wait();
  364. return exists_modtime != 0;
  365. }
  366. uint64_t FileAccessNetwork::_get_modified_time(const String &p_file) {
  367. FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton;
  368. nc->lock_mutex();
  369. nc->put_32(id);
  370. nc->put_32(COMMAND_GET_MODTIME);
  371. CharString cs = p_file.utf8();
  372. nc->put_32(cs.length());
  373. nc->client->put_data((const uint8_t *)cs.ptr(), cs.length());
  374. nc->unlock_mutex();
  375. DEBUG_PRINT("MODTIME POST");
  376. nc->sem.post();
  377. sem.wait();
  378. return exists_modtime;
  379. }
  380. uint32_t FileAccessNetwork::_get_unix_permissions(const String &p_file) {
  381. ERR_PRINT("Getting UNIX permissions from network drives is not implemented yet");
  382. return 0;
  383. }
  384. Error FileAccessNetwork::_set_unix_permissions(const String &p_file, uint32_t p_permissions) {
  385. ERR_PRINT("Setting UNIX permissions on network drives is not implemented yet");
  386. return ERR_UNAVAILABLE;
  387. }
  388. void FileAccessNetwork::configure() {
  389. GLOBAL_DEF("network/remote_fs/page_size", 65536);
  390. ProjectSettings::get_singleton()->set_custom_property_info("network/remote_fs/page_size", PropertyInfo(Variant::INT, "network/remote_fs/page_size", PROPERTY_HINT_RANGE, "1,65536,1,or_greater")); //is used as denominator and can't be zero
  391. GLOBAL_DEF("network/remote_fs/page_read_ahead", 4);
  392. ProjectSettings::get_singleton()->set_custom_property_info("network/remote_fs/page_read_ahead", PropertyInfo(Variant::INT, "network/remote_fs/page_read_ahead", PROPERTY_HINT_RANGE, "0,8,1,or_greater"));
  393. }
  394. FileAccessNetwork::FileAccessNetwork() {
  395. eof_flag = false;
  396. opened = false;
  397. pos = 0;
  398. FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton;
  399. nc->lock_mutex();
  400. id = nc->last_id++;
  401. nc->accesses[id] = this;
  402. nc->unlock_mutex();
  403. page_size = GLOBAL_GET("network/remote_fs/page_size");
  404. read_ahead = GLOBAL_GET("network/remote_fs/page_read_ahead");
  405. waiting_on_page = -1;
  406. last_page = -1;
  407. }
  408. FileAccessNetwork::~FileAccessNetwork() {
  409. close();
  410. FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton;
  411. nc->lock_mutex();
  412. id = nc->last_id++;
  413. nc->accesses.erase(id);
  414. nc->unlock_mutex();
  415. }