NetworkFile.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. #include "JSONParser.h"
  24. #include "ContainerTypes.h"
  25. #include "Log.h"
  26. #include "MathUtils.h"
  27. #include "NetworkFile.h"
  28. #include "NetworkFilesystem.h"
  29. #include "StringStream.h"
  30. #include "StringUtils.h"
  31. #include "TempAllocator.h"
  32. #include "Types.h"
  33. #include "DynamicString.h"
  34. namespace crown
  35. {
  36. //-----------------------------------------------------------------------------
  37. NetworkFile::NetworkFile(const NetAddress& addr, uint16_t port, const char* filename)
  38. : File(FOM_READ)
  39. , m_address(addr)
  40. , m_port(port)
  41. , m_position(0)
  42. {
  43. string::strncpy(m_filename, filename, MAX_PATH_LENGTH);
  44. m_socket.connect(addr, port);
  45. }
  46. //-----------------------------------------------------------------------------
  47. NetworkFile::~NetworkFile()
  48. {
  49. m_socket.close();
  50. }
  51. //-----------------------------------------------------------------------------
  52. void NetworkFile::seek(size_t position)
  53. {
  54. m_position = position;
  55. }
  56. //-----------------------------------------------------------------------------
  57. void NetworkFile::seek_to_end()
  58. {
  59. m_position = size();
  60. }
  61. //-----------------------------------------------------------------------------
  62. void NetworkFile::skip(size_t bytes)
  63. {
  64. m_position += bytes;
  65. }
  66. //-----------------------------------------------------------------------------
  67. void NetworkFile::read(void* buffer, size_t size)
  68. {
  69. TempAllocator1024 alloc;
  70. StringStream command(alloc);
  71. // Request the file
  72. command << "{\"type\":\"filesystem\",\"filesystem\":\"read\",";
  73. command << "\"file\":\"" << m_filename << "\",";
  74. command << "\"position\":" << m_position << ",";
  75. command << "\"size\":" << size << "}";
  76. network_filesystem::send(m_socket, command.c_str());
  77. // Wait for response
  78. Array<char> response(default_allocator());
  79. network_filesystem::read_response(m_socket, response);
  80. // Parse the response
  81. JSONParser json(array::begin(response));
  82. JSONElement root = json.root();
  83. DynamicString data_base64;
  84. root.key("data").to_string(data_base64);
  85. size_t out_len = 0;
  86. unsigned char* data = math::base64_decode(data_base64.c_str(), data_base64.length(), &out_len);
  87. memcpy(buffer, data, sizeof(unsigned char) * out_len);
  88. default_allocator().deallocate(data);
  89. }
  90. //-----------------------------------------------------------------------------
  91. void NetworkFile::write(const void* /*buffer*/, size_t /*size*/)
  92. {
  93. CE_FATAL("Cannot write to a network file");
  94. }
  95. //-----------------------------------------------------------------------------
  96. bool NetworkFile::copy_to(File& file, size_t size)
  97. {
  98. return true;
  99. }
  100. //-----------------------------------------------------------------------------
  101. bool NetworkFile::end_of_file()
  102. {
  103. return position() == size();
  104. }
  105. //-----------------------------------------------------------------------------
  106. bool NetworkFile::is_valid()
  107. {
  108. return true;
  109. }
  110. //-----------------------------------------------------------------------------
  111. void NetworkFile::flush()
  112. {
  113. // Do nothing
  114. }
  115. //-----------------------------------------------------------------------------
  116. size_t NetworkFile::position()
  117. {
  118. return m_position;
  119. }
  120. //-----------------------------------------------------------------------------
  121. size_t NetworkFile::size()
  122. {
  123. TempAllocator1024 alloc;
  124. StringStream command(alloc);
  125. // Request the file
  126. command << "{\"type\":\"filesystem\",\"filesystem\":\"size\",";
  127. command << "\"file\":\"" << m_filename << "\"}";
  128. network_filesystem::send(m_socket, command.c_str());
  129. // Wait for response
  130. Array<char> response(default_allocator());
  131. network_filesystem::read_response(m_socket, response);
  132. JSONParser parser(array::begin(response));
  133. JSONElement root = parser.root();
  134. return (size_t) root.key("size").to_int();
  135. }
  136. //-----------------------------------------------------------------------------
  137. bool NetworkFile::can_read() const
  138. {
  139. return true;
  140. }
  141. //-----------------------------------------------------------------------------
  142. bool NetworkFile::can_write() const
  143. {
  144. return false;
  145. }
  146. //-----------------------------------------------------------------------------
  147. bool NetworkFile::can_seek() const
  148. {
  149. return true;
  150. }
  151. } // namespace crown