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_TASK_H 17#define LOOP_TASK_H 18#include <stdlib.h> 19 20#include "init_hashmap.h" 21#include "le_utils.h" 22#include "list.h" 23#include "loop_event.h" 24 25#ifndef LOOP_EVENT_USE_MUTEX 26#define LoopMutexInit(x) (void)(x) 27#define LoopMutexLock(x) (void)(x) 28#define LoopMutexUnlock(x) (void)(x) 29#define LoopMutexDestroy(x) (void)(x) 30#else 31#include <pthread.h> 32#define LoopMutex pthread_mutex_t 33#define LoopMutexInit(x) pthread_mutex_init(x, NULL) 34#define LoopMutexLock(x) pthread_mutex_lock(x) 35#define LoopMutexUnlock(x) pthread_mutex_unlock(x) 36#define LoopMutexDestroy(x) pthread_mutex_destroy(x) 37#endif 38 39#ifdef __cplusplus 40#if __cplusplus 41extern "C" { 42#endif 43#endif 44 45typedef struct { 46 ListNode node; 47 uint32_t buffSize; 48 uint32_t dataSize; 49 int32_t result; 50 uint8_t data[0]; 51} LE_Buffer; 52 53#define TASK_FLAGS_INVALID 0x80000000 54typedef LE_STATUS (*HandleTaskEvent)(const LoopHandle loop, const TaskHandle task, uint32_t oper); 55typedef void (*HandleTaskClose)(const LoopHandle loop, const TaskHandle task); 56typedef void (*DumpTaskInfo)(const TaskHandle task); 57#define TASKINFO \ 58 uint32_t flags; \ 59 union { \ 60 int fd; \ 61 } taskId 62 63typedef struct { 64 TASKINFO; 65} TaskId; 66 67typedef struct LiteTask_ { 68 TASKINFO; 69 HashNode hashNode; 70 LE_Close close; 71 DumpTaskInfo dumpTaskInfo; 72 HandleTaskEvent handleEvent; 73 HandleTaskClose innerClose; 74 uint16_t userDataOffset; 75 uint16_t userDataSize; 76} BaseTask; 77 78typedef struct { 79 BaseTask base; 80 LE_IncommingConnect incommingConnect; 81 char server[0]; 82} StreamServerTask; 83 84typedef struct { 85 BaseTask base; 86#ifdef LOOP_EVENT_USE_MUTEX 87 LoopMutex mutex; 88#else 89 char mutex; 90#endif 91 ListHead buffHead; 92} StreamTask; 93 94typedef struct { 95 StreamTask stream; 96 StreamServerTask *serverTask; 97 LE_SendMessageComplete sendMessageComplete; 98 LE_RecvMessage recvMessage; 99 LE_DisConnectComplete disConnectComplete; 100 LE_HandleRecvMsg handleRecvMsg; 101} StreamConnectTask; 102 103typedef struct { 104 StreamTask stream; 105 LE_DisConnectComplete disConnectComplete; 106 LE_ConnectComplete connectComplete; 107 LE_SendMessageComplete sendMessageComplete; 108 LE_RecvMessage recvMessage; 109 LE_HandleRecvMsg handleRecvMsg; 110 uint32_t connected : 1; 111 char server[0]; 112} StreamClientTask; 113 114typedef struct { 115 StreamTask stream; 116 LE_ProcessAsyncEvent processAsyncEvent; 117} AsyncEventTask; 118 119typedef struct { 120 BaseTask base; 121 uint32_t events; 122 ProcessWatchEvent processEvent; 123} WatcherTask; 124 125LE_Buffer *CreateBuffer(uint32_t bufferSize); 126LE_Buffer *GetNextBuffer(StreamTask *task, const LE_Buffer *next); 127LE_Buffer *GetFirstBuffer(StreamTask *task); 128int IsBufferEmpty(StreamTask *task); 129 130void FreeBuffer(const LoopHandle loop, StreamTask *task, LE_Buffer *buffer); 131void AddBuffer(StreamTask *task, LE_Buffer *buffer); 132 133BaseTask *CreateTask(const LoopHandle loopHandle, int fd, const LE_BaseInfo *info, uint32_t size); 134void CloseTask(const LoopHandle loopHandle, BaseTask *task); 135int GetSocketFd(const TaskHandle task); 136int CheckTaskFlags(const BaseTask *task, uint32_t flags); 137 138#ifdef __cplusplus 139#if __cplusplus 140} 141#endif 142#endif 143 144#endif 145