MPD  0.20.18
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 #include <sys/stat.h>
35 
36 struct nfsfh;
37 class NfsConnection;
38 
48  enum class State {
49  INITIAL,
50  DEFER,
51  MOUNT,
52  OPEN,
53  STAT,
54  READ,
55  IDLE,
56  };
57 
58  State state;
59 
60  std::string server, export_name;
61  const char *path;
62 
63  NfsConnection *connection;
64 
65  nfsfh *fh;
66 
67 public:
68  NfsFileReader();
70 
71  void Close();
72  void DeferClose();
73 
79  void Open(const char *uri);
80 
91  void Read(uint64_t offset, size_t size);
92 
99  void CancelRead();
100 
101  bool IsIdle() const {
102  return state == State::IDLE;
103  }
104 
105 protected:
113  virtual void OnNfsFileOpen(uint64_t size) = 0;
114 
120  virtual void OnNfsFileRead(const void *data, size_t size) = 0;
121 
127  virtual void OnNfsFileError(std::exception_ptr &&e) = 0;
128 
129 private:
134  void CancelOrClose();
135 
136  void OpenCallback(nfsfh *_fh);
137  void StatCallback(const struct stat *st);
138 
139  /* virtual methods from NfsLease */
140  void OnNfsConnectionReady() final;
141  void OnNfsConnectionFailed(std::exception_ptr e) final;
142  void OnNfsConnectionDisconnected(std::exception_ptr e) final;
143 
144  /* virtual methods from NfsCallback */
145  void OnNfsCallback(unsigned status, void *data) final;
146  void OnNfsError(std::exception_ptr &&e) final;
147 
148  /* virtual methods from DeferredMonitor */
149  void RunDeferred() final;
150 };
151 
152 #endif
A helper class which helps with reading from a file.
Definition: FileReader.hxx:47
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.
bool IsIdle() const
Definition: FileReader.hxx:101
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...
The connection is now in "idle" mode, and no response shall be generated.
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