1 /*
2 * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 "udp_session.h"
17 #include <arpa/inet.h>
18 #include <iostream>
19 #include "common/common_macro.h"
20 #include "common/media_log.h"
21 #include "network/socket/socket_utils.h"
22 #include "utils/utils.h"
23
24 namespace OHOS {
25 namespace Sharing {
UdpSession(SocketInfo::Ptr socket)26 UdpSession::UdpSession(SocketInfo::Ptr socket) : BaseNetworkSession(socket)
27 {
28 SHARING_LOGD("trace.");
29 }
30
~UdpSession()31 UdpSession ::~UdpSession()
32 {
33 SHARING_LOGD("trace.");
34 }
35
Start()36 bool UdpSession::Start()
37 {
38 SHARING_LOGD("trace.");
39 if (socket_) {
40 SHARING_LOGD("udpSession AddFdListener.");
41
42 auto eventRunner = OHOS::AppExecFwk::EventRunner::Create(true);
43 eventHandler_ = std::make_shared<UdpSessionEventHandler>();
44 eventHandler_->SetSession(shared_from_this());
45 eventHandler_->SetEventRunner(eventRunner);
46 eventRunner->Run();
47
48 eventListener_ = std::make_shared<UdpSessionEventListener>();
49 eventListener_->SetSession(shared_from_this());
50
51 return eventListener_->AddFdListener(socket_->GetPeerFd(), eventListener_, eventHandler_);
52 }
53
54 return false;
55 }
56
Shutdown()57 void UdpSession::Shutdown()
58 {
59 SHARING_LOGD("trace.");
60 if (socket_) {
61 if (eventListener_) {
62 eventListener_->RemoveFdListener(socket_->GetPeerFd());
63 }
64 SocketUtils::ShutDownSocket(socket_->GetPeerFd());
65 SocketUtils::CloseSocket(socket_->GetPeerFd());
66 socket_.reset();
67 }
68 }
69
Send(const DataBuffer::Ptr & buf,int32_t nSize)70 bool UdpSession::Send(const DataBuffer::Ptr &buf, int32_t nSize)
71 {
72 MEDIA_LOGD("trace.");
73 return UdpSession::Send(buf->Peek(), nSize);
74 }
75
Send(const char * buf,int32_t nSize)76 bool UdpSession::Send(const char *buf, int32_t nSize)
77 {
78 MEDIA_LOGD("trace.");
79 if (socket_ == nullptr) {
80 SHARING_LOGE("socket nullptr!");
81 return false;
82 }
83 MEDIA_LOGD("fd: %{public}d, address: %{public}s, port: %{public}d, len: %{public}d.", socket_->GetLocalFd(),
84 GetAnonyString(inet_ntoa(socket_->udpClientAddr_.sin_addr)).c_str(), socket_->udpClientAddr_.sin_port,
85 socket_->udpClientLen_);
86 RETURN_FALSE_IF_NULL(buf);
87
88 int32_t retCode = ::sendto(socket_->GetLocalFd(), buf, nSize, 0, (struct sockaddr *)&socket_->udpClientAddr_,
89 socket_->udpClientLen_);
90 if ((retCode < 0) && (errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN)) {
91 SHARING_LOGE("send error!");
92 return false;
93 } else if (retCode > 0) {
94 return true;
95 } else {
96 SHARING_LOGE("sendSocket error: %{public}s!", strerror(errno));
97 return false;
98 }
99 }
100
OnSessionReadble(int32_t fd)101 void UdpSession::OnSessionReadble(int32_t fd)
102 {
103 SHARING_LOGD("fd: %{public}d, local_fd:%{public}d, thread_id: %{public}llu.", fd, socket_->GetLocalFd(),
104 GetThreadId());
105 if (socket_ == nullptr) {
106 MEDIA_LOGE("socket nullptr!");
107 return;
108 }
109 if (fd == socket_->GetLocalFd()) {
110 int32_t retCode = 0;
111 do {
112 DataBuffer::Ptr buf = std::make_shared<DataBuffer>(DEAFULT_READ_BUFFER_SIZE);
113 struct sockaddr_in clientAddr;
114 socklen_t len = sizeof(struct sockaddr_in);
115 retCode = ::recvfrom(fd, buf->Data(), DEAFULT_READ_BUFFER_SIZE, 0, (struct sockaddr *)&clientAddr, &len);
116 MEDIA_LOGD("recvSocket len: %{public}d, address: %{public}s, port: %{public}d.", retCode,
117 GetAnonyString(inet_ntoa(clientAddr.sin_addr)).c_str(), clientAddr.sin_port);
118
119 if (retCode > 0) {
120 buf->UpdateSize(retCode);
121 if (callback_) {
122 callback_->OnSessionReadData(fd, std::move(buf));
123 }
124 } else if (retCode == 0) {
125 MEDIA_LOGW("recvSocket RET CODE 0!");
126 } else {
127 MEDIA_LOGE("recvSocket error!");
128 }
129 } while (retCode > 0);
130 } else {
131 MEDIA_LOGD("onReadable receive msg.");
132 }
133 }
134 } // namespace Sharing
135 } // namespace OHOS