!1546 add shared manager

Merge pull request !1546 from maosiping/master
This commit is contained in:
openharmony_ci 2024-09-11 02:57:43 +00:00 committed by Gitee
commit 97383ed719
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
6 changed files with 137 additions and 17 deletions

View File

@ -14,6 +14,7 @@
*/ */
#include <netdb.h> #include <netdb.h>
#include "net_address.h" #include "net_address.h"
#include "netstack_log.h" #include "netstack_log.h"
#include "securec.h" #include "securec.h"
@ -28,6 +29,9 @@ NetAddress::NetAddress(const NetAddress &other) : address_(other.address_), fami
void NetAddress::SetIpAddress(const std::string &address) void NetAddress::SetIpAddress(const std::string &address)
{ {
if (address.empty()) {
return;
}
if (family_ == Family::IPv4) { if (family_ == Family::IPv4) {
in6_addr ipv6{}; in6_addr ipv6{};
if (inet_pton(AF_INET6, address.c_str(), &ipv6) > 0) { if (inet_pton(AF_INET6, address.c_str(), &ipv6) > 0) {
@ -54,8 +58,9 @@ void NetAddress::SetIpAddress(const std::string &address)
} }
} }
if (family_ == Family::IPv4) { if (family_ == Family::IPv4) {
auto inet = atoi(address.c_str()); size_t size = SIZE_MAX;
if (inet >= 0) { auto inet = std::stoi(address, &size);
if (size == address.length()) {
in_addr addr{}; in_addr addr{};
addr.s_addr = static_cast<in_addr_t>(inet); addr.s_addr = static_cast<in_addr_t>(inet);
address_ = inet_ntoa(addr); address_ = inet_ntoa(addr);

View File

@ -84,6 +84,8 @@ public:
void Emit(const std::string &type, const std::pair<napi_value, napi_value> &argv); void Emit(const std::string &type, const std::pair<napi_value, napi_value> &argv);
void EmitSharedManager(const std::string &type, const std::pair<napi_value, napi_value> &argv);
void SetNeedPromise(bool needPromise); void SetNeedPromise(bool needPromise);
[[nodiscard]] bool IsNeedPromise() const; [[nodiscard]] bool IsNeedPromise() const;
@ -104,6 +106,8 @@ public:
[[nodiscard]] std::shared_ptr<EventManager> GetSharedManager() const; [[nodiscard]] std::shared_ptr<EventManager> GetSharedManager() const;
void SetSharedManager(const std::shared_ptr<EventManager> &sharedManager);
void CreateReference(napi_value value); void CreateReference(napi_value value);
void DeleteReference(); void DeleteReference();
@ -155,7 +159,14 @@ private:
std::string errorMessage_; std::string errorMessage_;
protected:
std::shared_ptr<EventManager> sharedManager_; std::shared_ptr<EventManager> sharedManager_;
private:
napi_ref callbackBak1_ = nullptr;
napi_ref callbackBak2_ = nullptr;
napi_ref callbackBak3_ = nullptr;
napi_ref callbackBak4_ = nullptr;
}; };
} // namespace OHOS::NetStack } // namespace OHOS::NetStack

View File

@ -23,11 +23,11 @@
#include <vector> #include <vector>
#include "base_async_work.h" #include "base_async_work.h"
#include "base_context.h"
#include "napi/native_api.h" #include "napi/native_api.h"
#include "napi/native_common.h" #include "napi/native_common.h"
#include "base_context.h"
#include "netstack_log.h"
#include "napi_utils.h" #include "napi_utils.h"
#include "netstack_log.h"
namespace OHOS::NetStack { namespace OHOS::NetStack {
class EventManager; class EventManager;
@ -57,20 +57,15 @@ napi_value InterfaceWithSharedManager(napi_env env, napi_callback_info info, con
NETSTACK_LOGE("get event manager in napi_unwrap failed, napi_ret is %{public}d", napi_ret); NETSTACK_LOGE("get event manager in napi_unwrap failed, napi_ret is %{public}d", napi_ret);
return NapiUtils::GetUndefined(env); return NapiUtils::GetUndefined(env);
} }
if (sharedManager == nullptr) {
NETSTACK_LOGE("get event manager in napi_unwrap failed, napi_ret is %{public}d", napi_ret); auto context = new (std::nothrow) Context(env, nullptr);
return NapiUtils::GetUndefined(env);
}
auto manager = *sharedManager;
if (manager == nullptr) {
NETSTACK_LOGE("get event manager in napi_unwrap failed, napi_ret is %{public}d", napi_ret);
return NapiUtils::GetUndefined(env);
}
auto context = new Context(env, manager);
if (!context) { if (!context) {
NETSTACK_LOGE("new context is nullptr"); NETSTACK_LOGE("new context is nullptr");
return NapiUtils::GetUndefined(env); return NapiUtils::GetUndefined(env);
} }
if (sharedManager) {
context->SetSharedManager(*sharedManager);
}
context->ParseParams(params, paramsCount); context->ParseParams(params, paramsCount);
if (context->IsNeedThrowException()) { // only api9 or later need throw exception. if (context->IsNeedThrowException()) { // only api9 or later need throw exception.
napi_throw_error(env, std::to_string(context->GetErrorCode()).c_str(), context->GetErrorMessage().c_str()); napi_throw_error(env, std::to_string(context->GetErrorCode()).c_str(), context->GetErrorMessage().c_str());
@ -93,6 +88,55 @@ napi_value InterfaceWithSharedManager(napi_env env, napi_callback_info info, con
return NapiUtils::GetUndefined(env); return NapiUtils::GetUndefined(env);
} }
template <class Context>
napi_value InterfaceWithOutAsyncWorkWithSharedManager(napi_env env, napi_callback_info info,
bool (*Work)(napi_env, napi_value, Context *),
const std::string &asyncWorkName, AsyncWorkExecutor executor,
AsyncWorkCallback callback)
{
static_assert(std::is_base_of<BaseContext, Context>::value);
napi_value thisVal = nullptr;
size_t paramsCount = MAX_PARAM_NUM;
napi_value params[MAX_PARAM_NUM] = {nullptr};
NAPI_CALL(env, napi_get_cb_info(env, info, &paramsCount, params, &thisVal, nullptr));
std::shared_ptr<EventManager> *sharedManager = nullptr;
auto napi_ret = napi_unwrap(env, thisVal, reinterpret_cast<void **>(&sharedManager));
if (napi_ret != napi_ok) {
NETSTACK_LOGE("get event manager in napi_unwrap failed, napi_ret is %{public}d", napi_ret);
return NapiUtils::GetUndefined(env);
}
auto context = new (std::nothrow) Context(env, nullptr);
if (!context) {
NETSTACK_LOGE("new context is nullptr");
return NapiUtils::GetUndefined(env);
}
if (sharedManager) {
context->SetSharedManager(*sharedManager);
}
context->ParseParams(params, paramsCount);
napi_value ret = NapiUtils::GetUndefined(env);
if (NapiUtils::GetValueType(env, context->GetCallback()) != napi_function && context->IsNeedPromise()) {
NETSTACK_LOGD("%{public}s is invoked in promise mode", asyncWorkName.c_str());
ret = context->CreatePromise();
} else {
NETSTACK_LOGD("%{public}s is invoked in callback mode", asyncWorkName.c_str());
}
context->CreateReference(thisVal);
if (Work != nullptr) {
if (!Work(env, thisVal, context)) {
NETSTACK_LOGE("work failed error code = %{public}d", context->GetErrorCode());
}
}
if (!context->IsParseOK() || context->IsPermissionDenied() || context->IsNoAllowedHost() ||
context->GetSharedManager()->IsEventDestroy()) {
context->CreateAsyncWork(asyncWorkName, executor, callback);
}
return ret;
}
template <class Context> template <class Context>
napi_value Interface(napi_env env, napi_callback_info info, const std::string &asyncWorkName, napi_value Interface(napi_env env, napi_callback_info info, const std::string &asyncWorkName,
bool (*Work)(napi_env, napi_value, Context *), AsyncWorkExecutor executor, bool (*Work)(napi_env, napi_value, Context *), AsyncWorkExecutor executor,
@ -252,6 +296,9 @@ napi_value NewInstanceWithSharedManager(napi_env env, napi_callback_info info, c
napi_value OnSharedManager(napi_env env, napi_callback_info info, const std::initializer_list<std::string> &events, napi_value OnSharedManager(napi_env env, napi_callback_info info, const std::initializer_list<std::string> &events,
bool asyncCallback); bool asyncCallback);
napi_value OnceSharedManager(napi_env env, napi_callback_info info, const std::initializer_list<std::string> &events,
bool asyncCallback);
napi_value OffSharedManager(napi_env env, napi_callback_info info, const std::initializer_list<std::string> &events); napi_value OffSharedManager(napi_env env, napi_callback_info info, const std::initializer_list<std::string> &events);
} // namespace OHOS::NetStack::ModuleTemplate } // namespace OHOS::NetStack::ModuleTemplate
#endif /* COMMUNICATIONNETSTACK_NETSTACK_MODULE_TEMPLATE_H */ #endif /* COMMUNICATIONNETSTACK_NETSTACK_MODULE_TEMPLATE_H */

View File

@ -92,12 +92,18 @@ napi_status BaseContext::SetCallback(napi_value callback)
if (callback_ != nullptr) { if (callback_ != nullptr) {
(void)napi_delete_reference(env_, callback_); (void)napi_delete_reference(env_, callback_);
} }
return napi_create_reference(env_, callback, 1, &callback_); auto status = napi_create_reference(env_, callback, 1, &callback_);
callbackBak1_ = callback_;
callbackBak2_ = callback_;
callbackBak3_ = callback_;
callbackBak4_ = callback_;
return status;
} }
void BaseContext::DeleteCallback() void BaseContext::DeleteCallback()
{ {
if (callback_ == nullptr) { if (callback_ == nullptr || callback_ != callbackBak1_ || callback_ != callbackBak2_ ||
callback_ != callbackBak3_ || callback_ != callbackBak4_) {
return; return;
} }
(void)napi_delete_reference(env_, callback_); (void)napi_delete_reference(env_, callback_);
@ -180,7 +186,8 @@ std::string BaseContext::GetErrorMessage() const
napi_value BaseContext::GetCallback() const napi_value BaseContext::GetCallback() const
{ {
if (callback_ == nullptr) { if (callback_ == nullptr || callback_ != callbackBak1_ || callback_ != callbackBak2_ ||
callback_ != callbackBak3_ || callback_ != callbackBak4_) {
return nullptr; return nullptr;
} }
napi_value callback = nullptr; napi_value callback = nullptr;
@ -198,6 +205,13 @@ const std::string &BaseContext::GetAsyncWorkName() const
return asyncWorkName_; return asyncWorkName_;
} }
void BaseContext::EmitSharedManager(const std::string &type, const std::pair<napi_value, napi_value> &argv)
{
if (sharedManager_ != nullptr) {
sharedManager_->Emit(type, argv);
}
}
void BaseContext::Emit(const std::string &type, const std::pair<napi_value, napi_value> &argv) void BaseContext::Emit(const std::string &type, const std::pair<napi_value, napi_value> &argv)
{ {
if (manager_ != nullptr) { if (manager_ != nullptr) {
@ -225,6 +239,11 @@ std::shared_ptr<EventManager> BaseContext::GetSharedManager() const
return sharedManager_; return sharedManager_;
} }
void BaseContext::SetSharedManager(const std::shared_ptr<EventManager> &sharedManager)
{
sharedManager_ = sharedManager;
}
void BaseContext::ParseParams(napi_value *params, size_t paramsCount) void BaseContext::ParseParams(napi_value *params, size_t paramsCount)
{ {
if (paramsCount != 0 && paramsCount != 1) { if (paramsCount != 0 && paramsCount != 1) {

View File

@ -74,6 +74,43 @@ napi_value OnSharedManager(napi_env env, napi_callback_info info, const std::ini
return NapiUtils::GetUndefined(env); return NapiUtils::GetUndefined(env);
} }
napi_value OnceSharedManager(napi_env env, napi_callback_info info, const std::initializer_list<std::string> &events,
bool asyncCallback)
{
napi_value thisVal = nullptr;
size_t paramsCount = MAX_PARAM_NUM;
napi_value params[MAX_PARAM_NUM] = {nullptr};
NAPI_CALL(env, napi_get_cb_info(env, info, &paramsCount, params, &thisVal, nullptr));
if (paramsCount != EVENT_PARAM_NUM || NapiUtils::GetValueType(env, params[0]) != napi_string ||
NapiUtils::GetValueType(env, params[1]) != napi_function) {
NETSTACK_LOGE("on off once interface para: [string, function]");
return NapiUtils::GetUndefined(env);
}
std::string event = NapiUtils::GetStringFromValueUtf8(env, params[0]);
if (std::find(events.begin(), events.end(), event) == events.end()) {
return NapiUtils::GetUndefined(env);
}
std::shared_ptr<EventManager> *sharedManager = nullptr;
auto napi_ret = napi_unwrap(env, thisVal, reinterpret_cast<void **>(&sharedManager));
if (napi_ret != napi_ok) {
NETSTACK_LOGE("get event manager in napi_unwrap failed, napi_ret is %{public}d", napi_ret);
return NapiUtils::GetUndefined(env);
}
if (sharedManager == nullptr) {
NETSTACK_LOGE("get event manager in napi_unwrap failed, napi_ret is %{public}d", napi_ret);
return NapiUtils::GetUndefined(env);
}
auto manager = *sharedManager;
if (manager != nullptr) {
manager->AddListener(env, event, params[1], true, asyncCallback);
}
return NapiUtils::GetUndefined(env);
}
napi_value OffSharedManager(napi_env env, napi_callback_info info, const std::initializer_list<std::string> &events) napi_value OffSharedManager(napi_env env, napi_callback_info info, const std::initializer_list<std::string> &events)
{ {
napi_value thisVal = nullptr; napi_value thisVal = nullptr;

View File

@ -16,6 +16,7 @@
#include "napi_utils.h" #include "napi_utils.h"
#include <algorithm> #include <algorithm>
#include <cmath>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <initializer_list> #include <initializer_list>