loop_event.h 6.5 KB
Newer Older
X
xionglei6 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef LOOP_EVENT_H
#define LOOP_EVENT_H
#include <stdint.h>
#include <stdlib.h>
#include <sys/signalfd.h>

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif

// 配置
#define LOOP_EVENT_USE_EPOLL 1
#define LOOP_DEFAULT_BUFFER 1024
#define LOOP_MAX_BUFFER (1024 * 64)

#define LOOP_MAX_CLIENT 1024
#define LOOP_MAX_SOCKET 1024
#define DEFAULT_TIMEOUT 1000

typedef enum {
    Event_Read = (0x0001),
    Event_Write = (0x0002),
    Event_Error = (0x0004),
    Event_Free = (0x0008),
    Event_Timeout = (0x0010),
    Event_Signal = (0x0020),
} EventOper;

typedef enum {
    LE_SUCCESS = 0,
    LE_FAILURE = 10000,
    LE_INVALID_PARAM,
    LE_NO_MEMORY,
    LE_DIS_CONNECTED
} LE_STATUS;

typedef struct {
    uint32_t flags;
} LoopBase;

typedef LoopBase *LoopHandle;
typedef LoopBase *TaskHandle;
typedef LoopBase *TimerHandle;
typedef LoopBase *SignalHandle;
typedef LoopBase *WatcherHandle;
typedef void *BufferHandle;

LoopHandle LE_GetDefaultLoop(void);
LE_STATUS LE_CreateLoop(LoopHandle *loopHandle);
void LE_RunLoop(const LoopHandle loopHandle);
void LE_StopLoop(const LoopHandle loopHandle);
void LE_CloseTask(const LoopHandle loopHandle, const TaskHandle taskHandle);

/**
 * 申请一个buffer,用于消息的发送和接收
 */
BufferHandle LE_CreateBuffer(const LoopHandle loopHandle, uint32_t bufferSize);
void LE_FreeBuffer(const LoopHandle loopHandle, const TaskHandle taskHandle, const BufferHandle handle);
uint8_t *LE_GetBufferInfo(const BufferHandle handle, uint32_t *dataSize, uint32_t *buffSize);
void *LE_GetUserData(const TaskHandle handle);

typedef void (*LE_Close)(const TaskHandle taskHandle);
typedef struct {
    uint32_t flags;
    LE_Close close;
    uint16_t userDataSize;
} LE_BaseInfo;

/**
 * 数据流服务,可以指定使用pipe或者普通的tcp
 */
#define TASK_STREAM 0x01
#define TASK_TCP (0x01 << 8)
#define TASK_PIPE (0x02 << 8)
#define TASK_SERVER (0x01 << 16)
#define TASK_CONNECT (0x02 << 16)
#define TASK_TEST (0x01 << 24)
typedef void (*LE_DisConntectComplete)(const TaskHandle client);
typedef void (*LE_ConntectComplete)(const TaskHandle client);
typedef void (*LE_SendMessageComplete)(const TaskHandle taskHandle, BufferHandle handle);
typedef void (*LE_RecvMessage)(const TaskHandle taskHandle, const uint8_t *buffer, uint32_t buffLen);
typedef int (*LE_IncommingConntect)(const LoopHandle loopHandle, const TaskHandle serverTask);
typedef struct {
    LE_BaseInfo baseInfo;
    char *server;
    LE_DisConntectComplete disConntectComplete;
    LE_IncommingConntect incommingConntect;
    LE_SendMessageComplete sendMessageComplete;
    LE_RecvMessage recvMessage;
} LE_StreamServerInfo;

typedef struct {
    LE_BaseInfo baseInfo;
    char *server;
    LE_DisConntectComplete disConntectComplete;
    LE_ConntectComplete connectComplete;
    LE_SendMessageComplete sendMessageComplete;
    LE_RecvMessage recvMessage;
} LE_StreamInfo;

LE_STATUS LE_CreateStreamServer(const LoopHandle loopHandle,
    TaskHandle *taskHandle, const LE_StreamServerInfo *info);
LE_STATUS LE_CreateStreamClient(const LoopHandle loopHandle,
    TaskHandle *taskHandle, const LE_StreamInfo *info);
LE_STATUS LE_AcceptStreamClient(const LoopHandle loopHandle,
    const TaskHandle serverTask, TaskHandle *taskHandle, const LE_StreamInfo *info);
LE_STATUS LE_Send(const LoopHandle loopHandle,
    const TaskHandle taskHandle, const BufferHandle handle, uint32_t buffLen);
void LE_CloseStreamTask(const LoopHandle loopHandle, const TaskHandle taskHandle);
127
int LE_GetSocketFd(const TaskHandle taskHandle);
X
xionglei6 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

/**
 * 异步事件服务
 */
typedef void (*LE_ProcessAsyncEvent)(const TaskHandle taskHandle,
    uint64_t eventId, const uint8_t *buffer, uint32_t buffLen);
#define TASK_EVENT 0x02
#define TASK_ASYNC_EVENT (0x01 << 8)
LE_STATUS LE_CreateAsyncTask(const LoopHandle loopHandle,
    TaskHandle *taskHandle, LE_ProcessAsyncEvent processAsyncEvent);
LE_STATUS LE_StartAsyncEvent(const LoopHandle loopHandle,
    const TaskHandle taskHandle, uint64_t eventId, const uint8_t *data, uint32_t buffLen);
void LE_StopAsyncTask(const LoopHandle loopHandle, const TaskHandle taskHandle);
#ifdef STARTUP_INIT_TEST
void LE_DoAsyncEvent(const LoopHandle loopHandle, const TaskHandle taskHandle);
#endif

/**
 * 定时器处理
 */
#define TASK_TIME 0x04
typedef void (*LE_ProcessTimer)(const TimerHandle taskHandle, void *context);

LE_STATUS LE_CreateTimer(const LoopHandle loopHandle, TimerHandle *timer,
    LE_ProcessTimer processTimer, void *context);
LE_STATUS LE_StartTimer(const LoopHandle loopHandle,
    const TimerHandle timer, uint64_t timeout, uint64_t repeat);
void LE_StopTimer(const LoopHandle loopHandle, const TimerHandle timer);

#define TASK_SIGNAL 0x08
typedef void (*LE_ProcessSignal)(const struct signalfd_siginfo *siginfo);
LE_STATUS LE_CreateSignalTask(const LoopHandle loopHandle,
    SignalHandle *signalHandle, LE_ProcessSignal processSignal);
LE_STATUS LE_AddSignal(const LoopHandle loopHandle, const SignalHandle signalHandle, int signal);
LE_STATUS LE_RemoveSignal(const LoopHandle loopHandle, const SignalHandle signalHandle, int signal);
void LE_CloseSignalTask(const LoopHandle loopHandle, const SignalHandle signalHandle);

/**
 * 监控句柄变化
 */
X
xionglei6 已提交
168
#define TASK_WATCHER 0x10
X
xionglei6 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
#define WATCHER_ONCE  0x0100
typedef void (*ProcessWatchEvent)(const WatcherHandle taskHandle, int fd, uint32_t *events, const void *context);
typedef struct {
    int fd;
    uint32_t flags;
    uint32_t events;
    LE_Close close;
    ProcessWatchEvent processEvent;
} LE_WatchInfo;
LE_STATUS LE_StartWatcher(const LoopHandle loopHandle,
    WatcherHandle *watcherHandle, const LE_WatchInfo *info, const void *context);
void LE_RemoveWatcher(const LoopHandle loopHandle, const WatcherHandle watcherHandle);

#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif
#endif