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 "stream_socket.h"
17
18 namespace OHOS {
19 namespace Sensors {
20 #ifndef OHOS_BUILD_ENABLE_RUST
21 #undef LOG_TAG
22 #define LOG_TAG "StreamSocket"
23 #endif // OHOS_BUILD_ENABLE_RUST
24
StreamSocket()25 StreamSocket::StreamSocket() {}
26
~StreamSocket()27 StreamSocket::~StreamSocket()
28 {
29 #ifdef OHOS_BUILD_ENABLE_RUST
30 StreamSocketClose(streamSocketPtr_.get());
31 #else
32 Close();
33 #endif // OHOS_BUILD_ENABLE_RUST
34 }
35
36 #ifndef OHOS_BUILD_ENABLE_RUST
OnReadPackets(CircleStreamBuffer & circBuf,StreamSocket::PacketCallBackFun callbackFun)37 void StreamSocket::OnReadPackets(CircleStreamBuffer &circBuf, StreamSocket::PacketCallBackFun callbackFun)
38 {
39 constexpr size_t headSize = sizeof(PackHead);
40 for (size_t i = 0; i < ONCE_PROCESS_NETPACKET_LIMIT; ++i) {
41 const size_t unreadSize = circBuf.UnreadSize();
42 if (unreadSize < headSize) {
43 break;
44 }
45 size_t dataSize = unreadSize - headSize;
46 char *buf = const_cast<char *>(circBuf.ReadBuf());
47 CHKPB(buf);
48 PackHead *head = reinterpret_cast<PackHead *>(buf);
49 CHKPB(head);
50 if (head->size < 0 || head->size > MAX_PACKET_BUF_SIZE) {
51 SEN_HILOGE("Packet header parsing error, and this error cannot be recovered. The buffer will be reset"
52 " head->size:%{public}zu, unreadSize:%{public}zu", head->size, unreadSize);
53 circBuf.Reset();
54 break;
55 }
56 if (head->size > dataSize) {
57 break;
58 }
59 NetPacket pkt(head->idMsg);
60 if ((head->size > 0) && (!pkt.Write(&buf[headSize], head->size))) {
61 SEN_HILOGW("Error writing data in the NetPacket. It will be retried next time. messageid:%{public}d,"
62 "size:%{public}zu", head->idMsg, head->size);
63 break;
64 }
65 if (!circBuf.SeekReadPos(pkt.GetPacketLength())) {
66 SEN_HILOGW("Set read position error, and this error cannot be recovered, and the buffer will be reset"
67 " packetSize:%{public}zu, unreadSize:%{public}zu", pkt.GetPacketLength(), unreadSize);
68 circBuf.Reset();
69 break;
70 }
71 callbackFun(pkt);
72 if (circBuf.IsEmpty()) {
73 circBuf.Reset();
74 break;
75 }
76 }
77 }
78 #endif // OHOS_BUILD_ENABLE_RUST
79
Close()80 void StreamSocket::Close()
81 {
82 #ifdef OHOS_BUILD_ENABLE_RUST
83 StreamSocketClose(streamSocketPtr_.get());
84 #else
85 if (fd_ >= 0) {
86 auto rf = fdsan_close_with_tag(fd_, TAG);
87 if (rf != 0) {
88 SEN_HILOGE("Socket close failed, rf:%{public}d", rf);
89 }
90 }
91 fd_ = -1;
92 #endif // OHOS_BUILD_ENABLE_RUST
93 }
94
GetFd() const95 int32_t StreamSocket::GetFd() const
96 {
97 #ifdef OHOS_BUILD_ENABLE_RUST
98 return StreamSocketGetFd(streamSocketPtr_.get());
99 #else
100 return fd_;
101 #endif // OHOS_BUILD_ENABLE_RUST
102 }
103 } // namespace Sensors
104 } // namespace OHOS