home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mmpm21tk.zip / TK / FSSHT / FSSHREAD.C < prev    next >
C/C++ Source or Header  |  1993-04-05  |  11KB  |  240 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /*                    Copyright (c) IBM Corporation 1992, 1993              */
  4. /*                           All Rights Reserved                            */
  5. /*                                                                          */
  6. /* SOURCE FILE NAME: FSSHREAD.C                                             */
  7. /*                                                                          */
  8. /* DESCRIPTIVE NAME:  File System Stream Handler Read routine               */
  9. /*                                                                          */
  10. /* FUNCTION: This function is the routine that reads an object from the     */
  11. /*           file system.                                                   */
  12. /*                                                                          */
  13. /* ENTRY POINTS: FsshRead                                                   */
  14. /*                                                                          */
  15. /*************************** END OF SPECIFICATIONS **************************/
  16. #define  INCL_NOPMAPI                  /* no PM include files required */
  17. #define  INCL_DOSSEMAPHORES
  18. #define  INCL_DOSPROCESS
  19. #define  INCL_DOSERRORS
  20. #define  INCL_MMIO
  21. #include <os2.h>
  22. #include <os2me.h>
  23. #include <hhpheap.h>
  24. #include <shi.h>
  25.  
  26. /************************** START OF SPECIFICATIONS *************************/
  27. /*                                                                          */
  28. /* SUBROUTINE NAME: FsshRead                                                */
  29. /*                                                                          */
  30. /* DESCRIPTIVE NAME: File System Stream Handler Read routine                */
  31. /*                                                                          */
  32. /* FUNCTION: Reads object from the file system and supplies it to the       */
  33. /*           target stream handler.                                         */
  34. /* NOTES:                                                                   */
  35. /*                                                                          */
  36. /* ENTRY POINT: FsshRead                                                    */
  37. /*   LINKAGE:   CALL NEAR (0:32)                                            */
  38. /*                                                                          */
  39. /* INPUT: Pointer to sib (stream instance block)                            */
  40. /*                                                                          */
  41. /* EXIT-NORMAL: NO_ERROR (0)                                                */
  42. /*                                                                          */
  43. /* EXIT-ERROR:                                                              */
  44. /*   ERROR_BUFFER_NOT_AVAILABLE                                             */
  45. /*                                                                          */
  46. /* SIDE EFFECTS:                                                            */
  47. /*                                                                          */
  48. /* INTERNAL REFERENCES:                                                     */
  49. /*        ROUTINES: None                                                    */
  50. /*                                                                          */
  51. /* EXTERNAL REFERENCES:                                                     */
  52. /*        ROUTINES:                                                         */
  53. /*          DosWaitEventSem                                                 */
  54. /*          CheckNSleep                                                     */
  55. /*          DosResetEventSem                                                */
  56. /*          SMHEntryPoint                                                   */
  57. /*          SleepNCheck                                                     */
  58. /*          mmioRead                                                        */
  59. /*          ShIOError                                                       */
  60. /*          mmioGetLastError                                                */
  61. /*          ReportEvent                                                     */
  62. /*        DATA STRUCTURES:                                                  */
  63. /*          npget                                                           */
  64. /*          sib                                                             */
  65. /*          hmtxGlobalData                                                       */
  66. /*                                                                          */
  67. /*                                                                          */
  68. /*************************** END OF SPECIFICATIONS **************************/
  69.  
  70. VOID FsshRead(psib)
  71. PSIB psib;                              /* Stream Instance Block */
  72.  
  73. { /* Start of FsshRead */
  74.  
  75. RC          rc = NO_ERROR;              /* local return code */
  76. char        NeedBuf;                    /* local loop boolean */
  77. LONG        NumBytesIO;                 /* Number of bytes from mmio */
  78. PARM_NOTIFY npget, npret;               /* parms for SMH_NOTIFY calls */
  79. SRCBUFTAB   SrcBufTab = {0};            /* Source buffer table */
  80. ULONG       ulStopped = DO_SLEEP;       /* did we get stop discard or flush */
  81. BOOL        bAtEOS = FALSE;             /* End of Stream indicator */
  82. ULONG       ulPostCount;                /* Temp to hold count */
  83. BOOL        bFirstRead = TRUE;          /* This is the first read  */
  84.  
  85.   /* Before we start lets do some init stuff: */
  86.  
  87.   npget.ulFunction = SMH_NOTIFY;
  88.   npget.hid = psib->HandlerID;
  89.   npget.hstream = psib->hStream;
  90.   npget.ulGetNumEntries = 1L;
  91.   npget.ulRetNumEntries = 0L;
  92.   npget.pGetBufTab = &SrcBufTab;
  93.   npget.pRetBufTab = NULL;
  94.  
  95.   npret.ulFunction = SMH_NOTIFY;
  96.   npret.hid = psib->HandlerID;
  97.   npret.hstream = psib->hStream;
  98.   npret.ulFlags = BUF_RETURNFULL;
  99.   npret.ulGetNumEntries = 0L;
  100.   npret.ulRetNumEntries = 1L;
  101.   npret.pGetBufTab = NULL;
  102.   npret.pRetBufTab = &SrcBufTab;
  103.  
  104.   /* Wait until we get the ShcStart */
  105.  
  106.   DosWaitEventSem(psib->hevStop, SEM_INDEFINITE_WAIT);
  107.  
  108.   /* We will loop forever getting an empty buffer, calling the device to */
  109.   /* fill up the buffer, sending it to the consumer.  During each iteration */
  110.   /* of the loop we will check the action flags for asynchronous requests */
  111.   /* to do things. */
  112.  
  113.   if (psib->ulActionFlags & SIBACTFLG_KILL)
  114.     { /* Must have been a create error */
  115.       rc = 1L;
  116.     } /* Must have been a create error */
  117.  
  118.   /*  Start the main loop */
  119.  
  120.   while (!rc)
  121.     { /* while no severe error */
  122.  
  123.     if (psib->ulActionFlags)
  124.       rc = CheckNSleep(psib);
  125.  
  126.     /*
  127.      * Get a buffer
  128.      */
  129.     NeedBuf = TRUE;
  130.     while ((NeedBuf) && (!rc))
  131.       { /* while we don't have a buffer */
  132.  
  133.         /* Clear the stop sem, so if after we call ssm to get a buffer if */
  134.         /* it returns none avail then we won't miss a SSMBuffer Start before */
  135.         /* we go to sleep. */
  136.  
  137.         DosResetEventSem(psib->hevStop, &ulPostCount);
  138.  
  139.         npget.ulFlags = BUF_GETEMPTY;
  140.         rc = SMHEntryPoint(&npget); /* get a buffer */
  141.         if (!rc)
  142.           {
  143.             NeedBuf = FALSE;
  144.             /* make sure attribute is 0 so we don't pass around a bad value */
  145.             SrcBufTab.ulMessageParm = 0L;
  146.           }
  147.         else
  148.           { /* return code from smhnotify */
  149.             if (rc == ERROR_BUFFER_NOT_AVAILABLE)
  150.               { /* buffer not available */
  151.  
  152.                 /* the smhnotify resets the num entries to 0 when none avail */
  153.                 npget.ulGetNumEntries = 1L;
  154.  
  155.                 ulStopped = DO_SLEEP;
  156.                 rc = SleepNCheck(psib, &ulStopped);
  157.  
  158.               } /* buffer not available */
  159.           } /* return code from smhnotify */
  160.       } /* while we don't have a buffer */
  161.  
  162.     /* We have a buffer or an error */
  163.  
  164.     if (!rc)
  165.       { /* have a buffer - do the read */
  166.  
  167.         NumBytesIO = mmioRead((HMMIO)psib->ulAssocP1,
  168.                                 (PCHAR)SrcBufTab.pBuffer,
  169.                                 (LONG)SrcBufTab.ulLength);
  170.  
  171.         if ((NumBytesIO == -1L) || ((NumBytesIO == 0L) && (bFirstRead == TRUE)))
  172.           { /* an error */
  173.  
  174.             SrcBufTab.ulLength = 0L;
  175.             /* get the real error code */
  176.             rc = mmioGetLastError((HMMIO)psib->ulAssocP1);
  177.  
  178.             rc = ShIOError(psib, npret, rc);
  179.  
  180.           } /* an error */
  181.         else
  182.           { /* We have some data */
  183.             bFirstRead = FALSE;
  184.             if (NumBytesIO != (LONG)SrcBufTab.ulLength)
  185.               { /* End of stream */
  186.                 npret.ulFlags |= BUF_EOS;
  187.                 bAtEOS = TRUE;
  188.                 DosResetEventSem(psib->hevStop, &ulPostCount);
  189.                 SrcBufTab.ulLength = NumBytesIO;
  190.               } /* End of stream */
  191.  
  192.             /* Send the data to the stream manager */
  193.  
  194.             rc = SMHEntryPoint(&npret);
  195.             if (!rc)
  196.               { /* data sent ok */
  197.                 if (bAtEOS)
  198.                   {
  199.                     bAtEOS = FALSE;
  200.                     ulStopped = DO_SLEEP;
  201.                     rc = SleepNCheck(psib, &ulStopped);
  202.                   }
  203.               } /* data sent ok */
  204.           } /* We have some data */
  205.  
  206.         /* Clear the EOS if it was set. And attribute */
  207.         npret.ulFlags = BUF_RETURNFULL;
  208.         SrcBufTab.ulMessageParm = 0L;
  209.  
  210.       } /* have a buffer - do the read */
  211.  
  212.     } /* while no severe error */
  213.  
  214.   /* We get here if an error has occurred or a kill has */
  215.   /*  been sent.  In the case of the kill, reset the */
  216.   /*  return code to 0 (no error) and exit the thread. */
  217.   /*  Otherwise, report the error event and exit the */
  218.   /*  thread. */
  219.  
  220.   if (psib->ulActionFlags & SIBACTFLG_KILL)
  221.     {
  222.       rc = 0L;
  223.     }
  224.   else
  225.     {
  226.       ReportEvent(psib,
  227.                   rc,                   /* Return code */
  228.                   EVENT_ERROR,          /* event type */
  229.                   0L,                   /* user info */
  230.                   NONRECOVERABLE_ERROR);  /* Severe Error */
  231.     }
  232.  
  233.   /* Only set this flag when we no longer need access to the sib since */
  234.   /* Destroy may get control and Free the sib. */
  235.  
  236.   psib->ulActionFlags |= SIBACTFLG_THREAD_DEAD;
  237.   return;
  238.  
  239. } /* End of FsshRead */
  240.