lib/nfs: add more API documentation

This commit is contained in:
Max Kellermann 2017-01-10 23:15:16 +01:00
parent e847ddf011
commit 6c293a3d7f
2 changed files with 55 additions and 0 deletions

View File

@ -24,9 +24,21 @@
#include <exception> #include <exception>
/**
* Callbacks for an asynchronous libnfs operation.
*
* Note that no callback is invoked for cancelled operations.
*/
class NfsCallback { class NfsCallback {
public: public:
/**
* The operation completed successfully.
*/
virtual void OnNfsCallback(unsigned status, void *data) = 0; virtual void OnNfsCallback(unsigned status, void *data) = 0;
/**
* An error has occurred.
*/
virtual void OnNfsError(std::exception_ptr &&e) = 0; virtual void OnNfsError(std::exception_ptr &&e) = 0;
}; };

View File

@ -35,6 +35,14 @@
struct nfsfh; struct nfsfh;
class NfsConnection; class NfsConnection;
/**
* A helper class which helps with reading from a file. It obtains a
* connection lease (#NfsLease), opens the given file, "stats" the
* file, and finally allos you to read its contents.
*
* To get started, derive your class from it and implement the pure
* virtual methods, construct an instance, and call Open().
*/
class NfsFileReader : NfsLease, NfsCallback, DeferredMonitor { class NfsFileReader : NfsLease, NfsCallback, DeferredMonitor {
enum class State { enum class State {
INITIAL, INITIAL,
@ -63,14 +71,30 @@ public:
void DeferClose(); void DeferClose();
/** /**
* Open the file. This method is thread-safe.
*
* Throws std::runtime_error on error. * Throws std::runtime_error on error.
*/ */
void Open(const char *uri); void Open(const char *uri);
/** /**
* Attempt to read from the file. This may only be done after
* OnNfsFileOpen() has been called. Only one read operation
* may be performed at a time.
*
* This method is not thread-safe and must be called from
* within the I/O thread.
*
* Throws std::runtime_error on error. * Throws std::runtime_error on error.
*/ */
void Read(uint64_t offset, size_t size); void Read(uint64_t offset, size_t size);
/**
* Cancel the most recent Read() call.
*
* This method is not thread-safe and must be called from
* within the I/O thread.
*/
void CancelRead(); void CancelRead();
bool IsIdle() const { bool IsIdle() const {
@ -78,8 +102,27 @@ public:
} }
protected: protected:
/**
* The file has been opened successfully. It is a regular
* file, and its size is known. It is ready to be read from
* using Read().
*
* This method will be called from within the I/O thread.
*/
virtual void OnNfsFileOpen(uint64_t size) = 0; virtual void OnNfsFileOpen(uint64_t size) = 0;
/**
* A Read() has completed successfully.
*
* This method will be called from within the I/O thread.
*/
virtual void OnNfsFileRead(const void *data, size_t size) = 0; virtual void OnNfsFileRead(const void *data, size_t size) = 0;
/**
* An error has occurred, which can be either while waiting
* for OnNfsFileOpen(), or while waiting for OnNfsFileRead(),
* or if disconnected while idle.
*/
virtual void OnNfsFileError(std::exception_ptr &&e) = 0; virtual void OnNfsFileError(std::exception_ptr &&e) = 0;
private: private: