home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / netds / rpc / pipes / pipeproc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-23  |  4.7 KB  |  126 lines

  1. /*************************************************************************
  2.                     Copyright Microsoft Corp. 1992-1996
  3.                         Remote Machine pipe sample
  4.  
  5.   FILE      :   pipeproc.c
  6.  
  7.   PURPOSE   :    Remote procedures used in the RPC distributed application 
  8.                 pipe sample
  9.  
  10.   COMMENTS  :   This file is linked together with the server side of the 
  11.                 application.
  12.  
  13. *************************************************************************/
  14. #include "pipe.h"       // Generated by the MIDL compiler
  15. #include "common.h"     // Common definitions is locataed in  this file    
  16.  
  17. #define MAX_BUFFER_SIZE 1000       // Buffer used on the server side
  18.  
  19.  
  20. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  21. /* Procedure    :   void ScrambleFile(unsigned long, CHAR_PIPE_TYPE *)  */
  22. /* Desc.        :   This procedure receives a file from the client, and */
  23. /*                  then "encodes" the file by adding 1 to every        */
  24. /*                  character, and then sends the file back to the      */
  25. /*                  client. The file is sent back and forth using pipes */
  26. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  27. void ScrambleFile(unsigned long nAction, CHAR_PIPE_TYPE *in_out_pipe)
  28. {
  29.     FILE    *fpTemp;                    // Pointer to the temporary file
  30.     _TUCHAR pcBuffer[MAX_BUFFER_SIZE];  // Buffer to store the elements in
  31.  
  32.     unsigned long   
  33.         nActualTransferCount,   // Actual number of elements received 
  34.         nIdx,                   // Counter in loop 
  35.         nElementsToSend;        // Number of elements to send to the client
  36.     BOOL bDone = FALSE;         // Inidicates when the pipe is done
  37.     
  38.  
  39.     // Open up a file to temporary store the pulled data in
  40.     fpTemp = _tfopen(TEXT("tempfile.oak"), TEXT("w+"));
  41.     
  42.     _tprintf(TEXT("Retrieving data from the client...\n"));
  43.     // First process the entire pull. Input pipes must be transmitted first
  44.     while(FALSE == bDone)
  45.     {
  46.         in_out_pipe->pull(      // Grab a chunck of data from the client
  47.             in_out_pipe->state, // Pointer to the state 
  48.             pcBuffer,           // Buffer to put data in
  49.             MAX_BUFFER_SIZE,    // Max number of elements to receive
  50.             &nActualTransferCount); // Actual number of elements received
  51.     
  52.         // A data transfer count of 0 means the end of the pipe
  53.         if(nActualTransferCount == 0)
  54.         {
  55.             bDone = TRUE;
  56.         }
  57.         else        // Save the data to an intermediate file
  58.         {
  59.             fwrite(pcBuffer, sizeof(_TUCHAR), nActualTransferCount, fpTemp);
  60.         }
  61.     }
  62.  
  63.     // Reset the flag
  64.     bDone = FALSE;
  65.  
  66.     // Reset the filehandle to point at start of file
  67.     fseek(fpTemp, 0, SEEK_SET);
  68.     
  69.     _tprintf(TEXT("Sending the data back to the client...\n"));
  70.     // Now we can send the data back 
  71.     while(FALSE == bDone)
  72.     {
  73.         // Read a number of bytes from the file, into the buffer
  74.         nElementsToSend = fread(pcBuffer, 
  75.                                  sizeof(_TUCHAR), 
  76.                                  MAX_BUFFER_SIZE, 
  77.                                  fpTemp);
  78.  
  79.         // If no more elements to send, we are done. Must send 0 to the 
  80.         // client to notify that the pipe is empty
  81.         if(nElementsToSend == 0)
  82.         {
  83.             bDone = TRUE;
  84.         }
  85.  
  86.         // According to the action, scramble or descramble the data 
  87.         for (nIdx = 0; nIdx < nElementsToSend; nIdx++)
  88.         {
  89.             if(nAction == SCRAMBLE)
  90.             {
  91.                 pcBuffer[nIdx]++;
  92.             }
  93.             else
  94.             {
  95.                 pcBuffer[nIdx]--;
  96.             }
  97.         }
  98.  
  99.         // Send the buffer to the client
  100.         in_out_pipe->push(      // Send a chunck of data to the client
  101.             in_out_pipe->state, // Pointer to the state
  102.             pcBuffer,             // Date to send is in this buffer
  103.             nElementsToSend);  // The number of elements to send
  104.     }    // End while (!done)
  105.  
  106.     // Close the file 
  107.     fclose(fpTemp);
  108. }
  109.  
  110.  
  111.  
  112. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  113. /* Procedure    :   void ShutDown(void);                                */
  114. /* Desc.        :   This procedure send a message to the server that it */
  115. /*                  can    stop listening for remote procedure calls       */
  116. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  117. void ShutDown(void)
  118. {
  119.     RPC_STATUS nStatus;
  120.  
  121.     // Tell the server to stop listening for remote procedure calls 
  122.     _tprintf(TEXT("Shutting down the server\n"));
  123.     nStatus = RpcMgmtStopServerListening(NULL);
  124.     EXIT_IF_FAIL(nStatus, "RpcMgmtStopServerListening");
  125. }
  126.