--- libaitio/src/aitio.c 2011/11/02 13:13:48 1.7.2.1 +++ libaitio/src/aitio.c 2011/11/02 16:10:25 1.7.2.2 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitio.c,v 1.7.2.1 2011/11/02 13:13:48 misho Exp $ +* $Id: aitio.c,v 1.7.2.2 2011/11/02 16:10:25 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -592,18 +592,26 @@ ioCreatePIDFile(const char *csName, int ifExists) * @csFile = file for send * @sendLen = bytes to send, if 0 send all data * @offset = start file offset + * @sndbuf = SO_SNDBUF value, if 0 use default * return: 0 error, >0 ok, sended bytes */ size_t -ioSendFile(int s, const char *csFile, size_t sendLen, off_t offset) +ioSendFile(int s, const char *csFile, size_t sendLen, off_t offset, int sndbuf) { void *addr; int fd; - size_t len; + size_t len = 0; + register size_t off = 0; if (!csFile) return 0; + if (sndbuf) + if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof sndbuf) == -1) { + LOGERR; + return 0; + } + fd = open(csFile, O_RDONLY); if (fd == -1) { LOGERR; @@ -625,12 +633,14 @@ ioSendFile(int s, const char *csFile, size_t sendLen, } else close(fd); - len = write(s, addr, sendLen); + while (off < sendLen && (len = write(s, addr + off, sendLen - off)) != -1) + off += len; if (len == -1) { LOGERR; munmap(addr, sendLen); return 0; - } + } else + len = off; if (len != sendLen) { io_SetErr(ECANCELED, "Different sizes - request %u bytes, actually sended %u bytes\n", @@ -648,20 +658,29 @@ ioSendFile(int s, const char *csFile, size_t sendLen, * @csFile = file for receive * @recvLen = receive bytes * @over = overwrite file if exists with mode like 0644 + * @rcvbuf = SO_RCVBUF value, if 0 use default * return: 0 error, >0 ok, received bytes */ size_t -ioRecvFile(int s, const char *csFile, size_t recvLen, int over) +ioRecvFile(int s, const char *csFile, size_t recvLen, int over, int rcvbuf) { void *addr; int fd; - size_t len; + size_t len = 0; + register size_t off = 0; + struct pollfd pfd = { s, POLLIN | POLLPRI, 0 }; if (!csFile || !recvLen) return 0; if (!over && !access(csFile, F_OK)) return 0; + if (rcvbuf) + if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof rcvbuf) == -1) { + LOGERR; + return 0; + } + fd = open(csFile, O_WRONLY | O_CREAT | O_TRUNC, over); if (fd == -1) { LOGERR; @@ -689,13 +708,16 @@ ioRecvFile(int s, const char *csFile, size_t recvLen, } else close(fd); - len = read(s, addr, recvLen); + while (off < recvLen && poll(&pfd, 1, RECV_TIMEOUT) != -1) + while (off < recvLen && (len = read(s, addr + off, recvLen - off)) != -1) + off += len; if (len == -1) { LOGERR; munmap(addr, recvLen); unlink(csFile); return 0; - } + } else + len = off; if (len != recvLen) io_SetErr(EAGAIN, "Different sizes - request %u bytes, actually received %u bytes\n",