manoj 99/06/01 14:36:03
Modified: pthreads/src/main buff.c Log: Clean out the WIN32, etc. code in the buff routines. This lets us get rid of a few layers of function calls too, and the Windows, etc. functionality will be put back with the portable runtime work. Revision Changes Path 1.9 +13 -250 apache-apr/pthreads/src/main/buff.c Index: buff.c =================================================================== RCS file: /home/cvs/apache-apr/pthreads/src/main/buff.c,v retrieving revision 1.8 retrieving revision 1.9 diff -u -u -r1.8 -r1.9 --- buff.c 1999/05/07 06:57:50 1.8 +++ buff.c 1999/06/01 21:36:03 1.9 @@ -119,120 +119,10 @@ * futher I/O will be done */ -#ifdef WIN32 - -/* - select() sometimes returns 1 even though the write will block. We must work around this. -*/ - -static int sendwithtimeout(int sock, const char *buf, int len, int flags, int sec) +static int sendwithtimeout(int sock, const char *buf, int len, time_t sec) { - int iostate = 1; fd_set fdset; struct timeval tv; - int err = WSAEWOULDBLOCK; - int rv; - int retry; - - if (!(tv.tv_sec = ap_check_alarm())) - return (send(sock, buf, len, flags)); - - rv = ioctlsocket(sock, FIONBIO, &iostate); - iostate = 0; - if (rv) { - err = WSAGetLastError(); - ap_assert(0); - } - rv = send(sock, buf, len, flags); - if (rv == SOCKET_ERROR) { - err = WSAGetLastError(); - if (err == WSAEWOULDBLOCK) - do { - retry=0; - - FD_ZERO(&fdset); - FD_SET(sock, &fdset); - tv.tv_usec = 0; - rv = select(FD_SETSIZE, NULL, &fdset, NULL, &tv); - if (rv == SOCKET_ERROR) - err = WSAGetLastError(); - else if (rv == 0) { - ioctlsocket(sock, FIONBIO, &iostate); - if(ap_check_alarm() < 0) { - WSASetLastError(EINTR); /* Simulate an alarm() */ - return (SOCKET_ERROR); - } - } - else { - rv = send(sock, buf, len, flags); - if (rv == SOCKET_ERROR) { - err = WSAGetLastError(); - if(err == WSAEWOULDBLOCK) { - ap_log_error(APLOG_MARK,APLOG_DEBUG,NULL, - "select claimed we could write, but in fact we couldn't. This is a bug in Windows."); - retry=1; - Sleep(100); - } - } - } - } while(retry); - } - ioctlsocket(sock, FIONBIO, &iostate); - if (rv == SOCKET_ERROR) - WSASetLastError(err); - return (rv); -} - - -static int recvwithtimeout(int sock, char *buf, int len, int flags, int sec) -{ - int iostate = 1; - fd_set fdset; - struct timeval tv; - int err = WSAEWOULDBLOCK; - int rv; - - if (!(tv.tv_sec = ap_check_alarm())) - return (recv(sock, buf, len, flags)); - - rv = ioctlsocket(sock, FIONBIO, &iostate); - iostate = 0; - ap_assert(!rv); - rv = recv(sock, buf, len, flags); - if (rv == SOCKET_ERROR) { - err = WSAGetLastError(); - if (err == WSAEWOULDBLOCK) { - FD_ZERO(&fdset); - FD_SET(sock, &fdset); - tv.tv_usec = 0; - rv = select(FD_SETSIZE, &fdset, NULL, NULL, &tv); - if (rv == SOCKET_ERROR) - err = WSAGetLastError(); - else if (rv == 0) { - ioctlsocket(sock, FIONBIO, &iostate); - ap_check_alarm(); - WSASetLastError(WSAEWOULDBLOCK); - return (SOCKET_ERROR); - } - else { - rv = recv(sock, buf, len, flags); - if (rv == SOCKET_ERROR) - err = WSAGetLastError(); - } - } - } - ioctlsocket(sock, FIONBIO, &iostate); - if (rv == SOCKET_ERROR) - WSASetLastError(err); - return (rv); -} - -#else - -static int sendwithtimeout(int sock, const char *buf, int len, int flags, time_t sec) -{ - fd_set fdset; - struct timeval tv; int err = EAGAIN; int rv; @@ -264,7 +154,7 @@ return (rv); } -static int recvwithtimeout(int sock, char *buf, int len, int flags, time_t sec) +static int recvwithtimeout(int sock, char *buf, int len, time_t sec) { fd_set fdset; struct timeval tv; @@ -300,101 +190,6 @@ return (rv); } -#endif /* WIN32 */ - - -/* the lowest level reading primitive */ -static int ap_read(BUFF *fb, void *buf, int nbyte) -{ - int rv; - -#ifdef WIN32 - if (fb->hFH != INVALID_HANDLE_VALUE) { - if (!ReadFile(fb->hFH,buf,nbyte,&rv,NULL)) - rv = -1; - } - else -#endif - rv = read(fb->fd_in, buf, nbyte); - - return rv; -} - -static ap_inline int buff_read(BUFF *fb, void *buf, int nbyte, time_t sec) -{ - int rv; - -#if defined (WIN32) - if (fb->flags & B_SOCKET) { - rv = recvwithtimeout(fb->fd_in, buf, nbyte, 0, sec); - if (rv == SOCKET_ERROR) - errno = WSAGetLastError(); - } - else - rv = ap_read(fb, buf, nbyte); -#elif defined(TPF) - fd_set fds; - struct timeval tv; - - tpf_process_signals(); - if (fb->flags & B_SOCKET) { - alarm(rv = alarm(0)); - FD_ZERO(&fds); - FD_SET(fb->fd_in, &fds); - tv.tv_sec = rv+1; - tv.tv_usec = 0; - rv = ap_select(fb->fd_in + 1, &fds, NULL, NULL, &tv); - if (rv < 1) { - tpf_process_signals(); - return(rv); - } - } - rv = ap_read(fb, buf, nbyte); -#else - rv = recvwithtimeout(fb->fd_in, buf, nbyte, 0, sec); -#endif /* WIN32 */ - return rv; -} - -/* the lowest level writing primitive */ -static int ap_write(BUFF *fb, const void *buf, int nbyte) -{ - int rv; - -#ifdef WIN32 - if (fb->hFH != INVALID_HANDLE_VALUE) { - if (!WriteFile(fb->hFH,buf,nbyte,&rv,NULL)) - rv = -1; - } - else -#endif -#if defined (B_SFIO) - rv = sfwrite(fb->sf_out, buf, nbyte); -#else - rv = write(fb->fd, buf, nbyte); -#endif - - return rv; -} - -static ap_inline int buff_write(BUFF *fb, const void *buf, int nbyte, time_t sec) -{ - int rv; - -#if defined(WIN32) - if (fb->flags & B_SOCKET) { - rv = sendwithtimeout(fb->fd, buf, nbyte, 0, sec); - if (rv == SOCKET_ERROR) - errno = WSAGetLastError(); - } - else - rv = ap_write(fb, buf, nbyte); -#else - rv = sendwithtimeout(fb->fd, buf, nbyte, 0, sec); -#endif /* WIN32 */ - return rv; -} - static void doerror(BUFF *fb, int direction) { int errsave = errno; /* Save errno to prevent overwriting it below */ @@ -445,9 +240,6 @@ fb->fd = -1; fb->fd_in = -1; -#ifdef WIN32 - fb->hFH = INVALID_HANDLE_VALUE; -#endif #ifdef B_SFIO fb->sf_in = NULL; @@ -470,16 +262,6 @@ fb->fd_in = fd_in; } -#ifdef WIN32 -/* - * Push some Win32 handles onto the stream. - */ -API_EXPORT(void) ap_bpushh(BUFF *fb, HANDLE hFH) -{ - fb->hFH = hFH; -} -#endif - API_EXPORT(int) ap_bsetopt(BUFF *fb, int optname, const void *optval) { if (optname == BO_BYTECT) { @@ -665,7 +447,7 @@ */ -#if !defined (B_SFIO) || defined (WIN32) +#if !defined (B_SFIO) #define saferead saferead_guts #else static int saferead(BUFF *fb, char *buf, int nbyte, time_t sec) @@ -716,7 +498,7 @@ ap_bhalfduplex(fb); } do { - rv = buff_read(fb, buf, nbyte, sec); + rv = recvwithtimeout(fb->fd_in, buf, nbyte, sec); } while (rv == -1 && errno == EINTR && !(fb->flags & B_EOUT)); return (rv); } @@ -759,7 +541,7 @@ /* A wrapper around saferead which does error checking and EOF checking - * yeah, it's confusing, this calls saferead, which calls buff_read... + * yeah, it's confusing, this calls saferead, which calls recvwithtimeout... * and then there's the SFIO case. Note that saferead takes care * of EINTR. */ @@ -1101,7 +883,7 @@ return -1; while (nbyte > 0) { - i = buff_write(fb, buf, nbyte, sec); + i = sendwithtimeout(fb->fd, buf, nbyte, sec); if (i < 0) { if (errno != EAGAIN && errno != EINTR) { doerror(fb, B_WR); @@ -1166,7 +948,7 @@ } #endif -/* A wrapper for buff_write which deals with error conditions and +/* A wrapper for sendwithtimeout which deals with error conditions and * bytes_sent. Also handles non-blocking writes. */ static int write_with_errors(BUFF *fb, const void *buf, int nbyte, time_t sec) @@ -1174,7 +956,7 @@ int rv; do - rv = buff_write(fb, buf, nbyte, sec); + rv = sendwithtimeout(fb->fd, buf, nbyte, sec); while (rv == -1 && errno == EINTR && !(fb->flags & B_EOUT)); if (rv == -1) { if (errno != EAGAIN) { @@ -1516,33 +1298,14 @@ if (fb->flags & B_WR) rc1 = ap_bflush(fb); else - rc1 = 0; -#ifdef WIN32 - if (fb->flags & B_SOCKET) { - rc2 = ap_pclosesocket(fb->pool, fb->fd); - if (fb->fd_in != fb->fd) { - rc3 = ap_pclosesocket(fb->pool, fb->fd_in); - } - else { - rc3 = 0; - } + rc1 = 0; + rc2 = ap_pclosef(fb->pool, fb->fd); + if (fb->fd_in != fb->fd) { + rc3 = ap_pclosef(fb->pool, fb->fd_in); } - else if (fb->hFH != INVALID_HANDLE_VALUE) { - rc2 = ap_pcloseh(fb->pool, fb->hFH); - rc3 = 0; - } else { -#endif - rc2 = ap_pclosef(fb->pool, fb->fd); - if (fb->fd_in != fb->fd) { - rc3 = ap_pclosef(fb->pool, fb->fd_in); - } - else { - rc3 = 0; - } -#ifdef WIN32 + rc3 = 0; } -#endif fb->inptr = fb->inbase; fb->incnt = 0;