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 "local_socket_server_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 {
GetSocketFd() const25 int LocalSocketServerBaseContext::GetSocketFd() const
26 {
27 if (manager_ == nullptr) {
28 return -1;
29 }
30 LocalSocketServerManager *pManagerInfo = reinterpret_cast<LocalSocketServerManager *>(manager_->GetData());
31 return (pManagerInfo != nullptr) ? pManagerInfo->sockfd_ : -1;
32 }
33
SetSocketFd(int sock)34 void LocalSocketServerBaseContext::SetSocketFd(int sock)
35 {
36 if (manager_ == nullptr) {
37 return;
38 }
39 LocalSocketServerManager *pManagerInfo = reinterpret_cast<LocalSocketServerManager *>(manager_->GetData());
40 if (pManagerInfo != nullptr) {
41 pManagerInfo->sockfd_ = sock;
42 }
43 }
44
ParseParams(napi_value * params,size_t paramsCount)45 void LocalSocketServerListenContext::ParseParams(napi_value *params, size_t paramsCount)
46 {
47 if (!CheckParamsWithOptions(params, paramsCount)) {
48 return;
49 }
50 if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_ADDRESS)) {
51 socketPath_ = NapiUtils::GetStringPropertyUtf8(GetEnv(), params[0], KEY_ADDRESS);
52 }
53 if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
54 SetParseOK(SetCallback(params[PARAM_OPTIONS_AND_CALLBACK - 1]) == napi_ok);
55 return;
56 }
57 SetParseOK(true);
58 }
59
GetSocketPath() const60 const std::string &LocalSocketServerListenContext::GetSocketPath() const
61 {
62 return socketPath_;
63 }
64
ParseParams(napi_value * params,size_t paramsCount)65 void LocalSocketServerGetStateContext::ParseParams(napi_value *params, size_t paramsCount)
66 {
67 if (!CheckParamsWithoutOptions(params, paramsCount)) {
68 return;
69 }
70 if (paramsCount != PARAM_NONE) {
71 SetParseOK(SetCallback(params[0]) == napi_ok);
72 return;
73 }
74 SetParseOK(true);
75 }
76
GetStateRef()77 SocketStateBase &LocalSocketServerGetStateContext::GetStateRef()
78 {
79 return state_;
80 }
81
ParseParams(napi_value * params,size_t paramsCount)82 void LocalSocketServerSetExtraOptionsContext::ParseParams(napi_value *params, size_t paramsCount)
83 {
84 if (!CheckParamsWithOptions(params, paramsCount)) {
85 return;
86 }
87 if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_RECEIVE_BUFFER_SIZE)) {
88 options_.SetReceiveBufferSize(NapiUtils::GetUint32Property(GetEnv(), params[0], KEY_RECEIVE_BUFFER_SIZE));
89 options_.SetRecvBufSizeFlag(true);
90 }
91
92 if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_SEND_BUFFER_SIZE)) {
93 options_.SetSendBufferSize(NapiUtils::GetUint32Property(GetEnv(), params[0], KEY_SEND_BUFFER_SIZE));
94 options_.SetSendBufSizeFlag(true);
95 }
96
97 if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_SOCKET_TIMEOUT)) {
98 options_.SetSocketTimeout(NapiUtils::GetUint32Property(GetEnv(), params[0], KEY_SOCKET_TIMEOUT));
99 options_.SetTimeoutFlag(true);
100 }
101
102 if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
103 SetParseOK(SetCallback(params[PARAM_OPTIONS_AND_CALLBACK - 1]) == napi_ok);
104 return;
105 }
106 SetParseOK(true);
107 }
108
GetOptionsRef()109 LocalExtraOptions &LocalSocketServerSetExtraOptionsContext::GetOptionsRef()
110 {
111 return options_;
112 }
113
ParseParams(napi_value * params,size_t paramsCount)114 void LocalSocketServerGetExtraOptionsContext::ParseParams(napi_value *params, size_t paramsCount)
115 {
116 if (!CheckParamsWithoutOptions(params, paramsCount)) {
117 return;
118 }
119 if (paramsCount != PARAM_NONE) {
120 SetParseOK(SetCallback(params[0]) == napi_ok);
121 return;
122 }
123 SetParseOK(true);
124 }
125
GetOptionsRef()126 LocalExtraOptions &LocalSocketServerGetExtraOptionsContext::GetOptionsRef()
127 {
128 return options_;
129 }
130
GetData(napi_value sendOptions)131 bool LocalSocketServerSendContext::GetData(napi_value sendOptions)
132 {
133 if (NapiUtils::HasNamedProperty(GetEnv(), sendOptions, KEY_TIMEOUT)) {
134 return false;
135 }
136 napi_value jsData = NapiUtils::GetNamedProperty(GetEnv(), sendOptions, KEY_DATA);
137 if (NapiUtils::GetValueType(GetEnv(), jsData) == napi_string) {
138 std::string data = NapiUtils::GetStringFromValueUtf8(GetEnv(), jsData);
139 if (data.empty()) {
140 NETSTACK_LOGE("string data is empty");
141 return false;
142 }
143 options_.SetBuffer(data);
144 return true;
145 }
146
147 if (NapiUtils::ValueIsArrayBuffer(GetEnv(), jsData)) {
148 size_t length = 0;
149 void *data = NapiUtils::GetInfoFromArrayBufferValue(GetEnv(), jsData, &length);
150 if (data == nullptr || length == 0) {
151 NETSTACK_LOGE("arraybuffer data is empty");
152 return false;
153 }
154 options_.SetBuffer(data, length);
155 return true;
156 }
157 return false;
158 }
159
ParseParams(napi_value * params,size_t paramsCount)160 void LocalSocketServerSendContext::ParseParams(napi_value *params, size_t paramsCount)
161 {
162 if (!CheckParamsWithOptions(params, paramsCount)) {
163 return;
164 }
165 if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_ENCODING)) {
166 std::string encoding = NapiUtils::GetStringPropertyUtf8(GetEnv(), params[0], KEY_ENCODING);
167 options_.SetEncoding(encoding);
168 }
169 if (!GetData(params[0])) {
170 return;
171 }
172 if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
173 SetParseOK(SetCallback(params[1]) == napi_ok);
174 return;
175 }
176 SetParseOK(true);
177 }
178
GetAcceptFd()179 int LocalSocketServerSendContext::GetAcceptFd()
180 {
181 if (manager_ == nullptr) {
182 return -1;
183 }
184 LocalSocketServerManager *pManagerInfo = reinterpret_cast<LocalSocketServerManager *>(manager_->GetData());
185 return (pManagerInfo != nullptr) ? pManagerInfo->GetAcceptFd(clientId_) : -1;
186 }
187
GetOptionsRef()188 LocalSocketOptions &LocalSocketServerSendContext::GetOptionsRef()
189 {
190 return options_;
191 }
192
GetClientId() const193 int LocalSocketServerSendContext::GetClientId() const
194 {
195 return clientId_;
196 }
197
SetClientId(int clientId)198 void LocalSocketServerSendContext::SetClientId(int clientId)
199 {
200 clientId_ = clientId;
201 }
202
ParseParams(napi_value * params,size_t paramsCount)203 void LocalSocketServerCloseContext::ParseParams(napi_value *params, size_t paramsCount)
204 {
205 if (!CheckParamsWithoutOptions(params, paramsCount)) {
206 return;
207 }
208 if (paramsCount != PARAM_NONE) {
209 SetParseOK(SetCallback(params[0]) == napi_ok);
210 return;
211 }
212 SetParseOK(true);
213 }
214
GetClientId() const215 int LocalSocketServerCloseContext::GetClientId() const
216 {
217 return clientId_;
218 }
219
SetClientId(int clientId)220 void LocalSocketServerCloseContext::SetClientId(int clientId)
221 {
222 clientId_ = clientId;
223 }
224 } // namespace OHOS::NetStack::Socket