From 3e65dd03dd8b1c8ada1b1c3b39afce0277083483 Mon Sep 17 00:00:00 2001 From: zhongning5 Date: Wed, 6 Sep 2023 13:46:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9appssawn=20socket=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E6=B5=81=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhongning5 Change-Id: I093ffde2662f067c6487c8be6af136e45328d98f --- services/loopevent/socket/le_socket.c | 23 +++++++++++++++++++++++ services/loopevent/socket/le_socket.h | 2 ++ services/loopevent/task/le_streamtask.c | 6 +++++- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/services/loopevent/socket/le_socket.c b/services/loopevent/socket/le_socket.c index 0e27e7268..d3533c2c0 100644 --- a/services/loopevent/socket/le_socket.c +++ b/services/loopevent/socket/le_socket.c @@ -196,3 +196,26 @@ int AcceptSocket(int fd, int flags) SetNoBlock(clientFd); return clientFd; } + +INIT_LOCAL_API +int listenSocket(int fd, int flags, const char *server) +{ + int type = flags & 0x0000ff00; + LE_LOGV("listenSocket flags %x type %x server %s", flags, type, server); + SetNoBlock(fd); + if (!LE_TEST_FLAGS(flags, TASK_SERVER)) { + return 0; + } + if (type == TASK_TCP) { + int ret = listen(fd, LOOP_MAX_CLIENT); + LE_CHECK(ret >= 0, close(fd); + return ret, "Failed to listen socket"); + } else if (type == TASK_PIPE) { + int ret = listen(fd, LOOP_MAX_CLIENT); + LE_CHECK(ret >= 0, close(fd); + return ret, "Failed to listen socket error: %d", errno); + ret = chmod(server, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); + LE_CHECK(ret == 0, return -1, "Failed to chmod %s, err %d. ", server, errno); + } + return 0; +} diff --git a/services/loopevent/socket/le_socket.h b/services/loopevent/socket/le_socket.h index f4c0a10d6..c5e0054d1 100644 --- a/services/loopevent/socket/le_socket.h +++ b/services/loopevent/socket/le_socket.h @@ -28,6 +28,8 @@ INIT_LOCAL_API int CreateSocket(int flags, const char *server); INIT_LOCAL_API int AcceptSocket(int fd, int flags); +INIT_LOCAL_API +int listenSocket(int fd, int flags, const char *server); #ifdef __cplusplus #if __cplusplus diff --git a/services/loopevent/task/le_streamtask.c b/services/loopevent/task/le_streamtask.c index 95a651a0d..4707c1c8b 100644 --- a/services/loopevent/task/le_streamtask.c +++ b/services/loopevent/task/le_streamtask.c @@ -163,9 +163,13 @@ LE_STATUS LE_CreateStreamServer(const LoopHandle loopHandle, "Invalid parameters incommingConnect %s", info->server); int fd = info->socketId; + int ret = 0; if (info->socketId <= 0) { fd = CreateSocket(info->baseInfo.flags, info->server); LE_CHECK(fd > 0, return LE_FAILURE, "Failed to create socket %s", info->server); + } else { + ret = listenSocket(fd, info->baseInfo.flags, info->server); + LE_CHECK(ret == 0, return LE_FAILURE, "Failed to listen socket %s", info->server); } EventLoop *loop = (EventLoop *)loopHandle; @@ -177,7 +181,7 @@ LE_STATUS LE_CreateStreamServer(const LoopHandle loopHandle, task->base.innerClose = HandleStreamTaskClose_; task->incommingConnect = info->incommingConnect; loop->addEvent(loop, (const BaseTask *)task, Event_Read); - int ret = memcpy_s(task->server, strlen(info->server) + 1, info->server, strlen(info->server) + 1); + ret = memcpy_s(task->server, strlen(info->server) + 1, info->server, strlen(info->server) + 1); LE_CHECK(ret == 0, return LE_FAILURE, "Failed to copy server name %s", info->server); *taskHandle = (TaskHandle)task; return LE_SUCCESS;