Browse Source

fixed missing s3e files

dmuratshin 10 years ago
parent
commit
6f7df16dd8

+ 58 - 0
oxygine/src/core/s3e/HttpRequestTaskS3E.cpp

@@ -0,0 +1,58 @@
+#include "HttpRequestTaskS3E.h"
+
+spHttpRequestTask HttpRequestTask::create()
+{
+	return new HttpRequestTaskS3E;
+}
+
+void HttpRequestTask::init()
+{
+
+}
+
+void HttpRequestTask::release()
+{
+
+}
+
+
+HttpRequestTaskS3E::HttpRequestTaskS3E()
+{
+
+}
+
+void HttpRequestTaskS3E::_run()
+{
+	_http._cbDone = CLOSURE(this, &HttpRequestTaskS3E::onDone);
+	_http._cbError = CLOSURE(this, &HttpRequestTaskS3E::onError);
+	if (_fname.empty())
+	{
+		if (_postData.empty())
+			_http.get(_url);
+		else
+			_http.post(_url, (const char*)&_postData.front(), _postData.size());
+	}
+	else
+		_http.getFile(_url, _fname);
+
+	addRef();
+}
+
+void HttpRequestTaskS3E::onDone(MyHttp *)
+{
+	std::swap(_response, _http.getBuffer());
+	onComplete();
+	releaseRef();
+}
+
+void HttpRequestTaskS3E::onError(MyHttp *)
+{
+	AsyncTask::onError();
+	releaseRef();
+}
+
+
+spHttpRequestTask createHttpRequest()
+{
+	return new HttpRequestTaskS3E; 
+}

+ 18 - 0
oxygine/src/core/s3e/HttpRequestTaskS3E.h

@@ -0,0 +1,18 @@
+#pragma once
+#include "HttpRequestTask.h"
+#include "MyHttp.h"
+
+DECLARE_SMART(HttpRequestTaskS3E, spHttpRequestTaskS3E);
+class HttpRequestTaskS3E: public HttpRequestTask
+{
+public:
+	HttpRequestTaskS3E();
+	
+protected:
+	void _run();
+
+	void onDone(MyHttp *);
+	void onError(MyHttp *);
+	MyHttp _http;
+};
+

+ 313 - 0
oxygine/src/core/s3e/MyHttp.cpp

