home *** CD-ROM | disk | FTP | other *** search
- /*
- * $RCSfile: receive.c,v $
- * $Revision: 1.1.1.1 $
- * $Date: 1996/05/04 21:55:27 $
- */
- /**********************************************************************
- * EXODUS Database Toolkit Software
- * Copyright (c) 1991 Computer Sciences Department, University of
- * Wisconsin -- Madison
- * All Rights Reserved.
- *
- * Permission to use, copy, modify and distribute this software and its
- * documentation is hereby granted, provided that both the copyright
- * notice and this permission notice appear in all copies of the
- * software, derivative works or modified versions, and any portions
- * thereof, and that both notices appear in supporting documentation.
- *
- * THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY OF WISCONSIN --
- * MADISON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.
- * THE DEPARTMENT DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES
- * WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
- *
- * The EXODUS Project Group requests users of this software to return
- * any improvements or extensions that they make to:
- *
- * EXODUS Project Group
- * c/o David J. DeWitt and Michael J. Carey
- * Computer Sciences Department
- * University of Wisconsin -- Madison
- * Madison, WI 53706
- *
- * or exodus@cs.wisc.edu
- *
- * In addition, the EXODUS Project Group requests that users grant the
- * Computer Sciences Department rights to redistribute these changes.
- **********************************************************************/
-
- #include "sysdefs.h"
- #include "ess.h"
- #include "checking.h"
- #include "trace.h"
- #include "error.h"
- #include "list.h"
- #include "tid.h"
- #include "io.h"
- #include "lock.h"
- #include "object.h"
- #include "msgdefs.h"
- #include "host.h"
- #include "trans.h"
- #include "bf.h"
- #include "lsn.h"
- #include "loginfo.h"
- #include "serverinfo.h"
- #include "link.h"
- #include "bitvec.h"
- #include "msg_globals.h"
- #include "sm_globals.h"
- #include "trans_globals.h"
- #include "msg_funcs.h"
-
-
- extern int errno;
-
-
-
-
- int
- receive (
-
- SERVERINFO *serverInfo,
- char *buffer,
- int readBytes
- )
- {
-
- int numReady;
- int readyIndex;
- int bytesRead;
- int remainBytes;
- int totalBytes;
- LINK *link;
- SERVERINFO *readyServerInfo;
-
-
- TRPRINT(TR_MSG, TR_LEVEL_1, ("reading bytes:%d", readBytes));
-
- if (readBytes == 0)
- return esmNOERROR;
-
- /*
- * do a select on all the servers - this is necessary
- * because unless the client detects broken connections
- * and does a close on its side, servers will not be able
- * to get their ports upon recovery
- */
- for (;;) {
- select_again:
-
- ReadMask = SelectMask;
- #ifdef hpux
- numReady = select(SelectBits, (int*)&ReadMask, NULL, NULL, NULL);
- #else
- numReady = select(SelectBits, (fd_set*)&ReadMask, NULL, NULL, NULL);
- #endif
-
- if (numReady<0) {
- /*
- * See if the error was caused by an interupted select.
- */
- if (errno == EINTR) {
- /* Just try select again */
- numReady = 0;
- TRPRINT(TR_MSG|TR_DISK, TR_LEVEL_2,
- ("select interrupted: restart"));
- goto select_again;
- } else {
- /*
- * TODO: clean this up
- */
- SM_ERROR(TYPE_CRASH, errno);
- }
- }
-
- while ((readyIndex = selectReceive( &ReadMask )) >= 0) {
-
- /*
- * remove this link from the readmask so that
- * we'll not visit it until the next select
- */
- bic( &ReadMask, readyIndex );
- link = &(Links[readyIndex]);
-
- if (readyIndex != serverInfo->socket) {
-
- /*
- * get the serverInfo for this link
- */
- readyServerInfo = link->serverInfo;
-
- SM_ASSERT(LEVEL_1, (readyServerInfo != NULL));
-
- /*
- * try to read from this link
- * use the given buffer since we'll overwrite it when
- * the real response comes back
- */
- for (totalBytes = 0; totalBytes < 1; totalBytes += bytesRead) {
-
- bytesRead = read(readyServerInfo->socket, buffer,1);
-
- /*
- * make sure that no bytes were received since
- * we are not expecting a message from this server
- * NB: this may change if callbacks are introduced
- */
- SM_ASSERT(LEVEL_1, (bytesRead <= 0));
-
- if (bytesRead < 0) {
- SM_ERROR(TYPE_LOG, errno);
-
- switch(errno) {
- case ENETDOWN:
- case ENETUNREACH:
- case ENETRESET:
- case ECONNABORTED:
- case ECONNRESET:
-
- (void) detectedDisconnect(readyServerInfo);
-
- /*
- * exit the loop
- */
- totalBytes = 1;
-
- break;
-
- case EINTR:
-
- /*
- * The read was interupted, no bytes were
- * read. Start the read over by continuing.
- */
- continue;
-
- default:
- /* unsupported error */
- break;
- }
- } else {
- (void) detectedDisconnect(readyServerInfo);
-
- /*
- * exit the loop
- */
- totalBytes = 1;
- }
- }
- } else {
-
- /*
- * read the messages, repeatedly perhaps
- */
- for (totalBytes = 0; totalBytes < readBytes; totalBytes += bytesRead) {
-
- /*
- * calculate remaining bytes
- */
- remainBytes = readBytes - totalBytes;
- TRPRINT(TR_MSG, TR_LEVEL_1, ("want to read:%d", remainBytes));
-
- /*
- * read the remaining bytes
- */
- bytesRead = read(serverInfo->socket, buffer + totalBytes, (int) remainBytes);
-
- TRPRINT(TR_MSG, TR_LEVEL_1, ("bytesRead:%d", bytesRead));
-
- /*
- * check to see if the read succeeded
- */
- if (bytesRead < 0) {
- SM_ERROR(TYPE_LOG, errno);
-
- switch(errno) {
- case ENETDOWN:
- case ENETUNREACH:
- case ENETRESET:
- case ECONNABORTED:
- case ECONNRESET:
-
- (void) detectedDisconnect(serverInfo);
-
- break;
-
- case EINTR:
-
- /*
- * The read was interupted, no bytes were read.
- * Start the read over by continuing.
- */
- bytesRead = 0;
- continue;
-
- default:
- /* unsupported error */
- break;
- }
-
- return esmFAILURE;
- }
-
- /*
- * If no bytes were read, then the server (or connection) died
- */
- if (bytesRead == 0) {
-
- SM_ERROR(TYPE_LOG, esmSERVERDIED);
- (void) detectedDisconnect(serverInfo);
- return esmFAILURE;
- }
- if ( (bytesRead < remainBytes) && !(serverInfo->flags & SERV_ESTAB) ){
- /* We did not get all we asked for, and we were in the
- * process of doing an INIT_CLIENT, so we had better check
- * for an error in the protocol version
- */
- if( *((SHORT_MAGIC *) buffer) != MESSAGE_VERSION ) {
- return esmPROTOCOLVERSION;
- }
- }
- }
-
- incPages(readBytes, RECEIVED, readBytes==sizeof(MESSAGE)?TRUE:FALSE); /* grot */
-
- return esmNOERROR;
- }
- }
- }
- }
-