1 /*
2 * Copyright (c) 2021 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 #include "le_task.h"
16 #include "le_loop.h"
17
HandleWatcherEvent_(const LoopHandle loopHandle,const TaskHandle taskHandle,uint32_t oper)18 static LE_STATUS HandleWatcherEvent_(const LoopHandle loopHandle, const TaskHandle taskHandle, uint32_t oper)
19 {
20 LE_LOGV("HandleWatcherEvent_ fd: %d oper 0x%x", GetSocketFd(taskHandle), oper);
21 EventLoop *loop = (EventLoop *)loopHandle;
22 WatcherTask *watcher = (WatcherTask *)taskHandle;
23 int fd = GetSocketFd(taskHandle);
24 uint32_t events = oper;
25 uint64_t userData = *(uint64_t *)LE_GetUserData(taskHandle);
26 if (watcher->processEvent != NULL) {
27 watcher->processEvent(taskHandle, fd, &events, (void *)userData);
28 }
29 watcher = (WatcherTask *)GetTaskByFd((EventLoop *)loopHandle, fd);
30 LE_ONLY_CHECK(watcher != NULL, return 0);
31 if (watcher->base.flags & WATCHER_ONCE) {
32 loop->delEvent(loop, fd, watcher->events);
33 return 0;
34 }
35 if (events == 0) {
36 loop->delEvent(loop, fd, watcher->events);
37 return 0;
38 }
39 if (events != watcher->events) {
40 watcher->events = events;
41 loop->modEvent(loop, (const BaseTask *)taskHandle, watcher->events);
42 }
43 return LE_SUCCESS;
44 }
45
HandleWatcherTaskClose_(const LoopHandle loopHandle,const TaskHandle taskHandle)46 static void HandleWatcherTaskClose_(const LoopHandle loopHandle, const TaskHandle taskHandle)
47 {
48 CloseTask(loopHandle, (BaseTask *)taskHandle);
49 }
50
LE_StartWatcher(const LoopHandle loopHandle,WatcherHandle * watcherHandle,const LE_WatchInfo * info,const void * context)51 LE_STATUS LE_StartWatcher(const LoopHandle loopHandle,
52 WatcherHandle *watcherHandle, const LE_WatchInfo *info, const void *context)
53 {
54 LE_CHECK(loopHandle != NULL && watcherHandle != NULL, return LE_INVALID_PARAM, "Invalid parameters");
55 LE_CHECK(info != NULL && info->processEvent != NULL, return LE_INVALID_PARAM, "Invuint64_talid processEvent");
56
57 LE_BaseInfo baseInfo = {TASK_WATCHER | (info->flags & WATCHER_ONCE), info->close, sizeof(uint64_t)};
58 WatcherTask *task = (WatcherTask *)CreateTask(loopHandle, info->fd, &baseInfo, sizeof(WatcherTask));
59 LE_CHECK(task != NULL, return LE_NO_MEMORY, "Failed to create task");
60 task->base.handleEvent = HandleWatcherEvent_;
61 task->base.innerClose = HandleWatcherTaskClose_;
62 task->processEvent = info->processEvent;
63 task->events = info->events;
64 *(uint64_t *)(task + 1) = (uint64_t)context;
65
66 EventLoop *loop = (EventLoop *)loopHandle;
67 loop->addEvent(loop, (const BaseTask *)task, info->events);
68 *watcherHandle = (WatcherHandle)task;
69 return LE_SUCCESS;
70 }
71
LE_RemoveWatcher(const LoopHandle loopHandle,const WatcherHandle watcherHandle)72 void LE_RemoveWatcher(const LoopHandle loopHandle, const WatcherHandle watcherHandle)
73 {
74 LE_CHECK(loopHandle != NULL && watcherHandle != NULL, return, "Invalid parameters");
75 LE_CloseTask(loopHandle, watcherHandle);
76 }