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_client.h"
17f857971dSopenharmony_ci
18f857971dSopenharmony_ci#include "include/util.h"
19f857971dSopenharmony_ci
20f857971dSopenharmony_ci#undef LOG_TAG
21f857971dSopenharmony_ci#define LOG_TAG "StreamClient"
22f857971dSopenharmony_ci
23f857971dSopenharmony_cinamespace OHOS {
24f857971dSopenharmony_cinamespace Msdp {
25f857971dSopenharmony_cinamespace DeviceStatus {
26f857971dSopenharmony_ci
27f857971dSopenharmony_ciStreamClient::StreamClient()
28f857971dSopenharmony_ci{
29f857971dSopenharmony_ci    CALL_DEBUG_ENTER;
30f857971dSopenharmony_ci}
31f857971dSopenharmony_ci
32f857971dSopenharmony_ciStreamClient::~StreamClient()
33f857971dSopenharmony_ci{
34f857971dSopenharmony_ci    CALL_DEBUG_ENTER;
35f857971dSopenharmony_ci}
36f857971dSopenharmony_ci
37f857971dSopenharmony_ciint32_t StreamClient::StartConnect()
38f857971dSopenharmony_ci{
39f857971dSopenharmony_ci    CALL_DEBUG_ENTER;
40f857971dSopenharmony_ci    if (Socket() < 0) {
41f857971dSopenharmony_ci        FI_HILOGE("Socket failed");
42f857971dSopenharmony_ci        return RET_ERR;
43f857971dSopenharmony_ci    }
44f857971dSopenharmony_ci    OnConnected();
45f857971dSopenharmony_ci    return RET_OK;
46f857971dSopenharmony_ci}
47f857971dSopenharmony_ci
48f857971dSopenharmony_cibool StreamClient::SendMsg(const char *buf, size_t size) const
49f857971dSopenharmony_ci{
50f857971dSopenharmony_ci    CHKPF(buf);
51f857971dSopenharmony_ci    if ((size == 0) || (size > MAX_PACKET_BUF_SIZE)) {
52f857971dSopenharmony_ci        FI_HILOGE("Stream buffer size out of range");
53f857971dSopenharmony_ci        return false;
54f857971dSopenharmony_ci    }
55f857971dSopenharmony_ci    if (fd_ < 0) {
56f857971dSopenharmony_ci        FI_HILOGE("The fd_ is less than 0");
57f857971dSopenharmony_ci        return false;
58f857971dSopenharmony_ci    }
59f857971dSopenharmony_ci
60f857971dSopenharmony_ci    int32_t retryCount = 0;
61f857971dSopenharmony_ci    int32_t idx = 0;
62f857971dSopenharmony_ci    const int32_t bufSize = static_cast<int32_t>(size);
63f857971dSopenharmony_ci    int32_t remSize = bufSize;
64f857971dSopenharmony_ci    while (remSize > 0 && retryCount < SEND_RETRY_LIMIT) {
65f857971dSopenharmony_ci        retryCount += 1;
66f857971dSopenharmony_ci        ssize_t number = send(fd_, &buf[idx], remSize, MSG_DONTWAIT | MSG_NOSIGNAL);
67f857971dSopenharmony_ci        if (number < 0) {
68f857971dSopenharmony_ci            if (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK) {
69f857971dSopenharmony_ci                FI_HILOGW("Continue for errno EAGAIN|EINTR|EWOULDBLOCK, errno:%{public}d", errno);
70f857971dSopenharmony_ci                continue;
71f857971dSopenharmony_ci            }
72f857971dSopenharmony_ci            FI_HILOGE("Send return failed, error:%{public}d, fd:%{public}d", errno, fd_);
73f857971dSopenharmony_ci            return false;
74f857971dSopenharmony_ci        }
75f857971dSopenharmony_ci        idx += number;
76f857971dSopenharmony_ci        remSize -= number;
77f857971dSopenharmony_ci        if (remSize > 0) {
78f857971dSopenharmony_ci            usleep(SEND_RETRY_SLEEP_TIME);
79f857971dSopenharmony_ci        }
80f857971dSopenharmony_ci    }
81f857971dSopenharmony_ci    if (retryCount >= SEND_RETRY_LIMIT || remSize != 0) {
82f857971dSopenharmony_ci        FI_HILOGE("Too many times to send :%{public}d/%{public}d, size:%{public}d/%{public}d, fd:%{public}d",
83f857971dSopenharmony_ci            retryCount, SEND_RETRY_LIMIT, idx, bufSize, fd_);
84f857971dSopenharmony_ci        return false;
85f857971dSopenharmony_ci    }
86f857971dSopenharmony_ci    return true;
87f857971dSopenharmony_ci}
88f857971dSopenharmony_ci
89f857971dSopenharmony_cibool StreamClient::SendMsg(const NetPacket &pkt) const
90f857971dSopenharmony_ci{
91f857971dSopenharmony_ci    if (pkt.ChkRWError()) {
92f857971dSopenharmony_ci        FI_HILOGE("Read and write status is error");
93f857971dSopenharmony_ci        return false;
94f857971dSopenharmony_ci    }
95f857971dSopenharmony_ci    StreamBuffer buf;
96f857971dSopenharmony_ci    if (!pkt.MakeData(buf)) {
97f857971dSopenharmony_ci        FI_HILOGE("Failed to buffer packet");
98f857971dSopenharmony_ci        return false;
99f857971dSopenharmony_ci    }
100f857971dSopenharmony_ci    return SendMsg(buf.Data(), buf.Size());
101f857971dSopenharmony_ci}
102f857971dSopenharmony_ci
103f857971dSopenharmony_cibool StreamClient::StartClient(MsgClientFunCallback fun)
104f857971dSopenharmony_ci{
105f857971dSopenharmony_ci    CALL_DEBUG_ENTER;
106f857971dSopenharmony_ci    if (isRunning_ || hasConnected_) {
107f857971dSopenharmony_ci        FI_HILOGE("Client is connected or started");
108f857971dSopenharmony_ci        return false;
109f857971dSopenharmony_ci    }
110f857971dSopenharmony_ci    hasClient_ = true;
111f857971dSopenharmony_ci    recvFun_ = fun;
112f857971dSopenharmony_ci    if (StartConnect() < 0) {
113f857971dSopenharmony_ci        FI_HILOGW("Client connection failed, Try again later");
114f857971dSopenharmony_ci    }
115f857971dSopenharmony_ci    return true;
116f857971dSopenharmony_ci}
117f857971dSopenharmony_ci
118f857971dSopenharmony_civoid StreamClient::Stop()
119f857971dSopenharmony_ci{
120f857971dSopenharmony_ci    CALL_DEBUG_ENTER;
121f857971dSopenharmony_ci    hasClient_ = false;
122f857971dSopenharmony_ci    Close();
123f857971dSopenharmony_ci}
124f857971dSopenharmony_ci} // namespace DeviceStatus
125f857971dSopenharmony_ci} // namespace Msdp
126f857971dSopenharmony_ci} // namespace OHOS