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