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 typedef void (*LE_DisConnectComplete)(const TaskHandle client); 99 typedef void (*LE_ConnectComplete)(const TaskHandle client); 100 typedef void (*LE_SendMessageComplete)(const TaskHandle taskHandle, BufferHandle handle); 101 typedef void (*LE_RecvMessage)(const TaskHandle taskHandle, const uint8_t *buffer, uint32_t buffLen); 102 typedef int (*LE_IncommingConnect)(const LoopHandle loopHandle, const TaskHandle serverTask); 103 typedef struct { 104 LE_BaseInfo baseInfo; 105 char *server; 106 int socketId; 107 LE_DisConnectComplete disConnectComplete; 108 LE_IncommingConnect incommingConnect; 109 LE_SendMessageComplete sendMessageComplete; 110 LE_RecvMessage recvMessage; 111 } LE_StreamServerInfo; 112 113 typedef struct { 114 LE_BaseInfo baseInfo; 115 char *server; 116 LE_DisConnectComplete disConnectComplete; 117 LE_ConnectComplete connectComplete; 118 LE_SendMessageComplete sendMessageComplete; 119 LE_RecvMessage recvMessage; 120 } LE_StreamInfo; 121 122 LE_STATUS LE_CreateStreamServer(const LoopHandle loopHandle, 123 TaskHandle *taskHandle, const LE_StreamServerInfo *info); 124 LE_STATUS LE_CreateStreamClient(const LoopHandle loopHandle, 125 TaskHandle *taskHandle, const LE_StreamInfo *info); 126 LE_STATUS LE_AcceptStreamClient(const LoopHandle loopHandle, 127 const TaskHandle serverTask, TaskHandle *taskHandle, const LE_StreamInfo *info); 128 LE_STATUS LE_Send(const LoopHandle loopHandle, 129 const TaskHandle taskHandle, const BufferHandle handle, uint32_t buffLen); 130 void LE_CloseStreamTask(const LoopHandle loopHandle, const TaskHandle taskHandle); 131 int LE_GetSocketFd(const TaskHandle taskHandle); 132 133 /** 134 * 异步事件服务 135 */ 136 typedef void (*LE_ProcessAsyncEvent)(const TaskHandle taskHandle, 137 uint64_t eventId, const uint8_t *buffer, uint32_t buffLen); 138 #define TASK_EVENT 0x02 139 #define TASK_ASYNC_EVENT (0x01 << 8) 140 LE_STATUS LE_CreateAsyncTask(const LoopHandle loopHandle, 141 TaskHandle *taskHandle, LE_ProcessAsyncEvent processAsyncEvent); 142 LE_STATUS LE_StartAsyncEvent(const LoopHandle loopHandle, 143 const TaskHandle taskHandle, uint64_t eventId, const uint8_t *data, uint32_t buffLen); 144 void LE_StopAsyncTask(const LoopHandle loopHandle, const TaskHandle taskHandle); 145 #ifdef STARTUP_INIT_TEST 146 void LE_DoAsyncEvent(const LoopHandle loopHandle, const TaskHandle taskHandle); 147 #endif 148 149 /** 150 * 定时器处理 151 */ 152 #define TASK_TIME 0x04 153 typedef void (*LE_ProcessTimer)(const TimerHandle taskHandle, void *context); 154 155 LE_STATUS LE_CreateTimer(const LoopHandle loopHandle, TimerHandle *timer, 156 LE_ProcessTimer processTimer, void *context); 157 LE_STATUS LE_StartTimer(const LoopHandle loopHandle, 158 const TimerHandle timer, uint64_t timeout, uint64_t repeat); 159 void LE_StopTimer(const LoopHandle loopHandle, const TimerHandle timer); 160 161 #define TASK_SIGNAL 0x08 162 typedef void (*LE_ProcessSignal)(const struct signalfd_siginfo *siginfo); 163 LE_STATUS LE_CreateSignalTask(const LoopHandle loopHandle, 164 SignalHandle *signalHandle, LE_ProcessSignal processSignal); 165 LE_STATUS LE_AddSignal(const LoopHandle loopHandle, const SignalHandle signalHandle, int signal); 166 LE_STATUS LE_RemoveSignal(const LoopHandle loopHandle, const SignalHandle signalHandle, int signal); 167 void LE_CloseSignalTask(const LoopHandle loopHandle, const SignalHandle signalHandle); 168 169 /** 170 * 监控句柄变化 171 */ 172 #define TASK_WATCHER 0x10 173 #define WATCHER_ONCE 0x0100 174 typedef void (*ProcessWatchEvent)(const WatcherHandle taskHandle, int fd, uint32_t *events, const void *context); 175 typedef struct { 176 int fd; 177 uint32_t flags; 178 uint32_t events; 179 LE_Close close; 180 ProcessWatchEvent processEvent; 181 } LE_WatchInfo; 182 LE_STATUS LE_StartWatcher(const LoopHandle loopHandle, 183 WatcherHandle *watcherHandle, const LE_WatchInfo *info, const void *context); 184 void LE_RemoveWatcher(const LoopHandle loopHandle, const WatcherHandle watcherHandle); 185 186 #ifdef __cplusplus 187 #if __cplusplus 188 } 189 #endif 190 #endif 191 #endif