OpenHarmony开发者论坛

标题: ArkTs与C++通信时,在NAPI中调用C端的方法后,返回的结果会等待2分11秒才会返回? [打印本页]

作者: LCoder1024    时间: 2024-1-28 00:12
标题: ArkTs与C++通信时,在NAPI中调用C端的方法后,返回的结果会等待2分11秒才会返回?
[md]ArkTs与C++通信时,在NAPI中调用C端的方法【C会调用服务做网络请求】后,服务端的服务开启,就会立马返回结果码;如果服务端的服务未开启的时候,NAPI 调用C端的方法后  需要等待2分11秒才会返回结果码,(Android中会立马返回结果码)
[/md]
作者: 马迪    时间: 2024-1-29 09:58
没有代码,大家怎么看呢
大概率是代码写的有问题
作者: LCoder1024    时间: 2024-1-29 10:22
回复 马迪: unsigned char *stringToUnsignedChar(napi_env env, napi_value arg) {
    char *type = nullptr;
    size_t typeLen = 0;
    napi_get_value_string_utf8(env, arg, nullptr, 0, &typeLen);
    type = new char[typeLen + 1];
    napi_get_value_string_utf8(env, arg, type, typeLen + 1, &typeLen);
    string str = type;
    char *temp = (char *)str.data();
    unsigned char *unsignedCharPtr = reinterpret_cast<unsigned char *>(const_cast<char *>(temp));
    return unsignedCharPtr;
}

struct InitData {
    napi_async_work asyncWork = nullptr;
    napi_deferred deferred = nullptr;
    napi_ref callback = nullptr;
    unsigned char *args[5] = {};
    int result = 0;
};
static napi_value initialize(napi_env env, napi_callback_info info) {
    size_t argc = 5;
    napi_value args[5] = {nullptr};
    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    napi_value promise = nullptr;
    napi_deferred deferred = nullptr;
    napi_create_promise(env, &deferred, &promise);
    // 异步工作项上下文用户数据,传递到异步工作项的execute、complete之间传递数据
    auto initData = new InitData{
        .asyncWork = nullptr,
        .deferred = deferred,
    };
    initData->args[0] = stringToUnsignedChar(env, args[0]);
    initData->args[1] = stringToUnsignedChar(env, args[1]);
    initData->args[2] = stringToUnsignedChar(env, args[2]);
    initData->args[3] = stringToUnsignedChar(env, args[3]);
    initData->args[4] = stringToUnsignedChar(env, args[4]);

    // 创建async work,创建成功后通过最后一个参数(addonData->asyncWork)返回async work的handle
    napi_value resource = nullptr;
    napi_create_string_utf8(env, "initialize", NAPI_AUTO_LENGTH, &resource);
    napi_create_async_work(
        env, nullptr, resource, [](napi_env env, void *data) {
        InitData *initData = (InitData *)data;
    unsigned char *cfgFilePath = initData->args[0];
    unsigned char *userID = initData->args[1];
    unsigned char *appID = initData->args[2];
    unsigned char *deviceID = initData->args[3];
    unsigned char *certID = initData->args[4];
   

    initData->result = HSOF_Initialize_short(cfgFilePath, userID, appID, deviceID, certID); },
        [](napi_env env, napi_status status, void *data) {
            InitData *initData = (InitData *)data;
            napi_value result = nullptr;
            napi_create_int64(env, initData->result, &result);
            napi_resolve_deferred(env, initData->deferred, result);
            if (initData->callback != nullptr) {
                napi_delete_reference(env, initData->callback);
            }
            // 删除异步 work
            napi_delete_async_work(env, initData->asyncWork);
            delete initData;
            initData = nullptr;
        },
        (void *)initData, &initData->asyncWork);
    // 将刚创建的async work加到队列,由底层去调度执行
    napi_queue_async_work(env, initData->asyncWork);
    return promise;
}

EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports) {
    napi_property_descriptor desc[] = {
        {"initialize", nullptr, initialize, nullptr, nullptr, nullptr, napi_default, nullptr}};
    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
    return exports;
}
EXTERN_C_END
static napi_module demoModule = {
    .nm_version = 1,
    .nm_flags = 0,
    .nm_filename = nullptr,
    .nm_register_func = Init,
    .nm_modname = "Shield",
    .nm_priv = ((void *)0),
    .reserved = {0},
};

