• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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 "tcp_send_context.h"
17 
18 #include "context_key.h"
19 #include "socket_constant.h"
20 #include "event_manager.h"
21 #include "netstack_log.h"
22 #include "napi_utils.h"
23 
24 namespace OHOS::NetStack::Socket {
TcpSendContext(napi_env env,const std::shared_ptr<EventManager> & manager)25 TcpSendContext::TcpSendContext(napi_env env, const std::shared_ptr<EventManager> &manager)
26     : BaseContext(env, manager) {}
27 
ParseParams(napi_value * params,size_t paramsCount)28 void TcpSendContext::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     if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_ENCODING)) {
48         std::string encoding = NapiUtils::GetStringPropertyUtf8(GetEnv(), params[0], KEY_ENCODING);
49         options.SetEncoding(encoding);
50     }
51     if (!GetData(params[0])) {
52         if (paramsCount == PARAM_OPTIONS_AND_CALLBACK && SetCallback(params[1]) != napi_ok) {
53             NETSTACK_LOGE("failed to set callback");
54         }
55         return;
56     }
57 
58     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
59         SetParseOK(SetCallback(params[1]) == napi_ok);
60         return;
61     }
62     SetParseOK(true);
63 }
64 
GetSocketFd() const65 int TcpSendContext::GetSocketFd() const
66 {
67     return sharedManager_->GetData() ? static_cast<int>(reinterpret_cast<uint64_t>(sharedManager_->GetData())) : -1;
68 }
69 
CheckParamsType(napi_value * params,size_t paramsCount)70 bool TcpSendContext::CheckParamsType(napi_value *params, size_t paramsCount)
71 {
72     if (paramsCount == PARAM_JUST_OPTIONS) {
73         return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object;
74     }
75 
76     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
77         return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object &&
78                NapiUtils::GetValueType(GetEnv(), params[1]) == napi_function;
79     }
80     return false;
81 }
82 
GetData(napi_value udpSendOptions)83 bool TcpSendContext::GetData(napi_value udpSendOptions)
84 {
85     napi_value jsData = NapiUtils::GetNamedProperty(GetEnv(), udpSendOptions, KEY_DATA);
86     if (NapiUtils::GetValueType(GetEnv(), jsData) == napi_string) {
87         std::string data = NapiUtils::GetStringFromValueUtf8(GetEnv(), jsData);
88         if (data.empty()) {
89             NETSTACK_LOGE("string data is empty");
90             return true;
91         }
92         options.SetData(data);
93         return true;
94     }
95 
96     if (NapiUtils::ValueIsArrayBuffer(GetEnv(), jsData)) {
97         size_t length = 0;
98         void *data = NapiUtils::GetInfoFromArrayBufferValue(GetEnv(), jsData, &length);
99         if (data == nullptr) {
100             NETSTACK_LOGE("arraybuffer data is empty");
101             return true;
102         }
103         options.SetData(data, length);
104         return true;
105     }
106     return false;
107 }
108 
GetErrorCode() const109 int32_t TcpSendContext::GetErrorCode() const
110 {
111     if (BaseContext::IsPermissionDenied()) {
112         return PERMISSION_DENIED_CODE;
113     }
114 
115     auto err = BaseContext::GetErrorCode();
116     if (err == PARSE_ERROR_CODE) {
117         return PARSE_ERROR_CODE;
118     }
119 #if defined(IOS_PLATFORM)
120     err = ErrCodePlatformAdapter::GetOHOSErrCode(err);
121 #endif
122     return err + SOCKET_ERROR_CODE_BASE;
123 }
124 
GetErrorMessage() const125 std::string TcpSendContext::GetErrorMessage() const
126 {
127     if (BaseContext::IsPermissionDenied()) {
128         return PERMISSION_DENIED_MSG;
129     }
130 
131     auto errCode = BaseContext::GetErrorCode();
132     if (errCode == PARSE_ERROR_CODE) {
133         return PARSE_ERROR_MSG;
134     }
135 #if defined(IOS_PLATFORM)
136     std::string errMessage;
137     ErrCodePlatformAdapter::GetOHOSErrMessage(errCode, errMessage);
138     return errMessage;
139 #else
140     char err[MAX_ERR_NUM] = {0};
141     (void)strerror_r(errCode, err, MAX_ERR_NUM);
142     return err;
143 #endif
144 }
145 } // namespace OHOS::NetStack::Socket
146