SRVCHILD.C
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:4k
源码类别:

Windows编程

开发平台:

Visual C++

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