extern "C" __attribute__((constructor)) void RegisterShieldModule(void) {
    napi_module_register(&demoModule);
}
作者: LCoder1024    时间: 2024-1-29 10:23
代码补充

unsigned char *stringToUnsignedChar(napi_env env, napi_value arg) {
    char *type = nullptr;
    size_t typeLen = 0;
    napi_get_value_string_utf8(env, arg, nullptr, 0, &typeLen);
    type = new char[typeLen + 1];
    napi_get_value_string_utf8(env, arg, type, typeLen + 1, &typeLen);
    string str = type;
    char *temp = (char *)str.data();
    unsigned char *unsignedCharPtr = reinterpret_cast<unsigned char *>(const_cast<char *>(temp));
    return unsignedCharPtr;
}

struct InitData {
    napi_async_work asyncWork = nullptr;
    napi_deferred deferred = nullptr;
    napi_ref callback = nullptr;
    unsigned char *args[5] = {};
    int result = 0;
};
static napi_value initialize(napi_env env, napi_callback_info info) {
    size_t argc = 5;
    napi_value args[5] = {nullptr};
    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    napi_value promise = nullptr;
    napi_deferred deferred = nullptr;
    napi_create_promise(env, &deferred, &promise);
    // 异步工作项上下文用户数据,传递到异步工作项的execute、complete之间传递数据
    auto initData = new InitData{
        .asyncWork = nullptr,
        .deferred = deferred,
    };
    initData->args[0] = stringToUnsignedChar(env, args[0]);
    initData->args[1] = stringToUnsignedChar(env, args[1]);
    initData->args[2] = stringToUnsignedChar(env, args[2]);
    initData->args[3] = stringToUnsignedChar(env, args[3]);
    initData->args[4] = stringToUnsignedChar(env, args[4]);

    // 创建async work,创建成功后通过最后一个参数(addonData->asyncWork)返回async work的handle
    napi_value resource = nullptr;
    napi_create_string_utf8(env, "initialize", NAPI_AUTO_LENGTH, &resource);
    napi_create_async_work(
        env, nullptr, resource, [](napi_env env, void *data) {
        InitData *initData = (InitData *)data;
    unsigned char *cfgFilePath = initData->args[0];
    unsigned char *userID = initData->args[1];
    unsigned char *appID = initData->args[2];
    unsigned char *deviceID = initData->args[3];
    unsigned char *certID = initData->args[4];
   

    initData->result = HSOF_Initialize_short(cfgFilePath, userID, appID, deviceID, certID); },
        [](napi_env env, napi_status status, void *data) {
            InitData *initData = (InitData *)data;
            napi_value result = nullptr;
            napi_create_int64(env, initData->result, &result);
            napi_resolve_deferred(env, initData->deferred, result);
            if (initData->callback != nullptr) {
                napi_delete_reference(env, initData->callback);
            }
            // 删除异步 work
            napi_delete_async_work(env, initData->asyncWork);
            delete initData;
            initData = nullptr;
        },
        (void *)initData, &initData->asyncWork);
    // 将刚创建的async work加到队列,由底层去调度执行
    napi_queue_async_work(env, initData->asyncWork);
    return promise;
}

EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports) {
    napi_property_descriptor desc[] = {
        {"initialize", nullptr, initialize, nullptr, nullptr, nullptr, napi_default, nullptr}};
    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
    return exports;
}
EXTERN_C_END
static napi_module demoModule = {
    .nm_version = 1,
    .nm_flags = 0,
    .nm_filename = nullptr,
    .nm_register_func = Init,
    .nm_modname = "Shield",
    .nm_priv = ((void *)0),
    .reserved = {0},
};

extern "C" __attribute__((constructor)) void RegisterShieldModule(void) {
    napi_module_register(&demoModule);
}
作者: 马迪    时间: 2024-1-29 15:56
调试一下把,或者打打日志,看下HSOF_Initialize_short的实现




欢迎光临 OpenHarmony开发者论坛 (https://forums.openharmony.cn/) Powered by Discuz! X3.5