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 / ns / nhello / nsserv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-11  |  8.7 KB  |  271 lines

  1. /****************************************************************************
  2.                    Microsoft RPC Version 2.0
  3.            Copyright Microsoft Corp. 1992, 1993, 1994- 1996
  4.                        nhello Example
  5.  
  6.     FILE:       nsserv.c
  7.  
  8.     USAGE:      nsserv
  9.                          -m maxcalls
  10.                          -n mincalls
  11.                          -f flag for RpcServerListen wait
  12.                          -a nhello_sample_nsi_entry_name
  13.                          -t name_syntax_type
  14.  
  15.     PURPOSE:    Server side of RPC distributed application nhello
  16.  
  17.     FUNCTIONS:  main() - registers server as RPC server
  18.  
  19.     COMMENTS:
  20.  
  21. ****************************************************************************/
  22.  
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <ctype.h>
  26. #include <tchar.h>
  27. #include "nhello.h"   // header file generated by MIDL compiler
  28. #include "service.h"
  29.  
  30.  
  31. VOID ServiceStart (DWORD dwArgc, LPTSTR *lpszArgv)
  32. {
  33.     RPC_STATUS status;
  34.     RPC_STATUS temp_status;
  35.     RPC_BINDING_VECTOR * pBindingVector = NULL;
  36.     unsigned char * pszEntryName    = "/.:/nhello_sample";
  37.     unsigned char * pszSecurity     = NULL;
  38.     unsigned int    cMinCalls       = 1;
  39.     unsigned int    cMaxCalls       = 20;
  40.     unsigned int    fDontWait       = 0;
  41.     unsigned int    fNameSyntaxType = RPC_C_NS_SYNTAX_DEFAULT;
  42.     BOOL bRegistered = FALSE;
  43.     BOOL bEndpoint = FALSE;
  44.     BOOL bExported = FALSE;
  45.     BOOL bProto = FALSE;
  46.     DWORD i;
  47.  
  48.  
  49.     /* allow the user to override settings with command line switches */
  50.     for (i = 1; i < dwArgc; i++) {
  51.         if ((*(lpszArgv[i]) == '-') || (*(lpszArgv[i]) == '/')) {
  52.             switch (tolower(*((lpszArgv[i])+1))) {
  53.             case 'm':
  54.                 cMaxCalls = (unsigned int) atoi(lpszArgv[++i]);
  55.                 break;
  56.             case 'n':
  57.                 cMinCalls = (unsigned int) atoi(lpszArgv[++i]);
  58.                 break;
  59.             case 'f':
  60.                 fDontWait = (unsigned int) atoi(lpszArgv[++i]);
  61.                 break;
  62.             case 'a':
  63.                 pszEntryName = lpszArgv[++i];
  64.                 break;
  65.             case 't':
  66.                 fNameSyntaxType = (unsigned int) atoi(lpszArgv[++i]);
  67.                 break;
  68.             }
  69.         }
  70.     }
  71.  
  72.     // Prepare to Use All Protseqs
  73.     if (!ReportStatusToSCMgr(
  74.         SERVICE_START_PENDING, // service state
  75.         NO_ERROR,              // exit code
  76.         30000))                // wait hint  - UseAll can take quite a while
  77.         goto cleanup;
  78.  
  79.     printf("CallingRpcServerUseAllProtseqs...\n");
  80.  
  81.     status = RpcServerUseAllProtseqs( cMaxCalls, pszSecurity );
  82.     printf("RpcServerUseAllProtseqs returned 0x%x\n", status);
  83.     if (status)
  84.         goto cleanup;
  85.  
  86.     // Prepare to Register Interface
  87.     if (!ReportStatusToSCMgr(
  88.         SERVICE_START_PENDING, // service state
  89.         NO_ERROR,              // exit code
  90.         30000))                 // wait hint
  91.         goto cleanup;
  92.  
  93.     status = RpcServerRegisterIf(nhello_v1_0_s_ifspec, // interface to register
  94.                                  NULL,   // MgrTypeUuid
  95.                                  NULL);  // MgrEpv; null means use default
  96.     printf( TEXT("RpcServerRegisterIf returned 0x%x\n"), status);
  97.     if (status)
  98.         goto cleanup;
  99.     else
  100.         bRegistered = TRUE;
  101.  
  102.  
  103.     // Prepare to Inquire Bindings
  104.     if (!ReportStatusToSCMgr(
  105.         SERVICE_START_PENDING, // service state
  106.         NO_ERROR,              // exit code
  107.         30000))                // wait hint  - Inq can take quite a while
  108.         goto cleanup;
  109.  
  110.     status = RpcServerInqBindings(&pBindingVector);
  111.     printf( TEXT("RpcServerInqBindings returned 0x%x\n"), status);
  112.     if (status)
  113.         goto cleanup;
  114.  
  115.  
  116.     // Prepare to Register Endpoint
  117.     if (!ReportStatusToSCMgr(
  118.         SERVICE_START_PENDING, // service state
  119.         NO_ERROR,              // exit code
  120.         45000))                // wait hint  - Inq can take quite a while
  121.         goto cleanup;
  122.  
  123.     status = RpcEpRegister(nhello_v1_0_s_ifspec,
  124.                            pBindingVector,
  125.                            NULL,
  126.                            NULL);
  127.     printf( TEXT("RpcEpRegister returned 0x%x\n"), status);
  128.     if (status)
  129.         goto cleanup;
  130.     else
  131.         bEndpoint = TRUE;
  132.  
  133.  
  134.     // Prepare to Export to NameService
  135.     if (!ReportStatusToSCMgr(
  136.         SERVICE_START_PENDING, // service state
  137.         NO_ERROR,              // exit code
  138.         45000))                // wait hint
  139.         goto cleanup;
  140.  
  141.     status = RpcNsBindingExport(fNameSyntaxType,  // name syntax type
  142.                                 pszEntryName,     // nsi entry name
  143.                                 nhello_v1_0_s_ifspec,
  144.                                 pBindingVector,   // set in previous call
  145.                                 NULL);            // UUID vector
  146.     printf( TEXT("RpcNsBindingExport returned 0x%x\n"), status);
  147.     if (status)
  148.         goto cleanup;
  149.     else
  150.         bExported = TRUE;
  151.  
  152.  
  153.     // Prepare to start listening.  At this point the service is initialized
  154.     if (!ReportStatusToSCMgr(
  155.         SERVICE_RUNNING, // service state
  156.         NO_ERROR,        // exit code
  157.         0))              // wait hint
  158.         goto cleanup;
  159.  
  160.     printf( TEXT("Calling RpcServerListen\n"));
  161.     status = RpcServerListen(cMinCalls,
  162.                              cMaxCalls,
  163.                              fDontWait );  // wait flag
  164.     printf( TEXT("RpcServerListen returned: 0x%x\n"), status);
  165.     if (status) {
  166.         goto cleanup;
  167.     }
  168.  
  169.     if (fDontWait) {
  170.         printf("Calling RpcMgmtWaitServerListen\n");
  171.         status = RpcMgmtWaitServerListen();  //  wait operation
  172.         printf("RpcMgmtWaitServerListen returned: 0x%x\n", status);
  173.     }
  174.  
  175.  
  176.   cleanup:
  177.  
  178.     if ( bExported )
  179.     {
  180.         ReportStatusToSCMgr(
  181.             SERVICE_STOP_PENDING,  // service state
  182.             NO_ERROR,              // exit code
  183.             3000);                 // wait hint
  184.  
  185.         temp_status = RpcNsBindingUnexport(RPC_C_NS_SYNTAX_DEFAULT,  // name syntax type
  186.                                            pszEntryName,     // nsi entry name
  187.                                            nhello_v1_0_s_ifspec,
  188.                                            NULL);            // UUID vector
  189.         printf( TEXT("RpcNsBindingUnexport returned 0x%x\n"), temp_status);
  190.     }
  191.  
  192.  
  193.     if ( bEndpoint )
  194.     {
  195.         ReportStatusToSCMgr(
  196.             SERVICE_STOP_PENDING,  // service state
  197.             NO_ERROR,              // exit code
  198.             3000);                 // wait hint
  199.  
  200.         temp_status = RpcEpUnregister(nhello_v1_0_s_ifspec,
  201.                                       pBindingVector,
  202.                                       NULL);
  203.         printf( TEXT("RpcEpUnregister returned 0x%x\n"), temp_status);
  204.     }
  205.  
  206.     if ( pBindingVector )
  207.     {
  208.         ReportStatusToSCMgr(
  209.             SERVICE_STOP_PENDING,  // service state
  210.             NO_ERROR,              // exit code
  211.             3000);                 // wait hint
  212.  
  213.         temp_status = RpcBindingVectorFree(&pBindingVector);
  214.         printf( TEXT("RpcBindingVectorFree returned 0x%x\n"), temp_status);
  215.     }
  216.  
  217.     if ( bRegistered )
  218.     {
  219.         ReportStatusToSCMgr(
  220.             SERVICE_STOP_PENDING,  // service state
  221.             NO_ERROR,              // exit code
  222.             3000);                 // wait hint
  223.  
  224.         temp_status = RpcServerUnregisterIf(nhello_v1_0_s_ifspec, // interface to register
  225.                                      NULL,   // MgrTypeUuid
  226.                                      1);     // wait for outstanding calls
  227.         printf( TEXT("RpcServerUnregisterIf returned 0x%x\n"), temp_status);
  228.     }
  229.  
  230.     ReportStatusToSCMgr(
  231.         SERVICE_STOP_PENDING,  // service state
  232.         NO_ERROR,              // exit code
  233.         3000);                 // wait hint
  234.  
  235.  
  236. }
  237.  
  238.  
  239. void ServiceStop( )
  240. {
  241.     RPC_STATUS status;
  242.  
  243.     ReportStatusToSCMgr(
  244.         SERVICE_STOP_PENDING,  // service state
  245.         NO_ERROR,              // exit code
  246.         3000);                 // wait hint
  247.  
  248.     status = RpcMgmtIsServerListening( NULL );
  249.     printf( TEXT("RpcMgmtIsServerListening returned 0x%x\n"), status);
  250.  
  251.     if ( status == RPC_S_OK )
  252.         RpcMgmtStopServerListening( NULL );
  253.  
  254. }
  255.  
  256. /*********************************************************************/
  257. /*                 MIDL allocate and free                            */
  258. /*********************************************************************/
  259.  
  260. void __RPC_FAR * __RPC_USER midl_user_allocate(size_t len)
  261. {
  262.     return(malloc(len));
  263. }
  264.  
  265. void __RPC_USER midl_user_free(void __RPC_FAR * ptr)
  266. {
  267.     free(ptr);
  268. }
  269.  
  270. /* end file nsserv.c */
  271.