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 inet_ntoa(socket_->udpClientAddr_.sin_addr), socket_->udpClientAddr_.sin_port, socket_->udpClientLen_);
85 RETURN_FALSE_IF_NULL(buf);
86
87 int32_t retCode = ::sendto(socket_->GetLocalFd(), buf, nSize, 0, (struct sockaddr *)&socket_->udpClientAddr_,
88 socket_->udpClientLen_);
89 if ((retCode < 0) && (errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN)) {
90 SHARING_LOGE("send error!");
91 return false;
92 } else if (retCode > 0) {
93 return true;
94 } else {
95 SHARING_LOGE("sendSocket error: %{public}s!", strerror(errno));
96 return false;
97 }
98 }
99
OnSessionReadble(int32_t fd)100 void UdpSession::OnSessionReadble(int32_t fd)
101 {
102 SHARING_LOGD("fd: %{public}d, local_fd:%{public}d, thread_id: %{public}llu.", fd, socket_->GetLocalFd(),
103 GetThreadId());
104 if (socket_ == nullptr) {
105 MEDIA_LOGE("socket nullptr!");
106 return;
107 }
108 if (fd == socket_->GetLocalFd()) {
109 int32_t retCode = 0;
110 do {
111 DataBuffer::Ptr buf = std::make_shared<DataBuffer>(DEAFULT_READ_BUFFER_SIZE);
112 struct sockaddr_in clientAddr;
113 socklen_t len = sizeof(struct sockaddr_in);
114 retCode = ::recvfrom(fd, buf->Data(), DEAFULT_READ_BUFFER_SIZE, 0, (struct sockaddr *)&clientAddr, &len);
115 MEDIA_LOGD("recvSocket len: %{public}d, address: %{public}s, port: %{public}d.", retCode,
116 inet_ntoa(clientAddr.sin_addr), clientAddr.sin_port);
117
118 if (retCode > 0) {
119 buf->UpdateSize(retCode);
120 if (callback_) {
121 callback_->OnSessionReadData(fd, std::move(buf));
122 }
123 } else if (retCode == 0) {
124 MEDIA_LOGW("recvSocket RET CODE 0!");
125 } else {
126 MEDIA_LOGE("recvSocket error!");
127 }
128 } while (retCode > 0);
129 } else {
130 MEDIA_LOGD("onReadable receive msg.");
131 }
132 }
133 } // namespace Sharing
134 } // namespace OHOS