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:
Tamás Bálint Misius 2019-03-07 20:28:48 +01:00 committed by jacob1
parent d958adf487
commit 7fb0b52d79
6 changed files with 113 additions and 113 deletions

View File

@ -662,7 +662,7 @@ RequestStatus Client::ParseServerReturn(ByteString &result, int status, bool jso
if (json) if (json)
{ {
std::istringstream datastream(result.c_str()); std::istringstream datastream(result);
Json::Value root; Json::Value root;
try try
@ -1449,7 +1449,7 @@ SaveInfo * Client::GetSave(int saveID, int saveDate)
{ {
try try
{ {
std::istringstream dataStream(data.c_str()); std::istringstream dataStream(data);
Json::Value objDocument; Json::Value objDocument;
dataStream >> objDocument; dataStream >> objDocument;
@ -1516,7 +1516,7 @@ std::vector<std::pair<ByteString, int> > * Client::GetTags(int start, int count,
{ {
try try
{ {
std::istringstream dataStream(data.c_str()); std::istringstream dataStream(data);
Json::Value objDocument; Json::Value objDocument;
dataStream >> objDocument; dataStream >> objDocument;
@ -1580,7 +1580,7 @@ std::vector<SaveInfo*> * Client::SearchSaves(int start, int count, String query,
{ {
try try
{ {
std::istringstream dataStream(data.c_str()); std::istringstream dataStream(data);
Json::Value objDocument; Json::Value objDocument;
dataStream >> objDocument; dataStream >> objDocument;
@ -1633,7 +1633,7 @@ std::list<ByteString> * Client::RemoveTag(int saveID, ByteString tag)
{ {
try try
{ {
std::istringstream dataStream(data.c_str()); std::istringstream dataStream(data);
Json::Value responseObject; Json::Value responseObject;
dataStream >> responseObject; dataStream >> responseObject;
@ -1672,7 +1672,7 @@ std::list<ByteString> * Client::AddTag(int saveID, ByteString tag)
{ {
try try
{ {
std::istringstream dataStream(data.c_str()); std::istringstream dataStream(data);
Json::Value responseObject; Json::Value responseObject;
dataStream >> responseObject; dataStream >> responseObject;

View File

@ -9,28 +9,28 @@ namespace http
Request::Request(ByteString uri_, bool keepAlive): Request::Request(ByteString uri_, bool keepAlive):
http(NULL), http(NULL),
keepAlive(keepAlive), keepAlive(keepAlive),
downloadData(NULL), requestData(NULL),
downloadSize(0), requestSize(0),
downloadStatus(0), requestStatus(0),
postData(""), postData(""),
postDataBoundary(""), postDataBoundary(""),
userID(""), userID(""),
userSession(""), userSession(""),
downloadFinished(false), requestFinished(false),
downloadCanceled(false), requestCanceled(false),
downloadStarted(false) requestStarted(false)
{ {
uri = ByteString(uri_); 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() Request::~Request()
{ {
if (http && (keepAlive || downloadCanceled)) if (http && (keepAlive || requestCanceled))
http_async_req_close(http); http_async_req_close(http);
if (downloadData) if (requestData)
free(downloadData); free(requestData);
} }
// add post data to a request // add post data to a request
@ -46,7 +46,7 @@ void Request::AddPostData(std::pair<ByteString, ByteString> data)
AddPostData(postData); 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) void Request::AuthHeaders(ByteString ID, ByteString session)
{ {
if (ID != "0") if (ID != "0")
@ -54,7 +54,7 @@ void Request::AuthHeaders(ByteString ID, ByteString session)
userSession = session; userSession = session;
} }
// start the download thread // start the request thread
void Request::Start() void Request::Start()
{ {
if (CheckStarted() || CheckDone()) if (CheckStarted() || CheckDone())
@ -66,78 +66,78 @@ void Request::Start()
if (postDataBoundary.length()) if (postDataBoundary.length())
http_add_multipart_header(http, postDataBoundary); http_add_multipart_header(http, postDataBoundary);
RequestManager::Ref().Lock(); RequestManager::Ref().Lock();
downloadStarted = true; requestStarted = true;
RequestManager::Ref().Unlock(); 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) ByteString Request::Finish(int *status)
{ {
if (CheckCanceled()) if (CheckCanceled())
return ""; // shouldn't happen but just in case return ""; // shouldn't happen but just in case
while (!CheckDone()); // block while (!CheckDone()); // block
RequestManager::Ref().Lock(); RequestManager::Ref().Lock();
downloadStarted = false; requestStarted = false;
if (status) if (status)
*status = downloadStatus; *status = requestStatus;
ByteString ret; ByteString ret;
if (downloadData) if (requestData)
{ {
ret = ByteString(downloadData, downloadData + downloadSize); ret = ByteString(requestData, requestData + requestSize);
free(downloadData); free(requestData);
} }
downloadData = NULL; requestData = NULL;
if (!keepAlive) if (!keepAlive)
downloadCanceled = true; requestCanceled = true;
RequestManager::Ref().Unlock(); RequestManager::Ref().Unlock();
return ret; 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) void Request::CheckProgress(int *total, int *done)
{ {
RequestManager::Ref().Lock(); RequestManager::Ref().Lock();
if (!downloadFinished && http) if (!requestFinished && http)
http_async_get_length(http, total, done); http_async_get_length(http, total, done);
else else
*total = *done = 0; *total = *done = 0;
RequestManager::Ref().Unlock(); RequestManager::Ref().Unlock();
} }
// returns true if the download has finished // returns true if the request has finished
bool Request::CheckDone() bool Request::CheckDone()
{ {
RequestManager::Ref().Lock(); RequestManager::Ref().Lock();
bool ret = downloadFinished; bool ret = requestFinished;
RequestManager::Ref().Unlock(); RequestManager::Ref().Unlock();
return ret; return ret;
} }
// returns true if the download was canceled // returns true if the request was canceled
bool Request::CheckCanceled() bool Request::CheckCanceled()
{ {
RequestManager::Ref().Lock(); RequestManager::Ref().Lock();
bool ret = downloadCanceled; bool ret = requestCanceled;
RequestManager::Ref().Unlock(); RequestManager::Ref().Unlock();
return ret; return ret;
} }
// returns true if the download is running // returns true if the request is running
bool Request::CheckStarted() bool Request::CheckStarted()
{ {
RequestManager::Ref().Lock(); RequestManager::Ref().Lock();
bool ret = downloadStarted; bool ret = requestStarted;
RequestManager::Ref().Unlock(); RequestManager::Ref().Unlock();
return ret; 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() void Request::Cancel()
{ {
RequestManager::Ref().Lock(); RequestManager::Ref().Lock();
downloadCanceled = true; requestCanceled = true;
RequestManager::Ref().Unlock(); RequestManager::Ref().Unlock();
} }

View File

@ -1,5 +1,5 @@
#ifndef DOWNLOAD_H #ifndef REQUEST_H
#define DOWNLOAD_H #define REQUEST_H
#include <map> #include <map>
#include "common/String.h" #include "common/String.h"
@ -12,9 +12,9 @@ class Request
void *http; void *http;
bool keepAlive; bool keepAlive;
char *downloadData; char *requestData;
int downloadSize; int requestSize;
int downloadStatus; int requestStatus;
ByteString postData; ByteString postData;
ByteString postDataBoundary; ByteString postDataBoundary;
@ -22,9 +22,9 @@ class Request
ByteString userID; ByteString userID;
ByteString userSession; ByteString userSession;
volatile bool downloadFinished; volatile bool requestFinished;
volatile bool downloadCanceled; volatile bool requestCanceled;
volatile bool downloadStarted; volatile bool requestStarted;
public: public:
Request(ByteString uri, bool keepAlive = false); Request(ByteString uri, bool keepAlive = false);

View File

@ -11,11 +11,11 @@ RequestManager::RequestManager():
lastUsed(time(NULL)), lastUsed(time(NULL)),
managerRunning(false), managerRunning(false),
managerShutdown(false), managerShutdown(false),
downloads(std::vector<Request*>()), requests(std::vector<Request*>()),
downloadsAddQueue(std::vector<Request*>()) requestsAddQueue(std::vector<Request*>())
{ {
pthread_mutex_init(&downloadLock, NULL); pthread_mutex_init(&requestLock, NULL);
pthread_mutex_init(&downloadAddLock, NULL); pthread_mutex_init(&requestAddLock, NULL);
} }
RequestManager::~RequestManager() RequestManager::~RequestManager()
@ -25,28 +25,28 @@ RequestManager::~RequestManager()
void RequestManager::Shutdown() void RequestManager::Shutdown()
{ {
pthread_mutex_lock(&downloadLock); pthread_mutex_lock(&requestLock);
pthread_mutex_lock(&downloadAddLock); pthread_mutex_lock(&requestAddLock);
for (std::vector<Request*>::iterator iter = downloads.begin(); iter != downloads.end(); ++iter) for (std::vector<Request*>::iterator iter = requests.begin(); iter != requests.end(); ++iter)
{ {
Request *download = (*iter); Request *request = (*iter);
if (download->http) if (request->http)
http_force_close(download->http); http_force_close(request->http);
download->downloadCanceled = true; request->requestCanceled = true;
delete download; delete request;
} }
downloads.clear(); requests.clear();
downloadsAddQueue.clear(); requestsAddQueue.clear();
managerShutdown = true; managerShutdown = true;
pthread_mutex_unlock(&downloadAddLock); pthread_mutex_unlock(&requestAddLock);
pthread_mutex_unlock(&downloadLock); pthread_mutex_unlock(&requestLock);
if (threadStarted) if (threadStarted)
pthread_join(downloadThread, NULL); pthread_join(requestThread, NULL);
http_done(); http_done();
} }
//helper function for download //helper function for request
TH_ENTRY_POINT void* RequestManagerHelper(void* obj) TH_ENTRY_POINT void* RequestManagerHelper(void* obj)
{ {
RequestManager *temp = (RequestManager*)obj; RequestManager *temp = (RequestManager*)obj;
@ -71,59 +71,59 @@ void RequestManager::Start()
{ {
managerRunning = true; managerRunning = true;
lastUsed = time(NULL); lastUsed = time(NULL);
pthread_create(&downloadThread, NULL, &RequestManagerHelper, this); pthread_create(&requestThread, NULL, &RequestManagerHelper, this);
} }
void RequestManager::Update() void RequestManager::Update()
{ {
unsigned int numActiveDownloads = 0; unsigned int numActiveRequests = 0;
while (!managerShutdown) while (!managerShutdown)
{ {
pthread_mutex_lock(&downloadAddLock); pthread_mutex_lock(&requestAddLock);
if (downloadsAddQueue.size()) 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); pthread_mutex_unlock(&requestAddLock);
if (downloads.size()) if (requests.size())
{ {
numActiveDownloads = 0; numActiveRequests = 0;
pthread_mutex_lock(&downloadLock); pthread_mutex_lock(&requestLock);
for (size_t i = 0; i < downloads.size(); i++) for (size_t i = 0; i < requests.size(); i++)
{ {
Request *download = downloads[i]; Request *request = requests[i];
if (download->downloadCanceled) if (request->requestCanceled)
{ {
if (download->http && download->downloadStarted) if (request->http && request->requestStarted)
http_force_close(download->http); http_force_close(request->http);
delete download; delete request;
downloads.erase(downloads.begin()+i); requests.erase(requests.begin()+i);
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); request->requestData = http_async_req_stop(request->http, &request->requestStatus, &request->requestSize);
download->downloadFinished = true; request->requestFinished = true;
if (!download->keepAlive) if (!request->keepAlive)
download->http = NULL; request->http = NULL;
} }
lastUsed = time(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; managerRunning = false;
pthread_mutex_unlock(&downloadLock); pthread_mutex_unlock(&requestLock);
return; return;
} }
Platform::Millisleep(1); Platform::Millisleep(1);
@ -132,33 +132,33 @@ void RequestManager::Update()
void RequestManager::EnsureRunning() void RequestManager::EnsureRunning()
{ {
pthread_mutex_lock(&downloadLock); pthread_mutex_lock(&requestLock);
if (!managerRunning) if (!managerRunning)
{ {
if (threadStarted) if (threadStarted)
pthread_join(downloadThread, NULL); pthread_join(requestThread, NULL);
else else
threadStarted = true; threadStarted = true;
Start(); Start();
} }
pthread_mutex_unlock(&downloadLock); pthread_mutex_unlock(&requestLock);
} }
void RequestManager::AddDownload(Request *download) void RequestManager::AddRequest(Request *request)
{ {
pthread_mutex_lock(&downloadAddLock); pthread_mutex_lock(&requestAddLock);
downloadsAddQueue.push_back(download); requestsAddQueue.push_back(request);
pthread_mutex_unlock(&downloadAddLock); pthread_mutex_unlock(&requestAddLock);
EnsureRunning(); EnsureRunning();
} }
void RequestManager::Lock() void RequestManager::Lock()
{ {
pthread_mutex_lock(&downloadAddLock); pthread_mutex_lock(&requestAddLock);
} }
void RequestManager::Unlock() void RequestManager::Unlock()
{ {
pthread_mutex_unlock(&downloadAddLock); pthread_mutex_unlock(&requestAddLock);
} }
} }

View File

@ -1,5 +1,5 @@
#ifndef DOWNLOADMANAGER_H #ifndef REQUESTMANAGER_H
#define DOWNLOADMANAGER_H #define REQUESTMANAGER_H
#include "common/tpt-thread.h" #include "common/tpt-thread.h"
#include <ctime> #include <ctime>
#include <vector> #include <vector>
@ -12,17 +12,17 @@ class Request;
class RequestManager : public Singleton<RequestManager> class RequestManager : public Singleton<RequestManager>
{ {
private: private:
pthread_t downloadThread; pthread_t requestThread;
pthread_mutex_t downloadLock; pthread_mutex_t requestLock;
pthread_mutex_t downloadAddLock; pthread_mutex_t requestAddLock;
bool threadStarted; bool threadStarted;
ByteString proxy; ByteString proxy;
int lastUsed; int lastUsed;
volatile bool managerRunning; volatile bool managerRunning;
volatile bool managerShutdown; volatile bool managerShutdown;
std::vector<Request*> downloads; std::vector<Request*> requests;
std::vector<Request*> downloadsAddQueue; std::vector<Request*> requestsAddQueue;
void Start(); void Start();
public: public:
@ -35,12 +35,12 @@ public:
void Update(); void Update();
void EnsureRunning(); void EnsureRunning();
void AddDownload(Request *download); void AddRequest(Request *request);
void RemoveDownload(int id); void RemoveRequest(int id);
void Lock(); void Lock();
void Unlock(); void Unlock();
}; };
} }
#endif // DOWNLOADMANAGER_H #endif // REQUESTMANAGER_H

View File

@ -149,7 +149,7 @@ void UpdateActivity::NotifyError(Task * sender)
if (result == ConfirmPrompt::ResultOkay) if (result == ConfirmPrompt::ResultOkay)
{ {
#ifndef UPDATESERVER #ifndef UPDATESERVER
Platform::OpenURI("http://powdertoy.co.uk/http/Request.html"); Platform::OpenURI("http://powdertoy.co.uk/Download.html");
#endif #endif
} }
a->Exit(); a->Exit();