MPD  0.20.6
FileReader.hxx
Go to the documentation of this file.
1 /*
2  * Copyright 2003-2017 The Music Player Daemon Project
3  * http://www.musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef MPD_NFS_FILE_READER_HXX
21 #define MPD_NFS_FILE_READER_HXX
22 
23 #include "check.h"
24 #include "Lease.hxx"
25 #include "Callback.hxx"
27 #include "Compiler.h"
28 
29 #include <string>
30 #include <exception>
31 
32 #include <stdint.h>
33 #include <stddef.h>
34 
35 struct nfsfh;
36 class NfsConnection;
37 
47  enum class State {
48  INITIAL,
49  DEFER,
50  MOUNT,
51  OPEN,
52  STAT,
53  READ,
54  IDLE,
55  };
56 
57  State state;
58 
59  std::string server, export_name;
60  const char *path;
61 
62  NfsConnection *connection;
63 
64  nfsfh *fh;
65 
66 public:
67  NfsFileReader();
69 
70  void Close();
71  void DeferClose();
72 
78  void Open(const char *uri);
79 
90  void Read(uint64_t offset, size_t size);
91 
98  void CancelRead();
99 
100  bool IsIdle() const {
101  return state == State::IDLE;
102  }
103 
104 protected:
112  virtual void OnNfsFileOpen(uint64_t size) = 0;
113 
119  virtual void OnNfsFileRead(const void *data, size_t size) = 0;
120 
126  virtual void OnNfsFileError(std::exception_ptr &&e) = 0;
127 
128 private:
133  void CancelOrClose();
134 
135  void OpenCallback(nfsfh *_fh);
136  void StatCallback(const struct stat *st);
137 
138  /* virtual methods from NfsLease */
139  void OnNfsConnectionReady() final;
140  void OnNfsConnectionFailed(std::exception_ptr e) final;
141  void OnNfsConnectionDisconnected(std::exception_ptr e) final;
142 
143  /* virtual methods from NfsCallback */
144  void OnNfsCallback(unsigned status, void *data) final;
145  void OnNfsError(std::exception_ptr &&e) final;
146 
147  /* virtual methods from DeferredMonitor */
148  void RunDeferred() final;
149 };
150 
151 #endif
A helper class which helps with reading from a file.
Definition: FileReader.hxx:46
Defer execution of an event into an EventLoop.
virtual void OnNfsFileRead(const void *data, size_t size)=0
A Read() has completed successfully.
void Read(uint64_t offset, size_t size)
Attempt to read from the file.
virtual void OnNfsFileOpen(uint64_t size)=0
The file has been opened successfully.
virtual void OnNfsFileError(std::exception_ptr &&e)=0
An error has occurred, which can be either while waiting for OnNfsFileOpen(), or while waiting for On...
bool IsIdle() const
Definition: FileReader.hxx:100
void Open(const char *uri)
Open the file.
An asynchronous connection to a NFS server.
Definition: Connection.hxx:42
void CancelRead()
Cancel the most recent Read() call.
void DeferClose()
Callbacks for an asynchronous libnfs operation.
Definition: Callback.hxx:32
int e
Definition: Log.hxx:115
const Storage const char * uri