network_file.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) 2012-2014 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "json_parser.h"
  6. #include "container_types.h"
  7. #include "log.h"
  8. #include "math_utils.h"
  9. #include "network_file.h"
  10. #include "network_filesystem.h"
  11. #include "string_stream.h"
  12. #include "string_utils.h"
  13. #include "temp_allocator.h"
  14. #include "types.h"
  15. #include "dynamic_string.h"
  16. namespace crown
  17. {
  18. NetworkFile::NetworkFile(const NetAddress& addr, uint16_t port, const char* filename)
  19. : File(FOM_READ)
  20. , _address(addr)
  21. , _port(port)
  22. , _position(0)
  23. {
  24. strncpy(_filename, filename, MAX_PATH_LENGTH);
  25. _socket.connect(addr, port);
  26. }
  27. NetworkFile::~NetworkFile()
  28. {
  29. _socket.close();
  30. }
  31. void NetworkFile::seek(size_t position)
  32. {
  33. _position = position;
  34. }
  35. void NetworkFile::seek_to_end()
  36. {
  37. _position = size();
  38. }
  39. void NetworkFile::skip(size_t bytes)
  40. {
  41. _position += bytes;
  42. }
  43. void NetworkFile::read(void* buffer, size_t size)
  44. {
  45. using namespace string_stream;
  46. TempAllocator1024 alloc;
  47. StringStream command(alloc);
  48. // Request the file
  49. command << "{\"type\":\"filesystem\",\"filesystem\":\"read\",";
  50. command << "\"file\":\"" << _filename << "\",";
  51. command << "\"position\":" << _position << ",";
  52. command << "\"size\":" << size << "}";
  53. network_filesystem::send(_socket, c_str(command));
  54. // Wait for response
  55. Array<char> response(default_allocator());
  56. network_filesystem::read_response(_socket, response);
  57. // Parse the response
  58. JSONParser json(array::begin(response));
  59. JSONElement root = json.root();
  60. // DynamicString data_base64;
  61. // root.key("data").to_string(data_base64);
  62. // size_t out_len = 0;
  63. // unsigned char* data = base64_decode(data_base64.c_str(), data_base64.length(), &out_len);
  64. // memcpy(buffer, data, sizeof(unsigned char) * out_len);
  65. // default_allocator().deallocate(data);
  66. }
  67. void NetworkFile::write(const void* /*buffer*/, size_t /*size*/)
  68. {
  69. CE_FATAL("Cannot write to a network file");
  70. }
  71. bool NetworkFile::copy_to(File& file, size_t size)
  72. {
  73. return true;
  74. }
  75. bool NetworkFile::end_of_file()
  76. {
  77. return position() == size();
  78. }
  79. bool NetworkFile::is_valid()
  80. {
  81. return true;
  82. }
  83. void NetworkFile::flush()
  84. {
  85. // Do nothing
  86. }
  87. size_t NetworkFile::position()
  88. {
  89. return _position;
  90. }
  91. size_t NetworkFile::size()
  92. {
  93. using namespace string_stream;
  94. TempAllocator1024 alloc;
  95. StringStream command(alloc);
  96. // Request the file
  97. command << "{\"type\":\"filesystem\",\"filesystem\":\"size\",";
  98. command << "\"file\":\"" << _filename << "\"}";
  99. network_filesystem::send(_socket, c_str(command));
  100. // Wait for response
  101. Array<char> response(default_allocator());
  102. network_filesystem::read_response(_socket, response);
  103. JSONParser parser(array::begin(response));
  104. JSONElement root = parser.root();
  105. return (size_t) root.key("size").to_int();
  106. }
  107. bool NetworkFile::can_read() const
  108. {
  109. return true;
  110. }
  111. bool NetworkFile::can_write() const
  112. {
  113. return false;
  114. }
  115. bool NetworkFile::can_seek() const
  116. {
  117. return true;
  118. }
  119. } // namespace crown