• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #include <cinttypes>
19 
20 namespace OHOS {
21 namespace Msdp {
22 namespace DeviceStatus {
23 namespace {
24 constexpr OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "StreamSocket" };
25 } // namespace
26 
StreamSocket()27 StreamSocket::StreamSocket() {}
28 
~StreamSocket()29 StreamSocket::~StreamSocket()
30 {
31     Close();
32     EpollClose();
33 }
34 
EpollCreate()35 int32_t StreamSocket::EpollCreate()
36 {
37     epollFd_ = ::epoll_create1(EPOLL_CLOEXEC);
38     if (epollFd_ < 0) {
39         FI_HILOGE("epoll_create1 failed:%{public}s", ::strerror(errno));
40         return RET_ERR;
41     }
42     return RET_OK;
43 }
44 
EpollCtl(int32_t fd,int32_t op,struct epoll_event & event)45 int32_t StreamSocket::EpollCtl(int32_t fd, int32_t op, struct epoll_event &event)
46 {
47     if (fd < 0) {
48         FI_HILOGE("Invalid fd:%{public}d", fd);
49         return RET_ERR;
50     }
51     if (epollFd_ < 0) {
52         FI_HILOGE("Invalid epollFd:%{public}d", epollFd_);
53         return RET_ERR;
54     }
55     if (::epoll_ctl(epollFd_, op, fd, &event) != 0) {
56         FI_HILOGE("epoll_ctl(%{public}d,%{public}d,%{public}d) failed:%{public}s", epollFd_, op, fd, ::strerror(errno));
57         return RET_ERR;
58     }
59     return RET_OK;
60 }
61 
EpollWait(int32_t maxevents,int32_t timeout,struct epoll_event & events)62 int32_t StreamSocket::EpollWait(int32_t maxevents, int32_t timeout, struct epoll_event &events)
63 {
64     if (epollFd_ < 0) {
65         FI_HILOGE("Invalid epollFd:%{public}d", epollFd_);
66         return RET_ERR;
67     }
68     return epoll_wait(epollFd_, &events, maxevents, timeout);
69 }
70 
OnReadPackets(CircleStreamBuffer & circBuf,StreamSocket::PacketCallBackFun callbackFun)71 void StreamSocket::OnReadPackets(CircleStreamBuffer &circBuf, StreamSocket::PacketCallBackFun callbackFun)
72 {
73     constexpr int32_t headSize = static_cast<int32_t>(sizeof(PackHead));
74     for (int32_t i = 0; i < ONCE_PROCESS_NETPACKET_LIMIT; i++) {
75         const int32_t residualSize = circBuf.ResidualSize();
76         if (residualSize < headSize) {
77             break;
78         }
79         int32_t dataSize = residualSize - headSize;
80         char *buf = const_cast<char *>(circBuf.ReadBuf());
81         CHKPB(buf);
82         PackHead *head = reinterpret_cast<PackHead *>(buf);
83         CHKPB(head);
84         if ((static_cast<int32_t>(head->size) < 0) || (static_cast<size_t>(head->size) > MAX_PACKET_BUF_SIZE)) {
85             FI_HILOGE("Packet header parsing error, and this error cannot be recovered, the buffer will be reset, "
86                 "head->size:%{public}d, residualSize:%{public}d", head->size, residualSize);
87             circBuf.Reset();
88             break;
89         }
90         if (head->size > dataSize) {
91             break;
92         }
93         NetPacket pkt(head->idMsg);
94         if ((head->size > 0) && (!pkt.Write(&buf[headSize], head->size))) {
95             FI_HILOGW("Error writing data in the NetPacket, it will be retried next time, messageid:%{public}d, "
96                 "size:%{public}d", head->idMsg, head->size);
97             break;
98         }
99         if (!circBuf.SeekReadPos(pkt.GetPacketLength())) {
100             FI_HILOGW("Set read position error, and this error cannot be recovered, and the buffer will be reset, "
101                 "packetSize:%{public}d, residualSize:%{public}d", pkt.GetPacketLength(), residualSize);
102             circBuf.Reset();
103             break;
104         }
105         callbackFun(pkt);
106         if (circBuf.empty()) {
107             circBuf.Reset();
108             break;
109         }
110     }
111 }
112 
EpollClose()113 void StreamSocket::EpollClose()
114 {
115     if (epollFd_ >= 0) {
116         if (close(epollFd_) < 0) {
117             FI_HILOGE("Close epoll fd failed, error:%{public}s, epollFd_:%{public}d", strerror(errno), epollFd_);
118         }
119         epollFd_ = -1;
120     }
121 }
122 
Close()123 void StreamSocket::Close()
124 {
125     if (fd_ >= 0) {
126         int32_t rf = close(fd_);
127         if (rf < 0) {
128             FI_HILOGE("Socket close failed rf:%{public}d", rf);
129         }
130     }
131     fd_ = -1;
132 }
133 } // namespace DeviceStatus
134 } // namespace Msdp
135 } // namespace OHOS