le_task.h 3.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
/*
 * 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_TASK_H
#define LOOP_TASK_H
#include <stdlib.h>

#include "init_hashmap.h"
#include "le_utils.h"
#include "list.h"
#include "loop_event.h"

#ifndef LOOP_EVENT_USE_MUTEX
M
Mupceet 已提交
26 27 28
#define LoopMutexInit(x) (void)(x)
#define LoopMutexLock(x) (void)(x)
#define LoopMutexUnlock(x) (void)(x)
X
xionglei6 已提交
29 30 31 32 33 34 35 36
#else
#include <pthread.h>
#define LoopMutex pthread_mutex_t
#define LoopMutexInit(x) pthread_mutex_init(x, NULL)
#define LoopMutexLock(x) pthread_mutex_lock(x)
#define LoopMutexUnlock(x) pthread_mutex_unlock(x)
#endif

X
xlei1030 已提交
37 38 39 40 41 42
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif

X
xionglei6 已提交
43 44 45 46 47 48 49
typedef struct {
    ListNode node;
    uint32_t buffSize;
    uint32_t dataSize;
    uint8_t data[0];
} LE_Buffer;

M
Mupceet 已提交
50
#define TASK_FLAGS_INVALID 0x80000000
X
xionglei6 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
typedef LE_STATUS (*HandleTaskEvent)(const LoopHandle loop, const TaskHandle task, uint32_t oper);
typedef void (*HandleTaskClose)(const LoopHandle loop, const TaskHandle task);
#define TASKINFO \
    uint32_t flags; \
    union { \
        int fd; \
    } taskId

typedef struct {
    TASKINFO;
} TaskId;

typedef struct LiteTask_ {
    TASKINFO;
    HashNode hashNode;
    LE_Close close;
    HandleTaskEvent handleEvent;
    HandleTaskClose innerClose;
    uint16_t userDataOffset;
    uint16_t userDataSize;
} BaseTask;

typedef struct {
    BaseTask base;
L
laiguizhong 已提交
75
    LE_IncommingConnect incommingConnect;
X
xionglei6 已提交
76 77 78 79 80
    char server[0];
} StreamServerTask;

typedef struct {
    BaseTask base;
M
Mupceet 已提交
81
#ifdef LOOP_EVENT_USE_MUTEX
X
xionglei6 已提交
82
    LoopMutex mutex;
M
Mupceet 已提交
83 84 85
#else
    char mutex;
#endif
X
xionglei6 已提交
86 87 88 89 90 91 92 93
    ListHead buffHead;
} StreamTask;

typedef struct {
    StreamTask stream;
    StreamServerTask *serverTask;
    LE_SendMessageComplete sendMessageComplete;
    LE_RecvMessage recvMessage;
L
laiguizhong 已提交
94
    LE_DisConnectComplete disConnectComplete;
X
xionglei6 已提交
95 96 97 98
} StreamConnectTask;

typedef struct {
    StreamTask stream;
L
laiguizhong 已提交
99 100
    LE_DisConnectComplete disConnectComplete;
    LE_ConnectComplete connectComplete;
X
xionglei6 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113
    LE_SendMessageComplete sendMessageComplete;
    LE_RecvMessage recvMessage;
    uint32_t connected : 1;
    char server[0];
} StreamClientTask;

typedef struct {
    StreamTask stream;
    LE_ProcessAsyncEvent processAsyncEvent;
} AsyncEventTask;

typedef struct {
    BaseTask base;
M
Mupceet 已提交
114
    uint32_t events;
X
xionglei6 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    ProcessWatchEvent processEvent;
} WatcherTask;

LE_Buffer *CreateBuffer(uint32_t bufferSize);
LE_Buffer *GetNextBuffer(StreamTask *task, const LE_Buffer *next);
LE_Buffer *GetFirstBuffer(StreamTask *task);
int IsBufferEmpty(StreamTask *task);

void FreeBuffer(const LoopHandle loop, StreamTask *task, LE_Buffer *buffer);
void AddBuffer(StreamTask *task, LE_Buffer *buffer);

BaseTask *CreateTask(const LoopHandle loopHandle, int fd, const LE_BaseInfo *info, uint32_t size);
void CloseTask(const LoopHandle loopHandle, BaseTask *task);
int GetSocketFd(const TaskHandle task);
int CheckTaskFlags(const BaseTask *task, uint32_t flags);
X
xlei1030 已提交
130 131 132 133 134 135 136

#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif

M
Mupceet 已提交
137
#endif