• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "websocket/define.h"
18 #include "websocket/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 && errno == EINTR) {
39             LOGW("Recv payload failed, errno == EINTR");
40         }
41         if (len <= 0) {
42             LOGE("Recv payload in while failed, len = %{public}ld, errno = %{public}d", static_cast<long>(len), errno);
43             return false;
44         }
45         recvLen += static_cast<size_t>(len);
46     }
47     return true;
48 }
49 
Recv(int32_t client,uint8_t * buf,size_t totalLen,int32_t flags)50 bool Recv(int32_t client, uint8_t* buf, size_t totalLen, int32_t flags)
51 {
52     return Recv(client, reinterpret_cast<char *>(buf), totalLen, flags);
53 }
54 
Send(int32_t client,const std::string & message,int32_t flags)55 bool Send(int32_t client, const std::string& message, int32_t flags)
56 {
57     return Send(client, message.c_str(), message.size(), flags);
58 }
59 
Send(int32_t client,const char * buf,size_t totalLen,int32_t flags)60 bool Send(int32_t client, const char* buf, size_t totalLen, int32_t flags)
61 {
62     size_t sendLen = 0;
63     while (sendLen < totalLen) {
64         ssize_t len = send(client, buf + sendLen, totalLen - sendLen, flags);
65         if (len <= 0) {
66             LOGE("Send Message in while failed, len = %{public}ld, errno = %{public}d", static_cast<long>(len), errno);
67             return false;
68         }
69         sendLen += static_cast<size_t>(len);
70     }
71     return true;
72 }
73 
NetToHostLongLong(uint8_t * buf,uint32_t len)74 uint64_t NetToHostLongLong(uint8_t* buf, uint32_t len)
75 {
76     uint64_t result = 0;
77     for (uint32_t i = 0; i < len; i++) {
78         result |= buf[i];
79         if ((i + 1) < len) {
80             result <<= 8; // 8: result need shift left 8 bits in order to big endian convert to int
81         }
82     }
83     return result;
84 }
85 } // OHOS::ArkCompiler::Toolchain
86