file_access_network.cpp 13 KB

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