file_access_network.cpp 13 KB

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