1f857971dSopenharmony_ci/*
2f857971dSopenharmony_ci * Copyright (c) 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 "socket_session.h"
17f857971dSopenharmony_ci
18f857971dSopenharmony_ci#include <sstream>
19f857971dSopenharmony_ci
20f857971dSopenharmony_ci#include <sys/socket.h>
21f857971dSopenharmony_ci#include <unistd.h>
22f857971dSopenharmony_ci
23f857971dSopenharmony_ci#include "proto.h"
24f857971dSopenharmony_ci
25f857971dSopenharmony_ci#undef LOG_TAG
26f857971dSopenharmony_ci#define LOG_TAG "SocketSession"
27f857971dSopenharmony_ci
28f857971dSopenharmony_cinamespace OHOS {
29f857971dSopenharmony_cinamespace Msdp {
30f857971dSopenharmony_cinamespace DeviceStatus {
31f857971dSopenharmony_ci
32f857971dSopenharmony_ciSocketSession::SocketSession(const std::string &programName, int32_t moduleType,
33f857971dSopenharmony_ci                             int32_t tokenType, int32_t fd, int32_t uid, int32_t pid)
34f857971dSopenharmony_ci    : fd_(fd), uid_(uid), pid_(pid), tokenType_(tokenType), programName_(programName)
35f857971dSopenharmony_ci{}
36f857971dSopenharmony_ci
37f857971dSopenharmony_ciSocketSession::~SocketSession()
38f857971dSopenharmony_ci{
39f857971dSopenharmony_ci    if ((fd_ >= 0) && (::close(fd_) != 0)) {
40f857971dSopenharmony_ci        FI_HILOGE("close(%{public}d) failed:%{public}s", fd_, ::strerror(errno));
41f857971dSopenharmony_ci    }
42f857971dSopenharmony_ci}
43f857971dSopenharmony_ci
44f857971dSopenharmony_cibool SocketSession::SendMsg(NetPacket &pkt) const
45f857971dSopenharmony_ci{
46f857971dSopenharmony_ci    if (pkt.ChkRWError()) {
47f857971dSopenharmony_ci        FI_HILOGE("Read and write status is error");
48f857971dSopenharmony_ci        return false;
49f857971dSopenharmony_ci    }
50f857971dSopenharmony_ci    StreamBuffer buf;
51f857971dSopenharmony_ci    if (!pkt.MakeData(buf)) {
52f857971dSopenharmony_ci        FI_HILOGE("Failed to buffer packet");
53f857971dSopenharmony_ci        return false;
54f857971dSopenharmony_ci    }
55f857971dSopenharmony_ci    return SendMsg(buf.Data(), buf.Size());
56f857971dSopenharmony_ci}
57f857971dSopenharmony_ci
58f857971dSopenharmony_cibool SocketSession::SendMsg(const char *buf, size_t size) const
59f857971dSopenharmony_ci{
60f857971dSopenharmony_ci    CHKPF(buf);
61f857971dSopenharmony_ci    if ((size == 0) || (size > MAX_PACKET_BUF_SIZE)) {
62f857971dSopenharmony_ci        FI_HILOGE("buf size:%{public}zu", size);
63f857971dSopenharmony_ci        return false;
64f857971dSopenharmony_ci    }
65f857971dSopenharmony_ci    if (fd_ < 0) {
66f857971dSopenharmony_ci        FI_HILOGE("The fd_ is less than 0");
67f857971dSopenharmony_ci        return false;
68f857971dSopenharmony_ci    }
69f857971dSopenharmony_ci
70f857971dSopenharmony_ci    int32_t idx = 0;
71f857971dSopenharmony_ci    int32_t retryCount = 0;
72f857971dSopenharmony_ci    const int32_t bufSize = static_cast<int32_t>(size);
73f857971dSopenharmony_ci    int32_t remSize = bufSize;
74f857971dSopenharmony_ci    FI_HILOGD("Rem size:%{public}d", remSize);
75f857971dSopenharmony_ci    while (remSize > 0 && retryCount < SEND_RETRY_LIMIT) {
76f857971dSopenharmony_ci        retryCount += 1;
77f857971dSopenharmony_ci        FI_HILOGD("Send message to client (%{public}d, %{public}d)", fd_, pid_);
78f857971dSopenharmony_ci        ssize_t count = send(fd_, &buf[idx], remSize, MSG_DONTWAIT | MSG_NOSIGNAL);
79f857971dSopenharmony_ci        if (count < 0) {
80f857971dSopenharmony_ci            if (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK) {
81f857971dSopenharmony_ci                usleep(SEND_RETRY_SLEEP_TIME);
82f857971dSopenharmony_ci                FI_HILOGW("Continue for errno EAGAIN|EINTR|EWOULDBLOCK, errno:%{public}d, pid:%{public}d", errno, pid_);
83f857971dSopenharmony_ci                continue;
84f857971dSopenharmony_ci            }
85f857971dSopenharmony_ci            FI_HILOGE("Send return failed, error:%{public}d, fd:%{public}d, pid:%{public}d", errno, fd_, pid_);
86f857971dSopenharmony_ci            return false;
87f857971dSopenharmony_ci        }
88f857971dSopenharmony_ci        idx += count;
89f857971dSopenharmony_ci        remSize -= count;
90f857971dSopenharmony_ci        if (remSize > 0) {
91f857971dSopenharmony_ci            usleep(SEND_RETRY_SLEEP_TIME);
92f857971dSopenharmony_ci        }
93f857971dSopenharmony_ci    }
94f857971dSopenharmony_ci    if (retryCount >= SEND_RETRY_LIMIT || remSize != 0) {
95f857971dSopenharmony_ci        FI_HILOGE("Send too many times:%{public}d/%{public}d, size:%{public}d/%{public}d, fd:%{public}d,"
96f857971dSopenharmony_ci            "pid:%{public}d", retryCount, SEND_RETRY_LIMIT, idx, bufSize, fd_, pid_);
97f857971dSopenharmony_ci        return false;
98f857971dSopenharmony_ci    }
99f857971dSopenharmony_ci    return true;
100f857971dSopenharmony_ci}
101f857971dSopenharmony_ci
102f857971dSopenharmony_cistd::string SocketSession::ToString() const
103f857971dSopenharmony_ci{
104f857971dSopenharmony_ci    std::ostringstream oss;
105f857971dSopenharmony_ci    oss << "fd = " << fd_
106f857971dSopenharmony_ci        << ((fd_ < 0) ? ", closed" : ", opened")
107f857971dSopenharmony_ci        << ", pid = " << pid_
108f857971dSopenharmony_ci        << ", tokenType = " << tokenType_
109f857971dSopenharmony_ci        << std::endl;
110f857971dSopenharmony_ci    return oss.str();
111f857971dSopenharmony_ci}
112f857971dSopenharmony_ci
113f857971dSopenharmony_civoid SocketSession::Dispatch(const struct epoll_event &ev)
114f857971dSopenharmony_ci{
115f857971dSopenharmony_ci    if ((ev.events & EPOLLIN) == EPOLLIN) {
116f857971dSopenharmony_ci        FI_HILOGD("Data received (%{public}d)", fd_);
117f857971dSopenharmony_ci    } else if ((ev.events & (EPOLLHUP | EPOLLERR)) != 0) {
118f857971dSopenharmony_ci        FI_HILOGE("Epoll hangup:%{public}s", ::strerror(errno));
119f857971dSopenharmony_ci    }
120f857971dSopenharmony_ci}
121f857971dSopenharmony_ci} // namespace DeviceStatus
122f857971dSopenharmony_ci} // namespace Msdp
123f857971dSopenharmony_ci} // namespace OHOS