From 561818f2308e1f1325a944755cb126cf74d6f91a Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Mon, 4 Nov 1996 15:43:25 +0000 Subject: Wow! Real buffering at last! svn path=/trunk/; revision=477 --- socket.c | 69 ++++++++++++++++++---------------------------------------------- 1 file changed, 19 insertions(+), 50 deletions(-) (limited to 'socket.c') diff --git a/socket.c b/socket.c index 3dcdccaa..c4ef1513 100644 --- a/socket.c +++ b/socket.c @@ -24,6 +24,8 @@ #endif #include "socket.h" +#define INTERNAL_BUFSIZE 2048 + FILE *Socket(host, clientPort) char *host; int clientPort; @@ -32,7 +34,8 @@ int clientPort; unsigned long inaddr; struct sockaddr_in ad; struct hostent *hp; - + FILE *sockfp; + memset(&ad, 0, sizeof(ad)); ad.sin_family = AF_INET; @@ -58,10 +61,11 @@ int clientPort; return (FILE *)NULL; } - return fdopen(sock, "r+"); + sockfp = fdopen(sock, "r+"); + setvbuf(sockfp, NULL, _IOLBF, INTERNAL_BUFSIZE); + return sockfp; } - #if defined(HAVE_STDARG_H) int SockPrintf(FILE *sockfp, char* format, ...) { @@ -86,45 +90,12 @@ va_dcl { } -/* - * FIXME: This needs to be recoded to use stdio, if that's possible. - * - * If you think these functions are too slow and inefficient, you're - * absolutely right. I wish I could figure out what to do about it. - * The ancestral popclient used static buffering here to cut down on the - * number of read(2) calls, but we can't do that because we can have - * two or more sockets open at a time. - * - * The right thing to do would be to use stdio for internal per-socket - * buffering here (which is why Socket() returns a file pointer) but - * this causes mysterious lossage. In case someone ever finds a way - * around this, a note on Carl Harris's original implementation said: - * - * Size of buffer for internal buffering read function - * don't increase beyond the maximum atomic read/write size for - * your sockets, or you'll take a potentially huge performance hit - * - * #define INTERNAL_BUFSIZE 2048 - * - */ - int SockWrite(buf,len,sockfp) char *buf; int len; FILE *sockfp; { - int n, wrlen = 0; - - while (len) - { - n = write(fileno(sockfp), buf, len); - if (n <= 0) - return -1; - len -= n; - wrlen += n; - buf += n; - } - return wrlen; + return(fputs(buf, sockfp)); } int SockGets(buf, len, sockfp) @@ -132,21 +103,19 @@ char *buf; int len; FILE *sockfp; { - int rdlen = 0; - - while (--len) + if (fgets(buf, len, sockfp) == (char *)NULL) + return(-1); + else { - if (read(fileno(sockfp), buf, 1) != 1) - return -1; - else - rdlen++; - if (*buf == '\n') - break; - if (*buf != '\r') /* remove all CRs */ - buf++; + char *sp, *tp; + + for (tp = sp = buf; *sp; sp++) + if (*sp != '\r' && *sp != '\n') + *tp++ = *sp; + *tp++ = '\0'; + + return(strlen(buf)); } - *buf = 0; - return rdlen; } /* socket.c ends here */ -- cgit v1.2.3