file_access_network.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /**************************************************************************/
  2. /* file_access_network.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "core/io/file_access.h"
  33. #include "core/io/stream_peer_tcp.h"
  34. #include "core/os/semaphore.h"
  35. #include "core/os/thread.h"
  36. class FileAccessNetwork;
  37. class FileAccessNetworkClient {
  38. struct BlockRequest {
  39. int32_t id;
  40. uint64_t offset;
  41. int32_t size;
  42. };
  43. List<BlockRequest> block_requests;
  44. Semaphore sem;
  45. Thread thread;
  46. bool quit = false;
  47. Mutex mutex;
  48. Mutex blockrequest_mutex;
  49. HashMap<int, FileAccessNetwork *> accesses;
  50. Ref<StreamPeerTCP> client;
  51. int32_t last_id = 0;
  52. int32_t lockcount = 0;
  53. Vector<uint8_t> block;
  54. void _thread_func();
  55. static void _thread_func(void *s);
  56. void put_32(int32_t p_32);
  57. void put_64(int64_t p_64);
  58. int32_t get_32();
  59. int64_t get_64();
  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 = false;
  75. uint64_t total_size = 0;
  76. mutable uint64_t pos = 0;
  77. int32_t id = -1;
  78. mutable bool eof_flag = false;
  79. mutable int32_t last_page = -1;
  80. mutable uint8_t *last_page_buff = nullptr;
  81. int32_t page_size = 0;
  82. int32_t read_ahead = 0;
  83. mutable int waiting_on_page = -1;
  84. struct Page {
  85. int activity = 0;
  86. bool queued = false;
  87. Vector<uint8_t> buffer;
  88. };
  89. mutable Vector<Page> pages;
  90. mutable Error response;
  91. uint64_t exists_modtime = 0;
  92. friend class FileAccessNetworkClient;
  93. void _queue_page(int32_t p_page) const;
  94. void _respond(uint64_t p_len, Error p_status);
  95. void _set_block(uint64_t p_offset, const Vector<uint8_t> &p_block);
  96. void _close();
  97. public:
  98. enum Command {
  99. COMMAND_OPEN_FILE,
  100. COMMAND_READ_BLOCK,
  101. COMMAND_CLOSE,
  102. COMMAND_FILE_EXISTS,
  103. COMMAND_GET_MODTIME,
  104. };
  105. enum Response {
  106. RESPONSE_OPEN,
  107. RESPONSE_DATA,
  108. RESPONSE_FILE_EXISTS,
  109. RESPONSE_GET_MODTIME,
  110. };
  111. virtual Error open_internal(const String &p_path, int p_mode_flags) override; ///< open a file
  112. virtual bool is_open() const override; ///< true when file is open
  113. virtual void seek(uint64_t p_position) override; ///< seek to a given position
  114. virtual void seek_end(int64_t p_position = 0) override; ///< seek from the end of file
  115. virtual uint64_t get_position() const override; ///< get position in the file
  116. virtual uint64_t get_length() const override; ///< get size of the file
  117. virtual bool eof_reached() const override; ///< reading passed EOF
  118. virtual uint8_t get_8() const override; ///< get a byte
  119. virtual uint64_t get_buffer(uint8_t *p_dst, uint64_t p_length) const override;
  120. virtual Error get_error() const override; ///< get last error
  121. virtual void flush() override;
  122. virtual void store_8(uint8_t p_dest) override; ///< store a byte
  123. virtual bool file_exists(const String &p_path) override; ///< return true if a file exists
  124. virtual uint64_t _get_modified_time(const String &p_file) override;
  125. virtual uint32_t _get_unix_permissions(const String &p_file) override;
  126. virtual Error _set_unix_permissions(const String &p_file, uint32_t p_permissions) override;
  127. static void configure();
  128. FileAccessNetwork();
  129. ~FileAccessNetwork();
  130. };
  131. #endif // FILE_ACCESS_NETWORK_H