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_connection.h"
17f857971dSopenharmony_ci
18f857971dSopenharmony_ci#include <sys/socket.h>
19f857971dSopenharmony_ci#include <unistd.h>
20f857971dSopenharmony_ci
21f857971dSopenharmony_ci#include "devicestatus_define.h"
22f857971dSopenharmony_ci#include "include/util.h"
23f857971dSopenharmony_ci
24f857971dSopenharmony_ci#undef LOG_TAG
25f857971dSopenharmony_ci#define LOG_TAG "SocketConnection"
26f857971dSopenharmony_ci
27f857971dSopenharmony_cinamespace OHOS {
28f857971dSopenharmony_cinamespace Msdp {
29f857971dSopenharmony_cinamespace DeviceStatus {
30f857971dSopenharmony_ci
31f857971dSopenharmony_ciSocketConnection::SocketConnection(int32_t socketFd,
32f857971dSopenharmony_ci                                   std::function<void(NetPacket&)> recv,
33f857971dSopenharmony_ci                                   std::function<void()> onDisconnected)
34f857971dSopenharmony_ci    : socketFd_(socketFd), recv_(recv), onDisconnected_(onDisconnected)
35f857971dSopenharmony_ci{}
36f857971dSopenharmony_ci
37f857971dSopenharmony_ciSocketConnection::~SocketConnection()
38f857971dSopenharmony_ci{
39f857971dSopenharmony_ci    if ((socketFd_ >= 0) && (::close(socketFd_) != 0)) {
40f857971dSopenharmony_ci        FI_HILOGE("close(%{public}d) failed:%{public}s", socketFd_, ::strerror(errno));
41f857971dSopenharmony_ci    }
42f857971dSopenharmony_ci}
43f857971dSopenharmony_ci
44f857971dSopenharmony_cistd::shared_ptr<SocketConnection> SocketConnection::Connect(std::function<int32_t()> socket,
45f857971dSopenharmony_ci    std::function<void(NetPacket&)> recv, std::function<void()> onDisconnected)
46f857971dSopenharmony_ci{
47f857971dSopenharmony_ci    CALL_DEBUG_ENTER;
48f857971dSopenharmony_ci    CHKPP(socket);
49f857971dSopenharmony_ci    int32_t sockFd = socket();
50f857971dSopenharmony_ci    if (sockFd < 0) {
51f857971dSopenharmony_ci        return nullptr;
52f857971dSopenharmony_ci    }
53f857971dSopenharmony_ci    return std::make_shared<SocketConnection>(sockFd, recv, onDisconnected);
54f857971dSopenharmony_ci}
55f857971dSopenharmony_ci
56f857971dSopenharmony_civoid SocketConnection::OnReadable(int32_t fd)
57f857971dSopenharmony_ci{
58f857971dSopenharmony_ci    CALL_DEBUG_ENTER;
59f857971dSopenharmony_ci    char buf[MAX_PACKET_BUF_SIZE] {};
60f857971dSopenharmony_ci    ssize_t numRead;
61f857971dSopenharmony_ci
62f857971dSopenharmony_ci    do {
63f857971dSopenharmony_ci        numRead = ::recv(fd, buf, sizeof(buf), MSG_DONTWAIT);
64f857971dSopenharmony_ci        if (numRead > 0) {
65f857971dSopenharmony_ci            buffer_.Write(buf, numRead);
66f857971dSopenharmony_ci            OnReadPackets(buffer_, recv_);
67f857971dSopenharmony_ci        } else if (numRead < 0) {
68f857971dSopenharmony_ci            if (errno == EINTR) {
69f857971dSopenharmony_ci                FI_HILOGD("recv was interrupted, read again");
70f857971dSopenharmony_ci                continue;
71f857971dSopenharmony_ci            }
72f857971dSopenharmony_ci            if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
73f857971dSopenharmony_ci                FI_HILOGW("No available data");
74f857971dSopenharmony_ci            } else {
75f857971dSopenharmony_ci                FI_HILOGE("recv failed:%{public}s", ::strerror(errno));
76f857971dSopenharmony_ci            }
77f857971dSopenharmony_ci            break;
78f857971dSopenharmony_ci        } else {
79f857971dSopenharmony_ci            FI_HILOGE("EOF happened");
80f857971dSopenharmony_ci            OnShutdown(fd);
81f857971dSopenharmony_ci            break;
82f857971dSopenharmony_ci        }
83f857971dSopenharmony_ci    } while (numRead == sizeof(buf));
84f857971dSopenharmony_ci}
85f857971dSopenharmony_ci
86f857971dSopenharmony_civoid SocketConnection::OnShutdown(int32_t fd)
87f857971dSopenharmony_ci{
88f857971dSopenharmony_ci    if (onDisconnected_) {
89f857971dSopenharmony_ci        onDisconnected_();
90f857971dSopenharmony_ci    }
91f857971dSopenharmony_ci}
92f857971dSopenharmony_ci
93f857971dSopenharmony_civoid SocketConnection::OnException(int32_t fd)
94f857971dSopenharmony_ci{
95f857971dSopenharmony_ci    OnShutdown(fd);
96f857971dSopenharmony_ci}
97f857971dSopenharmony_ci} // namespace DeviceStatus
98f857971dSopenharmony_ci} // namespace Msdp
99f857971dSopenharmony_ci} // namespace OHOS