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_EVENT_H 17 #define LOOP_EVENT_H 18 #include <stdint.h> 19 #include <stdlib.h> 20 #include <sys/signalfd.h> 21 22 #ifdef __cplusplus 23 #if __cplusplus 24 extern "C" { 25 #endif 26 #endif 27 28 // 配置 29 #define LOOP_EVENT_USE_EPOLL 1 30 #define LOOP_DEFAULT_BUFFER (1024 * 5) 31 #define LOOP_MAX_BUFFER (1024 * 64) 32 33 #define LOOP_MAX_CLIENT 1024 34 #define LOOP_MAX_SOCKET 1024 35 #define DEFAULT_TIMEOUT 1000 36 37 typedef enum { 38 EVENT_READ = (0x0001), 39 EVENT_WRITE = (0x0002), 40 EVENT_ERROR = (0x0004), 41 EVENT_FREE = (0x0008), 42 EVENT_TIMEOUT = (0x0010), 43 EVENT_SIGNAL = (0x0020), 44 } EventOper; 45 46 typedef enum { 47 LE_SUCCESS = 0, 48 LE_FAILURE = 10000, 49 LE_INVALID_PARAM, 50 LE_NO_MEMORY, 51 LE_DIS_CONNECTED, 52 LE_INVALID_TASK 53 } LE_STATUS; 54 55 typedef struct { 56 uint32_t flags; 57 } LoopBase; 58 59 typedef LoopBase *LoopHandle; 60 typedef LoopBase *TaskHandle; 61 typedef LoopBase *TimerHandle; 62 typedef LoopBase *SignalHandle; 63 typedef LoopBase *WatcherHandle; 64 typedef void *BufferHandle; 65 66 LoopHandle LE_GetDefaultLoop(void); 67 LE_STATUS LE_CreateLoop(LoopHandle *loopHandle); 68 void LE_RunLoop(const LoopHandle loopHandle); 69 void LE_CloseLoop(const LoopHandle loopHandle); 70 void LE_StopLoop(const LoopHandle loopHandle); 71 void LE_CloseTask(const LoopHandle loopHandle, const TaskHandle taskHandle); 72 73 /** 74 * 申请一个buffer,用于消息的发送和接收 75 */ 76 BufferHandle LE_CreateBuffer(const LoopHandle loopHandle, uint32_t bufferSize); 77 void LE_FreeBuffer(const LoopHandle loopHandle, const TaskHandle taskHandle, const BufferHandle handle); 78 uint8_t *LE_GetBufferInfo(const BufferHandle handle, uint32_t *dataSize, uint32_t *buffSize); 79 void *LE_GetUserData(const TaskHandle handle); 80 int32_t LE_GetSendResult(const BufferHandle handle); 81 82 typedef void (*LE_Close)(const TaskHandle taskHandle); 83 typedef struct { 84 uint32_t flags; 85 LE_Close close; 86 uint16_t userDataSize; 87 } LE_BaseInfo; 88 89 /** 90 * 数据流服务,可以指定使用pipe或者普通的tcp 91 */ 92 #define TASK_STREAM 0x01 93 #define TASK_TCP (0x01 << 8) 94 #define TASK_PIPE (0x02 << 8) 95 #define TASK_SERVER (0x01 << 16) 96 #define TASK_CONNECT (0x02 << 16) 97 #define TASK_TEST (0x01 << 24) 98 #define TASK_PUBLIC (0x01 << 25) // If the socket can be publicly connected 99 typedef void (*LE_DisConnectComplete)(const TaskHandle client); 100 typedef void (*LE_ConnectComplete)(const TaskHandle client); 101 typedef void (*LE_SendMessageComplete)(const TaskHandle taskHandle, BufferHandle handle); 102 typedef void (*LE_RecvMessage)(const TaskHandle taskHandle, const uint8_t *buffer, uint32_t buffLen); 103 typedef int (*LE_IncommingConnect)(const LoopHandle loopHandle, const TaskHandle serverTask); 104 typedef struct { 105 LE_BaseInfo baseInfo; 106 char *server; 107 int socketId; 108 LE_DisConnectComplete disConnectComplete; 109 LE_IncommingConnect incommingConnect; 110 LE_SendMessageComplete sendMessageComplete; 111 LE_RecvMessage recvMessage; 112 } LE_StreamServerInfo; 113 114 typedef struct { 115 LE_BaseInfo baseInfo; 116 char *server; 117 LE_DisConnectComplete disConnectComplete; 118 LE_ConnectComplete connectComplete; 119 LE_SendMessageComplete sendMessageComplete; 120 LE_RecvMessage recvMessage; 121 } LE_StreamInfo; 122 123 LE_STATUS LE_CreateStreamServer(const LoopHandle loopHandle, 124 TaskHandle *taskHandle, const LE_StreamServerInfo *info); 125 LE_STATUS LE_CreateStreamClient(const LoopHandle loopHandle, 126 TaskHandle *taskHandle, const LE_StreamInfo *info); 127 LE_STATUS LE_AcceptStreamClient(const LoopHandle loopHandle, 128 const TaskHandle serverTask, TaskHandle *taskHandle, const LE_StreamInfo *info); 129 LE_STATUS LE_Send(const LoopHandle loopHandle, 130 const TaskHandle taskHandle, const BufferHandle handle, uint32_t buffLen); 131 void LE_CloseStreamTask(const LoopHandle loopHandle, const TaskHandle taskHandle); 132 int LE_GetSocketFd(const TaskHandle taskHandle); 133 134 /** 135 * 异步事件服务 136 */ 137 typedef void (*LE_ProcessAsyncEvent)(const TaskHandle taskHandle, 138 uint64_t eventId, const uint8_t *buffer, uint32_t buffLen); 139 #define TASK_EVENT 0x02 140 #define TASK_ASYNC_EVENT (0x01 << 8) 141 LE_STATUS LE_CreateAsyncTask(const LoopHandle loopHandle, 142 TaskHandle *taskHandle, LE_ProcessAsyncEvent processAsyncEvent); 143 LE_STATUS LE_StartAsyncEvent(const LoopHandle loopHandle, 144 const TaskHandle taskHandle, uint64_t eventId, const uint8_t *data, uint32_t buffLen); 145 void LE_StopAsyncTask(const LoopHandle loopHandle, const TaskHandle taskHandle); 146 #ifdef STARTUP_INIT_TEST 147 void LE_DoAsyncEvent(const LoopHandle loopHandle, const TaskHandle taskHandle); 148 #endif 149 150 /** 151 * 定时器处理 152 */ 153 #define TASK_TIME 0x04 154 typedef void (*LE_ProcessTimer)(const TimerHandle taskHandle, void *context); 155 156 LE_STATUS LE_CreateTimer(const LoopHandle loopHandle, TimerHandle *timer, 157 LE_ProcessTimer processTimer, void *context); 158 LE_STATUS LE_StartTimer(const LoopHandle loopHandle, 159 const TimerHandle timer, uint64_t timeout, uint64_t repeat); 160 void LE_StopTimer(const LoopHandle loopHandle, const TimerHandle timer); 161 162 #define TASK_SIGNAL 0x08 163 typedef void (*LE_ProcessSignal)(const struct signalfd_siginfo *siginfo); 164 LE_STATUS LE_CreateSignalTask(const LoopHandle loopHandle, 165 SignalHandle *signalHandle, LE_ProcessSignal processSignal); 166 LE_STATUS LE_AddSignal(const LoopHandle loopHandle, const SignalHandle signalHandle, int signal); 167 LE_STATUS LE_RemoveSignal(const LoopHandle loopHandle, const SignalHandle signalHandle, int signal); 168 void LE_CloseSignalTask(const LoopHandle loopHandle, const SignalHandle signalHandle); 169 170 /** 171 * 监控句柄变化 172 */ 173 #define TASK_WATCHER 0x10 174 #define WATCHER_ONCE 0x0100 175 typedef void (*ProcessWatchEvent)(const WatcherHandle taskHandle, int fd, uint32_t *events, const void *context); 176 typedef struct { 177 int fd; 178 uint32_t flags; 179 uint32_t events; 180 LE_Close close; 181 ProcessWatchEvent processEvent; 182 } LE_WatchInfo; 183 LE_STATUS LE_StartWatcher(const LoopHandle loopHandle, 184 WatcherHandle *watcherHandle, const LE_WatchInfo *info, const void *context); 185 void LE_RemoveWatcher(const LoopHandle loopHandle, const WatcherHandle watcherHandle); 186 187 /** 188 * Idle Processing:Idle handlers will be called for every loop 189 */ 190 191 /* Idle Handler */ 192 typedef void *IdleHandle; 193 194 /** 195 * @brief Idle process function prototype 196 * 197 * @param taskHandle idle handler 198 * @param context idle function context 199 * @return None 200 */ 201 typedef void (*LE_ProcessIdle)(const IdleHandle taskHandle, void *context); 202 203 /** 204 * @brief Add a new idle handler 205 * 206 * @param loopHandle the running loop this idle will be attached 207 * @param idle optional output parameter for the created idle handler 208 * @param processIdle the idle handler function 209 * @param context optional idle handler context 210 * @param repeat if the idle function will be repeated forevent (non zero) or once (zero) 211 * @return status code, 0 means succeed 212 */ 213 LE_STATUS LE_AddIdle(const LoopHandle loopHandle, IdleHandle *idle, 214 LE_ProcessIdle processIdle, void *context, int repeat); 215 216 /** 217 * @brief Delete an idle handler 218 * 219 * @param idle idle handler 220 * @return None 221 */ 222 void LE_DelIdle(IdleHandle idle); 223 224 /** 225 * @brief Execute an function once in the next loop 226 * 227 * @param loopHandle the running loop this idle will be attached 228 * @param idle the function to be executed 229 * @param context optional idle handler context 230 * @return status code, 0 means succeed 231 */ 232 int LE_DelayProc(const LoopHandle loopHandle, LE_ProcessIdle idle, void *context); 233 234 #ifdef __cplusplus 235 #if __cplusplus 236 } 237 #endif 238 #endif 239 #endif