Skip to content

Commit

Permalink
fix unzip issue, forcing HttpServer to stop before closing.
Browse files Browse the repository at this point in the history
  • Loading branch information
pigpigyyy committed Feb 1, 2024
1 parent 4bf8734 commit 356b981
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 25 deletions.
5 changes: 5 additions & 0 deletions Source/Basic/Content.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,11 @@ void Content::unzipAsync(String zipFile, String folderPath, const std::function<
return;
}
auto zip = std::make_shared<ZipFile>(fullZipPath);
if (!zip->isOK()) {
Error("failed to unzip file \"{}\"", zipFile.toString());
callback(false);
return;
}
std::string rootDir;
BLOCK_START
auto entries = zip->getDirEntries(""s, false);
Expand Down
7 changes: 7 additions & 0 deletions Source/Basic/Director.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#include "Effect/Effect.h"
#include "Entity/Entity.h"
#include "GUI/ImGuiDora.h"
#include "Http/HttpServer.h"
#include "Input/Controller.h"
#include "Input/Keyboard.h"
#include "Input/TouchDispather.h"
Expand Down Expand Up @@ -520,6 +521,12 @@ void Director::handleSDLEvent(const SDL_Event& event) {
_stoped = true;
Event::send("AppQuit"_slice);
cleanup();
if (Singleton<HttpServer>::isInitialized()) {
SharedHttpServer.stop();
}
if (Singleton<HttpClient>::isInitialized()) {
SharedHttpClient.stop();
}
break;
// The application is being terminated by the OS.
case SDL_APP_TERMINATING:
Expand Down
69 changes: 49 additions & 20 deletions Source/Http/HttpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -506,11 +506,32 @@ const char* HttpServer::getVersion() {
return CPPHTTPLIB_VERSION;
}

void HttpClient::downloadAsync(String url, String filePath, const std::function<void (bool interrupted, uint64_t current, uint64_t total)>& progress) {
/* HttpClient */

HttpClient::HttpClient()
: _thread(nullptr),
_stopped(false) {
}

HttpClient::~HttpClient() {
stop();
}

bool HttpClient::isStopped() const {
return _stopped;
}

void HttpClient::downloadAsync(String url, String filePath, const std::function<void(bool interrupted, uint64_t current, uint64_t total)>& progress) {
if (_stopped) {
progress(true, 0, 0);
return;
}
if (!_thread) {
_thread = SharedAsyncThread.newThread();
}
static std::regex urlRegex(
R"(^(([^:\/?#]+):)?(//([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?)",
std::regex::extended
);
std::regex::extended);
std::smatch matchResult;
auto urlStr = url.toString();
std::string schemeHostPort, pathToGet;
Expand Down Expand Up @@ -543,7 +564,7 @@ void HttpClient::downloadAsync(String url, String filePath, const std::function<
progress(true, 0, 0);
return;
}
SharedAsyncThread.run([schemeHostPort, fileStr = filePath.toString(), urlStr, progress, pathToGet]() {
_thread->run([schemeHostPort, fileStr = filePath.toString(), urlStr, progress, pathToGet]() {
try {
httplib::Client client(schemeHostPort);
client.enable_server_certificate_verification(false);
Expand All @@ -553,24 +574,28 @@ void HttpClient::downloadAsync(String url, String filePath, const std::function<
Error("invalid local file path \"{}\" to download to", fileStr);
return;
}
auto result = client.Get(pathToGet, [&](const char* data, size_t data_length) -> bool {
if (!out.write(data, data_length)) {
Error("failed to write downloaded file for \"{}\"", urlStr);
SharedApplication.invokeInLogic([progress]() {
progress(true, 0, 0);
auto result = client.Get(
pathToGet, [&](const char* data, size_t data_length) -> bool {
if (SharedHttpClient.isStopped()) {
return false;
} else if (!out.write(data, data_length)) {
Error("failed to write downloaded file for \"{}\"", urlStr);
SharedApplication.invokeInLogic([progress]() {
progress(true, 0, 0);
});
std::error_code err;
fs::remove_all(fileStr, err);
WarnIf(err, "failed to remove download file \"{}\" due to \"{}\".", fileStr, err.message());
return false;
}
return true;
},
[&](uint64_t current, uint64_t total) -> bool {
SharedApplication.invokeInLogic([progress, current, total]() {
progress(false, current, total);
});
std::error_code err;
fs::remove_all(fileStr, err);
WarnIf(err, "failed to remove download file \"{}\" due to \"{}\".", fileStr, err.message());
return false;
}
return true;
}, [&](uint64_t current, uint64_t total) -> bool {
SharedApplication.invokeInLogic([progress, current, total]() {
progress(false, current, total);
return true;
});
return true;
});
if (!result || result.error() != httplib::Error::Success) {
Error("failed to download \"{}\" due to {}", urlStr, httplib::to_string(result.error()));
SharedApplication.invokeInLogic([progress]() {
Expand All @@ -591,4 +616,8 @@ void HttpClient::downloadAsync(String url, String filePath, const std::function<
});
}

void HttpClient::stop() {
_stopped = true;
}

NS_DORA_END
16 changes: 15 additions & 1 deletion Source/Http/HttpServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,21 @@ class HttpServer {

class HttpClient {
public:
static void downloadAsync(String url, String filePath, const std::function<void (bool interrupted, uint64_t current, uint64_t total)>& progress);
PROPERTY_BOOL(Stopped);
virtual ~HttpClient();
void stop();
void downloadAsync(String url, String filePath, const std::function<void(bool interrupted, uint64_t current, uint64_t total)>& progress);

protected:
HttpClient();

private:
Async* _thread;
bool _stopped;
SINGLETON_REF(HttpClient, AsyncThread, Director);
};

#define SharedHttpClient \
Dora::Singleton<Dora::HttpClient>::shared()

NS_DORA_END
1 change: 1 addition & 0 deletions Source/Lua/Builtin/Initialization.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ do
dora.Keyboard = dora.Keyboard()
dora.DB = dora.DB()
dora.HttpServer = dora.HttpServer()
dora.HttpClient = dora.HttpClient()
dora.Platformer.Decision.AI = dora.Platformer.Decision.AI()
dora.Platformer.Data = dora.Platformer.Data()
end
Expand Down
3 changes: 3 additions & 0 deletions Source/Lua/LuaManual.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ int HttpServer_postSchedule(lua_State* L);
int HttpServer_upload(lua_State* L);
inline HttpServer* HttpServer_shared() { return &SharedHttpServer; }

/* HttpClient */
inline HttpClient* HttpClient_shared() { return &SharedHttpClient; }

/* Effect */
inline Pass* Effect_get(Effect* self, size_t index) { return self->get(index - 1); }

Expand Down
6 changes: 5 additions & 1 deletion Tools/dora-dora/src/LogView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ const LogView = memo((props: LogViewProps) => {
color: Color.TextSecondary
}}
rowHeight={22}
enableSearch stream follow/>
selectableLines
enableSearch
stream
follow
/>
</DialogContent>
<DialogActions>
<form noValidate autoComplete="off" style={{width: "100%"}} onSubmit={onSubmit}>
Expand Down
4 changes: 2 additions & 2 deletions Tools/dora-dora/src/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ i18n
runFailed: "failed to run {{title}}",
startUnzip: "start to unzip file {{title}}",
doneUnzip: "finished unziping file {{title}}",
failedUnzip: "failed to unzip file {{title}}, make sure no same named folder exist",
failedUnzip: "failed to unzip file {{title}}, make sure it's a ZIP and no same named folder exist",
failedTS: "TypeScript compile error, check log for details",
},
popup: {
Expand Down Expand Up @@ -172,7 +172,7 @@ i18n
runFailed: "运行 {{title}} 失败",
startUnzip: "开始解压缩 {{title}}",
doneUnzip: "完成解压缩 {{title}}",
failedUnzip: "解压缩 {{title}} 未成功,请确保目录下无同名文件夹",
failedUnzip: "解压缩 {{title}} 失败,请确认为 ZIP 文件且目录下无同名文件夹",
failedTS: "TypeScript 编译报错,请查看日志",
},
popup: {
Expand Down
3 changes: 2 additions & 1 deletion Tools/tolua++/Dora.h
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,8 @@ class HttpServer

class HttpClient
{
static void downloadAsync(String url, String filePath, tolua_function_void progress);
void downloadAsync(String url, String filePath, tolua_function_void progress);
static tolua_outside HttpClient* HttpClient_shared @ create();
};

namespace ML {
Expand Down

0 comments on commit 356b981

Please sign in to comment.