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