NetworkFilesystem.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "Filesystem.h"
  25. #include "OsSocket.h"
  26. #include "OS.h" // For MAX_PATH_LENGTH
  27. #include "Log.h"
  28. namespace crown
  29. {
  30. namespace network_filesystem
  31. {
  32. inline void read_response(TCPSocket socket, Array<char>& response)
  33. {
  34. // Read message length
  35. uint32_t msg_len = 0;
  36. socket.read(&msg_len, 4);
  37. array::resize(response, msg_len);
  38. socket.read(array::begin(response), msg_len);
  39. // Ensure NUL-terminated
  40. array::push_back(response, '\0');
  41. }
  42. inline void send(TCPSocket socket, const char* msg)
  43. {
  44. uint32_t msg_len = string::strlen(msg);
  45. socket.write(&msg_len, 4);
  46. socket.write(msg, msg_len);
  47. }
  48. } // namespace network_filesystem
  49. /// Access files on a remote file server.
  50. /// All the file paths can be either relative or absolute.
  51. /// When a relative path is given, it is automatically translated
  52. /// to its absolute counterpart based on the file server's root path.
  53. /// Accessing files using absolute path directly is also possible,
  54. /// but platform-specific and thus generally not recommended.
  55. ///
  56. /// @ingroup Filesystem
  57. class NetworkFilesystem : public Filesystem
  58. {
  59. public:
  60. /// Sets the root path to the current working directory of
  61. /// the engine executable.
  62. NetworkFilesystem();
  63. /// Sets the file server address and port.
  64. /// @note
  65. /// The @a root_path must be absolute.
  66. NetworkFilesystem(const NetAddress& addr, uint16_t port);
  67. /// Opens the file at the given @a path with the given @a mode.
  68. File* open(const char* path, FileOpenMode mode);
  69. /// Closes the given @a file.
  70. void close(File* file);
  71. /// Returns true if @a path is a directory.
  72. bool is_directory(const char* path);
  73. /// Returns true if @a path is a regular file.
  74. bool is_file(const char* path);
  75. /// Creates the directory at the given @a path.
  76. void create_directory(const char* path);
  77. /// Deletes the directory at the given @a path.
  78. void delete_directory(const char* path);
  79. /// Creates the file at the given @a path.
  80. void create_file(const char* path);
  81. /// Deletes the file at the given @a path.
  82. void delete_file(const char* path);
  83. /// Returns the relative file names in the given @a path.
  84. void list_files(const char* path, Vector<DynamicString>& files);
  85. /// Returns the absolute path of the given @a path based on
  86. /// the root path of the file source. If @a path is absolute,
  87. /// the given path is returned.
  88. void get_absolute_path(const char* path, DynamicString& os_path);
  89. private:
  90. // Returns a new connection to the file server
  91. TCPSocket new_connection();
  92. private:
  93. NetAddress m_address;
  94. uint16_t m_port;
  95. };
  96. } // namespace crown