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 / srvctos.c < prev    next >
C/C++ Source or Header  |  1997-10-12  |  3KB  |  155 lines

  1.  
  2. /******************************************************************************\
  3. *       This is a part of the Microsoft Source Code Samples. 
  4. *       Copyright 1995 - 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 (c) 1997  Microsoft Corporation
  15.  
  16. Module Name:
  17.  
  18.     SrvCtoS.c
  19.  
  20. Abstract:
  21.  
  22.     This file implements the client-to-server flow
  23.     of data for remote server.  The data is the keyboard
  24.     or piped input that the client received and sent
  25.     over the wire to us, bracketed by BEGINMARK and ENDMARK
  26.     bytes so we can display nice attribution comments in
  27.     brackets next to input lines.
  28.  
  29. Author:
  30.  
  31.     Dave Hart  30 May 1997
  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 <io.h>
  45. #include <string.h>
  46. #include "Remote.h"
  47. #include "Server.h"
  48.  
  49.  
  50.  
  51. VOID
  52. FASTCALL
  53. StartReadClientInput(
  54.     PREMOTE_CLIENT pClient
  55.     )
  56. {
  57.     //
  58.     // Start read of data from this client's stdin.
  59.     //
  60.  
  61.     if ( ! ReadFileEx(
  62.                pClient->PipeReadH,
  63.                pClient->ReadBuffer,
  64.                BUFFSIZE - 1,                  // allow for null term
  65.                &pClient->ReadOverlapped,
  66.                ReadClientInputCompleted
  67.                )) {
  68.  
  69.         CloseClient(pClient);
  70.     }
  71. }
  72.  
  73.  
  74. VOID
  75. WINAPI
  76. ReadClientInputCompleted(
  77.     DWORD dwError,
  78.     DWORD cbRead,
  79.     LPOVERLAPPED lpO
  80.     )
  81. {
  82.     PREMOTE_CLIENT pClient;
  83.  
  84.     pClient = CONTAINING_RECORD(lpO, REMOTE_CLIENT, ReadOverlapped);
  85.  
  86.     if (HandleSessionError(pClient, dwError) ||
  87.         !cbRead) {
  88.  
  89.         return;
  90.     }
  91.  
  92.     pClient->ReadBuffer[cbRead] = 0;
  93.  
  94.     if (FilterCommand(pClient, pClient->ReadBuffer, cbRead)) {
  95.  
  96.         //
  97.         // Local command, don't pass it to child app, just
  98.         // start another client read.
  99.         //
  100.  
  101.         if ( ! ReadFileEx(
  102.                    pClient->PipeReadH,
  103.                    pClient->ReadBuffer,
  104.                    BUFFSIZE - 1,                  // allow for null term
  105.                    &pClient->ReadOverlapped,
  106.                    ReadClientInputCompleted
  107.                    )) {
  108.  
  109.             CloseClient(pClient);
  110.         }
  111.  
  112.     } else {
  113.  
  114.         //
  115.         // Write buffer to child stdin.
  116.         //
  117.  
  118.         if ( ! WriteFileEx(
  119.                    hWriteChildStdIn,
  120.                    pClient->ReadBuffer,
  121.                    cbRead,
  122.                    &pClient->ReadOverlapped,
  123.                    WriteChildStdInCompleted
  124.                    )) {
  125.  
  126.             // Child is going away.  Let this client's chain of IO stop.
  127.         }
  128.     }
  129. }
  130.  
  131.  
  132. VOID
  133. WINAPI
  134. WriteChildStdInCompleted(
  135.     DWORD dwError,
  136.     DWORD cbWritten,
  137.     LPOVERLAPPED lpO
  138.     )
  139. {
  140.     PREMOTE_CLIENT pClient;
  141.  
  142.     pClient = CONTAINING_RECORD(lpO, REMOTE_CLIENT, ReadOverlapped);
  143.  
  144.     if (HandleSessionError(pClient, dwError)) {
  145.  
  146.         return;
  147.     }
  148.  
  149.     //
  150.     // Start another read against the client input.
  151.     //
  152.  
  153.     StartReadClientInput(pClient);
  154. }
  155.