home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / sdktools / winnt / remote / srvchild.c < prev    next >
C/C++ Source or Header  |  1997-10-12  |  4KB  |  177 lines

  1.  
  2. /******************************************************************************\
  3. *       This is a part of the Microsoft Source Code Samples. 
  4. *       Copyright 1992 - 1997 Microsoft Corporation.
  5. *       All rights reserved. 
  6. *       This source code is only intended as a supplement to 
  7. *       Microsoft Development Tools and/or WinHelp documentation.
  8. *       See these sources for detailed information regarding the 
  9. *       Microsoft samples programs.
  10. \******************************************************************************/
  11.  
  12. /*++
  13.  
  14. Copyright 1992 - 1997 Microsoft Corporation
  15.  
  16. Module Name:
  17.  
  18.     SrvChild.c
  19.  
  20. Abstract:
  21.  
  22.     The server component of Remote. It spawns a child process
  23.     and redirects the stdin/stdout/stderr of child to itself.
  24.     Waits for connections from clients - passing the
  25.     output of child process to client and the input from clients
  26.     to child process.
  27.  
  28. Author:
  29.  
  30.     Rajivendra Nath  2-Jan-1992
  31.     Dave Hart        30 May 1997 split from Server.c
  32.  
  33. Environment:
  34.  
  35.     Console App. User mode.
  36.  
  37. Revision History:
  38.  
  39. --*/
  40.  
  41. #include <windows.h>
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <process.h>
  45. #include <io.h>
  46. #include <string.h>
  47. #include "Remote.h"
  48. #include "Server.h"
  49.  
  50.  
  51. VOID
  52. FASTCALL
  53. StartChildOutPipeRead(
  54.     VOID
  55.     )
  56. {
  57.     ReadChildOverlapped.OffsetHigh =
  58.         ReadChildOverlapped.Offset = 0;
  59.  
  60.     if ( ! ReadFileEx(
  61.                hReadChildOutput,
  62.                ReadChildBuffer,
  63.                sizeof(ReadChildBuffer) - 1,                  // allow for null term
  64.                &ReadChildOverlapped,
  65.                ReadChildOutputCompleted
  66.                )) {
  67.  
  68.         if (INVALID_HANDLE_VALUE != hWriteChildStdIn) {
  69.  
  70.             CANCELIO( hWriteChildStdIn );
  71.             CloseHandle( hWriteChildStdIn );
  72.             hWriteChildStdIn = INVALID_HANDLE_VALUE;
  73.         }
  74.     }
  75. }
  76.  
  77.  
  78. VOID
  79. WINAPI
  80. ReadChildOutputCompleted(
  81.     DWORD dwError,
  82.     DWORD cbRead,
  83.     LPOVERLAPPED lpO
  84.     )
  85. {
  86.     UNREFERENCED_PARAMETER(lpO);
  87.  
  88.     //
  89.     // We can get called after hWriteTempFile
  90.     // is closed after the child has exited.
  91.     //
  92.  
  93.     if (! dwError &&
  94.         INVALID_HANDLE_VALUE != hWriteTempFile) {
  95.  
  96.         //
  97.         // Start a write to the temp file.
  98.         //
  99.  
  100.         ReadChildOverlapped.OffsetHigh = 0;
  101.         ReadChildOverlapped.Offset = dwWriteFilePointer;
  102.  
  103.         if ( ! WriteFileEx(
  104.                    hWriteTempFile,
  105.                    ReadChildBuffer,
  106.                    cbRead,
  107.                    &ReadChildOverlapped,
  108.                    WriteTempFileCompleted
  109.                    )) {
  110.  
  111.             dwError = GetLastError();
  112.  
  113.             if (ERROR_DISK_FULL == dwError) {
  114.  
  115.                 printf("Remote: disk full writing temp file %s, exiting\n", SaveFileName);
  116.  
  117.                 if (INVALID_HANDLE_VALUE != hWriteChildStdIn) {
  118.  
  119.                     CANCELIO( hWriteChildStdIn );
  120.                     CloseHandle( hWriteChildStdIn );
  121.                     hWriteChildStdIn = INVALID_HANDLE_VALUE;
  122.                 }
  123.  
  124.             } else {
  125.  
  126.                 ErrorExit("WriteFileEx for temp file failed.");
  127.             }
  128.         }
  129.     }
  130. }
  131.  
  132.  
  133. VOID
  134. WINAPI
  135. WriteTempFileCompleted(
  136.     DWORD dwError,
  137.     DWORD cbWritten,
  138.     LPOVERLAPPED lpO
  139.     )
  140. {
  141.     UNREFERENCED_PARAMETER(lpO);
  142.  
  143.     if (dwError) {
  144.  
  145.         if (ERROR_DISK_FULL == dwError) {
  146.  
  147.             printf("Remote: disk full writing temp file %s, exiting\n", SaveFileName);
  148.  
  149.             if (INVALID_HANDLE_VALUE != hWriteChildStdIn) {
  150.  
  151.                 CANCELIO( hWriteChildStdIn );
  152.                 CloseHandle( hWriteChildStdIn );
  153.                 hWriteChildStdIn = INVALID_HANDLE_VALUE;
  154.             }
  155.  
  156.             return;
  157.  
  158.         } else {
  159.  
  160.             SetLastError(dwError);
  161.             ErrorExit("WriteTempFileCompleted may need work");
  162.         }
  163.     }
  164.  
  165.     dwWriteFilePointer += cbWritten;
  166.  
  167.     TRACE(CHILD, ("Wrote %d bytes to temp file\n", cbWritten));
  168.  
  169.     StartServerToClientFlow();
  170.  
  171.     //
  172.     // Start another read against the child input.
  173.     //
  174.  
  175.     StartChildOutPipeRead();
  176. }
  177.