Skip to content

Commit

Permalink
Restructure async calls
Browse files Browse the repository at this point in the history
  • Loading branch information
feiy committed Dec 18, 2023
1 parent 8d0c534 commit 969a902
Show file tree
Hide file tree
Showing 21 changed files with 355 additions and 523 deletions.
10 changes: 4 additions & 6 deletions Three.V8/GamePlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <string>
#include <filesystem>
#include <gui/UIManager.h>
#include <utils/AsyncCallbacks.h>
#include "GamePlayer.h"

#define ENABLE_MSAA 1
Expand Down Expand Up @@ -57,7 +58,7 @@ void GamePlayer::LoadScript(const char* dir, const char* filename)
void GamePlayer::UnloadScript()
{
if (m_context != nullptr)
{
{
v8::Isolate* isolate = m_v8vm.m_isolate;
v8::HandleScope handle_scope(isolate);
v8::Context::Scope context_scope(m_context->m_context.Get(isolate));
Expand All @@ -72,11 +73,8 @@ void GamePlayer::UnloadScript()

void GamePlayer::Idle()
{
if (m_context != nullptr)
{
m_context->CheckPendings();
v8::platform::PumpMessageLoop(m_v8vm.m_platform.get(), m_v8vm.m_isolate);
}
AsyncCallbacks::CheckPendings();
v8::platform::PumpMessageLoop(m_v8vm.m_platform.get(), m_v8vm.m_isolate);
}

void GamePlayer::Draw(int width, int height)
Expand Down
75 changes: 0 additions & 75 deletions Three.V8/binding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -583,81 +583,6 @@ void GameContext::remove_object(void* ptr)
m_objects.erase(ptr);
}

void GameContext::add_ws_client(WSClient* client)
{
m_ws_clients.insert(client);
}

void GameContext::remove_ws_client(WSClient* client)
{
auto iter = m_ws_clients.find(client);
if (iter != m_ws_clients.end())
{
m_ws_clients.erase(iter);
}
}

void GameContext::add_opus_recorder(OpusRecorder* rec)
{
m_opus_recorders.insert(rec);
}

void GameContext::remove_opus_recorder(OpusRecorder* rec)
{
auto iter = m_opus_recorders.find(rec);
if (iter != m_opus_recorders.end())
{
m_opus_recorders.erase(iter);
}
}


void GameContext::add_avc_recorder(AVCRecorder* rec)
{
m_avc_recorders.insert(rec);
}

void GameContext::remove_avc_recorder(AVCRecorder* rec)
{
auto iter = m_avc_recorders.find(rec);
if (iter != m_avc_recorders.end())
{
m_avc_recorders.erase(iter);
}
}

void GameContext::CheckPendings()
{
m_http->CheckPendings();

{
auto iter = m_ws_clients.begin();
while (iter != m_ws_clients.end())
{
(*iter)->CheckPending();
iter++;
}
}
#if THREE_MM
{
auto iter = m_opus_recorders.begin();
while (iter != m_opus_recorders.end())
{
(*iter)->CheckPending();
iter++;
}
}
{
auto iter = m_avc_recorders.begin();
while (iter != m_avc_recorders.end())
{
(*iter)->CheckPending();
iter++;
}
}
#endif
}

void GameContext::SetPrintCallbacks(void* ptr, PrintCallback print_callback, PrintCallback error_callback)
{
m_print_callback_data = ptr;
Expand Down
19 changes: 1 addition & 18 deletions Three.V8/binding.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ struct GlobalDefinitions

class GamePlayer;
class HttpClient;
class WSClient;
class OpusRecorder;
class AVCRecorder;
class UIManager;
class GameContext
{
Expand All @@ -76,26 +73,12 @@ class GameContext
void regiter_object(v8::Local<v8::Object> obj, Dtor dtor);
void remove_object(void* ptr);

void add_ws_client(WSClient* client);
void remove_ws_client(WSClient* client);

void add_opus_recorder(OpusRecorder* rec);
void remove_opus_recorder(OpusRecorder* rec);

void add_avc_recorder(AVCRecorder* rec);
void remove_avc_recorder(AVCRecorder* rec);

void CheckPendings();

typedef void (*PrintCallback)(void* ptr, const char* str);
void SetPrintCallbacks(void* ptr, PrintCallback print_callback, PrintCallback error_callback);

private:
GamePlayer* m_gamePlayer;
std::unique_ptr<HttpClient> m_http;
std::unordered_set<WSClient*> m_ws_clients;
std::unordered_set<OpusRecorder*> m_opus_recorders;
std::unordered_set<AVCRecorder*> m_avc_recorders;
std::unique_ptr<HttpClient> m_http;
std::unique_ptr<UIManager> m_ui_manager;
static GlobalDefinitions s_globals;
void _create_context();
Expand Down
3 changes: 1 addition & 2 deletions Three.V8/multimedia/AVCRecorder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ void WrapperAVCRecorder::dtor(void* ptr, GameContext* ctx)
if (data != nullptr)
{
delete data;
self->SetCallback(nullptr, nullptr);
}
}
ctx->remove_avc_recorder(self);
delete self;
}

Expand All @@ -66,7 +66,6 @@ void WrapperAVCRecorder::New(const v8::FunctionCallbackInfo<v8::Value>& info)
AVCRecorder* self = new AVCRecorder(id_device);
info.This()->SetAlignedPointerInInternalField(0, self);
lctx.ctx()->regiter_object(info.This(), dtor);
lctx.ctx()->add_avc_recorder(self);
}


Expand Down
7 changes: 3 additions & 4 deletions Three.V8/multimedia/OpusRecorder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ void WrapperOpusRecorder::dtor(void* ptr, GameContext* ctx)
if (data != nullptr)
{
delete data;
self->SetCallback(nullptr, nullptr);
}
}
ctx->remove_opus_recorder(self);
}
delete self;
}

