file_access_network.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*************************************************************************/
  2. /* file_access_network.h */
  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. #ifndef FILE_ACCESS_NETWORK_H
  30. #define FILE_ACCESS_NETWORK_H
  31. #include "os/file_access.h"
  32. #include "os/semaphore.h"
  33. #include "os/thread.h"
  34. #include "io/stream_peer_tcp.h"
  35. class FileAccessNetwork;
  36. class FileAccessNetworkClient {
  37. struct BlockRequest {
  38. int id;
  39. uint64_t offset;
  40. int size;
  41. };
  42. int ml;
  43. List<BlockRequest> block_requests;
  44. Semaphore *sem;
  45. Thread *thread;
  46. bool quit;
  47. Mutex *mutex;
  48. Mutex *blockrequest_mutex;
  49. Map<int,FileAccessNetwork*> accesses;
  50. Ref<StreamPeerTCP> client;
  51. int last_id;
  52. Vector<uint8_t> block;
  53. void _thread_func();
  54. static void _thread_func(void *s);
  55. void put_32(int p_32);
  56. void put_64(int64_t p_64);
  57. int get_32();
  58. int64_t get_64();
  59. int lockcount;
  60. void lock_mutex();
  61. void unlock_mutex();
  62. friend class FileAccessNetwork;
  63. static FileAccessNetworkClient *singleton;
  64. public:
  65. static FileAccessNetworkClient *get_singleton() { return singleton; }
  66. Error connect(const String& p_host,int p_port,const String& p_password="");
  67. FileAccessNetworkClient();
  68. ~FileAccessNetworkClient();
  69. };
  70. class FileAccessNetwork : public FileAccess {
  71. Semaphore *sem;
  72. Semaphore *page_sem;
  73. Mutex *buffer_mutex;
  74. bool opened;
  75. size_t total_size;
  76. mutable size_t pos;
  77. int id;
  78. mutable bool eof_flag;
  79. mutable int last_page;
  80. mutable uint8_t *last_page_buff;
  81. uint32_t page_size;
  82. int read_ahead;
  83. int max_pages;
  84. mutable int waiting_on_page;
  85. mutable int last_activity_val;
  86. struct Page {
  87. int activity;
  88. bool queued;
  89. Vector<uint8_t> buffer;
  90. Page() { activity=0; queued=false; }
  91. };
  92. mutable Vector< Page > pages;
  93. mutable Error response;
  94. uint64_t exists_modtime;
  95. friend class FileAccessNetworkClient;
  96. void _queue_page(int p_page) const;
  97. void _respond(size_t p_len,Error p_status);
  98. void _set_block(size_t p_offset,const Vector<uint8_t>& p_block);
  99. public:
  100. enum Command {
  101. COMMAND_OPEN_FILE,
  102. COMMAND_READ_BLOCK,
  103. COMMAND_CLOSE,
  104. COMMAND_FILE_EXISTS,
  105. COMMAND_GET_MODTIME,
  106. };
  107. enum Response {
  108. RESPONSE_OPEN,
  109. RESPONSE_DATA,
  110. RESPONSE_FILE_EXISTS,
  111. RESPONSE_GET_MODTIME,
  112. };
  113. virtual Error _open(const String& p_path, int p_mode_flags); ///< open a file
  114. virtual void close(); ///< close a file
  115. virtual bool is_open() const; ///< true when file is open
  116. virtual void seek(size_t p_position); ///< seek to a given position
  117. virtual void seek_end(int64_t p_position=0); ///< seek from the end of file
  118. virtual size_t get_pos() const; ///< get position in the file
  119. virtual size_t get_len() const; ///< get size of the file
  120. virtual bool eof_reached() const; ///< reading passed EOF
  121. virtual uint8_t get_8() const; ///< get a byte
  122. virtual int get_buffer(uint8_t *p_dst, int p_length) const;
  123. virtual Error get_error() const; ///< get last error
  124. virtual void store_8(uint8_t p_dest); ///< store a byte
  125. virtual bool file_exists(const String& p_path); ///< return true if a file exists
  126. virtual uint64_t _get_modified_time(const String& p_file);
  127. FileAccessNetwork();
  128. ~FileAccessNetwork();
  129. };
  130. #endif // FILE_ACCESS_NETWORK_H