From f8af8b7295bdd851ffc4f602cbec68a44814e4c5 Mon Sep 17 00:00:00 2001 From: Vincent Wei Date: Mon, 6 Jan 2020 15:02:48 +0800 Subject: [PATCH] Implement sock_sendmsg_t and sock_recvmsg_t. --- src/misc/sockio.c | 92 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 73 insertions(+), 19 deletions(-) diff --git a/src/misc/sockio.c b/src/misc/sockio.c index d8417f36..12885d92 100644 --- a/src/misc/sockio.c +++ b/src/misc/sockio.c @@ -11,35 +11,35 @@ // ////////////////////////////////////////////////////////////////////////////// /* - * This file is part of MiniGUI, a mature cross-platform windowing + * This file is part of MiniGUI, a mature cross-platform windowing * and Graphics User Interface (GUI) support system for embedded systems * and smart IoT devices. - * + * * Copyright (C) 2002~2018, Beijing FMSoft Technologies Co., Ltd. * Copyright (C) 1998~2002, WEI Yongming - * + * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with this program. If not, see . - * + * * Or, - * + * * As this program is a library, any link to this program must follow * GNU General Public License version 3 (GPLv3). If you cannot accept * GPLv3, you need to be licensed from FMSoft. - * + * * If you have got a commercial license of this program, please use it * under the terms and conditions of the commercial license. - * + * * For more information about the commercial license, please refer to * . */ @@ -70,13 +70,14 @@ #include "sharedres.h" #include "sockio.h" -int sock_write_t (int fd, const void* buff, int count, unsigned int timeout) +ssize_t sock_write_t (int fd, const void* buff, size_t count, DWORD timeout) { const void* pts = buff; - int status = 0, n; - unsigned int start_tick = SHAREDRES_TIMER_COUNTER; + ssize_t status = 0, n; + DWORD start_tick = SHAREDRES_TIMER_COUNTER; - if (count < 0) return SOCKERR_OK; + if (count == 0) + return SOCKERR_OK; while (status != count) { n = write (fd, pts + status, count - status); @@ -84,8 +85,7 @@ int sock_write_t (int fd, const void* buff, int count, unsigned int timeout) if (errno == EPIPE) return SOCKERR_CLOSED; else if (errno == EINTR) { - if (timeout && (SHAREDRES_TIMER_COUNTER - > start_tick + timeout)) { + if (timeout && (SHAREDRES_TIMER_COUNTER > start_tick + timeout)) { return SOCKERR_TIMEOUT; } continue; @@ -99,11 +99,11 @@ int sock_write_t (int fd, const void* buff, int count, unsigned int timeout) return status; } -int sock_read_t (int fd, void* buff, int count, unsigned int timeout) +ssize_t sock_read_t (int fd, void* buff, size_t count, DWORD timeout) { void* pts = buff; - int status = 0, n; - unsigned int start_tick = SHAREDRES_TIMER_COUNTER; + ssize_t status = 0, n; + DWORD start_tick = SHAREDRES_TIMER_COUNTER; if (count <= 0) return SOCKERR_OK; while (status != count) { @@ -127,4 +127,58 @@ int sock_read_t (int fd, void* buff, int count, unsigned int timeout) return status; } -#endif +ssize_t sock_sendmsg_t (int fd, const struct msghdr *msg, int flags, DWORD timeout) +{ + ssize_t n; + DWORD start_tick = SHAREDRES_TIMER_COUNTER; + + if (msg == NULL) + return SOCKERR_OK; + + do { + n = sendmsg (fd, msg, flags); + + if (n < 0) { + if (errno == EPIPE) + return SOCKERR_CLOSED; + else if (errno == EINTR) { + if (timeout && (SHAREDRES_TIMER_COUNTER > start_tick + timeout)) { + return SOCKERR_TIMEOUT; + } + } + else + return SOCKERR_IO; + } + } while (n < 0); + + return n; +} + +ssize_t sock_recvmsg_t (int fd, struct msghdr *msg, int flags, DWORD timeout) +{ + ssize_t n; + DWORD start_tick = SHAREDRES_TIMER_COUNTER; + + if (msg == NULL) + return SOCKERR_OK; + + do { + n = recvmsg (fd, msg, flags); + + if (n < 0) { + if (errno == EPIPE) + return SOCKERR_CLOSED; + else if (errno == EINTR) { + if (timeout && (SHAREDRES_TIMER_COUNTER > start_tick + timeout)) { + return SOCKERR_TIMEOUT; + } + } + else + return SOCKERR_IO; + } + } while (n < 0); + + return n; +} + +#endif