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
HandleWatcherTaskClose_(const LoopHandle loopHandle,const TaskHandle taskHandle)18 static void HandleWatcherTaskClose_(const LoopHandle loopHandle, const TaskHandle taskHandle)
19 {
20 LE_LOGV("HandleWatcherTaskClose_ fd: %d ", GetSocketFd(taskHandle));
21 CloseTask(loopHandle, (BaseTask *)taskHandle);
22 DelTask((EventLoop *)loopHandle, (BaseTask *)taskHandle);
23 }
24
HandleWatcherEvent_(const LoopHandle loopHandle,const TaskHandle taskHandle,uint32_t oper)25 static LE_STATUS HandleWatcherEvent_(const LoopHandle loopHandle, const TaskHandle taskHandle, uint32_t oper)
26 {
27 LE_LOGV("HandleWatcherEvent_ fd: %d oper 0x%x", GetSocketFd(taskHandle), oper);
28 EventLoop *loop = (EventLoop *)loopHandle;
29 WatcherTask *watcher = (WatcherTask *)taskHandle;
30 int fd = GetSocketFd(taskHandle);
31 uint32_t events = oper;
32 uint64_t userData = *(uint64_t *)LE_GetUserData(taskHandle);
33 if (watcher->processEvent != NULL) {
34 watcher->processEvent(taskHandle, fd, &events, (void *)userData);
35 }
36 watcher = (WatcherTask *)GetTaskByFd((EventLoop *)loopHandle, fd);
37 LE_ONLY_CHECK(watcher != NULL, return 0);
38 if (watcher->base.flags & WATCHER_ONCE) {
39 HandleWatcherTaskClose_((LoopHandle)loop, (TaskHandle)watcher);
40 return 0;
41 }
42 if (events == 0) {
43 HandleWatcherTaskClose_((LoopHandle)loop, (TaskHandle)watcher);
44 return 0;
45 }
46 if (events != watcher->events) {
47 watcher->events = events;
48 loop->modEvent(loop, (const BaseTask *)taskHandle, watcher->events);
49 }
50 return LE_SUCCESS;
51 }
52
DumpWatcherTaskInfo_(const TaskHandle task)53 static void DumpWatcherTaskInfo_(const TaskHandle task)
54 {
55 INIT_CHECK(task != NULL, return);
56 BaseTask *baseTask = (BaseTask *)task;
57
58 WatcherTask *watcherTask = (WatcherTask *)baseTask;
59 printf("\tfd: %d \n", watcherTask->base.taskId.fd);
60 printf("\t TaskType: %s \n", "WatcherTask");
61 }
62
LE_StartWatcher(const LoopHandle loopHandle,WatcherHandle * watcherHandle,const LE_WatchInfo * info,const void * context)63 LE_STATUS LE_StartWatcher(const LoopHandle loopHandle,
64 WatcherHandle *watcherHandle, const LE_WatchInfo *info, const void *context)
65 {
66 LE_CHECK(loopHandle != NULL && watcherHandle != NULL, return LE_INVALID_PARAM, "Invalid parameters");
67 LE_CHECK(info != NULL && info->processEvent != NULL, return LE_INVALID_PARAM, "Invuint64_talid processEvent");
68
69 LE_BaseInfo baseInfo = {TASK_WATCHER | (info->flags & WATCHER_ONCE), info->close, sizeof(uint64_t)};
70 WatcherTask *task = (WatcherTask *)CreateTask(loopHandle, info->fd, &baseInfo, sizeof(WatcherTask));
71 LE_CHECK(task != NULL, return LE_NO_MEMORY, "Failed to create task");
72 task->base.handleEvent = HandleWatcherEvent_;
73 task->base.innerClose = HandleWatcherTaskClose_;
74 task->base.dumpTaskInfo = DumpWatcherTaskInfo_;
75 task->processEvent = info->processEvent;
76 task->events = info->events;
77 *(uint64_t *)(task + 1) = (uint64_t)context;
78
79 EventLoop *loop = (EventLoop *)loopHandle;
80 loop->addEvent(loop, (const BaseTask *)task, info->events);
81 *watcherHandle = (WatcherHandle)task;
82 return LE_SUCCESS;
83 }
84
LE_RemoveWatcher(const LoopHandle loopHandle,const WatcherHandle watcherHandle)85 void LE_RemoveWatcher(const LoopHandle loopHandle, const WatcherHandle watcherHandle)
86 {
87 LE_CHECK(loopHandle != NULL && watcherHandle != NULL, return, "Invalid parameters");
88 LE_CloseTask(loopHandle, watcherHandle);
89 }