• 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 #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     DelTask((EventLoop *)loopHandle, (BaseTask *)taskHandle);
50 }
51 
LE_StartWatcher(const LoopHandle loopHandle,WatcherHandle * watcherHandle,const LE_WatchInfo * info,const void * context)52 LE_STATUS LE_StartWatcher(const LoopHandle loopHandle,
53     WatcherHandle *watcherHandle, const LE_WatchInfo *info, const void *context)
54 {
55     LE_CHECK(loopHandle != NULL && watcherHandle != NULL, return LE_INVALID_PARAM, "Invalid parameters");
56     LE_CHECK(info != NULL && info->processEvent != NULL, return LE_INVALID_PARAM, "Invuint64_talid processEvent");
57 
58     LE_BaseInfo baseInfo = {TASK_WATCHER | (info->flags & WATCHER_ONCE), info->close, sizeof(uint64_t)};
59     WatcherTask *task = (WatcherTask *)CreateTask(loopHandle, info->fd, &baseInfo, sizeof(WatcherTask));
60     LE_CHECK(task != NULL, return LE_NO_MEMORY, "Failed to create task");
61     task->base.handleEvent = HandleWatcherEvent_;
62     task->base.innerClose = HandleWatcherTaskClose_;
63     task->processEvent = info->processEvent;
64     task->events = info->events;
65     *(uint64_t *)(task + 1) = (uint64_t)context;
66 
67     EventLoop *loop = (EventLoop *)loopHandle;
68     loop->addEvent(loop, (const BaseTask *)task, info->events);
69     *watcherHandle = (WatcherHandle)task;
70     return LE_SUCCESS;
71 }
72 
LE_RemoveWatcher(const LoopHandle loopHandle,const WatcherHandle watcherHandle)73 void LE_RemoveWatcher(const LoopHandle loopHandle, const WatcherHandle watcherHandle)
74 {
75     LE_CHECK(loopHandle != NULL && watcherHandle != NULL, return, "Invalid parameters");
76     LE_CloseTask(loopHandle, watcherHandle);
77 }