@@ -0,0 +1,313 @@
+#include "MyHttp.h"
+//#include "net/downloader.h"
+//#include "shared.h"
+//#include "utils/debug_tools.h"
+//#include "utils/net_utils.h"
+
+#define NETWORK_AVAILABLE() OX_ASSERT(s3eSocketGetInt(S3E_SOCKET_NETWORK_AVAILABLE) == 1)
+
+
+
+const int httpTimeout = 20000;
+
+const int bufSize = 1024 * 64;
+
+MyHttp::MyHttp() :_http(0), _status(status_none), _handle(0), _method(GETFILE)
+{
+
+}
+
+MyHttp::~MyHttp()
+{
+	destroy();
+}
+
+void MyHttp::destroy()
+{
+	if (_http)
+		_http->Cancel();
+	delete _http;
+	_http = 0;
+	if (_handle)
+		file::close(_handle);
+	_handle = 0;
+	_status = status_none;
+	_data.clear();
+	_tempBuffer.clear();
+}
+
+unsigned int MyHttp::getTotalSize() const
+{
+	return _http->ContentLength();
+}
+
+unsigned int MyHttp::getReceivedSize() const
+{
+	return _http->ContentReceived();
+}
+
+int MyHttp::_delayedError(void* systemData, void* userData)
+{
+	MyHttp *http = (MyHttp*)userData;
+	http->onError();
+	return 0;
+}
+
+void MyHttp::getFile(const string &url, const string &name)
+{
+	destroy();
+
+	_method = GETFILE;
+	_url = url;
+
+	log::messageln("getfile:");
+	puts(url.c_str());
+	
+	_status = status_inprogress;
+	_http = new CIwHTTP;
+
+	if (!name.empty())
+		_handle = file::open(name.c_str(), "wb");
+	
+	_http->Get(url.c_str(), _gotHeaders, this);
+}
+
+void MyHttp::get(const string &url)
+{
+	destroy();
+
+	_method = GET;
+	_url = url;
+	log::messageln("post: %s", url.c_str());
+	_status = status_inprogress;
+
+
+	_http = new CIwHTTP;
+	//_http->SetRequestHeader("Content-type", "application/x-www-form-urlencoded");
+
+	if (!isNetworkAvaible())
+	{
+		//it is too dangerous call onError from there
+		//do it at next update
+#ifdef __S3E__
+		s3eThreadEnqueueCallback(s3eThreadGetCurrent(), _delayedError, this);
+#endif
+		return;
+	}
+
+
+	_http->Get(url.c_str(), _gotHeaders, this);
+}
+
+void MyHttp::post(const string &url, const char *data, int size)
+{
+	destroy();
+
+	_post = data;
+	_postSize = size;
+	_method = POST;
+	_url = url;
+	log::messageln("post: %s", url.c_str());
+	_status = status_inprogress;
+
+
+	_http = new CIwHTTP;
+	_http->SetRequestHeader("Content-type", "application/x-www-form-urlencoded");
+
+	if (!isNetworkAvaible())
+	{
+		//it is too dangerous call onError from there
+		//do it at next update
+#ifdef __S3E__
+		s3eThreadEnqueueCallback(s3eThreadGetCurrent(), _delayedError, this);
+#endif
+		return;
+	}	
+
+	
+	_http->Post(url.c_str(), data, size, _gotHeaders, this);
+}
+
+int	MyHttp::getResponseCode() const
+{
+	return _http->GetResponseCode();
+}
+
+void MyHttp::gotHeaders()
+{
+	//log::messageln("gotHeaders");
+	if (_http->GetStatus() == S3E_RESULT_ERROR)
+	{
+		onError();
+	}
+	else
+	{
+		if (_cbProgress)
+			_cbProgress(this, 0);
+
+		int resp = _http->GetResponseCode();
+		if (resp != 200)
+		{
+			if (resp == 302)
+			{
+				string res;
+				_http->GetHeader("Location", res);
+				if (!res.empty())
+				{
+					switch (_method)
+					{
+					case MyHttp::GETFILE:
+						get(res);
+						break;
+					case MyHttp::GET:
+						getFile(res, "");
+						break;
+					case MyHttp::POST:
+						post(res, _post, _postSize);
+						break;
+					default:
+						break;
+					}
+					getFile(res, "");
+					return;
+				}
+			}
+			
+			_status = status_error;
+			if (_cbError) 
+				_cbError(this);
+			return;
+		}
+
+		int len = _http->ContentExpected();
+		if (!len)		
+			len = 1024;
+
+		if (!_handle)
+			_data.reserve(len);
+
+		len = min(bufSize, len);
+		_tempBuffer.resize(len);
+
+		_http->ReadDataAsync((char*)&_tempBuffer.front(), len, httpTimeout, _gotData, this);
+	}
+}
+
+void MyHttp::progress(int size)
+{
+	if (_cbProgress)
+		_cbProgress(this, size);
+
+	if (_handle)
+		file::write(_handle, &_tempBuffer.front(), size);
+	else
+		_data.insert(_data.end(), _tempBuffer.begin(), _tempBuffer.begin() + size);
+
+	
+	int rec = _http->ContentReceived();
+	/*
+	int32 v = 0;
+	_http->GetHeader("Content-Length", v);
+	string r;
+	_http->GetHeader("Content-Range", r);
+	*/
+	int ln = _http->ContentLength();
+
+	if (!ln)
+	{
+		//something is wrong
+		ln = rec + _http->ContentExpected();
+	}
+
+	//if (rec != ln)
+	if (!_http->ContentFinished())
+	{
+		int len = ln - rec;
+
+		len = min(bufSize, len);
+		_tempBuffer.resize(len);
+
+		_http->ReadDataAsync((char*)&_tempBuffer.front(), len, httpTimeout, _gotData, this);
+		return;
+	}
+
+	if (_handle)
+		file::close(_handle);
+	_handle = 0;
+	_status = status_done;
+	if (_cbDone) 
+		_cbDone(this);
+}
+
+void MyHttp::onError()
+{
+	log::messageln("http error: %s", _url.c_str());
+	if (_handle)
+		file::close(_handle);
+	_handle = 0;
+	_status = status_error;
+	if (_cbError) 
+		_cbError(this);
+}
+
+void MyHttp::gotData(int size)
+{
+	progress(size);
+}
+
+int MyHttp::_gotData (void* systemData, void* userData)
+{
+	MyHttp *http = (MyHttp *)userData;
+	http->gotData((int)systemData);
+	return 0;
+}
+
+int MyHttp::_gotHeaders (void* systemData, void* userData)
+{
+	MyHttp *http = (MyHttp *)userData;
+	http->gotHeaders();
+	return 0;
+}
+
+
+class SingleHttpAsyncRequest: public Object
+{
+public:
+	void get(const char *url)
+	{
+		_http.getFile(url, "");
+		_http._cbDone = CLOSURE(this, &SingleHttpAsyncRequest::_delete);
+		_http._cbError = CLOSURE(this, &SingleHttpAsyncRequest::_delete);
+	}
+
+	void post(const char *url, const char *data, int size)
+	{
+		_http.post(url, data, size);
+		_http._cbDone = CLOSURE(this, &SingleHttpAsyncRequest::_delete);
+		_http._cbError = CLOSURE(this, &SingleHttpAsyncRequest::_delete);
+	}
+
+	void _delete(MyHttp *)
+	{
+		delete this;
+	}
+	MyHttp _http;
+};
+
+void makeSingleHttpAsyncGetRequest(const char *url)
+{
+	if (!isNetworkAvaible())
+		return;
+
+	SingleHttpAsyncRequest *r = new SingleHttpAsyncRequest;		
+	r->get(url);
+}
+
+void makeSingleHttpAsyncPostRequest(const char *url, const char *data, int size)
+{
+	if (!isNetworkAvaible())
+		return;
+
+	SingleHttpAsyncRequest *r = new SingleHttpAsyncRequest;			
+	r->post(url, data, size);
+}

