ManaPlus
download.h
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2009-2010 The Mana Developers
4  * Copyright (C) 2011-2019 The ManaPlus Developers
5  * Copyright (C) 2019-2021 Andrei Karas
6  *
7  * This file is part of The ManaPlus Client.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #ifndef NET_DOWNLOAD_H
24 #define NET_DOWNLOAD_H
25 
27 
28 #include <curl/curl.h>
29 
30 #include <string>
31 #include <queue>
32 
33 #include "localconsts.h"
34 
35 typedef int (*DownloadUpdate)(void *ptr,
36  const DownloadStatusT status,
37  size_t total,
38  const size_t remaining);
39 
40 // Matches what CURL expects
41 typedef size_t (*WriteFunction)( void *ptr, size_t size, size_t nmemb,
42  void *stream);
43 
44 struct SDL_Thread;
45 
46 namespace Net
47 {
49 {
50  public:
51  Download(void *const ptr,
52  const std::string &url,
53  const DownloadUpdate updateFunction,
54  const bool ignoreError,
55  const bool isUpload,
56  const bool isXml);
57 
59 
60  ~Download();
61 
62  void addHeader(const std::string &header);
63 
67  void noCache();
68 
69  void setFile(const std::string &filename,
70  const int64_t adler32);
71 
73 
80  bool start();
81 
86  void cancel();
87 
88  void addMirror(const std::string &str)
89  { mUrlQueue.push(str); }
90 
91  const char *getError() const A_WARN_UNUSED;
92 
93  void setIgnoreError(const bool n)
94  { mIgnoreError = n; }
95 
96  static size_t writeFunction(void *ptr, size_t size,
97  size_t nmemb, void *stream);
98 
99  static void prepareForm(curl_httppost **form,
100  const std::string &fileName);
101 
102  static unsigned long fadler32(FILE *const file) A_WARN_UNUSED;
103 
104  static void addProxy(CURL *const curl);
105 
106  static void secureCurl(CURL *const curl);
107 
108  static void addHeaders(CURL *const curl);
109 
110  static void addCommonFlags(CURL *const curl);
111 
112  static unsigned long adlerBuffer(const char *const buffer, int size);
113 
114  static std::string getUploadResponse()
115  { return mUploadResponse; }
116 
117  private:
118  static int downloadThread(void *ptr);
119  static int downloadProgress(void *clientp, double dltotal,
120  double dlnow, double ultotal,
121  double ulnow);
122  static std::string mUploadResponse;
123  void *mPtr;
124  std::string mUrl;
125  struct
126  {
127  unsigned cancel : 1;
128  unsigned memoryWrite: 1;
129  unsigned checkAdler: 1;
131  std::string mFileName;
132  std::queue<std::string> mUrlQueue;
134  unsigned long mAdler;
136  SDL_Thread *mThread;
137  CURL *mCurl;
138  curl_slist *mHeaders;
139  curl_httppost *mFormPost;
140  char *mError;
142  bool mUpload;
143  bool mIsXml;
144 };
145 
146 } // namespace Net
147 
148 #endif // NET_DOWNLOAD_H
static std::string mUploadResponse
Definition: download.h:122
void setFile(const std::string &filename, const int64_t adler32)
Definition: download.cpp:166
unsigned memoryWrite
Definition: download.h:128
static unsigned long fadler32(FILE *const file)
Definition: download.cpp:123
bool mUpload
Definition: download.h:142
void addHeader(const std::string &header)
Definition: download.cpp:155
static void addCommonFlags(CURL *const curl)
Definition: download.cpp:613
static void secureCurl(CURL *const curl)
Definition: download.cpp:574
unsigned cancel
Definition: download.h:127
char * mError
Definition: download.h:140
static unsigned long adlerBuffer(const char *const buffer, int size)
Definition: download.cpp:147
static void addProxy(CURL *const curl)
Definition: download.cpp:509
unsigned checkAdler
Definition: download.h:129
std::queue< std::string > mUrlQueue
Definition: download.h:132
void addMirror(const std::string &str)
Definition: download.h:88
void setWriteFunction(WriteFunction write)
Definition: download.cpp:182
void setIgnoreError(const bool n)
Definition: download.h:93
WriteFunction mWriteFunction
Definition: download.h:133
unsigned long mAdler
Definition: download.h:134
void * mPtr
Definition: download.h:123
static void prepareForm(curl_httppost **form, const std::string &fileName)
Definition: download.cpp:621
struct Net::Download::@15 mOptions
void noCache()
Definition: download.cpp:160
const char * getError() const
Definition: download.cpp:218
curl_httppost * mFormPost
Definition: download.h:139
Download(void *const ptr, const std::string &url, const DownloadUpdate updateFunction, const bool ignoreError, const bool isUpload, const bool isXml)
Definition: download.cpp:57
static std::string getUploadResponse()
Definition: download.h:114
curl_slist * mHeaders
Definition: download.h:138
static void addHeaders(CURL *const curl)
Definition: download.cpp:608
static int downloadThread(void *ptr)
Definition: download.cpp:246
SDL_Thread * mThread
Definition: download.h:136
DownloadUpdate mUpdateFunction
Definition: download.h:135
std::string mFileName
Definition: download.h:131
CURL * mCurl
Definition: download.h:137
std::string mUrl
Definition: download.h:124
static size_t writeFunction(void *ptr, size_t size, size_t nmemb, void *stream)
Definition: download.cpp:643
static int downloadProgress(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
Definition: download.cpp:223
bool start()
Definition: download.cpp:188
bool mIgnoreError
Definition: download.h:141
int(* DownloadUpdate)(void *ptr, const DownloadStatusT status, size_t total, const size_t remaining)
Definition: download.h:35
size_t(* WriteFunction)(void *ptr, size_t size, size_t nmemb, void *stream)
Definition: download.h:41
DownloadStatus ::T DownloadStatusT
#define A_WARN_UNUSED
Definition: localconsts.h:161
#define final
Definition: localconsts.h:46
#define A_DELETE_COPY(func)
Definition: localconsts.h:53
bool url(InputEvent &event)
Definition: commands.cpp:64
int size()
Definition: emotedb.cpp:306
int64_t write(File *const file, const void *const buffer, const uint32_t objSize, const uint32_t objCount)
Definition: fs.cpp:826
std::string fileName
Definition: testmain.cpp:39