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 / pipes.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-23  |  7.4 KB  |  185 lines

  1. /*************************************************************************
  2.                     Copyright Microsoft Corp. 1992-1996
  3.                         Remote Machine pipe sample
  4.  
  5.   FILE      :   pipes.c
  6.  
  7.   USAGE     :   server  -p protocol_sequence
  8.                         -e endpoint
  9.  
  10.   PURPOSE   :   Server side of the RPC distributed application pipe.
  11.   
  12.   COMMENTS  :   This application uses the implicit binding method.
  13.  
  14. *************************************************************************/
  15. #include "pipe.h"       // Generated by the MIDL compiler
  16. #include "common.h"     // Common definitions is locataed in  this file    
  17.  
  18. // Local Procedures 
  19. void CleanUpServer();   // Unregisters the interface
  20.  
  21. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  22. /*  Procedure   :   void Usage(_TUCHAR *)                               */
  23. /*  Desc        :   This procedure prints out an error message if the   */
  24. /*                  command line arguments are wrong                    */
  25. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  26. void Usage(_TUCHAR * pszProgramName)
  27. {
  28.     _tprintf(TEXT("USAGE : %s [-option]\n"), pszProgramName);
  29.     _tprintf(TEXT("Options : -p Protocol Sequence\n"));  
  30.     _tprintf(TEXT("          -e Endpoint\n"));  
  31.     exit(EXECUTION_FAILED);
  32. }
  33.  
  34.  
  35. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  36. /* The server main program                                              */
  37. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  38. int _CRTAPI1 main(int argc, char *argv[])
  39. {
  40.     RPC_STATUS      nStatus;        // Error status returned    
  41.     int             nNumArgs;       // Number of command line arguments
  42.     int             nIdx;           // Counter in loops
  43.     
  44.     // Variabels used for selecting the protocol and the endpoint
  45.     _TUCHAR *pszProtocolSequence    = PROTOCOL_SEQUENCE;
  46.     _TUCHAR *pszEndpoint            = END_POINT;
  47.     _TUCHAR *pszSecurity            = NULL;
  48.  
  49.  
  50. // Get a common handle on the command line arguments for both UNICODE
  51. // and ASCII
  52. #ifdef _UNICODE
  53.     LPWSTR    *szArglist = CommandLineToArgvW(GetCommandLine(), &nNumArgs);
  54.     if (NULL == szArglist)
  55.     {
  56.         _tprintf(TEXT("SERVER.C : CommandLineToArgW failed"));
  57.         exit(EXECUTION_FAILED);
  58.     }
  59. #else
  60.     char **szArglist    = argv;
  61.     nNumArgs = argc;
  62. #endif
  63.  
  64.     /* Allow the user to override settings with commandline switches    */
  65.     for (nIdx = 1; nIdx < nNumArgs; nIdx++) 
  66.     {
  67.         if((_tcscmp(szArglist[nIdx], TEXT("-p")) == 0) || 
  68.            (_tcscmp(szArglist[nIdx], TEXT("-P")) == 0))
  69.         {
  70.             pszProtocolSequence = szArglist[++nIdx];
  71.         }
  72.         else if((_tcscmp(szArglist[nIdx], TEXT("-e")) == 0) || 
  73.                 (_tcscmp(szArglist[nIdx], TEXT("-e")) == 0))
  74.         {
  75.             pszEndpoint = szArglist[++nIdx];
  76.         }
  77.         else 
  78.         {
  79.             Usage(szArglist[0]);  
  80.         }
  81.     }
  82.     
  83.     
  84.     /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  85.     /* Register the interface with the RPC run-time library             */
  86.     /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  87.     _tprintf(TEXT("Registering the interface\n"));
  88.     nStatus = RpcServerRegisterIf(
  89.         pipe_sample_v1_0_s_ifspec,  // Interface specification
  90.         NULL,                       // UUID to associate with MgrEnv arg.
  91.         NULL);                      // Managers entry point vector. (None)
  92.     EXIT_IF_FAIL(nStatus, "RpcServerRegisterIf");
  93.  
  94.  
  95.     /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  96.     /* Select Protocal sequence : This sample uses namedpipes as the    */
  97.     /* default protocol. The RpcServerUseProtseqEp function tells the   */
  98.     /* RPC run-time library to use the specified protocol sequence      */
  99.     /* combined with the specified endpoint for receiving remote        */
  100.     /* procedure calls                                                  */
  101.     /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  102.     _tprintf(TEXT("Selecting the protocol sequence to use.\t\"%s\"\n"),
  103.             pszProtocolSequence);
  104.     nStatus = RpcServerUseProtseqEp(
  105.         pszProtocolSequence,            // String with the protocol in
  106.         RPC_C_PROTSEQ_MAX_REQS_DEFAULT, // Max number of calls
  107.         pszEndpoint,                    // Endpoint addres information
  108.         pszSecurity);                   // Security 
  109.     EXIT_IF_FAIL(nStatus, "RpcServerUseProtseqsEp");
  110.  
  111.     
  112.     /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  113.     /* Now start listening for remote procedure calls from the client   */
  114.     /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  115.     RpcTryExcept
  116.     {
  117.         _tprintf(TEXT("Listening for remote calls...\n"));
  118.         nStatus = RpcServerListen(
  119.             1,                              // The minimum number of calls
  120.             RPC_C_LISTEN_MAX_CALLS_DEFAULT, // The maximum number of calls
  121.             FALSE);                         // Continue until stopped    
  122.         EXIT_IF_FAIL(nStatus, "RpcServerListen");
  123.     }
  124.     RpcExcept(DO_EXCEPTION)
  125.     {
  126.         // Print out the exception code 
  127.         _tprintf(TEXT("Run-time exception %u in %s at line %d\n"), 
  128.                 RpcExceptionCode(), TEXT(__FILE__), __LINE__);
  129.         exit(EXECUTION_FAILED);
  130.     }
  131.     RpcEndExcept
  132.  
  133.     // If no exceptions occured, clean up the server and exit
  134.     CleanUpServer();
  135.  
  136.     // Deallocate the memory used for the ARGLIST if using UNICODE
  137. #ifdef _UNICODE
  138.     if (NULL != szArglist)
  139.         free(szArglist);
  140. #endif
  141.  
  142.     return (EXECUTION_OK);
  143. }
  144.  
  145.  
  146. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  147. /* Procedure    : void CleanUpServer(RPC_BINDING_VECTOR);               */
  148. /* Desc.        : This procedure will unregister the interface          */
  149. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  150. void CleanUpServer(void)
  151. {
  152.     RPC_STATUS nStatus;          // Error status returned from RPC calls 
  153.  
  154.     /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  155.     /* Unregister the interface from the RPC run-time library           */
  156.     /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  157.     _tprintf(TEXT("Unregistering the Interface"));
  158.     nStatus = RpcServerUnregisterIf(
  159.         NULL, NULL, // Prevents server from receiving any new remote calls
  160.         FALSE);        // Wait until all the active calls are complete
  161.     EXIT_IF_FAIL(nStatus, "RpcServerUnRegisterIf");
  162. }
  163.  
  164.  
  165.  
  166. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  167. /* Procedure    :   midl_user_allocate() and midl_user_free()           */
  168. /* Desc.        :   These procedure are declared in the header file     */
  169. /*                  generated by the midl compiler. These procedures    */
  170. /*                  should be used for all memory allocation and        */
  171. /*                  deallocation.                                       */
  172. /*                  These procedures are also called by the stub code to*/
  173. /*                  allocate and free memory.                           */
  174. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  175. void __RPC_FAR * __RPC_API midl_user_allocate(size_t len)
  176. {
  177.     return (malloc(len));
  178. }
  179.  
  180. void __RPC_API midl_user_free(void __RPC_FAR * ptr)
  181. {
  182.     if(NULL != ptr)
  183.         free (ptr);
  184. }
  185.