• 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_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     if (manager_->GetData() == nullptr) {
50         return -1;
51     }
52     return (int)(uint64_t)manager_->GetData();
53 }
54 
CheckParamsType(napi_value * params,size_t paramsCount)55 bool TcpServerCommonContext::CheckParamsType(napi_value *params, size_t paramsCount)
56 {
57     if (paramsCount == PARAM_NONE) {
58         return true;
59     }
60 
61     if (paramsCount == PARAM_JUST_CALLBACK) {
62         if (NapiUtils::GetValueType(GetEnv(), params[0]) != napi_function) {
63             NETSTACK_LOGE("invalid parameter");
64             SetNeedThrowException(true);
65             SetError(PARSE_ERROR_CODE, PARSE_ERROR_MSG);
66             return false;
67         }
68         return true;
69     }
70     return false;
71 }
72 
GetErrorCode() const73 int32_t TcpServerCommonContext::GetErrorCode() const
74 {
75     if (BaseContext::IsPermissionDenied()) {
76         return PERMISSION_DENIED_CODE;
77     }
78 
79     auto err = BaseContext::GetErrorCode();
80     if (err == PARSE_ERROR_CODE) {
81         return PARSE_ERROR_CODE;
82     }
83     return err + SOCKET_ERROR_CODE_BASE;
84 }
85 
GetErrorMessage() const86 std::string TcpServerCommonContext::GetErrorMessage() const
87 {
88     if (BaseContext::IsPermissionDenied()) {
89         return PERMISSION_DENIED_MSG;
90     }
91 
92     auto errCode = BaseContext::GetErrorCode();
93     if (errCode == PARSE_ERROR_CODE) {
94         return PARSE_ERROR_MSG;
95     }
96     char err[MAX_ERR_NUM] = {0};
97     (void)strerror_r(errCode, err, MAX_ERR_NUM);
98     return err;
99 }
100 } // namespace OHOS::NetStack::Socket
101