Skip to content

Commit

Permalink
refactor: simplify the msgpack protocol implementation (#225)
Browse files Browse the repository at this point in the history
Co-authored-by: Hu Yueh-Wei <[email protected]>
  • Loading branch information
sunxilin and halajohn authored Nov 7, 2024
1 parent df881e6 commit 209bf38
Show file tree
Hide file tree
Showing 107 changed files with 454 additions and 3,038 deletions.
16 changes: 0 additions & 16 deletions core/include/ten_runtime/msg/cmd/custom/cmd.h

This file was deleted.

1 change: 0 additions & 1 deletion core/include/ten_runtime/ten.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "ten_runtime/extension_group/extension_group.h" // IWYU pragma: export
#include "ten_runtime/msg/audio_frame/audio_frame.h" // IWYU pragma: export
#include "ten_runtime/msg/cmd/close_app/cmd.h" // IWYU pragma: export
#include "ten_runtime/msg/cmd/custom/cmd.h" // IWYU pragma: export
#include "ten_runtime/msg/cmd_result/cmd_result.h" // IWYU pragma: export
#include "ten_runtime/msg/data/data.h" // IWYU pragma: export
#include "ten_runtime/msg/msg.h" // IWYU pragma: export
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ TEN_RUNTIME_PRIVATE_API ten_json_t *ten_raw_cmd_custom_to_json(
TEN_RUNTIME_API ten_cmd_t *ten_raw_cmd_custom_create_from_json(
ten_json_t *json, ten_error_t *err);

TEN_RUNTIME_API ten_shared_ptr_t *ten_cmd_custom_create_empty(void);
TEN_RUNTIME_API ten_shared_ptr_t *ten_cmd_custom_create(void);

TEN_RUNTIME_PRIVATE_API ten_cmd_t *ten_raw_cmd_custom_create(
const char *cmd_name);
Expand Down
5 changes: 5 additions & 0 deletions core/include_internal/ten_runtime/msg/loop_fields.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "ten_runtime/ten_config.h"

#include "ten_utils/lib/error.h"
#include "ten_utils/lib/smart_ptr.h"
#include "ten_utils/value/value_kv.h"

typedef struct ten_msg_t ten_msg_t;
Expand Down Expand Up @@ -46,3 +47,7 @@ TEN_RUNTIME_API void ten_msg_field_process_data_init(
TEN_RUNTIME_API bool ten_raw_msg_loop_all_fields(
ten_msg_t *self, ten_raw_msg_process_one_field_func_t cb, void *user_data,
ten_error_t *err);

TEN_RUNTIME_API bool ten_msg_loop_all_fields(
ten_shared_ptr_t *self, ten_raw_msg_process_one_field_func_t cb,
void *user_data, ten_error_t *err);
1 change: 0 additions & 1 deletion core/include_internal/ten_runtime/msg/msg_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include "include_internal/ten_runtime/msg/data/data.h"
#include "include_internal/ten_runtime/msg/msg.h"
#include "include_internal/ten_runtime/msg/video_frame/video_frame.h"
#include "ten_runtime/msg/cmd/custom/cmd.h"
#include "ten_runtime/msg/cmd/start_graph/cmd.h"
#include "ten_runtime/msg/msg.h"

Expand Down
4 changes: 4 additions & 0 deletions core/include_internal/ten_utils/value/value_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ TEN_UTILS_API bool ten_value_set_float64(ten_value_t *self, double value);

TEN_UTILS_API bool ten_value_set_string(ten_value_t *self, const char *value);

TEN_UTILS_API bool ten_value_set_string_with_size(ten_value_t *self,
const char *value,
size_t len);

TEN_UTILS_API bool ten_value_set_array_with_move(ten_value_t *self,
ten_list_t *value);

Expand Down
2 changes: 1 addition & 1 deletion core/src/ten_runtime/binding/go/native/msg/cmd/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ ten_go_status_t ten_go_cmd_create_cmd(const void *cmd_name, int cmd_name_len,
ten_go_status_t status;
ten_go_status_init_with_errno(&status, TEN_ERRNO_OK);

ten_shared_ptr_t *cmd = ten_cmd_custom_create_empty();
ten_shared_ptr_t *cmd = ten_cmd_custom_create();
TEN_ASSERT(cmd && ten_cmd_check_integrity(cmd), "Should not happen.");

ten_msg_set_name_with_size(cmd, cmd_name, cmd_name_len, NULL);
Expand Down
13 changes: 2 additions & 11 deletions core/src/ten_runtime/msg/cmd_base/cmd/custom/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
// Licensed under the Apache License, Version 2.0, with certain conditions.
// Refer to the "LICENSE" file in the root directory for more information.
//
#include "ten_runtime/msg/cmd/custom/cmd.h"
#include "include_internal/ten_runtime/msg/cmd_base/cmd/cmd.h"

#include <stdlib.h>
#include <string.h>

#include "include_internal/ten_runtime/common/constant_str.h"
#include "include_internal/ten_runtime/msg/cmd_base/cmd/cmd.h"
#include "include_internal/ten_runtime/msg/cmd_base/cmd/custom/cmd.h"
#include "include_internal/ten_runtime/msg/cmd_base/cmd/custom/field/field_info.h"
#include "include_internal/ten_runtime/msg/field/field_info.h"
Expand All @@ -25,7 +24,6 @@
#include "ten_utils/lib/smart_ptr.h"
#include "ten_utils/lib/string.h"
#include "ten_utils/macro/check.h"
#include "ten_utils/macro/mark.h"
#include "ten_utils/value/value.h"

static void ten_raw_cmd_custom_destroy(ten_cmd_t *self) {
Expand Down Expand Up @@ -53,7 +51,7 @@ static ten_cmd_t *ten_raw_cmd_custom_create_empty(void) {
return raw_cmd;
}

ten_shared_ptr_t *ten_cmd_custom_create_empty(void) {
ten_shared_ptr_t *ten_cmd_custom_create(void) {
return ten_shared_ptr_create(ten_raw_cmd_custom_create_empty(),
ten_raw_cmd_custom_destroy);
}
Expand All @@ -69,13 +67,6 @@ ten_cmd_t *ten_raw_cmd_custom_create(const char *cmd_name) {
return cmd;
}

ten_shared_ptr_t *ten_cmd_custom_create(const char *cmd_name) {
TEN_ASSERT(cmd_name, "Should not happen.");

ten_cmd_t *cmd = ten_raw_cmd_custom_create(cmd_name);
return ten_shared_ptr_create(cmd, ten_raw_cmd_custom_destroy);
}

static bool ten_raw_cmd_custom_init_from_json(ten_cmd_t *self, ten_json_t *json,
ten_error_t *err) {
TEN_ASSERT(self && ten_raw_cmd_check_integrity((ten_cmd_t *)self),
Expand Down
10 changes: 10 additions & 0 deletions core/src/ten_runtime/msg/loop_fields.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,13 @@ bool ten_raw_msg_loop_all_fields(ten_msg_t *self,

return loop_all_fields(self, cb, user_data, err);
}

bool ten_msg_loop_all_fields(ten_shared_ptr_t *self,
ten_raw_msg_process_one_field_func_t cb,
void *user_data, ten_error_t *err) {
TEN_ASSERT(self && ten_msg_check_integrity(self), "Invalid argument.");
TEN_ASSERT(cb, "Invalid argument.");

return ten_raw_msg_loop_all_fields(ten_msg_get_raw_msg(self), cb, user_data,
err);
}
9 changes: 8 additions & 1 deletion core/src/ten_runtime/msg/msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "ten_runtime/common/errno.h"
#include "ten_runtime/msg/audio_frame/audio_frame.h"
#include "ten_runtime/msg/cmd/close_app/cmd.h"
#include "ten_runtime/msg/cmd/stop_graph/cmd.h"
#include "ten_runtime/msg/cmd_result/cmd_result.h"
#include "ten_runtime/msg/data/data.h"
#include "ten_runtime/msg/msg.h"
Expand Down Expand Up @@ -862,7 +863,13 @@ ten_shared_ptr_t *ten_msg_create_from_msg_type(TEN_MSG_TYPE msg_type) {
case TEN_MSG_TYPE_CMD_CLOSE_APP:
return ten_cmd_close_app_create();
case TEN_MSG_TYPE_CMD:
return ten_cmd_custom_create_empty();
return ten_cmd_custom_create();
case TEN_MSG_TYPE_CMD_START_GRAPH:
return ten_cmd_start_graph_create();
case TEN_MSG_TYPE_CMD_STOP_GRAPH:
return ten_cmd_stop_graph_create();
case TEN_MSG_TYPE_CMD_TIMEOUT:
return ten_cmd_timeout_create(0);
case TEN_MSG_TYPE_CMD_RESULT:
return ten_cmd_result_create_from_cmd(TEN_STATUS_CODE_OK, NULL);
case TEN_MSG_TYPE_DATA:
Expand Down
14 changes: 9 additions & 5 deletions core/src/ten_utils/lib/sys/general/buf.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
#include <string.h>

#include "include_internal/ten_utils/lib/buf.h"
#include "ten_utils/macro/check.h"
#include "ten_utils/lib/alloc.h"
#include "ten_utils/lib/signature.h"
#include "ten_utils/macro/check.h"
#include "ten_utils/macro/memory.h"

bool ten_buf_check_integrity(ten_buf_t *self) {
Expand All @@ -26,7 +26,7 @@ bool ten_buf_check_integrity(ten_buf_t *self) {
return true;
}

static void ten_buf_reset_to_empty_directly(ten_buf_t *self) {
static void ten_buf_init_empty(ten_buf_t *self) {
TEN_ASSERT(self, "Invalid argument.");

ten_signature_set(&self->signature, TEN_BUF_SIGNATURE);
Expand Down Expand Up @@ -76,7 +76,7 @@ bool ten_buf_init_with_owned_data(ten_buf_t *self, size_t size) {
return false;
}

ten_buf_reset_to_empty_directly(self);
ten_buf_init_empty(self);

if (size != 0) {
self->data = TEN_MALLOC(size);
Expand All @@ -98,7 +98,7 @@ bool ten_buf_init_with_unowned_data(ten_buf_t *self, uint8_t *data,
return false;
}

ten_buf_reset_to_empty_directly(self);
ten_buf_init_empty(self);

self->data = data;
self->content_size = size;
Expand All @@ -118,7 +118,7 @@ bool ten_buf_init_with_copying_data(ten_buf_t *self, uint8_t *data,
return false;
}

ten_buf_reset_to_empty_directly(self);
ten_buf_init_empty(self);

self->data = TEN_MALLOC(size);
TEN_ASSERT(self->data, "Failed to allocate memory.");
Expand Down Expand Up @@ -300,5 +300,9 @@ void ten_buf_move(ten_buf_t *self, ten_buf_t *other) {
self->owns_memory = other->owns_memory;
self->is_fixed_size = other->is_fixed_size;

// The buf_t 'other' being moved to 'self' means that the ownership of the
// memory has been transferred.
other->owns_memory = false;

ten_buf_init_with_owned_data(other, 0);
}
11 changes: 11 additions & 0 deletions core/src/ten_utils/value/value_set.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ bool ten_value_set_string(ten_value_t *self, const char *value) {
return true;
}

TEN_UTILS_API bool ten_value_set_string_with_size(ten_value_t *self,
const char *value,
size_t len) {
TEN_ASSERT(self && ten_value_check_integrity(self), "Invalid argument.");
TEN_ASSERT(self->type == TEN_TYPE_STRING, "Invalid argument.");

ten_string_init_formatted(&self->content.string, "%.*s", len, value);

return true;
}

bool ten_value_set_array_with_move(ten_value_t *self, ten_list_t *value) {
TEN_ASSERT(self, "Invalid argument.");
TEN_ASSERT(self->type == TEN_TYPE_ARRAY, "Invalid argument.");
Expand Down
4 changes: 2 additions & 2 deletions packages/core_protocols/msgpack/common/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ ten_shared_ptr_t *ten_msgpack_parser_parse_data(ten_msgpack_parser_t *self) {
ten_msgpack_parser_feed_data(&msg_parser, self->unpacked.data.via.ext.ptr,
self->unpacked.data.via.ext.size);

new_msg = ten_msgpack_deserialize_msg_internal(&msg_parser.unpacker,
&msg_parser.unpacked);
new_msg =
ten_msgpack_deserialize_msg(&msg_parser.unpacker, &msg_parser.unpacked);

ten_msgpack_parser_deinit(&msg_parser);
} else if (rc == MSGPACK_UNPACK_CONTINUE) {
Expand Down
Loading

0 comments on commit 209bf38

Please sign in to comment.