• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "udp_extra_context.h"
17 
18 #include "context_key.h"
19 #include "socket_constant.h"
20 #include "event_manager.h"
21 #include "napi_utils.h"
22 #include "netstack_log.h"
23 
24 namespace OHOS::NetStack::Socket {
UdpSetExtraOptionsContext(napi_env env,EventManager * manager)25 UdpSetExtraOptionsContext::UdpSetExtraOptionsContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {}
26 
ParseParams(napi_value * params,size_t paramsCount)27 void UdpSetExtraOptionsContext::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_RECEIVE_BUFFER_SIZE)) {
47         options.SetReceiveBufferSize(NapiUtils::GetUint32Property(GetEnv(), params[0], KEY_RECEIVE_BUFFER_SIZE));
48     }
49 
50     if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_SEND_BUFFER_SIZE)) {
51         options.SetSendBufferSize(NapiUtils::GetUint32Property(GetEnv(), params[0], KEY_SEND_BUFFER_SIZE));
52     }
53 
54     if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_REUSE_ADDRESS)) {
55         options.SetReuseAddress(NapiUtils::GetBooleanProperty(GetEnv(), params[0], KEY_REUSE_ADDRESS));
56     }
57 
58     if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_SOCKET_TIMEOUT)) {
59         options.SetSocketTimeout(NapiUtils::GetUint32Property(GetEnv(), params[0], KEY_SOCKET_TIMEOUT));
60     }
61 
62     if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_BROADCAST)) {
63         options.SetBroadcast(NapiUtils::GetUint32Property(GetEnv(), params[0], KEY_BROADCAST));
64     }
65 
66     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
67         SetParseOK(SetCallback(params[1]) == napi_ok);
68         return;
69     }
70     SetParseOK(true);
71 }
72 
GetSocketFd() const73 int UdpSetExtraOptionsContext::GetSocketFd() const
74 {
75     return (int)(uint64_t)manager_->GetData();
76 }
77 
CheckParamsType(napi_value * params,size_t paramsCount)78 bool UdpSetExtraOptionsContext::CheckParamsType(napi_value *params, size_t paramsCount)
79 {
80     if (paramsCount == PARAM_JUST_OPTIONS) {
81         return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object;
82     }
83 
84     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
85         return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object &&
86                NapiUtils::GetValueType(GetEnv(), params[1]) == napi_function;
87     }
88     return false;
89 }
90 
GetErrorCode() const91 int32_t UdpSetExtraOptionsContext::GetErrorCode() const
92 {
93     if (BaseContext::IsPermissionDenied()) {
94         return PERMISSION_DENIED_CODE;
95     }
96 
97     auto err = BaseContext::GetErrorCode();
98     if (err == PARSE_ERROR_CODE) {
99         return PARSE_ERROR_CODE;
100     }
101 #if defined(IOS_PLATFORM)
102     err = ErrCodePlatformAdapter::GetOHOSErrCode(err);
103 #endif
104     return err + SOCKET_ERROR_CODE_BASE;
105 }
106 
GetErrorMessage() const107 std::string UdpSetExtraOptionsContext::GetErrorMessage() const
108 {
109     if (BaseContext::IsPermissionDenied()) {
110         return PERMISSION_DENIED_MSG;
111     }
112 
113     auto errCode = BaseContext::GetErrorCode();
114     if (errCode == PARSE_ERROR_CODE) {
115         return PARSE_ERROR_MSG;
116     }
117 #if defined(IOS_PLATFORM)
118     std::string errMessage;
119     ErrCodePlatformAdapter::GetOHOSErrMessage(errCode, errMessage);
120     return errMessage;
121 #else
122     char err[MAX_ERR_NUM] = {0};
123     (void)strerror_r(errCode, err, MAX_ERR_NUM);
124     return err;
125 #endif
126 }
127 } // namespace OHOS::NetStack::Socket
128