1f857971dSopenharmony_ci/*
2f857971dSopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3f857971dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f857971dSopenharmony_ci * you may not use this file except in compliance with the License.
5f857971dSopenharmony_ci * You may obtain a copy of the License at
6f857971dSopenharmony_ci *
7f857971dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f857971dSopenharmony_ci *
9f857971dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f857971dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f857971dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f857971dSopenharmony_ci * See the License for the specific language governing permissions and
13f857971dSopenharmony_ci * limitations under the License.
14f857971dSopenharmony_ci */
15f857971dSopenharmony_ci
16f857971dSopenharmony_ci#include "stream_socket.h"
17f857971dSopenharmony_ci
18f857971dSopenharmony_ci#include <cinttypes>
19f857971dSopenharmony_ci
20f857971dSopenharmony_ci#undef LOG_TAG
21f857971dSopenharmony_ci#define LOG_TAG "StreamSocket"
22f857971dSopenharmony_ci
23f857971dSopenharmony_cinamespace OHOS {
24f857971dSopenharmony_cinamespace Msdp {
25f857971dSopenharmony_cinamespace DeviceStatus {
26f857971dSopenharmony_ci
27f857971dSopenharmony_ciStreamSocket::StreamSocket() {}
28f857971dSopenharmony_ci
29f857971dSopenharmony_ciStreamSocket::~StreamSocket()
30f857971dSopenharmony_ci{
31f857971dSopenharmony_ci    Close();
32f857971dSopenharmony_ci    EpollClose();
33f857971dSopenharmony_ci}
34f857971dSopenharmony_ci
35f857971dSopenharmony_ciint32_t StreamSocket::EpollCreate()
36f857971dSopenharmony_ci{
37f857971dSopenharmony_ci    CALL_INFO_TRACE;
38f857971dSopenharmony_ci    epollFd_ = ::epoll_create1(EPOLL_CLOEXEC);
39f857971dSopenharmony_ci    if (epollFd_ < 0) {
40f857971dSopenharmony_ci        FI_HILOGE("epoll_create1 failed:%{public}s", ::strerror(errno));
41f857971dSopenharmony_ci        return RET_ERR;
42f857971dSopenharmony_ci    }
43f857971dSopenharmony_ci    return RET_OK;
44f857971dSopenharmony_ci}
45f857971dSopenharmony_ci
46f857971dSopenharmony_ciint32_t StreamSocket::EpollCtl(int32_t fd, int32_t op, struct epoll_event &event)
47f857971dSopenharmony_ci{
48f857971dSopenharmony_ci    if (fd < 0) {
49f857971dSopenharmony_ci        FI_HILOGE("Invalid fd:%{public}d", fd);
50f857971dSopenharmony_ci        return RET_ERR;
51f857971dSopenharmony_ci    }
52f857971dSopenharmony_ci    if (epollFd_ < 0) {
53f857971dSopenharmony_ci        FI_HILOGE("Invalid epollFd:%{public}d", epollFd_);
54f857971dSopenharmony_ci        return RET_ERR;
55f857971dSopenharmony_ci    }
56f857971dSopenharmony_ci    if (::epoll_ctl(epollFd_, op, fd, &event) != 0) {
57f857971dSopenharmony_ci        FI_HILOGE("epoll_ctl(%{public}d,%{public}d,%{public}d) failed:%{public}s", epollFd_, op, fd, ::strerror(errno));
58f857971dSopenharmony_ci        return RET_ERR;
59f857971dSopenharmony_ci    }
60f857971dSopenharmony_ci    return RET_OK;
61f857971dSopenharmony_ci}
62f857971dSopenharmony_ci
63f857971dSopenharmony_ciint32_t StreamSocket::EpollWait(int32_t maxevents, int32_t timeout, struct epoll_event &events)
64f857971dSopenharmony_ci{
65f857971dSopenharmony_ci    if (epollFd_ < 0) {
66f857971dSopenharmony_ci        FI_HILOGE("Invalid epollFd:%{public}d", epollFd_);
67f857971dSopenharmony_ci        return RET_ERR;
68f857971dSopenharmony_ci    }
69f857971dSopenharmony_ci    return epoll_wait(epollFd_, &events, maxevents, timeout);
70f857971dSopenharmony_ci}
71f857971dSopenharmony_ci
72f857971dSopenharmony_civoid StreamSocket::OnReadPackets(CircleStreamBuffer &circBuf, StreamSocket::PacketCallBackFun callbackFun)
73f857971dSopenharmony_ci{
74f857971dSopenharmony_ci    constexpr int32_t headSize = static_cast<int32_t>(sizeof(PackHead));
75f857971dSopenharmony_ci    for (int32_t i = 0; i < ONCE_PROCESS_NETPACKET_LIMIT; i++) {
76f857971dSopenharmony_ci        const int32_t residualSize = circBuf.ResidualSize();
77f857971dSopenharmony_ci        if (residualSize < headSize) {
78f857971dSopenharmony_ci            break;
79f857971dSopenharmony_ci        }
80f857971dSopenharmony_ci        int32_t dataSize = residualSize - headSize;
81f857971dSopenharmony_ci        char *buf = const_cast<char *>(circBuf.ReadBuf());
82f857971dSopenharmony_ci        CHKPB(buf);
83f857971dSopenharmony_ci        PackHead *head = reinterpret_cast<PackHead *>(buf);
84f857971dSopenharmony_ci        CHKPB(head);
85f857971dSopenharmony_ci        if ((static_cast<int32_t>(head->size) < 0) || (static_cast<size_t>(head->size) > MAX_PACKET_BUF_SIZE)) {
86f857971dSopenharmony_ci            FI_HILOGE("Packet header parsing error, and this error cannot be recovered, the buffer will be reset, "
87f857971dSopenharmony_ci                "head->size:%{public}d, residualSize:%{public}d", head->size, residualSize);
88f857971dSopenharmony_ci            circBuf.Reset();
89f857971dSopenharmony_ci            break;
90f857971dSopenharmony_ci        }
91f857971dSopenharmony_ci        if (head->size > dataSize) {
92f857971dSopenharmony_ci            break;
93f857971dSopenharmony_ci        }
94f857971dSopenharmony_ci        NetPacket pkt(head->idMsg);
95f857971dSopenharmony_ci        if ((head->size > 0) && (!pkt.Write(&buf[headSize], head->size))) {
96f857971dSopenharmony_ci            FI_HILOGW("Error writing data in the NetPacket, it will be retried next time, messageid:%{public}d, "
97f857971dSopenharmony_ci                "size:%{public}d", head->idMsg, head->size);
98f857971dSopenharmony_ci            break;
99f857971dSopenharmony_ci        }
100f857971dSopenharmony_ci        if (!circBuf.SeekReadPos(pkt.GetPacketLength())) {
101f857971dSopenharmony_ci            FI_HILOGW("Set read position error, and this error cannot be recovered, and the buffer will be reset, "
102f857971dSopenharmony_ci                "packetSize:%{public}d, residualSize:%{public}d", pkt.GetPacketLength(), residualSize);
103f857971dSopenharmony_ci            circBuf.Reset();
104f857971dSopenharmony_ci            break;
105f857971dSopenharmony_ci        }
106f857971dSopenharmony_ci        callbackFun(pkt);
107f857971dSopenharmony_ci        if (circBuf.empty()) {
108f857971dSopenharmony_ci            circBuf.Reset();
109f857971dSopenharmony_ci            break;
110f857971dSopenharmony_ci        }
111f857971dSopenharmony_ci    }
112f857971dSopenharmony_ci}
113f857971dSopenharmony_ci
114f857971dSopenharmony_civoid StreamSocket::EpollClose()
115f857971dSopenharmony_ci{
116f857971dSopenharmony_ci    if (epollFd_ >= 0) {
117f857971dSopenharmony_ci        if (close(epollFd_) < 0) {
118f857971dSopenharmony_ci            FI_HILOGE("Close epoll fd failed, error:%{public}s, epollFd_:%{public}d", strerror(errno), epollFd_);
119f857971dSopenharmony_ci        }
120f857971dSopenharmony_ci        epollFd_ = -1;
121f857971dSopenharmony_ci    }
122f857971dSopenharmony_ci}
123f857971dSopenharmony_ci
124f857971dSopenharmony_civoid StreamSocket::Close()
125f857971dSopenharmony_ci{
126f857971dSopenharmony_ci    if (fd_ >= 0) {
127f857971dSopenharmony_ci        int32_t rf = close(fd_);
128f857971dSopenharmony_ci        if (rf < 0) {
129f857971dSopenharmony_ci            FI_HILOGE("Socket close failed rf:%{public}d", rf);
130f857971dSopenharmony_ci        }
131f857971dSopenharmony_ci    }
132f857971dSopenharmony_ci    fd_ = -1;
133f857971dSopenharmony_ci}
134f857971dSopenharmony_ci} // namespace DeviceStatus
135f857971dSopenharmony_ci} // namespace Msdp
136f857971dSopenharmony_ci} // namespace OHOS