/*****************************************************************/
/**                                                             **/
/**             (C) Copyright 1992 the Regents of the           **/
/**        University of California.  All Rights Reserved.      **/
/**                                                             **/
/**        This work was produced under the sponsorship of      **/
/**         the U.S. Department of Energy.  The Government      **/
/**                retains certain rights therein.              **/
/**                                                             **/
/**                         Written by                          **/
/**         Distributed Programming Environments Project        **/
/**           Computer Software and Networking Division,        **/
/**                   Livermore Computer Center,                **/
/**            Lawrence Livermore National Laboratory           **/
/**                                                             **/
/*****************************************************************/

static char sccsid_SOCKreceive_data[] = "@(#)SOCKreceive.c	1.13 11/13/92 10:48:11  LLNL ";

/*
	Embedded Language for Remote Operations Services
	ELROS

	AUTHOR:		James A. Guse Network Systems Division
			Robyne M. Sumpter

	DATE WRITTEN:	6/28/90

	ROUTINE:	receive_data
	Version:	__ROr_SOCKreceive_data

PURPOSE:
	This routine obtains a requested number of bytes from the incoming
	network stream.

IN:
	instance - Buffer state is used
	size - the number of bytes to read.  If less than this number can be
		an error code is returned.

OUT:
	instance - Buffer and Receive state is updated.
	buffer - byte array to hold 'size' bytes.


CALLED ROUTINES:
	SReadRequest

	
MODIFICATION HISTORY:


*/

#include <elros/instance.h>
#include <malloc.h>
#include <errno.h>

#define HEAD	(instance->receive_state.buffers)
#define SOCK_INSTADDR(i)( (i)->subsystem->specific.puresockets )


#ifdef __RO_TASKING
#include <elros/tasking.h>
#endif



int __ROr_SOCKreceive_data(instance)
   __RO_Instance_Record *	instance;

#ifdef __RO_PLATFORM_PURESOCKETS

{
   int code;
   int done_avail;
   unsigned long absolute_timeout;

#ifndef __RO_PRODUCTION
   if (instance->subsystem->platform != __ROm_platform_PURESOCKETS)
      __RO_RETURN_RUNTIME(instance,__ROx_INTERNAL_PLATFORMNOTPURESOCKETS);
#endif

   done_avail = instance->receive_state.done_available;
   instance->receive_state.done_available = 0;
   if (!HEAD) {
      HEAD = (__RO_Receivebuffers) malloc(sizeof(*HEAD) +
                                    instance->receive_state.buffer_maximum);
      if (!HEAD)
        __RO_RETURN_RUNTIME(instance,__ROx_GENERAL_MALLOCFAILED);

      HEAD->data_ptr = HEAD->data_bytes;
      HEAD->data_length = 0;
      HEAD->forward = NULL;
      HEAD->backward = NULL;
   }

   absolute_timeout = __ROr_STDget_absolute_timeout(instance);
#ifdef __RO_TASKING
   do
   {
      if(__ROr_Scan( SOCK_INSTADDR(instance).socket, __ROm_READDIR, instance ))
         return(EXCEPTION);

      Alarm(absolute_timeout);
      SLEEP;

      if( TIME >= absolute_timeout )
      {
         __RO_RETURN_RUNTIME( instance, __ROx_GET_TIMEOUT );
      }

      if( (HEAD->data_length =
             __ROr_read(SOCK_INSTADDR(instance).socket,
                     HEAD->data_ptr,
                     instance->receive_state.buffer_maximum)) < 0
          && errno != EWOULDBLOCK)
      {
         __RO_RETURN_RUNTIME( instance, __ROx_SOCKET_CANTREAD);
      }
      if (__ROr_ClearSocket( SOCK_INSTADDR(instance).socket, __ROm_READDIR ))
         return(EXCEPTION);

   } while (HEAD->data_length < 0);
#else /* NO__RO_TASKING */
   do
   {
      if (__ROr_Single_Select( SOCK_INSTADDR(instance).socket,
                             __ROm_READDIR,instance->subsystem->timeout)
          == -1) {
        __RO_RETURN_RUNTIME( instance, __ROx_SOCKET_SELECTFAILED);
      }

      if( TIME >= absolute_timeout )
      {
         __RO_RETURN_RUNTIME( instance, __ROx_GET_TIMEOUT );
      }

      if( (HEAD->data_length =
             __ROr_read(SOCK_INSTADDR(instance).socket,
                     HEAD->data_ptr,
                     instance->receive_state.buffer_maximum)) < 0
          && errno != EWOULDBLOCK)
      {
         __RO_RETURN_RUNTIME( instance, __ROx_SOCKET_CANTREAD);
      }

   } while (HEAD->data_length < 0);
#endif

   if (HEAD->data_length == 0)
      if (done_avail)
      {
         instance->receive_state.done_taken = 1;
         __RO_RETURN_DONE(instance);
      }
      else 
      {
         __RO_RETURN_RUNTIME(instance,__ROx_GET_UNEXPECTDONE);
      }

   if (__ROr_log6receive(instance))
      return(EXCEPTION);

   return(SUCCESS);
}
#else

{
   __RO_RETURN_RUNTIME(instance,__ROx_INTERNAL_PURESOCKETSNOTLINKED);
}
#endif

