• 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 
16 #ifndef LOOP_TASK_H
17 #define LOOP_TASK_H
18 #include <stdlib.h>
19 
20 #include "init_hashmap.h"
21 #include "le_utils.h"
22 #include "list.h"
23 #include "loop_event.h"
24 
25 #ifndef LOOP_EVENT_USE_MUTEX
26 #define LoopMutex char
27 #define LoopMutexInit(x)
28 #define LoopMutexLock(x)
29 #define LoopMutexUnlock(x)
30 #else
31 #include <pthread.h>
32 #define LoopMutex pthread_mutex_t
33 #define LoopMutexInit(x) pthread_mutex_init(x, NULL)
34 #define LoopMutexLock(x) pthread_mutex_lock(x)
35 #define LoopMutexUnlock(x) pthread_mutex_unlock(x)
36 #endif
37 
38 typedef struct {
39     ListNode node;
40     uint32_t buffSize;
41     uint32_t dataSize;
42     uint8_t data[0];
43 } LE_Buffer;
44 
45 typedef LE_STATUS (*HandleTaskEvent)(const LoopHandle loop, const TaskHandle task, uint32_t oper);
46 typedef void (*HandleTaskClose)(const LoopHandle loop, const TaskHandle task);
47 #define TASKINFO \
48     uint32_t flags; \
49     union { \
50         int fd; \
51     } taskId
52 
53 typedef struct {
54     TASKINFO;
55 } TaskId;
56 
57 typedef struct LiteTask_ {
58     TASKINFO;
59     HashNode hashNode;
60     LE_Close close;
61     HandleTaskEvent handleEvent;
62     HandleTaskClose innerClose;
63     uint16_t userDataOffset;
64     uint16_t userDataSize;
65 } BaseTask;
66 
67 typedef struct {
68     BaseTask base;
69     LE_IncommingConntect incommingConntect;
70     char server[0];
71 } StreamServerTask;
72 
73 typedef struct {
74     BaseTask base;
75     LoopMutex mutex;
76     ListHead buffHead;
77 } StreamTask;
78 
79 typedef struct {
80     StreamTask stream;
81     StreamServerTask *serverTask;
82     LE_SendMessageComplete sendMessageComplete;
83     LE_RecvMessage recvMessage;
84     LE_DisConntectComplete disConntectComplete;
85 } StreamConnectTask;
86 
87 typedef struct {
88     StreamTask stream;
89     LE_DisConntectComplete disConntectComplete;
90     LE_ConntectComplete connectComplete;
91     LE_SendMessageComplete sendMessageComplete;
92     LE_RecvMessage recvMessage;
93     uint32_t connected : 1;
94     char server[0];
95 } StreamClientTask;
96 
97 typedef struct {
98     StreamTask stream;
99     LE_ProcessAsyncEvent processAsyncEvent;
100 } AsyncEventTask;
101 
102 typedef struct {
103     BaseTask base;
104     int events;
105     ProcessWatchEvent processEvent;
106 } WatcherTask;
107 
108 LE_Buffer *CreateBuffer(uint32_t bufferSize);
109 LE_Buffer *GetNextBuffer(StreamTask *task, const LE_Buffer *next);
110 LE_Buffer *GetFirstBuffer(StreamTask *task);
111 int IsBufferEmpty(StreamTask *task);
112 
113 void FreeBuffer(const LoopHandle loop, StreamTask *task, LE_Buffer *buffer);
114 void AddBuffer(StreamTask *task, LE_Buffer *buffer);
115 
116 BaseTask *CreateTask(const LoopHandle loopHandle, int fd, const LE_BaseInfo *info, uint32_t size);
117 void CloseTask(const LoopHandle loopHandle, BaseTask *task);
118 int GetSocketFd(const TaskHandle task);
119 int CheckTaskFlags(const BaseTask *task, uint32_t flags);
120 #endif