+ 76 - 0
oxygine/src/core/s3e/MyHttp.h

@@ -0,0 +1,76 @@
+#pragma once
+#include "core/oxygine.h"
+#include "closure/closure.h"
+#include "core/files_io.h"
+
+#include "IwHTTP.h"
+
+using namespace std;
+using namespace oxygine;
+
+class MyHttp
+{
+public:
+	typedef Closure<void (MyHttp *)> DownloadDoneCallback;
+	typedef Closure<void (MyHttp *)> ErrorCallback;
+	typedef Closure<void (MyHttp *, int)> ProgressCallback;
+
+	enum status
+	{
+		status_none,
+		status_inprogress,
+		status_done,
+		status_error
+	};
+
+	MyHttp();
+	virtual ~MyHttp();
+
+
+	DownloadDoneCallback	_cbDone;
+	ProgressCallback		_cbProgress;
+	ErrorCallback			_cbError;
+
+	void			getFile(const string &url, const string &name);
+	void			get(const string &url);
+	void			post(const string &url, const char *data, int size);
+
+
+	vector<unsigned char>&	getBuffer() {return _data;}
+	status			getStatus() const {return _status;}
+	unsigned int	getTotalSize() const;
+	unsigned int	getReceivedSize() const;
+	int				getResponseCode() const;
+
+private:
+
+	enum METHOD{GETFILE, GET, POST};
+	METHOD _method;
+	static int _gotHeaders (void* systemData, void* userData);
+	static int _gotData (void* systemData, void* userData);
+	static int _delayedError(void* systemData, void* userData);
+
+
+	void destroy();
+
+	void gotHeaders();
+	void gotData(int size);
+	void progress(int size);
+
+	void onError();
+
+	string _url;
+	const char *_post;
+	int _postSize;
+
+	vector<unsigned char> _tempBuffer;
+	vector<unsigned char> _data;
+	file::handle _handle;
+	CIwHTTP *_http;
+	
+	status _status;
+};
+
+
+void makeSingleHttpAsyncGetRequest(const char *url);
+void makeSingleHttpAsyncPostRequest(const char *url, const char *data, int size);