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 41 extern "C" { 42 #endif 43 #endif 44 45 typedef 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 54 typedef LE_STATUS (*HandleTaskEvent)(const LoopHandle loop, const TaskHandle task, uint32_t oper); 55 typedef void (*HandleTaskClose)(const LoopHandle loop, const TaskHandle task); 56 typedef void (*DumpTaskInfo)(const TaskHandle task); 57 #define TASKINFO \ 58 uint32_t flags; \ 59 union { \ 60 int fd; \ 61 } taskId 62 63 typedef struct { 64 TASKINFO; 65 } TaskId; 66 67 typedef 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 78 typedef struct { 79 BaseTask base; 80 LE_IncommingConnect incommingConnect; 81 char server[0]; 82 } StreamServerTask; 83 84 typedef 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 94 typedef struct { 95 StreamTask stream; 96 StreamServerTask *serverTask; 97 LE_SendMessageComplete sendMessageComplete; 98 LE_RecvMessage recvMessage; 99 LE_DisConnectComplete disConnectComplete; 100 } StreamConnectTask; 101 102 typedef struct { 103 StreamTask stream; 104 LE_DisConnectComplete disConnectComplete; 105 LE_ConnectComplete connectComplete; 106 LE_SendMessageComplete sendMessageComplete; 107 LE_RecvMessage recvMessage; 108 uint32_t connected : 1; 109 char server[0]; 110 } StreamClientTask; 111 112 typedef struct { 113 StreamTask stream; 114 LE_ProcessAsyncEvent processAsyncEvent; 115 } AsyncEventTask; 116 117 typedef struct { 118 BaseTask base; 119 uint32_t events; 120 ProcessWatchEvent processEvent; 121 } WatcherTask; 122 123 LE_Buffer *CreateBuffer(uint32_t bufferSize); 124 LE_Buffer *GetNextBuffer(StreamTask *task, const LE_Buffer *next); 125 LE_Buffer *GetFirstBuffer(StreamTask *task); 126 int IsBufferEmpty(StreamTask *task); 127 128 void FreeBuffer(const LoopHandle loop, StreamTask *task, LE_Buffer *buffer); 129 void AddBuffer(StreamTask *task, LE_Buffer *buffer); 130 131 BaseTask *CreateTask(const LoopHandle loopHandle, int fd, const LE_BaseInfo *info, uint32_t size); 132 void CloseTask(const LoopHandle loopHandle, BaseTask *task); 133 int GetSocketFd(const TaskHandle task); 134 int CheckTaskFlags(const BaseTask *task, uint32_t flags); 135 136 #ifdef __cplusplus 137 #if __cplusplus 138 } 139 #endif 140 #endif 141 142 #endif 143