home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / biblioteki / c_library / asyncio / source / waitpacket.c < prev    next >
C/C++ Source or Header  |  1977-12-31  |  2KB  |  72 lines

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