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 int (*LE_HandleRecvMsg)(const TaskHandle taskHandle, uint8_t* buffer, int bufferSize, int flags); 105 typedef struct { 106 LE_BaseInfo baseInfo; 107 char *server; 108 int socketId; 109 LE_DisConnectComplete disConnectComplete; 110 LE_IncommingConnect incommingConnect; 111 LE_SendMessageComplete sendMessageComplete; 112 LE_RecvMessage recvMessage; 113 } LE_StreamServerInfo; 114 115 typedef struct { 116 LE_BaseInfo baseInfo; 117 char *server; 118 LE_DisConnectComplete disConnectComplete; 119 LE_ConnectComplete connectComplete; 120 LE_SendMessageComplete sendMessageComplete; 121 LE_RecvMessage recvMessage; 122 LE_HandleRecvMsg handleRecvMsg; 123 } LE_StreamInfo; 124 125 LE_STATUS LE_CreateStreamServer(const LoopHandle loopHandle, 126 TaskHandle *taskHandle, const LE_StreamServerInfo *info); 127 LE_STATUS LE_CreateStreamClient(const LoopHandle loopHandle, 128 TaskHandle *taskHandle, const LE_StreamInfo *info); 129 LE_STATUS LE_AcceptStreamClient(const LoopHandle loopHandle, 130 const TaskHandle serverTask, TaskHandle *taskHandle, const LE_StreamInfo *info); 131 LE_STATUS LE_Send(const LoopHandle loopHandle, 132 const TaskHandle taskHandle, const BufferHandle handle, uint32_t buffLen); 133 void LE_CloseStreamTask(const LoopHandle loopHandle, const TaskHandle taskHandle); 134 int LE_GetSocketFd(const TaskHandle taskHandle); 135 136 /** 137 * 异步事件服务 138 */ 139 typedef void (*LE_ProcessAsyncEvent)(const TaskHandle taskHandle, 140 uint64_t eventId, const uint8_t *buffer, uint32_t buffLen); 141 #define TASK_EVENT 0x02 142 #define TASK_ASYNC_EVENT (0x01 << 8) 143 LE_STATUS LE_CreateAsyncTask(const LoopHandle loopHandle, 144 TaskHandle *taskHandle, LE_ProcessAsyncEvent processAsyncEvent); 145 LE_STATUS LE_StartAsyncEvent(const LoopHandle loopHandle, 146 const TaskHandle taskHandle, uint64_t eventId, const uint8_t *data, uint32_t buffLen); 147 void LE_StopAsyncTask(const LoopHandle loopHandle, const TaskHandle taskHandle); 148 #ifdef STARTUP_INIT_TEST 149 void LE_DoAsyncEvent(const LoopHandle loopHandle, const TaskHandle taskHandle); 150 #endif 151 152 /** 153 * 定时器处理 154 */ 155 #define TASK_TIME 0x04 156 typedef void (*LE_ProcessTimer)(const TimerHandle taskHandle, void *context); 157 158 LE_STATUS LE_CreateTimer(const LoopHandle loopHandle, TimerHandle *timer, 159 LE_ProcessTimer processTimer, void *context); 160 LE_STATUS LE_StartTimer(const LoopHandle loopHandle, 161 const TimerHandle timer, uint64_t timeout, uint64_t repeat); 162 void LE_StopTimer(const LoopHandle loopHandle, const TimerHandle timer); 163 164 #define TASK_SIGNAL 0x08 165 typedef void (*LE_ProcessSignal)(const struct signalfd_siginfo *siginfo); 166 LE_STATUS LE_CreateSignalTask(const LoopHandle loopHandle, 167 SignalHandle *signalHandle, LE_ProcessSignal processSignal); 168 LE_STATUS LE_AddSignal(const LoopHandle loopHandle, const SignalHandle signalHandle, int signal); 169 LE_STATUS LE_RemoveSignal(const LoopHandle loopHandle, const SignalHandle signalHandle, int signal); 170 void LE_CloseSignalTask(const LoopHandle loopHandle, const SignalHandle signalHandle); 171 172 /** 173 * 监控句柄变化 174 */ 175 #define TASK_WATCHER 0x10 176 #define WATCHER_ONCE 0x0100 177 typedef void (*ProcessWatchEvent)(const WatcherHandle taskHandle, int fd, uint32_t *events, const void *context); 178 typedef struct { 179 int fd; 180 uint32_t flags; 181 uint32_t events; 182 LE_Close close; 183 ProcessWatchEvent processEvent; 184 } LE_WatchInfo; 185 LE_STATUS LE_StartWatcher(const LoopHandle loopHandle, 186 WatcherHandle *watcherHandle, const LE_WatchInfo *info, const void *context); 187 void LE_RemoveWatcher(const LoopHandle loopHandle, const WatcherHandle watcherHandle); 188 189 /** 190 * Idle Processing:Idle handlers will be called for every loop 191 */ 192 193 /* Idle Handler */ 194 typedef void *IdleHandle; 195 196 /** 197 * @brief Idle process function prototype 198 * 199 * @param taskHandle idle handler 200 * @param context idle function context 201 * @return None 202 */ 203 typedef void (*LE_ProcessIdle)(const IdleHandle taskHandle, void *context); 204 205 /** 206 * @brief Add a new idle handler 207 * 208 * @param loopHandle the running loop this idle will be attached 209 * @param idle optional output parameter for the created idle handler 210 * @param processIdle the idle handler function 211 * @param context optional idle handler context 212 * @param repeat if the idle function will be repeated forevent (non zero) or once (zero) 213 * @return status code, 0 means succeed 214 */ 215 LE_STATUS LE_AddIdle(const LoopHandle loopHandle, IdleHandle *idle, 216 LE_ProcessIdle processIdle, void *context, int repeat); 217 218 /** 219 * @brief Delete an idle handler 220 * 221 * @param idle idle handler 222 * @return None 223 */ 224 void LE_DelIdle(IdleHandle idle); 225 226 /** 227 * @brief Execute an function once in the next loop 228 * 229 * @param loopHandle the running loop this idle will be attached 230 * @param idle the function to be executed 231 * @param context optional idle handler context 232 * @return status code, 0 means succeed 233 */ 234 int LE_DelayProc(const LoopHandle loopHandle, LE_ProcessIdle idle, void *context); 235 236 #ifdef __cplusplus 237 #if __cplusplus 238 } 239 #endif 240 #endif 241 #endif