Expand All @@ -65,8 +65,7 @@ void WrapperOpusRecorder::New(const v8::FunctionCallbackInfo<v8::Value>& info)

OpusRecorder* self = new OpusRecorder(id_device);
info.This()->SetAlignedPointerInInternalField(0, self);
lctx.ctx()->regiter_object(info.This(), dtor);
lctx.ctx()->add_opus_recorder(self);
lctx.ctx()->regiter_object(info.This(), dtor);
}


Expand Down
8 changes: 4 additions & 4 deletions Three.V8/network/WSClient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,17 @@ void WrapperWSClient::dtor(void* ptr, GameContext* ctx)
if (data != nullptr)
{
delete data;
self->SetOpenCallback(nullptr, nullptr);
}
}
{
WSClientCallbackData* data = (WSClientCallbackData*)self->GetMessageCallbackData();
if (data != nullptr)
{
delete data;
self->SetMessageCallback(nullptr, nullptr);
}
}
ctx->remove_ws_client(self);
}
delete self;
}

Expand All @@ -74,8 +75,7 @@ void WrapperWSClient::New(const v8::FunctionCallbackInfo<v8::Value>& info)

WSClient* self = new WSClient(url.c_str());
info.This()->SetAlignedPointerInInternalField(0, self);
lctx.ctx()->regiter_object(info.This(), dtor);
lctx.ctx()->add_ws_client(self);
lctx.ctx()->regiter_object(info.This(), dtor);
}

