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