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

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