file_access_network.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. /*************************************************************************/
  2. /* file_access_network.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "file_access_network.h"
  30. #include "marshalls.h"
  31. #include "globals.h"
  32. #include "os/os.h"
  33. #include "io/ip.h"
  34. #define DEBUG_PRINT(m_p) print_line(m_p)
  35. //#define DEBUG_TIME(m_what) printf("MS: %s - %lli\n",m_what,OS::get_singleton()->get_ticks_usec());
  36. //#define DEBUG_PRINT(m_p)
  37. #define DEBUG_TIME(m_what)
  38. void FileAccessNetworkClient::lock_mutex() {
  39. mutex->lock();
  40. lockcount++;
  41. }
  42. void FileAccessNetworkClient::unlock_mutex() {
  43. lockcount--;
  44. mutex->unlock();
  45. }
  46. void FileAccessNetworkClient::put_32(int p_32) {
  47. uint8_t buf[4];
  48. encode_uint32(p_32,buf);
  49. client->put_data(buf,4);
  50. DEBUG_PRINT("put32: "+itos(p_32));
  51. }
  52. void FileAccessNetworkClient::put_64(int64_t p_64) {
  53. uint8_t buf[8];
  54. encode_uint64(p_64,buf);
  55. client->put_data(buf,8);
  56. DEBUG_PRINT("put64: "+itos(p_64));
  57. }
  58. int FileAccessNetworkClient::get_32() {
  59. uint8_t buf[4];
  60. client->get_data(buf,4);
  61. return decode_uint32(buf);
  62. }
  63. int64_t FileAccessNetworkClient::get_64() {
  64. uint8_t buf[8];
  65. client->get_data(buf,8);
  66. return decode_uint64(buf);
  67. }
  68. void FileAccessNetworkClient::_thread_func() {
  69. client->set_nodelay(true);
  70. while(!quit) {
  71. DEBUG_PRINT("SEM WAIT - "+itos(sem->get()));
  72. Error err = sem->wait();
  73. DEBUG_TIME("sem_unlock");
  74. //DEBUG_PRINT("semwait returned "+itos(werr));
  75. DEBUG_PRINT("MUTEX LOCK "+itos(lockcount));
  76. DEBUG_PRINT("POPO");
  77. DEBUG_PRINT("PEPE");
  78. lock_mutex();
  79. DEBUG_PRINT("MUTEX PASS");
  80. blockrequest_mutex->lock();
  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. blockrequest_mutex->unlock();
  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=NULL;
  95. if (response!=FileAccessNetwork::RESPONSE_DATA) {
  96. ERR_FAIL_COND(!accesses.has(id));
  97. }
  98. if (accesses.has(id))
  99. fa=accesses[id];
  100. switch(response) {
  101. case FileAccessNetwork::RESPONSE_OPEN: {
  102. DEBUG_TIME("sem_open");
  103. int status = get_32();
  104. if (status!=OK) {
  105. fa->_respond(0,Error(status));
  106. } else {
  107. uint64_t len = get_64();
  108. fa->_respond(len,Error(status));
  109. }
  110. fa->sem->post();
  111. } break;
  112. case FileAccessNetwork::RESPONSE_DATA: {
  113. int64_t offset = get_64();
  114. uint32_t len = get_32();
  115. Vector<uint8_t> block;
  116. block.resize(len);
  117. client->get_data(block.ptr(),len);
  118. if (fa) //may have been queued
  119. fa->_set_block(offset,block);
  120. } break;
  121. case FileAccessNetwork::RESPONSE_FILE_EXISTS: {
  122. int status = get_32();
  123. fa->exists_modtime=status!=0;
  124. fa->sem->post();
  125. } break;
  126. case FileAccessNetwork::RESPONSE_GET_MODTIME: {
  127. uint64_t status = get_64();
  128. fa->exists_modtime=status;
  129. fa->sem->post();
  130. } break;
  131. }
  132. unlock_mutex();
  133. }
  134. }
  135. void FileAccessNetworkClient::_thread_func(void *s) {
  136. FileAccessNetworkClient *self =(FileAccessNetworkClient*)s;
  137. self->_thread_func();
  138. }
  139. Error FileAccessNetworkClient::connect(const String& p_host,int p_port,const String& p_password) {
  140. IP_Address ip;
  141. if (p_host.is_valid_ip_address()) {
  142. ip=p_host;
  143. } else {
  144. ip=IP::get_singleton()->resolve_hostname(p_host);
  145. }
  146. DEBUG_PRINT("IP: "+String(ip)+" port "+itos(p_port));
  147. Error err = client->connect(ip,p_port);
  148. ERR_FAIL_COND_V(err,err);
  149. while(client->get_status()==StreamPeerTCP::STATUS_CONNECTING) {
  150. //DEBUG_PRINT("trying to connect....");
  151. OS::get_singleton()->delay_usec(1000);
  152. }
  153. if (client->get_status()!=StreamPeerTCP::STATUS_CONNECTED) {
  154. return ERR_CANT_CONNECT;
  155. }
  156. CharString cs = p_password.utf8();
  157. put_32(cs.length());
  158. client->put_data((const uint8_t*)cs.ptr(),cs.length());
  159. int e = get_32();
  160. if (e!=OK) {
  161. return ERR_INVALID_PARAMETER;
  162. }
  163. thread = Thread::create(_thread_func,this);
  164. return OK;
  165. }
  166. FileAccessNetworkClient *FileAccessNetworkClient::singleton=NULL;
  167. FileAccessNetworkClient::FileAccessNetworkClient() {
  168. thread=NULL;
  169. mutex = Mutex::create();
  170. blockrequest_mutex = Mutex::create();
  171. quit=false;
  172. singleton=this;
  173. last_id=0;
  174. client = Ref<StreamPeerTCP>( StreamPeerTCP::create_ref() );
  175. sem=Semaphore::create();
  176. lockcount=0;
  177. }
  178. FileAccessNetworkClient::~FileAccessNetworkClient() {
  179. if (thread) {
  180. quit=true;
  181. sem->post();
  182. Thread::wait_to_finish(thread);
  183. memdelete(thread);
  184. }
  185. memdelete(blockrequest_mutex);
  186. memdelete(mutex);
  187. memdelete(sem);
  188. }
  189. void FileAccessNetwork::_set_block(size_t p_offset,const Vector<uint8_t>& p_block) {
  190. int page = p_offset/page_size;
  191. ERR_FAIL_INDEX(page,pages.size());
  192. if (page<pages.size()-1) {
  193. ERR_FAIL_COND(p_block.size()!=page_size);
  194. } else {
  195. ERR_FAIL_COND( (p_block.size() != (total_size%page_size)));
  196. }
  197. buffer_mutex->lock();
  198. pages[page].buffer=p_block;
  199. pages[page].queued=false;
  200. buffer_mutex->unlock();
  201. if (waiting_on_page==page) {
  202. waiting_on_page=-1;
  203. page_sem->post();
  204. }
  205. }
  206. void FileAccessNetwork::_respond(size_t p_len,Error p_status) {
  207. DEBUG_PRINT("GOT RESPONSE - len: "+itos(p_len)+" status: "+itos(p_status));
  208. response=p_status;
  209. if (response!=OK)
  210. return;
  211. opened=true;
  212. total_size=p_len;
  213. int pc = ((total_size-1)/page_size)+1;
  214. pages.resize(pc);
  215. }
  216. Error FileAccessNetwork::_open(const String& p_path, int p_mode_flags) {
  217. ERR_FAIL_COND_V(p_mode_flags!=READ,ERR_UNAVAILABLE);
  218. if (opened)
  219. close();
  220. FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton;
  221. DEBUG_PRINT("open: "+p_path);
  222. DEBUG_TIME("open_begin");
  223. nc->lock_mutex();
  224. nc->put_32(id);
  225. nc->accesses[id]=this;
  226. nc->put_32(COMMAND_OPEN_FILE);
  227. CharString cs =p_path.utf8();
  228. nc->put_32(cs.length());
  229. nc->client->put_data((const uint8_t*)cs.ptr(),cs.length());
  230. pos=0;
  231. eof_flag=false;
  232. last_page=-1;
  233. last_page_buff=NULL;
  234. // buffers.clear();
  235. nc->unlock_mutex();
  236. DEBUG_PRINT("OPEN POST");
  237. DEBUG_TIME("open_post");
  238. nc->sem->post(); //awaiting answer
  239. DEBUG_PRINT("WAIT...");
  240. sem->wait();
  241. DEBUG_TIME("open_end");
  242. DEBUG_PRINT("WAIT ENDED...");
  243. return response;
  244. }
  245. void FileAccessNetwork::close(){
  246. if (!opened)
  247. return;
  248. FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton;
  249. DEBUG_PRINT("CLOSE");
  250. nc->lock_mutex();
  251. nc->put_32(id);
  252. nc->put_32(COMMAND_CLOSE);
  253. pages.clear();
  254. opened=false;
  255. nc->unlock_mutex();
  256. }
  257. bool FileAccessNetwork::is_open() const{
  258. return opened;
  259. }
  260. void FileAccessNetwork::seek(size_t p_position){
  261. ERR_FAIL_COND(!opened);
  262. eof_flag=p_position>total_size;
  263. if (p_position>=total_size) {
  264. p_position=total_size;
  265. }
  266. pos=p_position;
  267. }
  268. void FileAccessNetwork::seek_end(int64_t p_position){
  269. seek(total_size+p_position);
  270. }
  271. size_t FileAccessNetwork::get_pos() const{
  272. ERR_FAIL_COND_V(!opened,0);
  273. return pos;
  274. }
  275. size_t FileAccessNetwork::get_len() const{
  276. ERR_FAIL_COND_V(!opened,0);
  277. return total_size;
  278. }
  279. bool FileAccessNetwork::eof_reached() const{
  280. ERR_FAIL_COND_V(!opened,false);
  281. return eof_flag;
  282. }
  283. uint8_t FileAccessNetwork::get_8() const{
  284. uint8_t v;
  285. get_buffer(&v,1);
  286. return v;
  287. }
  288. void FileAccessNetwork::_queue_page(int p_page) const {
  289. if (p_page>=pages.size())
  290. return;
  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=size_t(p_page)*page_size;
  297. br.size=page_size;
  298. nc->block_requests.push_back(br);
  299. pages[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. int FileAccessNetwork::get_buffer(uint8_t *p_dst, int p_length) const{
  307. //bool eof=false;
  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. // FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton;
  315. uint8_t *buff=last_page_buff;
  316. for(int i=0;i<p_length;i++) {
  317. int page=pos/page_size;
  318. if (page!=last_page) {
  319. buffer_mutex->lock();
  320. if (pages[page].buffer.empty()) {
  321. //fuck
  322. waiting_on_page=page;
  323. for(int j=0;j<read_ahead;j++) {
  324. _queue_page(page+j);
  325. }
  326. buffer_mutex->unlock();
  327. DEBUG_PRINT("wait");
  328. page_sem->wait();
  329. DEBUG_PRINT("done");
  330. } else {
  331. for(int j=0;j<read_ahead;j++) {
  332. _queue_page(page+j);
  333. }
  334. buff=pages[page].buffer.ptr();
  335. //queue pages
  336. buffer_mutex->unlock();
  337. }
  338. buff=pages[page].buffer.ptr();
  339. last_page_buff=buff;
  340. last_page=page;
  341. }
  342. p_dst[i]=buff[pos-uint64_t(page)*page_size];
  343. pos++;
  344. }
  345. return p_length;
  346. }
  347. Error FileAccessNetwork::get_error() const{
  348. return pos==total_size?ERR_FILE_EOF:OK;
  349. }
  350. void FileAccessNetwork::store_8(uint8_t p_dest) {
  351. ERR_FAIL();
  352. }
  353. bool FileAccessNetwork::file_exists(const String& p_path){
  354. FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton;
  355. nc->lock_mutex();
  356. nc->put_32(id);
  357. nc->put_32(COMMAND_FILE_EXISTS);
  358. CharString cs=p_path.utf8();
  359. nc->put_32(cs.length());
  360. nc->client->put_data((const uint8_t*)cs.ptr(),cs.length());
  361. nc->unlock_mutex();
  362. DEBUG_PRINT("FILE EXISTS POST");
  363. nc->sem->post();
  364. sem->wait();
  365. return exists_modtime!=0;
  366. }
  367. uint64_t FileAccessNetwork::_get_modified_time(const String& p_file){
  368. FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton;
  369. nc->lock_mutex();
  370. nc->put_32(id);
  371. nc->put_32(COMMAND_GET_MODTIME);
  372. CharString cs=p_file.utf8();
  373. nc->put_32(cs.length());
  374. nc->client->put_data((const uint8_t*)cs.ptr(),cs.length());
  375. nc->unlock_mutex();
  376. DEBUG_PRINT("MODTIME POST");
  377. nc->sem->post();
  378. sem->wait();
  379. return exists_modtime;
  380. }
  381. FileAccessNetwork::FileAccessNetwork() {
  382. eof_flag=false;
  383. opened=false;
  384. pos=0;
  385. sem=Semaphore::create();
  386. page_sem=Semaphore::create();
  387. buffer_mutex=Mutex::create();
  388. FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton;
  389. nc->lock_mutex();
  390. id=nc->last_id++;
  391. nc->accesses[id]=this;
  392. nc->unlock_mutex();
  393. page_size = GLOBAL_DEF("remote_fs/page_size",65536);
  394. read_ahead = GLOBAL_DEF("remote_fs/page_read_ahead",4);
  395. max_pages = GLOBAL_DEF("remote_fs/max_pages",20);
  396. last_activity_val=0;
  397. waiting_on_page=-1;
  398. last_page=-1;
  399. }
  400. FileAccessNetwork::~FileAccessNetwork() {
  401. close();
  402. memdelete(sem);
  403. memdelete(page_sem);
  404. memdelete(buffer_mutex);
  405. FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton;
  406. nc->lock_mutex();
  407. id=nc->last_id++;
  408. nc->accesses.erase(id);
  409. nc->unlock_mutex();
  410. }