void WrapperWSClient::Send(const v8::FunctionCallbackInfo<v8::Value>& info)
Expand Down
3 changes: 3 additions & 0 deletions ThreeEngine/ThreeEngine.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@
<ClCompile Include="savers\ProbeGridSaver.cpp" />
<ClCompile Include="scenes\Fog.cpp" />
<ClCompile Include="scenes\Scene.cpp" />
<ClCompile Include="utils\AsyncCallbacks.cpp" />
<ClCompile Include="utils\Cube2Octa.cpp" />
<ClCompile Include="utils\DDSImage.cpp" />
<ClCompile Include="utils\HDRImage.cpp" />
Expand Down Expand Up @@ -408,6 +409,8 @@
<ClInclude Include="savers\ProbeGridSaver.h" />
<ClInclude Include="scenes\Fog.h" />
<ClInclude Include="scenes\Scene.h" />
<ClInclude Include="utils\AsyncCallbacks.h" />
<ClInclude Include="utils\ConcurrentQueue.h" />
<ClInclude Include="utils\Cube2Octa.h" />
<ClInclude Include="utils\DDSImage.h" />
<ClInclude Include="utils\HDRImage.h" />
Expand Down
9 changes: 9 additions & 0 deletions ThreeEngine/ThreeEngine.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,9 @@
<ClCompile Include="renderers\routines\render_height.cpp">
<Filter>Source Files\renderers\routines</Filter>
</ClCompile>
<ClCompile Include="utils\AsyncCallbacks.cpp">
<Filter>Source Files\utils</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="scenes\Scene.h">
Expand Down Expand Up @@ -992,6 +995,12 @@
<ClInclude Include="renderers\routines\render_height.h">
<Filter>Header Files\renderers\routines</Filter>
</ClInclude>
<ClInclude Include="utils\AsyncCallbacks.h">
<Filter>Header Files\utils</Filter>
</ClInclude>
<ClInclude Include="utils\ConcurrentQueue.h">
<Filter>Header Files\utils</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
66 changes: 21 additions & 45 deletions ThreeEngine/network/HttpClient.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
#include <boost/url/src.hpp>
using namespace boost::urls;

#include "utils/AsyncCallbacks.h"
#include "HttpClient.h"
#include <iostream>
#include <thread>

#include "root_certificates.hpp"

class HttpClient::GetData : public Callable
{
public:
std::string url;
GetCallback callback;
void* userData = nullptr;
std::thread* thread = nullptr;
GetResult result;

void call() override
{
thread->join();
delete thread;
callback(result, userData);
}
};

HttpClient::HttpClient()
: m_resolver(m_ioc)
, m_ssl_ctx(ssl::context::tlsv12_client)
Expand All @@ -17,48 +35,7 @@ HttpClient::HttpClient()

HttpClient::~HttpClient()
{
// Get
{
auto iter = m_pending_gets.begin();
while (iter != m_pending_gets.end())
{
PendingGet* get_data = *iter;
get_data->thread->join();
delete get_data->thread;
delete get_data;
iter++;
}
}
}


void HttpClient::CheckPendings()
{
// Get
{
std::vector<PendingGet*> remove_lst;

auto iter = m_pending_gets.begin();
while (iter != m_pending_gets.end())
{
PendingGet* get_data = *iter;
if (get_data->finished)
{
get_data->thread->join();
delete get_data->thread;
get_data->callback(get_data->result, get_data->userData);

remove_lst.push_back(get_data);
}
iter++;
}

for (size_t i = 0; i < remove_lst.size(); i++)
{
delete remove_lst[i];
m_pending_gets.erase(remove_lst[i]);
}
}
}

bool HttpClient::Get(const char* url, std::vector<unsigned char>& data)
Expand Down Expand Up @@ -170,20 +147,19 @@ bool HttpClient::Get(const char* url, std::vector<unsigned char>& data)
return false;
}

void HttpClient::GetThread(HttpClient* self, PendingGet* get_data)
void HttpClient::GetThread(HttpClient* self, GetData* get_data)
{
get_data->result.result = self->Get(get_data->url.c_str(), get_data->result.data);
get_data->finished = true;
AsyncCallbacks::Add(get_data);
}

void HttpClient::GetAsync(const char* url, GetCallback callback, void* userData)
{
PendingGet* get_data = new PendingGet;
GetData* get_data = new GetData;
get_data->url = url;
get_data->callback = callback;
get_data->userData = userData;
get_data->thread = new std::thread(GetThread, this, get_data);
m_pending_gets.insert(get_data);
}

bool HttpClient::GetHeaders(const char* url, std::unordered_map<std::string, std::string>& headers)
Expand Down
Loading

0 comments on commit 969a902

Please sign in to comment.