Overte C++ Documentation
AssetRequest.h
1 //
2 // AssetRequest.h
3 // libraries/networking/src
4 //
5 // Created by Ryan Huffman on 2015/07/24
6 // Copyright 2015 High Fidelity, Inc.
7 //
8 // Distributed under the Apache License, Version 2.0.
9 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10 //
11 
12 #ifndef hifi_AssetRequest_h
13 #define hifi_AssetRequest_h
14 
15 #include <QByteArray>
16 #include <QObject>
17 #include <QString>
18 
19 #include "AssetClient.h"
20 #include "AssetUtils.h"
21 
22 #include "ByteRange.h"
23 
24 const QString ATP_SCHEME { "atp:" };
25 
26 class AssetRequest : public QObject {
27  Q_OBJECT
28 public:
29  enum State {
30  NotStarted = 0,
31  WaitingForData,
32  Finished
33  };
34 
35  enum Error {
36  NoError,
37  NotFound,
38  InvalidByteRange,
39  InvalidHash,
40  HashVerificationFailed,
41  SizeVerificationFailed,
42  NetworkError,
43  UnknownError
44  };
45  Q_ENUM(Error)
46  AssetRequest(const QString& hash, const ByteRange& byteRange = ByteRange());
47  virtual ~AssetRequest() override;
48 
49  Q_INVOKABLE void start();
50 
51  const QByteArray& getData() const { return _data; }
52  const State& getState() const { return _state; }
53  const Error& getError() const { return _error; }
54  const QString getErrorString() const;
55  QUrl getUrl() const { return AssetUtils::getATPUrl(_hash); }
56  QString getHash() const { return _hash; }
57 
58  bool loadedFromCache() const { return _loadedFromCache; }
59 
60 signals:
61  void finished(AssetRequest* thisRequest);
62  void progress(qint64 totalReceived, qint64 total);
63 
64 private:
65  int _requestID;
66  State _state = NotStarted;
67  Error _error = NoError;
68  uint64_t _totalReceived { 0 };
69  QString _hash;
70  QByteArray _data;
71  int _numPendingRequests { 0 };
72  MessageID _assetRequestID { INVALID_MESSAGE_ID };
73  const ByteRange _byteRange;
74  bool _loadedFromCache { false };
75 };
76 
77 #endif