Skip to content

Commit

Permalink
feat: add error handler for return_xxx (#358)
Browse files Browse the repository at this point in the history
* feat: add error handler for return_xxx

* fix: compile error

* fix: refine codes

---------

Co-authored-by: Hu Yueh-Wei <[email protected]>
  • Loading branch information
sunxilin and halajohn authored Dec 2, 2024
1 parent 01db6ac commit 15cece7
Show file tree
Hide file tree
Showing 35 changed files with 289 additions and 165 deletions.
62 changes: 56 additions & 6 deletions core/include/ten_runtime/binding/cpp/detail/ten_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,18 +201,51 @@ class ten_env_t {
return rc;
}

static void proxy_handle_return_error(::ten_env_t *ten_env, void *user_data,
::ten_error_t *err) {
TEN_ASSERT(ten_env, "Should not happen.");

auto *error_handler = static_cast<error_handler_func_t *>(user_data);
auto *cpp_ten_env =
static_cast<ten_env_t *>(ten_binding_handle_get_me_in_target_lang(
reinterpret_cast<ten_binding_handle_t *>(ten_env)));

if (err != nullptr) {
error_t cpp_err(err, false);
(*error_handler)(*cpp_ten_env, &cpp_err);
} else {
(*error_handler)(*cpp_ten_env, nullptr);
}

delete error_handler;
}

// If the 'cmd' has already been a command in the backward path, a extension
// could use this API to return the 'cmd' further.
bool return_result_directly(std::unique_ptr<cmd_result_t> &&cmd,
error_handler_func_t &&error_handler = nullptr,
error_t *err = nullptr) {
if (!cmd) {
TEN_ASSERT(0, "Invalid argument.");
return false;
}

auto rc = ten_env_return_result_directly(
c_ten_env, cmd->get_underlying_msg(),
err != nullptr ? err->get_c_error() : nullptr);
bool rc = false;
if (error_handler == nullptr) {
rc = ten_env_return_result_directly(
c_ten_env, cmd->get_underlying_msg(), nullptr, nullptr,
err != nullptr ? err->get_c_error() : nullptr);
} else {
auto *error_handler_ptr =
new error_handler_func_t(std::move(error_handler));

rc = ten_env_return_result_directly(
c_ten_env, cmd->get_underlying_msg(), proxy_handle_return_error,
error_handler_ptr, err != nullptr ? err->get_c_error() : nullptr);
if (!rc) {
delete error_handler_ptr;
}
}

if (rc) {
// The 'cmd' has been returned, so we should release the ownership of
Expand All @@ -228,6 +261,7 @@ class ten_env_t {

bool return_result(std::unique_ptr<cmd_result_t> &&cmd,
std::unique_ptr<cmd_t> &&target_cmd,
error_handler_func_t &&error_handler = nullptr,
error_t *err = nullptr) {
if (!cmd) {
TEN_ASSERT(0, "Invalid argument.");
Expand All @@ -238,9 +272,25 @@ class ten_env_t {
return false;
}

auto rc = ten_env_return_result(
c_ten_env, cmd->get_underlying_msg(), target_cmd->get_underlying_msg(),
err != nullptr ? err->get_c_error() : nullptr);
bool rc = false;

if (error_handler == nullptr) {
rc = ten_env_return_result(c_ten_env, cmd->get_underlying_msg(),
target_cmd->get_underlying_msg(), nullptr,
nullptr,
err != nullptr ? err->get_c_error() : nullptr);
} else {
auto *error_handler_ptr =
new error_handler_func_t(std::move(error_handler));

rc = ten_env_return_result(c_ten_env, cmd->get_underlying_msg(),
target_cmd->get_underlying_msg(),
proxy_handle_return_error, error_handler_ptr,
err != nullptr ? err->get_c_error() : nullptr);
if (!rc) {
delete error_handler_ptr;
}
}

if (rc) {
if (cmd->is_final()) {
Expand Down
19 changes: 12 additions & 7 deletions core/include/ten_runtime/ten_env/internal/return.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@
#include "ten_utils/lib/error.h"
#include "ten_utils/lib/smart_ptr.h"

TEN_RUNTIME_API bool ten_env_return_result(ten_env_t *self,
ten_shared_ptr_t *result,
ten_shared_ptr_t *target_cmd,
ten_error_t *err);
typedef void (*ten_env_return_result_error_handler_func_t)(ten_env_t *self,
void *user_data,
ten_error_t *err);

TEN_RUNTIME_API bool ten_env_return_result_directly(ten_env_t *self,
ten_shared_ptr_t *cmd,
ten_error_t *err);
TEN_RUNTIME_API bool ten_env_return_result(
ten_env_t *self, ten_shared_ptr_t *result, ten_shared_ptr_t *target_cmd,
ten_env_return_result_error_handler_func_t error_handler,
void *error_handler_user_data, ten_error_t *err);

TEN_RUNTIME_API bool ten_env_return_result_directly(
ten_env_t *self, ten_shared_ptr_t *cmd,
ten_env_return_result_error_handler_func_t error_handler,
void *error_handler_user_data, ten_error_t *err);

This file was deleted.

4 changes: 2 additions & 2 deletions core/src/ten_runtime/binding/go/interface/ten/ten_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ type TenEnv interface {
SendVideoFrame(videoFrame VideoFrame, handler ErrorHandler) error
SendAudioFrame(audioFrame AudioFrame, handler ErrorHandler) error

ReturnResult(result CmdResult, cmd Cmd) error
ReturnResultDirectly(result CmdResult) error
ReturnResult(result CmdResult, cmd Cmd, handler ErrorHandler) error
ReturnResultDirectly(result CmdResult, handler ErrorHandler) error

OnConfigureDone() error
OnInitDone() error
Expand Down
6 changes: 4 additions & 2 deletions core/src/ten_runtime/binding/go/interface/ten/ten_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ void ten_go_ten_env_on_create_instance_done(uintptr_t bridge_addr,

ten_go_error_t ten_go_ten_env_return_result(uintptr_t bridge_addr,
uintptr_t cmd_result_bridge_addr,
uintptr_t cmd_bridge_addr);
uintptr_t cmd_bridge_addr,
ten_go_handle_t handler_id);

ten_go_error_t ten_go_ten_env_return_result_directly(
uintptr_t bridge_addr, uintptr_t cmd_result_bridge_addr);
uintptr_t bridge_addr, uintptr_t cmd_result_bridge_addr,
ten_go_handle_t handler_id);

ten_go_error_t ten_go_ten_env_send_cmd(uintptr_t bridge_addr,
uintptr_t cmd_bridge_addr,
Expand Down
41 changes: 37 additions & 4 deletions core/src/ten_runtime/binding/go/interface/ten/ten_env_return.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ package ten
//#include "ten_env.h"
import "C"

func (p *tenEnv) ReturnResult(statusCmd CmdResult, cmd Cmd) error {
func (p *tenEnv) ReturnResult(
statusCmd CmdResult,
cmd Cmd,
handler ErrorHandler,
) error {
if statusCmd == nil {
return newTenError(
ErrnoInvalidArgument,
Expand All @@ -25,23 +29,39 @@ func (p *tenEnv) ReturnResult(statusCmd CmdResult, cmd Cmd) error {
)
}

cb := goHandleNil
if handler != nil {
cb = newGoHandle(handler)
}

defer func() {
p.keepAlive()
cmd.keepAlive()
statusCmd.keepAlive()
}()

return withCGO(func() error {
err := withCGO(func() error {
apiStatus := C.ten_go_ten_env_return_result(
p.cPtr,
statusCmd.getCPtr(),
cmd.getCPtr(),
cHandle(cb),
)
return withCGoError(&apiStatus)
})

if err != nil {
// Clean up the handle if there was an error.
loadAndDeleteGoHandle(cb)
}

return err
}

func (p *tenEnv) ReturnResultDirectly(statusCmd CmdResult) error {
func (p *tenEnv) ReturnResultDirectly(
statusCmd CmdResult,
handler ErrorHandler,
) error {
if statusCmd == nil {
return newTenError(
ErrnoInvalidArgument,
Expand All @@ -54,11 +74,24 @@ func (p *tenEnv) ReturnResultDirectly(statusCmd CmdResult) error {
statusCmd.keepAlive()
}()

return withCGO(func() error {
cb := goHandleNil
if handler != nil {
cb = newGoHandle(handler)
}

err := withCGO(func() error {
apiStatus := C.ten_go_ten_env_return_result_directly(
p.cPtr,
statusCmd.getCPtr(),
cHandle(cb),
)
return withCGoError(&apiStatus)
})

if err != nil {
// Clean up the handle if there was an error.
loadAndDeleteGoHandle(cb)
}

return err
}
Loading

0 comments on commit 15cece7

Please sign in to comment.