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,EventManager * manager)25 TcpServerSendContext::TcpServerSendContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {}
26
ParseParams(napi_value * params,size_t paramsCount)27 void TcpServerSendContext::ParseParams(napi_value *params, size_t paramsCount)
28 {
29 bool valid = CheckParamsType(params, paramsCount);
30 if (!valid) {
31 if (paramsCount == PARAM_JUST_CALLBACK) {
32 if (NapiUtils::GetValueType(GetEnv(), params[0]) == napi_function) {
33 SetCallback(params[0]);
34 }
35 return;
36 }
37 if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
38 if (NapiUtils::GetValueType(GetEnv(), params[1]) == napi_function) {
39 SetCallback(params[1]);
40 }
41 return;
42 }
43 return;
44 }
45
46 if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_ENCODING)) {
47 std::string encoding = NapiUtils::GetStringPropertyUtf8(GetEnv(), params[0], KEY_ENCODING);
48 options.SetEncoding(encoding);
49 }
50 if (!GetData(params[0])) {
51 if (paramsCount == PARAM_OPTIONS_AND_CALLBACK && SetCallback(params[1]) != napi_ok) {
52 NETSTACK_LOGE("failed to set callback");
53 }
54 return;
55 }
56
57 if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
58 SetParseOK(SetCallback(params[1]) == napi_ok);
59 return;
60 }
61 SetParseOK(true);
62 }
63
GetSocketFd() const64 int TcpServerSendContext::GetSocketFd() const
65 {
66 return manager_->GetData() ? static_cast<int>(reinterpret_cast<uint64_t>(manager_->GetData())) : -1;
67 }
68
CheckParamsType(napi_value * params,size_t paramsCount)69 bool TcpServerSendContext::CheckParamsType(napi_value *params, size_t paramsCount)
70 {
71 if (paramsCount == PARAM_JUST_OPTIONS) {
72 if (NapiUtils::GetValueType(GetEnv(), params[0]) != napi_object) {
73 NETSTACK_LOGE("invalid parameter");
74 SetNeedThrowException(true);
75 SetError(PARSE_ERROR_CODE, PARSE_ERROR_MSG);
76 return false;
77 }
78 return true;
79 }
80
81 if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
82 if (NapiUtils::GetValueType(GetEnv(), params[0]) != napi_object ||
83 NapiUtils::GetValueType(GetEnv(), params[1]) != napi_function) {
84 NETSTACK_LOGE("invalid parameter");
85 SetNeedThrowException(true);
86 SetError(PARSE_ERROR_CODE, PARSE_ERROR_MSG);
87 return false;
88 }
89 return true;
90 }
91 return false;
92 }
93
GetData(napi_value udpSendOptions)94 bool TcpServerSendContext::GetData(napi_value udpSendOptions)
95 {
96 napi_value jsData = NapiUtils::GetNamedProperty(GetEnv(), udpSendOptions, KEY_DATA);
97 if (NapiUtils::GetValueType(GetEnv(), jsData) == napi_string) {
98 std::string data = NapiUtils::GetStringFromValueUtf8(GetEnv(), jsData);
99 if (data.empty()) {
100 NETSTACK_LOGE("string data is empty");
101 return true;
102 }
103 options.SetData(data);
104 return true;
105 }
106
107 if (NapiUtils::ValueIsArrayBuffer(GetEnv(), jsData)) {
108 size_t length = 0;
109 void *data = NapiUtils::GetInfoFromArrayBufferValue(GetEnv(), jsData, &length);
110 if (data == nullptr) {
111 NETSTACK_LOGE("arraybuffer data is empty");
112 return true;
113 }
114 options.SetData(data, length);
115 return true;
116 }
117 return false;
118 }
119
GetErrorCode() const120 int32_t TcpServerSendContext::GetErrorCode() const
121 {
122 if (BaseContext::IsPermissionDenied()) {
123 return PERMISSION_DENIED_CODE;
124 }
125
126 auto err = BaseContext::GetErrorCode();
127 if (err == PARSE_ERROR_CODE) {
128 return PARSE_ERROR_CODE;
129 }
130 return err + SOCKET_SERVER_ERROR_CODE_BASE;
131 }
132
GetErrorMessage() const133 std::string TcpServerSendContext::GetErrorMessage() const
134 {
135 if (BaseContext::IsPermissionDenied()) {
136 return PERMISSION_DENIED_MSG;
137 }
138
139 auto errCode = BaseContext::GetErrorCode();
140 if (errCode == PARSE_ERROR_CODE) {
141 return PARSE_ERROR_MSG;
142 }
143 char err[MAX_ERR_NUM] = {0};
144 (void)strerror_r(errCode, err, MAX_ERR_NUM);
145 return err;
146 }
147 } // namespace OHOS::NetStack::Socket
148