• 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     if (eventHandler) {
86         eventHandler->RemoveFileDescriptorListener(fd);
87     }
88 
89     socketLocalFd_ = -1;
90     EventHandlerManager::GetInstance().DelFdListener(fd);
91     EventHandlerManager::GetInstance().DelEventHandler(fd);
92 }
93 
OnReadable(int32_t fd)94 void EventDescriptorListener::OnReadable(int32_t fd)
95 {
96     MEDIA_LOGD("trace.");
97     if (fd != socketLocalFd_) {
98         MEDIA_LOGW("onReadable socketLocalFd_: %{public}d.", socketLocalFd_);
99     }
100     auto spBuf = std::make_shared<DataBuffer>();
101     int32_t error = 0;
102     int32_t readSize = SocketUtils::ReadSocket(fd, spBuf, error);
103     if (readSize == 0) {
104         SHARING_LOGE("onReadable error close socket!");
105         if (error == ECONNRESET) {
106             OnClose(fd);
107         }
108     } else {
109         OnReadData(fd, spBuf);
110     }
111 }
112 
OnWritable(int32_t fd)113 void EventDescriptorListener::OnWritable(int32_t fd)
114 {
115     MEDIA_LOGD("fd: %{public}d.", fd);
116 }
117 
OnShutdown(int32_t fd)118 void EventDescriptorListener::OnShutdown(int32_t fd)
119 {
120     SHARING_LOGI("fd: %{public}d.", fd);
121     OnClose(fd);
122 }
123 
OnException(int32_t fd)124 void EventDescriptorListener::OnException(int32_t fd)
125 {
126     SHARING_LOGD("fd: %{public}d.", fd);
127 }
128 
OnReadData(int32_t fd,DataBuffer::Ptr & buf)129 void EventDescriptorListener::OnReadData(int32_t fd, DataBuffer::Ptr &buf)
130 {
131     MEDIA_LOGD("trace.");
132 }
133 
OnClose(int32_t fd)134 void EventDescriptorListener::OnClose(int32_t fd)
135 {
136     SHARING_LOGI("%{public}s.", __FUNCTION__);
137 }
138 
139 } // namespace Sharing
140 } // namespace OHOS
141