1 /*
2 * Copyright (c) 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 "common/log_wrapper.h"
17 #include "define.h"
18 #include "network.h"
19
20 namespace OHOS::ArkCompiler::Toolchain {
Recv(int32_t client, std::string& buffer, int32_t flags)21 bool Recv(int32_t client, std::string& buffer, int32_t flags)
22 {
23 if (buffer.empty()) {
24 return false;
25 }
26 auto succeeded = Recv(client, buffer.data(), buffer.size(), flags);
27 if (!succeeded) {
28 buffer.clear();
29 }
30 return succeeded;
31 }
32
Recv(int32_t client, char* buf, size_t totalLen, int32_t flags)33 bool Recv(int32_t client, char* buf, size_t totalLen, int32_t flags)
34 {
35 size_t recvLen = 0;
36 while (recvLen < totalLen) {
37 ssize_t len = 0;
38 while ((len = recv(client, buf + recvLen, totalLen - recvLen, flags)) < 0 &&
39 (errno == EINTR || errno == EAGAIN)) {
40 LOGW("Recv payload failed, errno = %{public}d", errno);
41 }
42 if (len <= 0) {
43 LOGE("Recv payload in while failed, len = %{public}ld, errno = %{public}d", static_cast<long>(len), errno);
44 return false;
45 }
46 recvLen += static_cast<size_t>(len);
47 }
48 return true;
49 }
50
Recv(int32_t client, uint8_t* buf, size_t totalLen, int32_t flags)51 bool Recv(int32_t client, uint8_t* buf, size_t totalLen, int32_t flags)
52 {
53 return Recv(client, reinterpret_cast<char *>(buf), totalLen, flags);
54 }
55
Send(int32_t client, const std::string& message, int32_t flags)56 bool Send(int32_t client, const std::string& message, int32_t flags)
57 {
58 return Send(client, message.c_str(), message.size(), flags);
59 }
60
Send(int32_t client, const char* buf, size_t totalLen, int32_t flags)61 bool Send(int32_t client, const char* buf, size_t totalLen, int32_t flags)
62 {
63 size_t sendLen = 0;
64 while (sendLen < totalLen) {
65 ssize_t len = send(client, buf + sendLen, totalLen - sendLen, flags);
66 if (len <= 0) {
67 LOGE("Send Message in while failed, len = %{public}ld, errno = %{public}d", static_cast<long>(len), errno);
68 return false;
69 }
70 sendLen += static_cast<size_t>(len);
71 }
72 return true;
73 }
74
NetToHostLongLong(uint8_t* buf, uint32_t len)75 uint64_t NetToHostLongLong(uint8_t* buf, uint32_t len)
76 {
77 uint64_t result = 0;
78 for (uint32_t i = 0; i < len; i++) {
79 result |= buf[i];
80 if ((i + 1) < len) {
81 result <<= 8; // 8: result need shift left 8 bits in order to big endian convert to int
82 }
83 }
84 return result;
85 }
86 } // OHOS::ArkCompiler::Toolchain
87