home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / biblioteki / asyncio / src / waitpacket.c < prev    next >
C/C++ Source or Header  |  1997-05-17  |  2KB  |  71 lines

  1. #include "async.h"
  2.  
  3.  
  4. /* this function waits for a packet to come back from the file system. If no
  5.  * packet is pending, state from the previous packet is returned. This ensures
  6.  * that once an error occurs, it state is maintained for the rest of the life
  7.  * of the file handle.
  8.  *
  9.  * This function also deals with IO errors, bringing up the needed DOS
  10.  * requesters to let the user retry an operation or cancel it.
  11.  */
  12. LONG
  13. AS_WaitPacket( AsyncFile *file )
  14. {
  15. #ifdef ASIO_NOEXTERNALS
  16.     struct ExecBase        *SysBase = file->af_SysBase;
  17.     struct DosLibrary    *DOSBase = file->af_DOSBase;
  18. #endif
  19.     LONG bytes;
  20.  
  21.     if( file->af_PacketPending )
  22.     {
  23.         while( TRUE )
  24.         {
  25.             /* This enables signalling when a packet comes back to the port */
  26.             file->af_PacketPort.mp_Flags = PA_SIGNAL;
  27.  
  28.             /* Wait for the packet to come back, and remove it from the message
  29.              * list. Since we know no other packets can come in to the port, we can
  30.              * safely use Remove() instead of GetMsg(). If other packets could come in,
  31.              * we would have to use GetMsg(), which correctly arbitrates access in such
  32.              * a case
  33.              */
  34.             Remove( ( struct Node * ) WaitPort( &file->af_PacketPort ) );
  35.  
  36.             /* set the port type back to PA_IGNORE so we won't be bothered with
  37.              * spurious signals
  38.              */
  39.             file->af_PacketPort.mp_Flags = PA_IGNORE;
  40.  
  41.             /* mark packet as no longer pending since we removed it */
  42.             file->af_PacketPending = FALSE;
  43.  
  44.             bytes = file->af_Packet.sp_Pkt.dp_Res1;
  45.  
  46.             if( bytes >= 0 )
  47.             {
  48.                 /* packet didn't report an error, so bye... */
  49.                 return( bytes );
  50.             }
  51.  
  52.             /* see if the user wants to try again... */
  53.             if( ErrorReport( file->af_Packet.sp_Pkt.dp_Res2, REPORT_STREAM, file->af_File, NULL ) )
  54.             {
  55.                 return( -1 );
  56.             }
  57.  
  58.             /* user wants to try again, resend the packet */
  59.             AS_SendPacket( file,
  60.                 file->af_Buffers[ file->af_ReadMode ?
  61.                     file->af_CurrentBuf :
  62.                     1 - file->af_CurrentBuf ] );
  63.         }
  64.     }
  65.  
  66.     /* last packet's error code, or 0 if packet was never sent */
  67.     SetIoErr( file->af_Packet.sp_Pkt.dp_Res2 );
  68.  
  69.     return( file->af_Packet.sp_Pkt.dp_Res1 );
  70. }
  71.