• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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         return;
52     }
53 
54     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
55         SetParseOK(SetCallback(params[1]) == napi_ok);
56         return;
57     }
58     SetParseOK(true);
59 }
60 
GetSocketFd() const61 int TcpServerSendContext::GetSocketFd() const
62 {
63     return (int)(uint64_t)manager_->GetData();
64 }
65 
CheckParamsType(napi_value * params,size_t paramsCount)66 bool TcpServerSendContext::CheckParamsType(napi_value *params, size_t paramsCount)
67 {
68     if (paramsCount == PARAM_JUST_OPTIONS) {
69         if (NapiUtils::GetValueType(GetEnv(), params[0]) != napi_object) {
70             NETSTACK_LOGE("invalid parameter");
71             SetNeedThrowException(true);
72             SetError(PARSE_ERROR_CODE, PARSE_ERROR_MSG);
73             return false;
74         }
75         return true;
76     }
77 
78     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
79         if (NapiUtils::GetValueType(GetEnv(), params[0]) != napi_object ||
80             NapiUtils::GetValueType(GetEnv(), params[1]) != napi_function) {
81             NETSTACK_LOGE("invalid parameter");
82             SetNeedThrowException(true);
83             SetError(PARSE_ERROR_CODE, PARSE_ERROR_MSG);
84             return false;
85         }
86         return true;
87     }
88     return false;
89 }
90 
GetData(napi_value udpSendOptions)91 bool TcpServerSendContext::GetData(napi_value udpSendOptions)
92 {
93     napi_value jsData = NapiUtils::GetNamedProperty(GetEnv(), udpSendOptions, KEY_DATA);
94     if (NapiUtils::GetValueType(GetEnv(), jsData) == napi_string) {
95         std::string data = NapiUtils::GetStringFromValueUtf8(GetEnv(), jsData);
96         if (data.empty()) {
97             NETSTACK_LOGE("string data is empty");
98             return false;
99         }
100         options.SetData(data);
101         return true;
102     }
103 
104     if (NapiUtils::ValueIsArrayBuffer(GetEnv(), jsData)) {
105         size_t length = 0;
106         void *data = NapiUtils::GetInfoFromArrayBufferValue(GetEnv(), jsData, &length);
107         if (data == nullptr) {
108             NETSTACK_LOGE("arraybuffer data is empty");
109             return false;
110         }
111         options.SetData(data, length);
112         return true;
113     }
114     return false;
115 }
116 
GetErrorCode() const117 int32_t TcpServerSendContext::GetErrorCode() const
118 {
119     if (BaseContext::IsPermissionDenied()) {
120         return PERMISSION_DENIED_CODE;
121     }
122 
123     auto err = BaseContext::GetErrorCode();
124     if (err == PARSE_ERROR_CODE) {
125         return PARSE_ERROR_CODE;
126     }
127     return err + SOCKET_SERVER_ERROR_CODE_BASE;
128 }
129 
GetErrorMessage() const130 std::string TcpServerSendContext::GetErrorMessage() const
131 {
132     if (BaseContext::IsPermissionDenied()) {
133         return PERMISSION_DENIED_MSG;
134     }
135 
136     auto errCode = BaseContext::GetErrorCode();
137     if (errCode == PARSE_ERROR_CODE) {
138         return PARSE_ERROR_MSG;
139     }
140     char err[MAX_ERR_NUM] = {0};
141     (void)strerror_r(errCode, err, MAX_ERR_NUM);
142     return err;
143 }
144 } // namespace OHOS::NetStack::Socket
145