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_common_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 {
TcpServerCommonContext(napi_env env,const std::shared_ptr<EventManager> & manager)25 TcpServerCommonContext::TcpServerCommonContext(napi_env env, const std::shared_ptr<EventManager> &manager)
26 : BaseContext(env, manager) {}
27
ParseParams(napi_value * params,size_t paramsCount)28 void TcpServerCommonContext::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 return;
39 }
40
41 if (paramsCount != PARAM_NONE) {
42 SetParseOK(SetCallback(params[0]) == napi_ok);
43 return;
44 }
45 SetParseOK(true);
46 }
47
GetSocketFd() const48 int TcpServerCommonContext::GetSocketFd() const
49 {
50 return sharedManager_->GetData() ? static_cast<int>(reinterpret_cast<uint64_t>(sharedManager_->GetData())) : -1;
51 }
52
TcpServerCloseContext(napi_env env,const std::shared_ptr<EventManager> & manager)53 TcpServerCloseContext::TcpServerCloseContext(napi_env env, const std::shared_ptr<EventManager> &manager)
54 :TcpServerCommonContext(env, manager) {}
55
SetSocketFd(int sock)56 void TcpServerCloseContext::SetSocketFd(int sock)
57 {
58 sharedManager_->SetData(reinterpret_cast<void *>(sock));
59 }
60
CheckParamsType(napi_value * params,size_t paramsCount)61 bool TcpServerCommonContext::CheckParamsType(napi_value *params, size_t paramsCount)
62 {
63 if (paramsCount == PARAM_NONE) {
64 return true;
65 }
66
67 if (paramsCount == PARAM_JUST_CALLBACK) {
68 if (NapiUtils::GetValueType(GetEnv(), params[0]) != napi_function) {
69 NETSTACK_LOGE("invalid parameter");
70 SetNeedThrowException(true);
71 SetError(PARSE_ERROR_CODE, PARSE_ERROR_MSG);
72 return false;
73 }
74 return true;
75 }
76 return false;
77 }
78
GetErrorCode() const79 int32_t TcpServerCommonContext::GetErrorCode() const
80 {
81 if (BaseContext::IsPermissionDenied()) {
82 return PERMISSION_DENIED_CODE;
83 }
84
85 auto err = BaseContext::GetErrorCode();
86 if (err == PARSE_ERROR_CODE) {
87 return PARSE_ERROR_CODE;
88 }
89 return err + SOCKET_ERROR_CODE_BASE;
90 }
91
GetErrorMessage() const92 std::string TcpServerCommonContext::GetErrorMessage() const
93 {
94 if (BaseContext::IsPermissionDenied()) {
95 return PERMISSION_DENIED_MSG;
96 }
97
98 auto errCode = BaseContext::GetErrorCode();
99 if (errCode == PARSE_ERROR_CODE) {
100 return PARSE_ERROR_MSG;
101 }
102 char err[MAX_ERR_NUM] = {0};
103 (void)strerror_r(errCode, err, MAX_ERR_NUM);
104 return err;
105 }
106 } // namespace OHOS::NetStack::Socket
107