file_access_network.cpp 13 KB

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