• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "connect_context.h"
17 
18 #include "context_key.h"
19 #include "socket_constant.h"
20 #include "net_address.h"
21 #include "event_manager.h"
22 #include "netstack_log.h"
23 #include "napi_utils.h"
24 
25 namespace OHOS::NetStack {
ConnectContext(napi_env env,EventManager * manager)26 ConnectContext::ConnectContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {}
27 
ParseParams(napi_value * params,size_t paramsCount)28 void ConnectContext::ParseParams(napi_value *params, size_t paramsCount)
29 {
30     bool valid = CheckParamsType(params, paramsCount);
31     if (!valid) {
32         if (paramsCount == PARAM_JUST_CALLBACK) {
33             if (NapiUtils::GetValueType(GetEnv(), params[0]) == napi_function) {
34                 SetCallback(params[0]);
35             }
36             return;
37         }
38         if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
39             if (NapiUtils::GetValueType(GetEnv(), params[1]) == napi_function) {
40                 SetCallback(params[1]);
41             }
42             return;
43         }
44         return;
45     }
46 
47     napi_value netAddress = NapiUtils::GetNamedProperty(GetEnv(), params[0], KEY_ADDRESS);
48 
49     std::string addr = NapiUtils::GetStringPropertyUtf8(GetEnv(), netAddress, KEY_ADDRESS);
50     if (addr.empty()) {
51         NETSTACK_LOGE("address is empty");
52     }
53     options.address.SetAddress(addr);
54     if (NapiUtils::HasNamedProperty(GetEnv(), netAddress, KEY_FAMILY)) {
55         uint32_t family = NapiUtils::GetUint32Property(GetEnv(), netAddress, KEY_FAMILY);
56         options.address.SetFamilyByJsValue(family);
57     }
58     if (NapiUtils::HasNamedProperty(GetEnv(), netAddress, KEY_PORT)) {
59         uint16_t port = static_cast<uint16_t>(NapiUtils::GetUint32Property(GetEnv(), netAddress, KEY_PORT));
60         options.address.SetPort(port);
61     }
62 
63     uint32_t timeout = NapiUtils::GetUint32Property(GetEnv(), params[0], KEY_TIMEOUT);
64     if (timeout != 0) {
65         options.SetTimeout(timeout);
66     }
67 
68     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
69         SetParseOK(SetCallback(params[1]) == napi_ok);
70         return;
71     }
72     SetParseOK(true);
73 }
74 
GetSocketFd() const75 int ConnectContext::GetSocketFd() const
76 {
77     return (int)(uint64_t)manager_->GetData();
78 }
79 
CheckParamsType(napi_value * params,size_t paramsCount)80 bool ConnectContext::CheckParamsType(napi_value *params, size_t paramsCount)
81 {
82     if (paramsCount == PARAM_JUST_OPTIONS) {
83         return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object &&
84                NapiUtils::GetValueType(GetEnv(), NapiUtils::GetNamedProperty(GetEnv(), params[0], KEY_ADDRESS)) ==
85                    napi_object;
86     }
87 
88     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
89         return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object &&
90                NapiUtils::GetValueType(GetEnv(), NapiUtils::GetNamedProperty(GetEnv(), params[0], KEY_ADDRESS)) ==
91                    napi_object &&
92                NapiUtils::GetValueType(GetEnv(), params[1]) == napi_function;
93     }
94     return false;
95 }
96 
GetErrorCode() const97 int32_t ConnectContext::GetErrorCode() const
98 {
99     if (BaseContext::IsPermissionDenied()) {
100         return PERMISSION_DENIED_CODE;
101     }
102 
103     auto err = BaseContext::GetErrorCode();
104     if (err == PARSE_ERROR_CODE) {
105         return PARSE_ERROR_CODE;
106     }
107     return err + SOCKET_ERROR_CODE_BASE;
108 }
109 
GetErrorMessage() const110 std::string ConnectContext::GetErrorMessage() const
111 {
112     if (BaseContext::IsPermissionDenied()) {
113         return PERMISSION_DENIED_MSG;
114     }
115 
116     auto errCode = BaseContext::GetErrorCode();
117     if (errCode == PARSE_ERROR_CODE) {
118         return PARSE_ERROR_MSG;
119     }
120     char err[MAX_ERR_NUM] = {0};
121     (void)strerror_r(errCode, err, MAX_ERR_NUM);
122     return err;
123 }
124 } // namespace OHOS::NetStack
125