Oxygine  1
2g game engine
HttpRequestTask.h
1 #pragma once
2 #include "oxygine-include.h"
3 #include "AsyncTask.h"
4 #include <vector>
5 #include <string>
6 #include "core/file.h"
7 
8 namespace oxygine
9 {
10  DECLARE_SMART(HttpRequestTask, spHttpRequestTask);
11  class HttpRequestTask: public AsyncTask
12  {
13  public:
14  static spHttpRequestTask create();
15  typedef HttpRequestTask* (*createHttpRequestCallback)();
16  typedef std::function< bool(int) > responseCodeChecker;
17  static void setCustomRequests(createHttpRequestCallback);
18  static void init();
19  static void release();
20 
21 
23  enum
24  {
25  ERROR = AsyncTask::ERROR,
26  PROGRESS = AsyncTask::PROGRESS,
27  COMPLETE = AsyncTask::COMPLETE,
28  };
29 
30  class ProgressEvent : public Event
31  {
32  public:
33  enum {EVENT = PROGRESS};
34  ProgressEvent(size_t Delta, size_t Loaded, size_t Total, bool First) : Event(PROGRESS), delta(Delta), loaded(Loaded), total(Total), first(First) {};
35 
36  size_t delta;
37  size_t loaded;
38  size_t total;
39  bool first;
40  };
41 
43  ~HttpRequestTask();
44 
45  const std::vector<unsigned char>& getResponse() const;
46  const std::vector<unsigned char>& getPostData() const;
47  const std::string& getFileName() const;
48  const std::string& getUrl() const;
49  size_t getReceivedSize() const;
50  size_t getExpectedSize() const;
51 
53  void getResponseSwap(std::vector<unsigned char>&);
54  int getResponseCode() const { return _responseCode; }
55  const responseCodeChecker& getResponseCodeChecker() const {return _responseCodeChecker;}
56  void addHeader(const std::string& key, const std::string& value);
57 
58  void setPostData(const std::vector<unsigned char>& data);
59  void setUrl(const std::string& url);
60  void setFileName(const std::string& name, bool continueDownload = false);
61  void setCacheEnabled(bool enabled);
62 
63 
64  void setResponseCodeChecker(const responseCodeChecker& f) {_responseCodeChecker = f;}
65  void setSuccessOnAnyResponseCode(bool en);
66  void setExpectedSize(size_t size) { _expectedContentSize = size; }
67 
68  protected:
69  bool _prerun() override;
70  void _onError() override;
71  void _onComplete() override;
72  void _dispatchComplete() override;
73  void _finalize(bool error) override;
74 
75  std::string _getRunInfo() const override { return _url; }
76 
77  void gotHeaders();
78  void write(const void* data, size_t size);
79 
80  //async
81  void asyncProgress(size_t delta, size_t loaded, size_t total);
82 
83  void dispatchProgress(size_t delta, size_t loaded, size_t total);
84 
85  virtual void _setFileName(const std::string& name) {}
86  virtual void _setUrl(const std::string& url) {}
87  virtual void _setPostData(const std::vector<unsigned char>& data) {}
88  virtual void _setCacheEnabled(bool enabled) {}
89  virtual void _addHeader(const std::string& key, const std::string& value) {}
90 
91  std::string _url;
92  std::string _fname;
93  file::handle _fhandle;
94  bool _writeFileError;
95  bool _cacheEnabled;
96  bool _firstTimeProgressDispatched;
97  bool _progressOnWrite;
98 
99  bool _progressDispatched;
100  unsigned int _progressDeltaDelayed;
101 
102  std::vector<unsigned char> _response;
103  std::vector<unsigned char> _postData;
104 
105  responseCodeChecker _responseCodeChecker;
106 
107  bool _suitableResponse;
108 
109  int _responseCode;
110  size_t _expectedContentSize;
111  size_t _receivedContentSize;
112 
113  bool _continueDownload;
114 
115  typedef std::vector< std::pair<std::string, std::string> > headers;
116  headers _headers;
117  };
118 }
Definition: HttpRequestTask.h:30
Definition: HttpRequestTask.h:11
Definition: Event.h:10
–oxgl-end–!
Definition: Actor.h:14
void getResponseSwap(std::vector< unsigned char > &)
Definition: AsyncTask.h:23