• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "event_descriptor_listener.h"
17 #include "common/media_log.h"
18 #include "common/sharing_log.h"
19 #include "event_handler_manager.h"
20 #include "network/socket/socket_utils.h"
21 #include "utils/data_buffer.h"
22 #include "utils/utils.h"
23 using namespace OHOS::AppExecFwk;
24 
25 namespace OHOS {
26 namespace Sharing {
27 
~EventDescriptorListener()28 EventDescriptorListener ::~EventDescriptorListener()
29 {
30     SHARING_LOGD("trace.");
31 }
32 
EventDescriptorListener()33 EventDescriptorListener::EventDescriptorListener()
34 {
35     SHARING_LOGD("trace.");
36 }
37 
EventDescriptorListener(int32_t fd)38 EventDescriptorListener::EventDescriptorListener(int32_t fd) : socketLocalFd_(fd)
39 {
40     SHARING_LOGD("trace.");
41 }
42 
GetSocketFd()43 int32_t EventDescriptorListener::GetSocketFd()
44 {
45     SHARING_LOGD("trace.");
46     return socketLocalFd_;
47 }
48 
AddFdListener(int32_t fd,const std::shared_ptr<FileDescriptorListener> & listener,const std::shared_ptr<OHOS::AppExecFwk::EventHandler> & handler,const std::string & taskName,uint32_t events)49 bool EventDescriptorListener::AddFdListener(int32_t fd, const std::shared_ptr<FileDescriptorListener> &listener,
50                                             const std::shared_ptr<OHOS::AppExecFwk::EventHandler> &handler,
51                                             const std::string &taskName, uint32_t events)
52 {
53     SHARING_LOGI("fd: %{public}d.", fd);
54     auto existHandler = EventHandlerManager::GetInstance().GetEventHandler(fd);
55     if (existHandler == nullptr && handler == nullptr) {
56         MEDIA_LOGE("eventHandler is nullptr!");
57         return false;
58     }
59 
60     auto eventHandler = handler;
61     if (eventHandler == nullptr) {
62         eventHandler = existHandler;
63     } else {
64         EventHandlerManager::GetInstance().AddEventHandler(fd, eventHandler);
65         EventHandlerManager::GetInstance().AddFdListener(fd, listener);
66     }
67     return ERR_OK == eventHandler->AddFileDescriptorListener(fd, events, listener, taskName);
68 }
69 
RemoveFdListener(int32_t fd,const std::shared_ptr<OHOS::AppExecFwk::EventHandler> & handler)70 void EventDescriptorListener::RemoveFdListener(int32_t fd,
71                                                const std::shared_ptr<OHOS::AppExecFwk::EventHandler> &handler)
72 {
73     SHARING_LOGI("fd: %{public}d.", fd);
74     auto existHandler = EventHandlerManager::GetInstance().GetEventHandler(fd);
75     if (existHandler == nullptr && handler == nullptr) {
76         MEDIA_LOGE("eventHandler is nullptr!");
77         return;
78     }
79 
80     auto eventHandler = handler;
81     if (eventHandler == nullptr) {
82         eventHandler = existHandler;
83     }
84 
85     eventHandler->RemoveFileDescriptorListener(fd);
86     socketLocalFd_ = -1;
87     EventHandlerManager::GetInstance().DelFdListener(fd);
88     EventHandlerManager::GetInstance().DelEventHandler(fd);
89 }
90 
OnReadable(int32_t fd)91 void EventDescriptorListener::OnReadable(int32_t fd)
92 {
93     MEDIA_LOGD("trace.");
94     if (fd != socketLocalFd_) {
95         MEDIA_LOGW("onReadable socketLocalFd_: %{public}d.", socketLocalFd_);
96     }
97     auto spBuf = std::make_shared<DataBuffer>();
98     int32_t error = 0;
99     int32_t readSize = SocketUtils::ReadSocket(fd, spBuf, error);
100     if (readSize == 0) {
101         SHARING_LOGE("onReadable error close socket!");
102         if (error == ECONNRESET) {
103             OnClose(fd);
104         }
105     } else {
106         OnReadData(fd, spBuf);
107     }
108 }
109 
OnWritable(int32_t fd)110 void EventDescriptorListener::OnWritable(int32_t fd)
111 {
112     MEDIA_LOGD("fd: %{public}d.", fd);
113 }
114 
OnShutdown(int32_t fd)115 void EventDescriptorListener::OnShutdown(int32_t fd)
116 {
117     SHARING_LOGI("fd: %{public}d.", fd);
118     OnClose(fd);
119 }
120 
OnException(int32_t fd)121 void EventDescriptorListener::OnException(int32_t fd)
122 {
123     SHARING_LOGD("fd: %{public}d.", fd);
124 }
125 
OnReadData(int32_t fd,DataBuffer::Ptr & buf)126 void EventDescriptorListener::OnReadData(int32_t fd, DataBuffer::Ptr &buf)
127 {
128     MEDIA_LOGD("trace.");
129 }
130 
OnClose(int32_t fd)131 void EventDescriptorListener::OnClose(int32_t fd)
132 {
133     SHARING_LOGI("%{public}s.", __FUNCTION__);
134 }
135 
136 } // namespace Sharing
137 } // namespace OHOS
138