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 / strout / server.c < prev    next >
C/C++ Source or Header  |  1996-07-23  |  8KB  |  184 lines

  1. /*************************************************************************
  2.                     Copyright Microsoft Corp. 1992-1996
  3.                         Remote Machine strout sample
  4.  
  5.   FILE      :   server.c
  6.  
  7.   USAGE     :   server  -p protocol_sequence
  8.                         -e endpoint
  9.                         
  10.   PURPOSE   :   Server side of the RPC distributed application strout.
  11.  
  12.   COMMENTS  :   This application uses the implicit binding method.
  13.  
  14. *************************************************************************/
  15. #include "strout.h"                 /* Generated by the midl compiler   */
  16. #include "common.h"                 /* Common definitions in this file  */
  17.  
  18. /* Local Procedures */
  19. void CleanUpServer(void);
  20.  
  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_OK);
  33. }
  34.  
  35.  
  36. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  37. /* The server main program                                              */
  38. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  39. int main(int argc, char *argv[])
  40. {
  41.     int             nNumArgs,   /* The number of command line arguments */
  42.                     nIdx;       /* Counter in loops                     */
  43.     RPC_STATUS      nStatus;    /* Error status returned                */
  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.  
  51. /* Get a common handle on the command line arguments for both UNICODE   */
  52. /* and ASCII                                                            */
  53. #ifdef _UNICODE
  54.     LPWSTR    *pArglist = CommandLineToArgvW(GetCommandLine(), &nNumArgs);
  55.     if (NULL == pArglist)
  56.     {
  57.         _tprintf(TEXT("SERVER.C : CommandLineToArgW failed"));
  58.         exit(EXECUTION_FAILED);
  59.     }
  60. #else
  61.     char **pArglist = argv;
  62.     nNumArgs = argc;
  63. #endif
  64.  
  65.     /* Allow the user to override settings with commandline switches    */
  66.     for (nIdx = 1; nIdx < nNumArgs; nIdx++) 
  67.     {
  68.         if((_tcscmp(pArglist[nIdx], TEXT("-p")) == 0) || 
  69.            (_tcscmp(pArglist[nIdx], TEXT("-P")) == 0))
  70.         {
  71.             pszProtocolSequence = pArglist[++nIdx];
  72.         }
  73.         else if((_tcscmp(pArglist[nIdx], TEXT("-e")) == 0) || 
  74.                 (_tcscmp(pArglist[nIdx], TEXT("-e")) == 0))
  75.         {
  76.             pszEndpoint = pArglist[++nIdx];
  77.         }
  78.         else 
  79.         {
  80.             Usage(pArglist[0]);
  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.         strout_sample_v1_0_s_ifspec,    // Interface specification
  90.         NULL,                           // UUID to ass. with MgrEnv arg.
  91.         NULL);                          // Managers entry point vector.
  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: \"%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);                         // Cont. until expl. 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.  
  134.     // If no exceptions occured, clean up the server and exit
  135.     CleanUpServer();
  136.  
  137.     // Deallocate the memory used for the ARGLIST if using UNICODE
  138. #ifdef _UNICODE
  139.     if (NULL != pArglist)
  140.         free(pArglist);
  141. #endif
  142.  
  143.     return (EXECUTION_OK);
  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 new remote calls
  160.         FALSE);     // Wait until all the active calls are complete 
  161.     EXIT_IF_FAIL(nStatus, "RpcServerUnRegisterIf");
  162. }
  163.  
  164. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  165. /* Procedure    :   midl_user_allocate() and midl_user_free()           */
  166. /* Desc.        :   These procedure are declared in the header file     */
  167. /*                  generated by the midl compiler. These procedures    */
  168. /*                  should be used for all memory allocation and        */
  169. /*                  deallocation.                                       */
  170. /*                  These procedures are also called by the stub code   */
  171. /*                  to allocate and free memory.                        */
  172. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  173. void __RPC_FAR * __RPC_API midl_user_allocate(size_t nLen)
  174. {
  175.     return (malloc(nLen));
  176. }
  177.  
  178. void __RPC_API midl_user_free(void __RPC_FAR * lpvPointer)
  179. {
  180.     if(NULL != lpvPointer)
  181.         free (lpvPointer);
  182. }
  183.  
  184.