file_access_network.h 5.1 KB

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