Skip to content

Commit

Permalink
Merge pull request #11700 from unknownbrackets/http-error
Browse files Browse the repository at this point in the history
http: Report errors reading discs
  • Loading branch information
hrydgard authored Dec 27, 2018
2 parents 8e17caf + 872fcec commit 127330f
Show file tree
Hide file tree
Showing 16 changed files with 109 additions and 116 deletions.
24 changes: 5 additions & 19 deletions Core/FileLoaders/CachingFileLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@

// Takes ownership of backend.
CachingFileLoader::CachingFileLoader(FileLoader *backend)
: backend_(backend) {
: ProxiedFileLoader(backend) {
}

void CachingFileLoader::Prepare() {
std::call_once(preparedFlag_, [this](){
filesize_ = backend_->FileSize();
filesize_ = ProxiedFileLoader::FileSize();
if (filesize_ > 0) {
InitCache();
}
Expand All @@ -41,27 +41,25 @@ CachingFileLoader::~CachingFileLoader() {
if (filesize_ > 0) {
ShutdownCache();
}
// Takes ownership.
delete backend_;
}

bool CachingFileLoader::Exists() {
if (exists_ == -1) {
exists_ = backend_->Exists() ? 1 : 0;
exists_ = ProxiedFileLoader::Exists() ? 1 : 0;
}
return exists_ == 1;
}

bool CachingFileLoader::ExistsFast() {
if (exists_ == -1) {
return backend_->ExistsFast();
return ProxiedFileLoader::ExistsFast();
}
return exists_ == 1;
}

bool CachingFileLoader::IsDirectory() {
if (isDirectory_ == -1) {
isDirectory_ = backend_->IsDirectory() ? 1 : 0;
isDirectory_ = ProxiedFileLoader::IsDirectory() ? 1 : 0;
}
return isDirectory_ == 1;
}
Expand All @@ -71,10 +69,6 @@ s64 CachingFileLoader::FileSize() {
return filesize_;
}

std::string CachingFileLoader::Path() const {
return backend_->Path();
}

size_t CachingFileLoader::ReadAt(s64 absolutePos, size_t bytes, void *data, Flags flags) {
Prepare();
if (absolutePos >= filesize_) {
Expand Down Expand Up @@ -288,11 +282,3 @@ void CachingFileLoader::StartReadAhead(s64 pos) {
});
th.detach();
}

bool CachingFileLoader::IsRemote() {
return backend_->IsRemote();
}

void CachingFileLoader::Cancel() {
backend_->Cancel();
}
7 changes: 1 addition & 6 deletions Core/FileLoaders/CachingFileLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,21 @@
#include "Common/CommonTypes.h"
#include "Core/Loaders.h"

class CachingFileLoader : public FileLoader {
class CachingFileLoader : public ProxiedFileLoader {
public:
CachingFileLoader(FileLoader *backend);
~CachingFileLoader() override;

bool IsRemote() override;
bool Exists() override;
bool ExistsFast() override;
bool IsDirectory() override;
s64 FileSize() override;
std::string Path() const override;

size_t ReadAt(s64 absolutePos, size_t bytes, size_t count, void *data, Flags flags = Flags::NONE) override {
return ReadAt(absolutePos, bytes * count, data, flags) / bytes;
}
size_t ReadAt(s64 absolutePos, size_t bytes, void *data, Flags flags = Flags::NONE) override;

void Cancel() override;

private:
void Prepare();
void InitCache();
Expand All @@ -61,7 +57,6 @@ class CachingFileLoader : public FileLoader {
};

s64 filesize_ = 0;
FileLoader *backend_;
int exists_ = -1;
int isDirectory_ = -1;
u64 generation_;
Expand Down
28 changes: 5 additions & 23 deletions Core/FileLoaders/DiskCachingFileLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ std::mutex DiskCachingFileLoader::cachesMutex_;

// Takes ownership of backend.
DiskCachingFileLoader::DiskCachingFileLoader(FileLoader *backend)
: backend_(backend) {
: ProxiedFileLoader(backend) {
}

void DiskCachingFileLoader::Prepare() {
std::call_once(preparedFlag_, [this]() {
filesize_ = backend_->FileSize();
filesize_ = ProxiedFileLoader::FileSize();
if (filesize_ > 0) {
InitCache();
}
Expand All @@ -57,13 +57,11 @@ DiskCachingFileLoader::~DiskCachingFileLoader() {
if (filesize_ > 0) {
ShutdownCache();
}
// Takes ownership.
delete backend_;
}

bool DiskCachingFileLoader::Exists() {
Prepare();
return backend_->Exists();
return ProxiedFileLoader::Exists();
}

bool DiskCachingFileLoader::ExistsFast() {
Expand All @@ -72,19 +70,11 @@ bool DiskCachingFileLoader::ExistsFast() {
return true;
}

bool DiskCachingFileLoader::IsDirectory() {
return backend_->IsDirectory();
}

s64 DiskCachingFileLoader::FileSize() {
Prepare();
return filesize_;
}

std::string DiskCachingFileLoader::Path() const {
return backend_->Path();
}

size_t DiskCachingFileLoader::ReadAt(s64 absolutePos, size_t bytes, void *data, Flags flags) {
Prepare();
size_t readSize;
Expand Down Expand Up @@ -115,14 +105,6 @@ size_t DiskCachingFileLoader::ReadAt(s64 absolutePos, size_t bytes, void *data,
return readSize;
}

bool DiskCachingFileLoader::IsRemote() {
return backend_->IsRemote();
}

void DiskCachingFileLoader::Cancel() {
backend_->Cancel();
}

std::vector<std::string> DiskCachingFileLoader::GetCachedPathsInUse() {
std::lock_guard<std::mutex> guard(cachesMutex_);

Expand All @@ -139,7 +121,7 @@ std::vector<std::string> DiskCachingFileLoader::GetCachedPathsInUse() {
void DiskCachingFileLoader::InitCache() {
std::lock_guard<std::mutex> guard(cachesMutex_);

std::string path = backend_->Path();
std::string path = ProxiedFileLoader::Path();
auto &entry = caches_[path];
if (!entry) {
entry = new DiskCachingFileLoaderCache(path, filesize_);
Expand All @@ -155,7 +137,7 @@ void DiskCachingFileLoader::ShutdownCache() {
if (cache_->Release()) {
// If it ran out of counts, delete it.
delete cache_;
caches_.erase(backend_->Path());
caches_.erase(ProxiedFileLoader::Path());
}
cache_ = nullptr;
}
Expand Down
8 changes: 1 addition & 7 deletions Core/FileLoaders/DiskCachingFileLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,20 @@

class DiskCachingFileLoaderCache;

class DiskCachingFileLoader : public FileLoader {
class DiskCachingFileLoader : public ProxiedFileLoader {
public:
DiskCachingFileLoader(FileLoader *backend);
~DiskCachingFileLoader() override;

bool IsRemote() override;
bool Exists() override;
bool ExistsFast() override;
bool IsDirectory() override;
s64 FileSize() override;
std::string Path() const override;

size_t ReadAt(s64 absolutePos, size_t bytes, size_t count, void *data, Flags flags = Flags::NONE) override {
return ReadAt(absolutePos, bytes * count, data, flags) / bytes;
}
size_t ReadAt(s64 absolutePos, size_t bytes, void *data, Flags flags = Flags::NONE) override;

void Cancel() override;

static std::vector<std::string> GetCachedPathsInUse();

private:
Expand All @@ -55,7 +50,6 @@ class DiskCachingFileLoader : public FileLoader {

std::once_flag preparedFlag_;
s64 filesize_ = 0;
FileLoader *backend_;
DiskCachingFileLoaderCache *cache_ = nullptr;

// We don't support concurrent disk cache access (we use memory cached indexes.)
Expand Down
12 changes: 11 additions & 1 deletion Core/FileLoaders/HTTPFileLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,23 @@ HTTPFileLoader::HTTPFileLoader(const std::string &filename)
void HTTPFileLoader::Prepare() {
std::call_once(preparedFlag_, [this](){
if (!client_.Resolve(url_.Host().c_str(), url_.Port())) {
// TODO: Should probably set some flag?
ERROR_LOG(LOADER, "HTTP request failed, unable to resolve: %s port %d", url_.Host().c_str(), url_.Port());
latestError_ = "Could not connect (name not resolved)";
return;
}

client_.SetDataTimeout(20.0);
Connect();
if (!connected_) {
ERROR_LOG(LOADER, "HTTP request failed, failed to connect: %s port %d", url_.Host().c_str(), url_.Port());
latestError_ = "Could not connect (refused to connect)";
return;
}

int err = client_.SendRequest("HEAD", url_.Resource().c_str());
if (err < 0) {
ERROR_LOG(LOADER, "HTTP request failed, failed to send request: %s port %d", url_.Host().c_str(), url_.Port());
latestError_ = "Could not connect (could not request data)";
Disconnect();
return;
}
Expand All @@ -49,6 +55,7 @@ void HTTPFileLoader::Prepare() {
if (code != 200) {
// Leave size at 0, invalid.
ERROR_LOG(LOADER, "HTTP request failed, got %03d for %s", code, filename_.c_str());
latestError_ = "Could not connect (invalid response)";
Disconnect();
return;
}
Expand Down Expand Up @@ -138,6 +145,7 @@ size_t HTTPFileLoader::ReadAt(s64 absolutePos, size_t bytes, void *data, Flags f

int err = client_.SendRequest("GET", url_.Resource().c_str(), requestHeaders, nullptr);
if (err < 0) {
latestError_ = "Invalid response reading data";
Disconnect();
return 0;
}
Expand All @@ -147,6 +155,7 @@ size_t HTTPFileLoader::ReadAt(s64 absolutePos, size_t bytes, void *data, Flags f
int code = client_.ReadResponseHeaders(&readbuf, responseHeaders);
if (code != 206) {
ERROR_LOG(LOADER, "HTTP server did not respond with range, received code=%03d", code);
latestError_ = "Invalid response reading data";
Disconnect();
return 0;
}
Expand Down Expand Up @@ -185,6 +194,7 @@ size_t HTTPFileLoader::ReadAt(s64 absolutePos, size_t bytes, void *data, Flags f

if (!supportedResponse) {
ERROR_LOG(LOADER, "HTTP server did not respond with the range we wanted.");
latestError_ = "Invalid response reading data";
return 0;
}

Expand Down
5 changes: 5 additions & 0 deletions Core/FileLoaders/HTTPFileLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ class HTTPFileLoader : public FileLoader {
cancelConnect_ = true;
}

std::string LatestError() const override {
return latestError_;
}

private:
void Prepare();

Expand All @@ -67,6 +71,7 @@ class HTTPFileLoader : public FileLoader {
std::string filename_;
bool connected_ = false;
bool cancelConnect_ = false;
const char *latestError_ = "";

std::once_flag preparedFlag_;
std::mutex readAtMutex_;
Expand Down
20 changes: 5 additions & 15 deletions Core/FileLoaders/RamCachingFileLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

// Takes ownership of backend.
RamCachingFileLoader::RamCachingFileLoader(FileLoader *backend)
: backend_(backend) {
: ProxiedFileLoader(backend) {
filesize_ = backend->FileSize();
if (filesize_ > 0) {
InitCache();
Expand All @@ -39,27 +39,25 @@ RamCachingFileLoader::~RamCachingFileLoader() {
if (filesize_ > 0) {
ShutdownCache();
}
// Takes ownership.
delete backend_;
}

bool RamCachingFileLoader::Exists() {
if (exists_ == -1) {
exists_ = backend_->Exists() ? 1 : 0;
exists_ = ProxiedFileLoader::Exists() ? 1 : 0;
}
return exists_ == 1;
}

bool RamCachingFileLoader::ExistsFast() {
if (exists_ == -1) {
return backend_->ExistsFast();
return ProxiedFileLoader::ExistsFast();
}
return exists_ == 1;
}

bool RamCachingFileLoader::IsDirectory() {
if (isDirectory_ == -1) {
isDirectory_ = backend_->IsDirectory() ? 1 : 0;
isDirectory_ = ProxiedFileLoader::IsDirectory() ? 1 : 0;
}
return isDirectory_ == 1;
}
Expand All @@ -68,10 +66,6 @@ s64 RamCachingFileLoader::FileSize() {
return filesize_;
}

std::string RamCachingFileLoader::Path() const {
return backend_->Path();
}

size_t RamCachingFileLoader::ReadAt(s64 absolutePos, size_t bytes, void *data, Flags flags) {
size_t readSize = 0;
if (cache_ == nullptr || (flags & Flags::HINT_UNCACHED) != 0) {
Expand Down Expand Up @@ -129,7 +123,7 @@ void RamCachingFileLoader::Cancel() {
aheadCancel_ = true;
}

backend_->Cancel();
ProxiedFileLoader::Cancel();
}

size_t RamCachingFileLoader::ReadFromCache(s64 pos, size_t bytes, void *data) {
Expand Down Expand Up @@ -270,7 +264,3 @@ u32 RamCachingFileLoader::NextAheadBlock() {

return 0xFFFFFFFF;
}

bool RamCachingFileLoader::IsRemote() {
return backend_->IsRemote();
}
5 changes: 1 addition & 4 deletions Core/FileLoaders/RamCachingFileLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@
#include "Common/CommonTypes.h"
#include "Core/Loaders.h"

class RamCachingFileLoader : public FileLoader {
class RamCachingFileLoader : public ProxiedFileLoader {
public:
RamCachingFileLoader(FileLoader *backend);
~RamCachingFileLoader() override;

bool IsRemote() override;
bool Exists() override;
bool ExistsFast() override;
bool IsDirectory() override;
s64 FileSize() override;
std::string Path() const override;

size_t ReadAt(s64 absolutePos, size_t bytes, size_t count, void *data, Flags flags = Flags::NONE) override {
return ReadAt(absolutePos, bytes * count, data, flags) / bytes;
Expand All @@ -59,7 +57,6 @@ class RamCachingFileLoader : public FileLoader {
};

s64 filesize_ = 0;
FileLoader *backend_;
u8 *cache_ = nullptr;
int exists_ = -1;
int isDirectory_ = -1;
Expand Down
Loading

0 comments on commit 127330f

Please sign in to comment.