More renaming, remove a few useless .c_str()s and fix a URL that had previously contained Download and thus fell victim to my mindless text replacement tricks
This commit is contained in:
parent
d958adf487
commit
7fb0b52d79
@ -662,7 +662,7 @@ RequestStatus Client::ParseServerReturn(ByteString &result, int status, bool jso
|
||||
|
||||
if (json)
|
||||
{
|
||||
std::istringstream datastream(result.c_str());
|
||||
std::istringstream datastream(result);
|
||||
Json::Value root;
|
||||
|
||||
try
|
||||
@ -1449,7 +1449,7 @@ SaveInfo * Client::GetSave(int saveID, int saveDate)
|
||||
{
|
||||
try
|
||||
{
|
||||
std::istringstream dataStream(data.c_str());
|
||||
std::istringstream dataStream(data);
|
||||
Json::Value objDocument;
|
||||
dataStream >> objDocument;
|
||||
|
||||
@ -1516,7 +1516,7 @@ std::vector<std::pair<ByteString, int> > * Client::GetTags(int start, int count,
|
||||
{
|
||||
try
|
||||
{
|
||||
std::istringstream dataStream(data.c_str());
|
||||
std::istringstream dataStream(data);
|
||||
Json::Value objDocument;
|
||||
dataStream >> objDocument;
|
||||
|
||||
@ -1580,7 +1580,7 @@ std::vector<SaveInfo*> * Client::SearchSaves(int start, int count, String query,
|
||||
{
|
||||
try
|
||||
{
|
||||
std::istringstream dataStream(data.c_str());
|
||||
std::istringstream dataStream(data);
|
||||
Json::Value objDocument;
|
||||
dataStream >> objDocument;
|
||||
|
||||
@ -1633,7 +1633,7 @@ std::list<ByteString> * Client::RemoveTag(int saveID, ByteString tag)
|
||||
{
|
||||
try
|
||||
{
|
||||
std::istringstream dataStream(data.c_str());
|
||||
std::istringstream dataStream(data);
|
||||
Json::Value responseObject;
|
||||
dataStream >> responseObject;
|
||||
|
||||
@ -1672,7 +1672,7 @@ std::list<ByteString> * Client::AddTag(int saveID, ByteString tag)
|
||||
{
|
||||
try
|
||||
{
|
||||
std::istringstream dataStream(data.c_str());
|
||||
std::istringstream dataStream(data);
|
||||
Json::Value responseObject;
|
||||
dataStream >> responseObject;
|
||||
|
||||
|
@ -9,28 +9,28 @@ namespace http
|
||||
Request::Request(ByteString uri_, bool keepAlive):
|
||||
http(NULL),
|
||||
keepAlive(keepAlive),
|
||||
downloadData(NULL),
|
||||
downloadSize(0),
|
||||
downloadStatus(0),
|
||||
requestData(NULL),
|
||||
requestSize(0),
|
||||
requestStatus(0),
|
||||
postData(""),
|
||||
postDataBoundary(""),
|
||||
userID(""),
|
||||
userSession(""),
|
||||
downloadFinished(false),
|
||||
downloadCanceled(false),
|
||||
downloadStarted(false)
|
||||
requestFinished(false),
|
||||
requestCanceled(false),
|
||||
requestStarted(false)
|
||||
{
|
||||
uri = ByteString(uri_);
|
||||
RequestManager::Ref().AddDownload(this);
|
||||
RequestManager::Ref().AddRequest(this);
|
||||
}
|
||||
|
||||
// called by download thread itself if download was canceled
|
||||
// called by request thread itself if request was canceled
|
||||
Request::~Request()
|
||||
{
|
||||
if (http && (keepAlive || downloadCanceled))
|
||||
if (http && (keepAlive || requestCanceled))
|
||||
http_async_req_close(http);
|
||||
if (downloadData)
|
||||
free(downloadData);
|
||||
if (requestData)
|
||||
free(requestData);
|
||||
}
|
||||
|
||||
// add post data to a request
|
||||
@ -46,7 +46,7 @@ void Request::AddPostData(std::pair<ByteString, ByteString> data)
|
||||
AddPostData(postData);
|
||||
}
|
||||
|
||||
// add userID and sessionID headers to the download. Must be done after download starts for some reason
|
||||
// add userID and sessionID headers to the request. Must be done after request starts for some reason
|
||||
void Request::AuthHeaders(ByteString ID, ByteString session)
|
||||
{
|
||||
if (ID != "0")
|
||||
@ -54,7 +54,7 @@ void Request::AuthHeaders(ByteString ID, ByteString session)
|
||||
userSession = session;
|
||||
}
|
||||
|
||||
// start the download thread
|
||||
// start the request thread
|
||||
void Request::Start()
|
||||
{
|
||||
if (CheckStarted() || CheckDone())
|
||||
@ -66,78 +66,78 @@ void Request::Start()
|
||||
if (postDataBoundary.length())
|
||||
http_add_multipart_header(http, postDataBoundary);
|
||||
RequestManager::Ref().Lock();
|
||||
downloadStarted = true;
|
||||
requestStarted = true;
|
||||
RequestManager::Ref().Unlock();
|
||||
}
|
||||
|
||||
|
||||
// finish the download (if called before the download is done, this will block)
|
||||
// finish the request (if called before the request is done, this will block)
|
||||
ByteString Request::Finish(int *status)
|
||||
{
|
||||
if (CheckCanceled())
|
||||
return ""; // shouldn't happen but just in case
|
||||
while (!CheckDone()); // block
|
||||
RequestManager::Ref().Lock();
|
||||
downloadStarted = false;
|
||||
requestStarted = false;
|
||||
if (status)
|
||||
*status = downloadStatus;
|
||||
*status = requestStatus;
|
||||
ByteString ret;
|
||||
if (downloadData)
|
||||
if (requestData)
|
||||
{
|
||||
ret = ByteString(downloadData, downloadData + downloadSize);
|
||||
free(downloadData);
|
||||
ret = ByteString(requestData, requestData + requestSize);
|
||||
free(requestData);
|
||||
}
|
||||
downloadData = NULL;
|
||||
requestData = NULL;
|
||||
if (!keepAlive)
|
||||
downloadCanceled = true;
|
||||
requestCanceled = true;
|
||||
RequestManager::Ref().Unlock();
|
||||
return ret;
|
||||
}
|
||||
|
||||
// returns the download size and progress (if the download has the correct length headers)
|
||||
// returns the request size and progress (if the request has the correct length headers)
|
||||
void Request::CheckProgress(int *total, int *done)
|
||||
{
|
||||
RequestManager::Ref().Lock();
|
||||
if (!downloadFinished && http)
|
||||
if (!requestFinished && http)
|
||||
http_async_get_length(http, total, done);
|
||||
else
|
||||
*total = *done = 0;
|
||||
RequestManager::Ref().Unlock();
|
||||
}
|
||||
|
||||
// returns true if the download has finished
|
||||
// returns true if the request has finished
|
||||
bool Request::CheckDone()
|
||||
{
|
||||
RequestManager::Ref().Lock();
|
||||
bool ret = downloadFinished;
|
||||
bool ret = requestFinished;
|
||||
RequestManager::Ref().Unlock();
|
||||
return ret;
|
||||
}
|
||||
|
||||
// returns true if the download was canceled
|
||||
// returns true if the request was canceled
|
||||
bool Request::CheckCanceled()
|
||||
{
|
||||
RequestManager::Ref().Lock();
|
||||
bool ret = downloadCanceled;
|
||||
bool ret = requestCanceled;
|
||||
RequestManager::Ref().Unlock();
|
||||
return ret;
|
||||
}
|
||||
|
||||
// returns true if the download is running
|
||||
// returns true if the request is running
|
||||
bool Request::CheckStarted()
|
||||
{
|
||||
RequestManager::Ref().Lock();
|
||||
bool ret = downloadStarted;
|
||||
bool ret = requestStarted;
|
||||
RequestManager::Ref().Unlock();
|
||||
return ret;
|
||||
|
||||
}
|
||||
|
||||
// cancels the download, the download thread will delete the Request* when it finishes (do not use Request in any way after canceling)
|
||||
// cancels the request, the request thread will delete the Request* when it finishes (do not use Request in any way after canceling)
|
||||
void Request::Cancel()
|
||||
{
|
||||
RequestManager::Ref().Lock();
|
||||
downloadCanceled = true;
|
||||
requestCanceled = true;
|
||||
RequestManager::Ref().Unlock();
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
#ifndef DOWNLOAD_H
|
||||
#define DOWNLOAD_H
|
||||
#ifndef REQUEST_H
|
||||
#define REQUEST_H
|
||||
#include <map>
|
||||
#include "common/String.h"
|
||||
|
||||
@ -12,9 +12,9 @@ class Request
|
||||
void *http;
|
||||
bool keepAlive;
|
||||
|
||||
char *downloadData;
|
||||
int downloadSize;
|
||||
int downloadStatus;
|
||||
char *requestData;
|
||||
int requestSize;
|
||||
int requestStatus;
|
||||
|
||||
ByteString postData;
|
||||
ByteString postDataBoundary;
|
||||
@ -22,9 +22,9 @@ class Request
|
||||
ByteString userID;
|
||||
ByteString userSession;
|
||||
|
||||
volatile bool downloadFinished;
|
||||
volatile bool downloadCanceled;
|
||||
volatile bool downloadStarted;
|
||||
volatile bool requestFinished;
|
||||
volatile bool requestCanceled;
|
||||
volatile bool requestStarted;
|
||||
|
||||
public:
|
||||
Request(ByteString uri, bool keepAlive = false);
|
||||
|
@ -11,11 +11,11 @@ RequestManager::RequestManager():
|
||||
lastUsed(time(NULL)),
|
||||
managerRunning(false),
|
||||
managerShutdown(false),
|
||||
downloads(std::vector<Request*>()),
|
||||
downloadsAddQueue(std::vector<Request*>())
|
||||
requests(std::vector<Request*>()),
|
||||
requestsAddQueue(std::vector<Request*>())
|
||||
{
|
||||
pthread_mutex_init(&downloadLock, NULL);
|
||||
pthread_mutex_init(&downloadAddLock, NULL);
|
||||
pthread_mutex_init(&requestLock, NULL);
|
||||
pthread_mutex_init(&requestAddLock, NULL);
|
||||
}
|
||||
|
||||
RequestManager::~RequestManager()
|
||||
@ -25,28 +25,28 @@ RequestManager::~RequestManager()
|
||||
|
||||
void RequestManager::Shutdown()
|
||||
{
|
||||
pthread_mutex_lock(&downloadLock);
|
||||
pthread_mutex_lock(&downloadAddLock);
|
||||
for (std::vector<Request*>::iterator iter = downloads.begin(); iter != downloads.end(); ++iter)
|
||||
pthread_mutex_lock(&requestLock);
|
||||
pthread_mutex_lock(&requestAddLock);
|
||||
for (std::vector<Request*>::iterator iter = requests.begin(); iter != requests.end(); ++iter)
|
||||
{
|
||||
Request *download = (*iter);
|
||||
if (download->http)
|
||||
http_force_close(download->http);
|
||||
download->downloadCanceled = true;
|
||||
delete download;
|
||||
Request *request = (*iter);
|
||||
if (request->http)
|
||||
http_force_close(request->http);
|
||||
request->requestCanceled = true;
|
||||
delete request;
|
||||
}
|
||||
downloads.clear();
|
||||
downloadsAddQueue.clear();
|
||||
requests.clear();
|
||||
requestsAddQueue.clear();
|
||||
managerShutdown = true;
|
||||
pthread_mutex_unlock(&downloadAddLock);
|
||||
pthread_mutex_unlock(&downloadLock);
|
||||
pthread_mutex_unlock(&requestAddLock);
|
||||
pthread_mutex_unlock(&requestLock);
|
||||
if (threadStarted)
|
||||
pthread_join(downloadThread, NULL);
|
||||
pthread_join(requestThread, NULL);
|
||||
|
||||
http_done();
|
||||
}
|
||||
|
||||
//helper function for download
|
||||
//helper function for request
|
||||
TH_ENTRY_POINT void* RequestManagerHelper(void* obj)
|
||||
{
|
||||
RequestManager *temp = (RequestManager*)obj;
|
||||
@ -71,59 +71,59 @@ void RequestManager::Start()
|
||||
{
|
||||
managerRunning = true;
|
||||
lastUsed = time(NULL);
|
||||
pthread_create(&downloadThread, NULL, &RequestManagerHelper, this);
|
||||
pthread_create(&requestThread, NULL, &RequestManagerHelper, this);
|
||||
}
|
||||
|
||||
void RequestManager::Update()
|
||||
{
|
||||
unsigned int numActiveDownloads = 0;
|
||||
unsigned int numActiveRequests = 0;
|
||||
while (!managerShutdown)
|
||||
{
|
||||
pthread_mutex_lock(&downloadAddLock);
|
||||
if (downloadsAddQueue.size())
|
||||
pthread_mutex_lock(&requestAddLock);
|
||||
if (requestsAddQueue.size())
|
||||
{
|
||||
for (size_t i = 0; i < downloadsAddQueue.size(); i++)
|
||||
for (size_t i = 0; i < requestsAddQueue.size(); i++)
|
||||
{
|
||||
downloads.push_back(downloadsAddQueue[i]);
|
||||
requests.push_back(requestsAddQueue[i]);
|
||||
}
|
||||
downloadsAddQueue.clear();
|
||||
requestsAddQueue.clear();
|
||||
}
|
||||
pthread_mutex_unlock(&downloadAddLock);
|
||||
if (downloads.size())
|
||||
pthread_mutex_unlock(&requestAddLock);
|
||||
if (requests.size())
|
||||
{
|
||||
numActiveDownloads = 0;
|
||||
pthread_mutex_lock(&downloadLock);
|
||||
for (size_t i = 0; i < downloads.size(); i++)
|
||||
numActiveRequests = 0;
|
||||
pthread_mutex_lock(&requestLock);
|
||||
for (size_t i = 0; i < requests.size(); i++)
|
||||
{
|
||||
Request *download = downloads[i];
|
||||
if (download->downloadCanceled)
|
||||
Request *request = requests[i];
|
||||
if (request->requestCanceled)
|
||||
{
|
||||
if (download->http && download->downloadStarted)
|
||||
http_force_close(download->http);
|
||||
delete download;
|
||||
downloads.erase(downloads.begin()+i);
|
||||
if (request->http && request->requestStarted)
|
||||
http_force_close(request->http);
|
||||
delete request;
|
||||
requests.erase(requests.begin()+i);
|
||||
i--;
|
||||
}
|
||||
else if (download->downloadStarted && !download->downloadFinished)
|
||||
else if (request->requestStarted && !request->requestFinished)
|
||||
{
|
||||
if (http_async_req_status(download->http) != 0)
|
||||
if (http_async_req_status(request->http) != 0)
|
||||
{
|
||||
download->downloadData = http_async_req_stop(download->http, &download->downloadStatus, &download->downloadSize);
|
||||
download->downloadFinished = true;
|
||||
if (!download->keepAlive)
|
||||
download->http = NULL;
|
||||
request->requestData = http_async_req_stop(request->http, &request->requestStatus, &request->requestSize);
|
||||
request->requestFinished = true;
|
||||
if (!request->keepAlive)
|
||||
request->http = NULL;
|
||||
}
|
||||
lastUsed = time(NULL);
|
||||
numActiveDownloads++;
|
||||
numActiveRequests++;
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&downloadLock);
|
||||
pthread_mutex_unlock(&requestLock);
|
||||
}
|
||||
if (time(NULL) > lastUsed+http_timeout*2 && !numActiveDownloads)
|
||||
if (time(NULL) > lastUsed+http_timeout*2 && !numActiveRequests)
|
||||
{
|
||||
pthread_mutex_lock(&downloadLock);
|
||||
pthread_mutex_lock(&requestLock);
|
||||
managerRunning = false;
|
||||
pthread_mutex_unlock(&downloadLock);
|
||||
pthread_mutex_unlock(&requestLock);
|
||||
return;
|
||||
}
|
||||
Platform::Millisleep(1);
|
||||
@ -132,33 +132,33 @@ void RequestManager::Update()
|
||||
|
||||
void RequestManager::EnsureRunning()
|
||||
{
|
||||
pthread_mutex_lock(&downloadLock);
|
||||
pthread_mutex_lock(&requestLock);
|
||||
if (!managerRunning)
|
||||
{
|
||||
if (threadStarted)
|
||||
pthread_join(downloadThread, NULL);
|
||||
pthread_join(requestThread, NULL);
|
||||
else
|
||||
threadStarted = true;
|
||||
Start();
|
||||
}
|
||||
pthread_mutex_unlock(&downloadLock);
|
||||
pthread_mutex_unlock(&requestLock);
|
||||
}
|
||||
|
||||
void RequestManager::AddDownload(Request *download)
|
||||
void RequestManager::AddRequest(Request *request)
|
||||
{
|
||||
pthread_mutex_lock(&downloadAddLock);
|
||||
downloadsAddQueue.push_back(download);
|
||||
pthread_mutex_unlock(&downloadAddLock);
|
||||
pthread_mutex_lock(&requestAddLock);
|
||||
requestsAddQueue.push_back(request);
|
||||
pthread_mutex_unlock(&requestAddLock);
|
||||
EnsureRunning();
|
||||
}
|
||||
|
||||
void RequestManager::Lock()
|
||||
{
|
||||
pthread_mutex_lock(&downloadAddLock);
|
||||
pthread_mutex_lock(&requestAddLock);
|
||||
}
|
||||
|
||||
void RequestManager::Unlock()
|
||||
{
|
||||
pthread_mutex_unlock(&downloadAddLock);
|
||||
pthread_mutex_unlock(&requestAddLock);
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
#ifndef DOWNLOADMANAGER_H
|
||||
#define DOWNLOADMANAGER_H
|
||||
#ifndef REQUESTMANAGER_H
|
||||
#define REQUESTMANAGER_H
|
||||
#include "common/tpt-thread.h"
|
||||
#include <ctime>
|
||||
#include <vector>
|
||||
@ -12,17 +12,17 @@ class Request;
|
||||
class RequestManager : public Singleton<RequestManager>
|
||||
{
|
||||
private:
|
||||
pthread_t downloadThread;
|
||||
pthread_mutex_t downloadLock;
|
||||
pthread_mutex_t downloadAddLock;
|
||||
pthread_t requestThread;
|
||||
pthread_mutex_t requestLock;
|
||||
pthread_mutex_t requestAddLock;
|
||||
bool threadStarted;
|
||||
ByteString proxy;
|
||||
|
||||
int lastUsed;
|
||||
volatile bool managerRunning;
|
||||
volatile bool managerShutdown;
|
||||
std::vector<Request*> downloads;
|
||||
std::vector<Request*> downloadsAddQueue;
|
||||
std::vector<Request*> requests;
|
||||
std::vector<Request*> requestsAddQueue;
|
||||
|
||||
void Start();
|
||||
public:
|
||||
@ -35,12 +35,12 @@ public:
|
||||
void Update();
|
||||
void EnsureRunning();
|
||||
|
||||
void AddDownload(Request *download);
|
||||
void RemoveDownload(int id);
|
||||
void AddRequest(Request *request);
|
||||
void RemoveRequest(int id);
|
||||
|
||||
void Lock();
|
||||
void Unlock();
|
||||
};
|
||||
}
|
||||
|
||||
#endif // DOWNLOADMANAGER_H
|
||||
#endif // REQUESTMANAGER_H
|
||||
|
@ -149,7 +149,7 @@ void UpdateActivity::NotifyError(Task * sender)
|
||||
if (result == ConfirmPrompt::ResultOkay)
|
||||
{
|
||||
#ifndef UPDATESERVER
|
||||
Platform::OpenURI("http://powdertoy.co.uk/http/Request.html");
|
||||
Platform::OpenURI("http://powdertoy.co.uk/Download.html");
|
||||
#endif
|
||||
}
|
||||
a->Exit();
|
||||
|
Loading…
Reference in New Issue
Block a user