• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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_server_send_context.h"
17 
18 #include "context_key.h"
19 #include "event_manager.h"
20 #include "napi_utils.h"
21 #include "netstack_log.h"
22 #include "socket_constant.h"
23 
24 namespace OHOS::NetStack::Socket {
TcpServerSendContext(napi_env env,const std::shared_ptr<EventManager> & manager)25 TcpServerSendContext::TcpServerSendContext(napi_env env, const std::shared_ptr<EventManager> &manager)
26     : BaseContext(env, manager) {}
27 
ParseParams(napi_value * params,size_t paramsCount)28 void TcpServerSendContext::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 TcpServerSendContext::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 TcpServerSendContext::CheckParamsType(napi_value *params, size_t paramsCount)
71 {
72     if (paramsCount == PARAM_JUST_OPTIONS) {
73         if (NapiUtils::GetValueType(GetEnv(), params[0]) != napi_object) {
74             NETSTACK_LOGE("invalid parameter");
75             SetNeedThrowException(true);
76             SetError(PARSE_ERROR_CODE, PARSE_ERROR_MSG);
77             return false;
78         }
79         return true;
80     }
81 
82     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
83         if (NapiUtils::GetValueType(GetEnv(), params[0]) != napi_object ||
84             NapiUtils::GetValueType(GetEnv(), params[1]) != napi_function) {
85             NETSTACK_LOGE("invalid parameter");
86             SetNeedThrowException(true);
87             SetError(PARSE_ERROR_CODE, PARSE_ERROR_MSG);
88             return false;
89         }
90         return true;
91     }
92     return false;
93 }
94 
GetData(napi_value udpSendOptions)95 bool TcpServerSendContext::GetData(napi_value udpSendOptions)
96 {
97     napi_value jsData = NapiUtils::GetNamedProperty(GetEnv(), udpSendOptions, KEY_DATA);
98     if (NapiUtils::GetValueType(GetEnv(), jsData) == napi_string) {
99         std::string data = NapiUtils::GetStringFromValueUtf8(GetEnv(), jsData);
100         if (data.empty()) {
101             NETSTACK_LOGE("string data is empty");
102             return true;
103         }
104         options.SetData(data);
105         return true;
106     }
107 
108     if (NapiUtils::ValueIsArrayBuffer(GetEnv(), jsData)) {
109         size_t length = 0;
110         void *data = NapiUtils::GetInfoFromArrayBufferValue(GetEnv(), jsData, &length);
111         if (data == nullptr) {
112             NETSTACK_LOGE("arraybuffer data is empty");
113             return true;
114         }
115         options.SetData(data, length);
116         return true;
117     }
118     return false;
119 }
120 
GetErrorCode() const121 int32_t TcpServerSendContext::GetErrorCode() const
122 {
123     if (BaseContext::IsPermissionDenied()) {
124         return PERMISSION_DENIED_CODE;
125     }
126 
127     auto err = BaseContext::GetErrorCode();
128     if (err == PARSE_ERROR_CODE) {
129         return PARSE_ERROR_CODE;
130     }
131     return err + SOCKET_SERVER_ERROR_CODE_BASE;
132 }
133 
GetErrorMessage() const134 std::string TcpServerSendContext::GetErrorMessage() const
135 {
136     if (BaseContext::IsPermissionDenied()) {
137         return PERMISSION_DENIED_MSG;
138     }
139 
140     auto errCode = BaseContext::GetErrorCode();
141     if (errCode == PARSE_ERROR_CODE) {
142         return PARSE_ERROR_MSG;
143     }
144     char err[MAX_ERR_NUM] = {0};
145     (void)strerror_r(errCode, err, MAX_ERR_NUM);
146     return err;
147 }
148 } // namespace OHOS::NetStack::Socket
149