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 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_STATUS; 53 54 typedef struct { 55 uint32_t flags; 56 } LoopBase; 57 58 typedef LoopBase *LoopHandle; 59 typedef LoopBase *TaskHandle; 60 typedef LoopBase *TimerHandle; 61 typedef LoopBase *SignalHandle; 62 typedef LoopBase *WatcherHandle; 63 typedef void *BufferHandle; 64 65 LoopHandle LE_GetDefaultLoop(void); 66 LE_STATUS LE_CreateLoop(LoopHandle *loopHandle); 67 void LE_RunLoop(const LoopHandle loopHandle); 68 void LE_StopLoop(const LoopHandle loopHandle); 69 void LE_CloseTask(const LoopHandle loopHandle, const TaskHandle taskHandle); 70 71 /** 72 * 申请一个buffer,用于消息的发送和接收 73 */ 74 BufferHandle LE_CreateBuffer(const LoopHandle loopHandle, uint32_t bufferSize); 75 void LE_FreeBuffer(const LoopHandle loopHandle, const TaskHandle taskHandle, const BufferHandle handle); 76 uint8_t *LE_GetBufferInfo(const BufferHandle handle, uint32_t *dataSize, uint32_t *buffSize); 77 void *LE_GetUserData(const TaskHandle handle); 78 79 typedef void (*LE_Close)(const TaskHandle taskHandle); 80 typedef struct { 81 uint32_t flags; 82 LE_Close close; 83 uint16_t userDataSize; 84 } LE_BaseInfo; 85 86 /** 87 * 数据流服务,可以指定使用pipe或者普通的tcp 88 */ 89 #define TASK_STREAM 0x01 90 #define TASK_TCP (0x01 << 8) 91 #define TASK_PIPE (0x02 << 8) 92 #define TASK_SERVER (0x01 << 16) 93 #define TASK_CONNECT (0x02 << 16) 94 #define TASK_TEST (0x01 << 24) 95 typedef void (*LE_DisConntectComplete)(const TaskHandle client); 96 typedef void (*LE_ConntectComplete)(const TaskHandle client); 97 typedef void (*LE_SendMessageComplete)(const TaskHandle taskHandle, BufferHandle handle); 98 typedef void (*LE_RecvMessage)(const TaskHandle taskHandle, const uint8_t *buffer, uint32_t buffLen); 99 typedef int (*LE_IncommingConntect)(const LoopHandle loopHandle, const TaskHandle serverTask); 100 typedef struct { 101 LE_BaseInfo baseInfo; 102 char *server; 103 LE_DisConntectComplete disConntectComplete; 104 LE_IncommingConntect incommingConntect; 105 LE_SendMessageComplete sendMessageComplete; 106 LE_RecvMessage recvMessage; 107 } LE_StreamServerInfo; 108 109 typedef struct { 110 LE_BaseInfo baseInfo; 111 char *server; 112 LE_DisConntectComplete disConntectComplete; 113 LE_ConntectComplete connectComplete; 114 LE_SendMessageComplete sendMessageComplete; 115 LE_RecvMessage recvMessage; 116 } LE_StreamInfo; 117 118 LE_STATUS LE_CreateStreamServer(const LoopHandle loopHandle, 119 TaskHandle *taskHandle, const LE_StreamServerInfo *info); 120 LE_STATUS LE_CreateStreamClient(const LoopHandle loopHandle, 121 TaskHandle *taskHandle, const LE_StreamInfo *info); 122 LE_STATUS LE_AcceptStreamClient(const LoopHandle loopHandle, 123 const TaskHandle serverTask, TaskHandle *taskHandle, const LE_StreamInfo *info); 124 LE_STATUS LE_Send(const LoopHandle loopHandle, 125 const TaskHandle taskHandle, const BufferHandle handle, uint32_t buffLen); 126 void LE_CloseStreamTask(const LoopHandle loopHandle, const TaskHandle taskHandle); 127 int LE_GetSocketFd(const TaskHandle taskHandle); 128 129 /** 130 * 异步事件服务 131 */ 132 typedef void (*LE_ProcessAsyncEvent)(const TaskHandle taskHandle, 133 uint64_t eventId, const uint8_t *buffer, uint32_t buffLen); 134 #define TASK_EVENT 0x02 135 #define TASK_ASYNC_EVENT (0x01 << 8) 136 LE_STATUS LE_CreateAsyncTask(const LoopHandle loopHandle, 137 TaskHandle *taskHandle, LE_ProcessAsyncEvent processAsyncEvent); 138 LE_STATUS LE_StartAsyncEvent(const LoopHandle loopHandle, 139 const TaskHandle taskHandle, uint64_t eventId, const uint8_t *data, uint32_t buffLen); 140 void LE_StopAsyncTask(const LoopHandle loopHandle, const TaskHandle taskHandle); 141 #ifdef STARTUP_INIT_TEST 142 void LE_DoAsyncEvent(const LoopHandle loopHandle, const TaskHandle taskHandle); 143 #endif 144 145 /** 146 * 定时器处理 147 */ 148 #define TASK_TIME 0x04 149 typedef void (*LE_ProcessTimer)(const TimerHandle taskHandle, void *context); 150 151 LE_STATUS LE_CreateTimer(const LoopHandle loopHandle, TimerHandle *timer, 152 LE_ProcessTimer processTimer, void *context); 153 LE_STATUS LE_StartTimer(const LoopHandle loopHandle, 154 const TimerHandle timer, uint64_t timeout, uint64_t repeat); 155 void LE_StopTimer(const LoopHandle loopHandle, const TimerHandle timer); 156 157 #define TASK_SIGNAL 0x08 158 typedef void (*LE_ProcessSignal)(const struct signalfd_siginfo *siginfo); 159 LE_STATUS LE_CreateSignalTask(const LoopHandle loopHandle, 160 SignalHandle *signalHandle, LE_ProcessSignal processSignal); 161 LE_STATUS LE_AddSignal(const LoopHandle loopHandle, const SignalHandle signalHandle, int signal); 162 LE_STATUS LE_RemoveSignal(const LoopHandle loopHandle, const SignalHandle signalHandle, int signal); 163 void LE_CloseSignalTask(const LoopHandle loopHandle, const SignalHandle signalHandle); 164 165 /** 166 * 监控句柄变化 167 */ 168 #define TASK_WATCHER 0x10 169 #define WATCHER_ONCE 0x0100 170 typedef void (*ProcessWatchEvent)(const WatcherHandle taskHandle, int fd, uint32_t *events, const void *context); 171 typedef struct { 172 int fd; 173 uint32_t flags; 174 uint32_t events; 175 LE_Close close; 176 ProcessWatchEvent processEvent; 177 } LE_WatchInfo; 178 LE_STATUS LE_StartWatcher(const LoopHandle loopHandle, 179 WatcherHandle *watcherHandle, const LE_WatchInfo *info, const void *context); 180 void LE_RemoveWatcher(const LoopHandle loopHandle, const WatcherHandle watcherHandle); 181 182 #ifdef __cplusplus 183 #if __cplusplus 184 } 185 #endif 186 #endif 187 #endif