Mana
Loading...
Searching...
No Matches
download.h
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2009-2012 The Mana Developers
4 *
5 * This file is part of The Mana Client.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include "utils/mutex.h"
22
23#include <cstdio>
24#include <optional>
25#include <string>
26
27#include <curl/curl.h>
28
29#pragma once
30
32{
35 Error,
37};
38
39struct SDL_Thread;
40
41namespace Net {
42
44{
45 public:
51
52 Download(const std::string &url);
53 ~Download();
54
55 void addHeader(const char *header);
56
60 void noCache();
61
62 void setFile(const std::string &filename,
63 std::optional<unsigned long> adler32 = {});
64
65 void setUseBuffer();
66
71 bool start();
72
77 void cancel();
78
82 std::string_view getBuffer() const;
83
85
86 const char *getError() const;
87
88 static unsigned long fadler32(FILE *file);
89
90 private:
91 static int downloadProgress(void *clientp,
92 curl_off_t dltotal, curl_off_t dlnow,
93 curl_off_t ultotal, curl_off_t ulnow);
94
95 static size_t writeBuffer(char *ptr, size_t size, size_t nmemb,
96 void *stream);
97
98 static int downloadThread(void *ptr);
99
101 std::string mUrl;
102 bool mCancel = false;
103 bool mMemoryWrite = false;
104 std::string mFileName;
105 std::optional<unsigned long> mAdler;
106 SDL_Thread *mThread = nullptr;
107 curl_slist *mHeaders = nullptr;
108 char mError[CURL_ERROR_SIZE];
109
112
114 char *mBuffer = nullptr;
115};
116
118{
119 return *mState.lock();
120}
121
122inline const char *Download::getError() const
123{
124 return mError;
125}
126
127} // namespace Net
State getState()
Definition download.h:117
char * mBuffer
Buffer for files downloaded to memory.
Definition download.h:114
std::optional< unsigned long > mAdler
Definition download.h:105
void setFile(const std::string &filename, std::optional< unsigned long > adler32={})
Definition download.cpp:90
std::string_view getBuffer() const
Returns a view on the downloaded data.
Definition download.cpp:132
bool mMemoryWrite
Definition download.h:103
void noCache()
Convience method for adding no-cache headers.
Definition download.cpp:84
void cancel()
Cancels the download.
Definition download.cpp:126
char mError[CURL_ERROR_SIZE]
Definition download.h:108
static int downloadProgress(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
A libcurl callback for reporting progress.
Definition download.cpp:141
static size_t writeBuffer(char *ptr, size_t size, size_t nmemb, void *stream)
A libcurl callback for writing to memory.
Definition download.cpp:159
curl_slist * mHeaders
Definition download.h:107
static unsigned long fadler32(FILE *file)
Calculates the Alder-32 checksum for the given file.
Definition download.cpp:41
ThreadSafe< State > mState
Definition download.h:100
static int downloadThread(void *ptr)
Definition download.cpp:174
SDL_Thread * mThread
Definition download.h:106
void addHeader(const char *header)
Definition download.cpp:77
const char * getError() const
Definition download.h:122
std::string mFileName
Definition download.h:104
std::string mUrl
Definition download.h:101
size_t mDownloadedBytes
Byte count currently downloaded in mMemoryBuffer.
Definition download.h:111
void setUseBuffer()
Definition download.cpp:100
bool start()
Starts the download thread.
Definition download.cpp:107
A template class for wrapping data that is accessed by multiple threads.
Definition mutex.h:111
State
All client states.
Definition client.h:59
DownloadStatus
Definition download.h:32
The network communication layer.
DownloadStatus status
Definition download.h:48