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 #include "le_loop.h"
17
18 #include <errno.h>
19 #include <sys/socket.h>
20
21 #include "le_socket.h"
22 #include "le_task.h"
23
HandleSendMsg_(const LoopHandle loopHandle,const TaskHandle taskHandle,const LE_SendMessageComplete complete)24 static LE_STATUS HandleSendMsg_(const LoopHandle loopHandle,
25 const TaskHandle taskHandle, const LE_SendMessageComplete complete)
26 {
27 EventLoop *loop = (EventLoop *)loopHandle;
28 StreamTask *stream = (StreamTask *)taskHandle;
29 LE_Buffer *buffer = GetFirstBuffer(stream);
30 while (buffer) {
31 int ret = write(GetSocketFd(taskHandle), buffer->data, buffer->dataSize);
32 LE_LOGV("HandleSendMsg_ fd:%d send data size %d %d", GetSocketFd(taskHandle), buffer->dataSize, ret);
33 buffer->result = (ret == (int)buffer->dataSize) ? 0 : errno;
34 if (complete != NULL) {
35 complete(taskHandle, buffer);
36 }
37 FreeBuffer(loopHandle, stream, buffer);
38 buffer = GetFirstBuffer(stream);
39 }
40 if (IsBufferEmpty(stream)) {
41 LE_LOGV("HandleSendMsg_ fd:%d empty wait read", GetSocketFd(taskHandle));
42 loop->modEvent(loop, (const BaseTask *)taskHandle, Event_Read);
43 return LE_SUCCESS;
44 }
45 return LE_SUCCESS;
46 }
47
HandleRecvMsg_(const LoopHandle loopHandle,const TaskHandle taskHandle,const LE_RecvMessage recvMessage)48 static LE_STATUS HandleRecvMsg_(const LoopHandle loopHandle,
49 const TaskHandle taskHandle, const LE_RecvMessage recvMessage)
50 {
51 LE_STATUS status = LE_SUCCESS;
52 LE_Buffer *buffer = CreateBuffer(LOOP_DEFAULT_BUFFER);
53 int readLen = 0;
54 while (1) {
55 readLen = recv(GetSocketFd(taskHandle), buffer->data, LOOP_DEFAULT_BUFFER, 0);
56 LE_LOGV("HandleRecvMsg fd:%d read msg len %d", GetSocketFd(taskHandle), readLen);
57 if (readLen < 0) {
58 if (errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN) {
59 continue;
60 }
61 status = LE_DIS_CONNECTED;
62 break;
63 } else if (readLen == 0) {
64 // 若另一端已关闭连接则返回0,这种关闭是对方主动且正常的关闭
65 status = LE_DIS_CONNECTED;
66 break;
67 } else {
68 break;
69 }
70 }
71 if (status != LE_SUCCESS) {
72 FreeBuffer(loopHandle, (StreamTask *)taskHandle, buffer);
73 return status;
74 }
75 if (recvMessage) {
76 recvMessage(taskHandle, buffer->data, readLen);
77 }
78 FreeBuffer(loopHandle, (StreamTask *)taskHandle, buffer);
79 return status;
80 }
81
HandleStreamEvent_(const LoopHandle loopHandle,const TaskHandle handle,uint32_t oper)82 static LE_STATUS HandleStreamEvent_(const LoopHandle loopHandle, const TaskHandle handle, uint32_t oper)
83 {
84 EventLoop *loop = (EventLoop *)loopHandle;
85 StreamConnectTask *stream = (StreamConnectTask *)handle;
86 LE_LOGV("HandleStreamEvent_ fd:%d oper 0x%x", GetSocketFd(handle), oper);
87
88 LE_STATUS status = LE_SUCCESS;
89 if (LE_TEST_FLAGS(oper, Event_Write)) {
90 status = HandleSendMsg_(loopHandle, handle, stream->sendMessageComplete);
91 }
92 if (LE_TEST_FLAGS(oper, Event_Read)) {
93 status = HandleRecvMsg_(loopHandle, handle, stream->recvMessage);
94 }
95 if (status == LE_DIS_CONNECTED) {
96 loop->delEvent(loop, GetSocketFd(handle), Event_Read | Event_Write);
97 if (stream->disConnectComplete) {
98 stream->disConnectComplete(handle);
99 }
100 LE_CloseStreamTask(loopHandle, handle);
101 }
102 return status;
103 }
104
HandleClientEvent_(const LoopHandle loopHandle,const TaskHandle handle,uint32_t oper)105 static LE_STATUS HandleClientEvent_(const LoopHandle loopHandle, const TaskHandle handle, uint32_t oper)
106 {
107 StreamClientTask *client = (StreamClientTask *)handle;
108 LE_LOGV("HandleClientEvent_ fd:%d oper 0x%x", GetSocketFd(handle), oper);
109
110 LE_STATUS status = LE_SUCCESS;
111 if (LE_TEST_FLAGS(oper, Event_Write)) {
112 LE_ONLY_CHECK(!(client->connected == 0 && client->connectComplete), client->connectComplete(handle));
113 client->connected = 1;
114 status = HandleSendMsg_(loopHandle, handle, client->sendMessageComplete);
115 }
116 if (LE_TEST_FLAGS(oper, Event_Read)) {
117 status = HandleRecvMsg_(loopHandle, handle, client->recvMessage);
118 }
119 if (status == LE_DIS_CONNECTED) {
120 if (client->disConnectComplete) {
121 client->disConnectComplete(handle);
122 }
123 client->connected = 0;
124 LE_CloseStreamTask(loopHandle, handle);
125 }
126 return status;
127 }
128
HandleStreamTaskClose_(const LoopHandle loopHandle,const TaskHandle taskHandle)129 static void HandleStreamTaskClose_(const LoopHandle loopHandle, const TaskHandle taskHandle)
130 {
131 BaseTask *task = (BaseTask *)taskHandle;
132 CloseTask(loopHandle, task);
133 if (task->taskId.fd > 0) {
134 close(task->taskId.fd);
135 }
136 }
137
HandleServerEvent_(const LoopHandle loopHandle,const TaskHandle serverTask,uint32_t oper)138 static LE_STATUS HandleServerEvent_(const LoopHandle loopHandle, const TaskHandle serverTask, uint32_t oper)
139 {
140 LE_LOGV("HandleServerEvent_ fd %d oper 0x%x", GetSocketFd(serverTask), oper);
141 if (!LE_TEST_FLAGS(oper, Event_Read)) {
142 return LE_FAILURE;
143 }
144 StreamServerTask *server = (StreamServerTask *)serverTask;
145 LE_ONLY_CHECK(server->incommingConnect != NULL, return LE_SUCCESS);
146
147 int ret = server->incommingConnect(loopHandle, serverTask);
148 if (ret != LE_SUCCESS) {
149 LE_LOGE("HandleServerEvent_ fd %d do not accept socket", GetSocketFd(serverTask));
150 }
151 EventLoop *loop = (EventLoop *)loopHandle;
152 loop->modEvent(loop, (const BaseTask *)serverTask, Event_Read);
153 return LE_SUCCESS;
154 }
155
LE_CreateStreamServer(const LoopHandle loopHandle,TaskHandle * taskHandle,const LE_StreamServerInfo * info)156 LE_STATUS LE_CreateStreamServer(const LoopHandle loopHandle,
157 TaskHandle *taskHandle, const LE_StreamServerInfo *info)
158 {
159 LE_CHECK(loopHandle != NULL && taskHandle != NULL && info != NULL, return LE_INVALID_PARAM, "Invalid parameters");
160 LE_CHECK(info->server != NULL, return LE_INVALID_PARAM, "Invalid parameters server");
161 LE_CHECK(info->incommingConnect != NULL, return LE_INVALID_PARAM,
162 "Invalid parameters incommingConnect %s", info->server);
163
164 int fd = info->socketId;
165 if (info->socketId <= 0) {
166 fd = CreateSocket(info->baseInfo.flags, info->server);
167 LE_CHECK(fd > 0, return LE_FAILURE, "Failed to create socket %s", info->server);
168 }
169
170 EventLoop *loop = (EventLoop *)loopHandle;
171 StreamServerTask *task = (StreamServerTask *)CreateTask(loopHandle, fd, &info->baseInfo,
172 sizeof(StreamServerTask) + strlen(info->server) + 1);
173 LE_CHECK(task != NULL, close(fd);
174 return LE_NO_MEMORY, "Failed to create task");
175 task->base.handleEvent = HandleServerEvent_;
176 task->base.innerClose = HandleStreamTaskClose_;
177 task->incommingConnect = info->incommingConnect;
178 loop->addEvent(loop, (const BaseTask *)task, Event_Read);
179 int ret = memcpy_s(task->server, strlen(info->server) + 1, info->server, strlen(info->server) + 1);
180 LE_CHECK(ret == 0, return LE_FAILURE, "Failed to copy server name %s", info->server);
181 *taskHandle = (TaskHandle)task;
182 return LE_SUCCESS;
183 }
184
LE_CreateStreamClient(const LoopHandle loopHandle,TaskHandle * taskHandle,const LE_StreamInfo * info)185 LE_STATUS LE_CreateStreamClient(const LoopHandle loopHandle,
186 TaskHandle *taskHandle, const LE_StreamInfo *info)
187 {
188 LE_CHECK(loopHandle != NULL && taskHandle != NULL && info != NULL, return LE_INVALID_PARAM, "Invalid parameters");
189 LE_CHECK(info->recvMessage != NULL, return LE_FAILURE, "Invalid parameters recvMessage %s", info->server);
190
191 int fd = CreateSocket(info->baseInfo.flags, info->server);
192 LE_CHECK(fd > 0, return LE_FAILURE, "Failed to create socket %s", info->server);
193
194 StreamClientTask *task = (StreamClientTask *)CreateTask(loopHandle, fd, &info->baseInfo, sizeof(StreamClientTask));
195 LE_CHECK(task != NULL, close(fd);
196 return LE_NO_MEMORY, "Failed to create task");
197 task->stream.base.handleEvent = HandleClientEvent_;
198 task->stream.base.innerClose = HandleStreamTaskClose_;
199 OH_ListInit(&task->stream.buffHead);
200 LoopMutexInit(&task->stream.mutex);
201
202 task->connectComplete = info->connectComplete;
203 task->sendMessageComplete = info->sendMessageComplete;
204 task->recvMessage = info->recvMessage;
205 task->disConnectComplete = info->disConnectComplete;
206 EventLoop *loop = (EventLoop *)loopHandle;
207 loop->addEvent(loop, (const BaseTask *)task, Event_Read);
208 *taskHandle = (TaskHandle)task;
209 return LE_SUCCESS;
210 }
211
LE_AcceptStreamClient(const LoopHandle loopHandle,const TaskHandle server,TaskHandle * taskHandle,const LE_StreamInfo * info)212 LE_STATUS LE_AcceptStreamClient(const LoopHandle loopHandle, const TaskHandle server,
213 TaskHandle *taskHandle, const LE_StreamInfo *info)
214 {
215 LE_CHECK(loopHandle != NULL && info != NULL, return LE_INVALID_PARAM, "Invalid parameters");
216 LE_CHECK(server != NULL && taskHandle != NULL, return LE_INVALID_PARAM, "Invalid parameters");
217 LE_CHECK(info->recvMessage != NULL, return LE_INVALID_PARAM, "Invalid parameters recvMessage");
218 int fd = -1;
219 if ((info->baseInfo.flags & TASK_TEST) != TASK_TEST) {
220 fd = AcceptSocket(GetSocketFd(server), info->baseInfo.flags);
221 LE_CHECK(fd > 0, return LE_FAILURE, "Failed to accept socket %d", GetSocketFd(server));
222 }
223 StreamConnectTask *task = (StreamConnectTask *)CreateTask(
224 loopHandle, fd, &info->baseInfo, sizeof(StreamConnectTask));
225 LE_CHECK(task != NULL, close(fd);
226 return LE_NO_MEMORY, "Failed to create task");
227 task->stream.base.handleEvent = HandleStreamEvent_;
228 task->stream.base.innerClose = HandleStreamTaskClose_;
229 task->disConnectComplete = info->disConnectComplete;
230 task->sendMessageComplete = info->sendMessageComplete;
231 task->recvMessage = info->recvMessage;
232 task->serverTask = (StreamServerTask *)server;
233 OH_ListInit(&task->stream.buffHead);
234 LoopMutexInit(&task->stream.mutex);
235 if ((info->baseInfo.flags & TASK_TEST) != TASK_TEST) {
236 EventLoop *loop = (EventLoop *)loopHandle;
237 loop->addEvent(loop, (const BaseTask *)task, Event_Read);
238 }
239 *taskHandle = (TaskHandle)task;
240 return 0;
241 }
242
LE_CloseStreamTask(const LoopHandle loopHandle,const TaskHandle taskHandle)243 void LE_CloseStreamTask(const LoopHandle loopHandle, const TaskHandle taskHandle)
244 {
245 LE_CHECK(loopHandle != NULL && taskHandle != NULL, return, "Invalid parameters");
246 LE_CloseTask(loopHandle, taskHandle);
247 }
248
LE_GetSocketFd(const TaskHandle taskHandle)249 int LE_GetSocketFd(const TaskHandle taskHandle)
250 {
251 LE_CHECK(taskHandle != NULL, return -1, "Invalid parameters");
252 return GetSocketFd(taskHandle);
253 }