file_access_network.h
1 /*************************************************************************/
2 /* file_access_network.h */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* http://www.godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
9 /* */
10 /* Permission is hereby granted, free of charge, to any person obtaining */
11 /* a copy of this software and associated documentation files (the */
12 /* "Software"), to deal in the Software without restriction, including */
13 /* without limitation the rights to use, copy, modify, merge, publish, */
14 /* distribute, sublicense, and/or sell copies of the Software, and to */
15 /* permit persons to whom the Software is furnished to do so, subject to */
16 /* the following conditions: */
17 /* */
18 /* The above copyright notice and this permission notice shall be */
19 /* included in all copies or substantial portions of the Software. */
20 /* */
21 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
22 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
23 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
24 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
25 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
26 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
27 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
28 /*************************************************************************/
29 #ifndef FILE_ACCESS_NETWORK_H
30 #define FILE_ACCESS_NETWORK_H
31 
32 #include "os/file_access.h"
33 #include "os/semaphore.h"
34 #include "os/thread.h"
35 #include "io/stream_peer_tcp.h"
36 
37 class FileAccessNetwork;
38 
40 
41 
42  struct BlockRequest {
43 
44  int id;
45  uint64_t offset;
46  int size;
47  };
48 
49  int ml;
50 
51  List<BlockRequest> block_requests;
52 
53  Semaphore *sem;
54  Thread *thread;
55  bool quit;
56  Mutex *mutex;
57  Mutex *blockrequest_mutex;
59  Ref<StreamPeerTCP> client;
60  int last_id;
61 
62  Vector<uint8_t> block;
63 
64  void _thread_func();
65  static void _thread_func(void *s);
66 
67  void put_32(int p_32);
68  void put_64(int64_t p_64);
69  int get_32();
70  int64_t get_64();
71  int lockcount;
72  void lock_mutex();
73  void unlock_mutex();
74 
75 friend class FileAccessNetwork;
76  static FileAccessNetworkClient *singleton;
77 
78 public:
79 
80  static FileAccessNetworkClient *get_singleton() { return singleton; }
81 
82  Error connect(const String& p_host,int p_port,const String& p_password="");
83 
86 
87 };
88 
89 class FileAccessNetwork : public FileAccess {
90 
91  Semaphore *sem;
92  Semaphore *page_sem;
93  Mutex *buffer_mutex;
94  bool opened;
95  size_t total_size;
96  mutable size_t pos;
97  int id;
98  mutable bool eof_flag;
99  mutable int last_page;
100  mutable uint8_t *last_page_buff;
101 
102  uint32_t page_size;
103  int read_ahead;
104  int max_pages;
105 
106  mutable int waiting_on_page;
107  mutable int last_activity_val;
108  struct Page {
109  int activity;
110  bool queued;
111  Vector<uint8_t> buffer;
112  Page() { activity=0; queued=false; }
113  };
114 
115  mutable Vector< Page > pages;
116 
117  mutable Error response;
118 
119  uint64_t exists_modtime;
120 friend class FileAccessNetworkClient;
121  void _queue_page(int p_page) const;
122  void _respond(size_t p_len,Error p_status);
123  void _set_block(size_t p_offset,const Vector<uint8_t>& p_block);
124 
125 public:
126 
127  enum Command {
128  COMMAND_OPEN_FILE,
129  COMMAND_READ_BLOCK,
130  COMMAND_CLOSE,
131  COMMAND_FILE_EXISTS,
132  COMMAND_GET_MODTIME,
133  };
134 
135  enum Response {
136  RESPONSE_OPEN,
137  RESPONSE_DATA,
138  RESPONSE_FILE_EXISTS,
139  RESPONSE_GET_MODTIME,
140  };
141 
142 
143  virtual Error _open(const String& p_path, int p_mode_flags);
144  virtual void close();
145  virtual bool is_open() const;
146 
147  virtual void seek(size_t p_position);
148  virtual void seek_end(int64_t p_position=0);
149  virtual size_t get_pos() const;
150  virtual size_t get_len() const;
151 
152  virtual bool eof_reached() const;
153 
154  virtual uint8_t get_8() const;
155  virtual int get_buffer(uint8_t *p_dst, int p_length) const;
156 
157  virtual Error get_error() const;
158 
159  virtual void store_8(uint8_t p_dest);
160 
161  virtual bool file_exists(const String& p_path);
162 
163  virtual uint64_t _get_modified_time(const String& p_file);
164 
167 };
168 
169 #endif // FILE_ACCESS_NETWORK_H
Definition: mutex.h:45
Definition: file_access_network.h:89
Definition: thread.h:42
Definition: file_access_network.h:39
Definition: semaphore.h:37
Definition: ustring.h:64
Definition: file_access.h:40