Network/HTTP.cpp

Sat, 08 Mar 2014 13:55:46 +0200

author
larukedi
date
Sat, 08 Mar 2014 13:55:46 +0200
changeset 401
85c7261e590a
parent 199
e6acf22bd64f
permissions
-rw-r--r--

added LICENSE and README.md files

153
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
1 #include "../Engine/OS.h"
199
e6acf22bd64f * Merged with 3.1 branch
cemkalyoncu
parents: 153
diff changeset
2
e6acf22bd64f * Merged with 3.1 branch
cemkalyoncu
parents: 153
diff changeset
3 #ifdef WIN32
e6acf22bd64f * Merged with 3.1 branch
cemkalyoncu
parents: 153
diff changeset
4 # undef APIENTRY
e6acf22bd64f * Merged with 3.1 branch
cemkalyoncu
parents: 153
diff changeset
5 # undef WINGDIAPI
e6acf22bd64f * Merged with 3.1 branch
cemkalyoncu
parents: 153
diff changeset
6 #endif
e6acf22bd64f * Merged with 3.1 branch
cemkalyoncu
parents: 153
diff changeset
7
e6acf22bd64f * Merged with 3.1 branch
cemkalyoncu
parents: 153
diff changeset
8
150
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
9 #include "../External/cURL/curl.h"
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
10 #include <stdexcept>
153
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
11 #include <cstring>
150
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
12 #include "HTTP.h"
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
13
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
14
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
15
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
16 namespace gge { namespace network {
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
17 namespace http {
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
18
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
19
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
20 void Initialize() {
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
21 curl_global_init(CURL_GLOBAL_ALL);
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
22 }
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
23
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
24 static size_t stringwriter(void *ptr, size_t size, size_t nmemb, void *stream) {
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
25 std::string &s=*(std::string*)stream;
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
26 s+=std::string((char*)ptr,size*nmemb);
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
27 return size*nmemb;
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
28 }
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
29
153
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
30 static size_t streamwriter(void *ptr, size_t size, size_t nmemb, void *stream) {
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
31 std::ostream &s=*(std::ostream*)stream;
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
32 s.write((char*)ptr,size*nmemb);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
33 if(s.bad())
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
34 return 0;
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
35 else
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
36 return size*nmemb;
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
37 }
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
38
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
39 static size_t vectorwriter(void *ptr, size_t size, size_t nmemb, void *vec) {
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
40 std::vector<Byte> &s=*(std::vector<Byte>*)vec;
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
41
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
42 auto prevsize=s.size();
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
43 s.resize(s.size()+size*nmemb);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
44 std::memcpy(&s[prevsize], ptr, size*nmemb);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
45
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
46 return size*nmemb;
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
47 }
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
48
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
49 static Error translateerror(CURLcode res) {
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
50 Error err;
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
51
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
52 switch(res) {
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
53 case CURLE_OK:
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
54 break;
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
55 case CURLE_UNSUPPORTED_PROTOCOL:
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
56 case CURLE_URL_MALFORMAT:
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
57 err=Error("Bad URL", Error::BadURL);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
58 case CURLE_COULDNT_RESOLVE_HOST:
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
59 err=Error("Cannot resolve host name", Error::HostResolutionFailed);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
60 case CURLE_COULDNT_CONNECT:
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
61 err=Error("Cannot connect to the host", Error::ConnectionFailed);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
62 case CURLE_REMOTE_ACCESS_DENIED:
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
63 err=Error("Access denied", Error::AccessDenied);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
64 case CURLE_OUT_OF_MEMORY:
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
65 err=Error("Out of memory", Error::OutOfMemory);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
66 case CURLE_LOGIN_DENIED:
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
67 err=Error("Login error", Error::LoginError);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
68 case CURLE_HTTP_RETURNED_ERROR:
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
69 err=Error("Page not found", Error::PageNotFound);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
70 default:
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
71 err=Error("Unknown error", Error::Unknown);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
72 }
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
73
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
74 return err;
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
75 }
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
76
150
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
77 std::string BlockingGetText(const std::string &URL) {
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
78 CURL *curl_handle = curl_easy_init();
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
79
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
80 if(!curl_handle)
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
81 throw std::runtime_error("Cannot create curl handle. Initialization missing?");
153
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
82
150
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
83 std::string s;
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
84
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
85 curl_easy_setopt(curl_handle, CURLOPT_URL, URL.c_str());
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
86 curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
87 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, stringwriter);
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
88 curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1L);
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
89 curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void*)&s);
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
90 curl_easy_setopt(curl_handle, CURLOPT_FAILONERROR, 1L);
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
91
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
92 CURLcode res=curl_easy_perform(curl_handle);
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
93
153
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
94 Error err=translateerror(res);
150
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
95
153
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
96 if(err.error)
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
97 throw err;
150
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
98
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
99 curl_easy_cleanup(curl_handle);
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
100
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
101 return s;
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
102 }
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
103
199
e6acf22bd64f * Merged with 3.1 branch
cemkalyoncu
parents: 153
diff changeset
104 int threadfncall nonblockingop(void *data) {
153
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
105 Nonblocking *me=(Nonblocking*)data;
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
106 CURLcode res=curl_easy_perform(me->curl);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
107
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
108 me->err=translateerror(res);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
109 me->isrunning=false;
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
110
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
111 return 0;
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
112 }
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
113
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
114 void Nonblocking::GetText(const std::string &URL) {
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
115 tempstr="";
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
116
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
117 if(isrunning)
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
118 throw std::runtime_error("Running another task at the moment.");
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
119
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
120 isrunning=true;
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
121 currentoperation=Text;
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
122
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
123
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
124 curl_easy_setopt(curl, CURLOPT_URL, URL.c_str());
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
125 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
126 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, stringwriter);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
127 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
128 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&tempstr);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
129 curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
130
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
131 os::RunInNewThread(&nonblockingop, this);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
132 }
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
133
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
134 void Nonblocking::GetFile(const std::string &URL, const std::string &filename) {
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
135 tempfile.open(filename, std::ios::binary);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
136
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
137 if(isrunning)
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
138 throw std::runtime_error("Running another task at the moment.");
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
139
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
140 isrunning=true;
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
141 currentoperation=File;
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
142
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
143 stream(URL, tempfile);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
144 }
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
145
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
146 void Nonblocking::GetStream(const std::string &URL, std::ostream &stream) {
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
147 if(isrunning)
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
148 throw std::runtime_error("Running another task at the moment.");
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
149
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
150 isrunning=true;
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
151 currentoperation=Stream;
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
152
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
153 this->stream(URL, stream);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
154 }
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
155
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
156 void Nonblocking::stream(const std::string &URL, std::ostream &stream) {
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
157
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
158 curl_easy_setopt(curl, CURLOPT_URL, URL.c_str());
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
159 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
160 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &streamwriter);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
161 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
162 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&stream);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
163 curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
164
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
165 os::RunInNewThread(&nonblockingop, this);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
166 }
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
167
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
168 void Nonblocking::GetData(const std::string &URL) {
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
169 if(isrunning)
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
170 throw std::runtime_error("Running another task at the moment.");
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
171
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
172 if(currentoperation==Data) {
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
173 gge::utils::CheckAndDelete(tempvec);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
174 }
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
175
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
176 isrunning=true;
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
177 currentoperation=Data;
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
178
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
179 tempvec=new std::vector<Byte>;
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
180
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
181 getdata(URL, *tempvec);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
182 }
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
183 void Nonblocking::GetData(const std::string &URL, std::vector<Byte> &vec) {
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
184 if(isrunning)
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
185 throw std::runtime_error("Running another task at the moment.");
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
186
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
187 if(currentoperation==Data) {
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
188 gge::utils::CheckAndDelete(tempvec);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
189 }
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
190
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
191 isrunning=true;
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
192 currentoperation=OwnedData;
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
193
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
194 tempvec=&vec;
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
195
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
196 getdata(URL, vec);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
197 }
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
198
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
199 void Nonblocking::getdata(const std::string &URL, std::vector<Byte> &vec) {
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
200
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
201 curl_easy_setopt(curl, CURLOPT_URL, URL.c_str());
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
202 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
203 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, vectorwriter);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
204 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
205 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&vec);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
206 curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
207
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
208 os::RunInNewThread(&nonblockingop, this);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
209 }
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
210
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
211 Nonblocking::Nonblocking() : isrunning(false),
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
212 TextTransferComplete("Text", this),
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
213 DataTransferComplete("Data", this),
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
214 FileTransferComplete("File", this)
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
215 {
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
216 curl=curl_easy_init();
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
217
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
218 token=gge::Main.BeforeRenderEvent.RegisterLambda([&]() {
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
219 if(currentoperation!=None && !isrunning) {
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
220 if(err.error!=0) {
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
221 TransferError(err);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
222 }
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
223 else {
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
224 switch(currentoperation) {
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
225 case Text:
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
226 TextTransferComplete(tempstr);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
227 break;
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
228 case Data:
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
229 DataTransferComplete(*tempvec);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
230 if(!isrunning) {
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
231 gge::utils::CheckAndDelete(tempvec);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
232 }
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
233 break;
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
234 case OwnedData:
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
235 DataTransferComplete(*tempvec);
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
236 tempvec=NULL;
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
237 break;
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
238 case File:
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
239 tempfile.close();
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
240 FileTransferComplete();
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
241 break;
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
242 case Stream:
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
243 FileTransferComplete();
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
244 break;
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
245 }
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
246 }
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
247 }
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
248 });
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
249
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
250 if(!curl)
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
251 throw std::runtime_error("Cannot create curl handle. Initialization missing?");
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
252 }
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
253
150
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
254
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
255 }
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
256
153
deef047528cf * Flip effect reverted to normal mode
cemkalyoncu
parents: 150
diff changeset
257
150
e91632ce48da * HTTP support (partial)
cemkalyoncu
parents:
diff changeset
258 }}

mercurial