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