Annotation of mstools/samples/rpc/data/xmit/xmits.c, revision 1.1.1.1

1.1       root        1: /****************************************************************************
                      2:                          Microsoft RPC Version 1.0
                      3:                         Copyright Microsoft Corp. 1992
                      4:                                xmit Example
                      5: 
                      6:     FILE:      xmits.c
                      7:     USAGE:     xmits    -n network_address
                      8:                         -p protocol_sequence
                      9:                         -e endpoint
                     10:                         -o options
                     11:                         -u uuid
                     12: 
                     13:     PURPOSE:   Server side of RPC distributed application xmit
                     14:     FUNCTIONS: main() - registers server as RPC server
                     15: 
                     16:                PDOUBLE_LINK_TYPE_to_xmit - convert list to array
                     17:                PDOUBLE_LINK_TYPE_from_xmit - convert array to list
                     18:                PDOUBLE_LINK_TYPE_free_inst - free linked list memory
                     19:                PDOUBLE_LINK_TYPE_free_xmit - free array memory
                     20:                MIDL_user_allocate - user-supplied memory allocator
                     21:                MIDL_user_free - user-supplied routine to free memory
                     22: 
                     23:                ArrayWalkProc - utility to display the array
                     24:                ListWalkProc - utility to display the linked list
                     25:                InsertNewNode - utility to add a node to the list
                     26: 
                     27:     COMMENTS:  This sample program generates a linked list to
                     28:                demonstrate how the list can be transmitted over
                     29:                the network more efficiently as a sized array.
                     30:                The pointers are rebuilt on the server side.
                     31: 
                     32:                The [transmit_as] attribute (used in the typedef of
                     33:                PDOUBLE_LINK_TYPE in the file XMIT.IDL) requires the
                     34:                four user-supplied functions whose names start with
                     35:                the name of the presented type, PDOUBLE_LINK_TYPE.
                     36: 
                     37:                The [in, out] attributes applied to remote procedure
                     38:                parameters require the two user-supplied functions
                     39:                MIDL_user_allocate and MIDL_user_free.
                     40: 
                     41:                The other functions are utilities that are used to
                     42:                build or display the data structures.
                     43: ****************************************************************************/
                     44: #include <stdlib.h>
                     45: #include <windows.h>
                     46: #include <string.h>
                     47: #include <stdio.h>
                     48: #include <ctype.h>
                     49: #include <rpc.h>    // RPC data structures and APIs
                     50: #include "xmit.h"    // header file generated by MIDL compiler
                     51: 
                     52: #define PURPOSE \
                     53: "This Microsoft RPC Version 1.0 sample program demonstrates\n\
                     54: the use of the [transmit_as] attribute. For more information\n\
                     55: about the attributes and the RPC API functions, see the\n\
                     56: RPC programming guide and reference.\n\n"
                     57: 
                     58: 
                     59: void Usage(char * pszProgramName)
                     60: {
                     61:     fprintf(stderr, "%s", PURPOSE);
                     62:     fprintf(stderr, "Usage:  %s\n", pszProgramName);
                     63:     fprintf(stderr, " -p protocol_sequence\n");
                     64:     fprintf(stderr, " -n network_address\n");
                     65:     fprintf(stderr, " -e endpoint\n");
                     66:     fprintf(stderr, " -o options\n");
                     67:     fprintf(stderr, " -u uuid\n");
                     68:     exit(1);
                     69: }
                     70: 
                     71: /* adds a new node to the linked list */
                     72: /* assign the new node the value sValue and place after pPrevious */
                     73: DOUBLE_LINK_TYPE * InsertNewNode(short sValue, DOUBLE_LINK_TYPE * pPrevious)
                     74: {
                     75: DOUBLE_LINK_TYPE * pNew;
                     76: 
                     77:     pNew = (DOUBLE_LINK_TYPE *) malloc(sizeof(DOUBLE_LINK_TYPE));
                     78:     if (pNew != NULL) {
                     79:        pNew->sNumber = sValue;     /* insert b between a and c */
                     80:        pNew->pPrevious = pPrevious;             /* prev(b) = a */
                     81:        if (pPrevious != NULL) {
                     82:            pNew->pNext = pPrevious->pNext;      /* next(b) = c */
                     83:            pPrevious->pNext = pNew;             /* next(a) = b */
                     84:            if (pNew->pNext != NULL)
                     85:                (pNew->pNext)->pPrevious = pNew; /* prev(c) = b */
                     86:        }
                     87:        else
                     88:            pNew->pNext = NULL;
                     89:     }
                     90:     return(pNew);
                     91: }
                     92: 
                     93: /* display the contents of the array */
                     94: void ArrayWalkProc(PDOUBLE_XMIT_TYPE pArray)
                     95: {
                     96:     int i;
                     97:     printf("Display contents of transmitted array:\n");
                     98: 
                     99:     for (i = 0; i < pArray->sSize; i++)
                    100:        printf("pArray->asNumber[%d] = %d\n", i, pArray->asNumber[i]);
                    101: }
                    102: 
                    103: /* display the contents of the linked list */
                    104: void ListWalkProc(DOUBLE_LINK_TYPE * pList)
                    105: {
                    106:     printf("Display contents of doubly linked list:\n");
                    107:     while (pList != NULL) {
                    108:        printf("pList @0x%x = %d, Next = 0x%x\n", pList, pList->sNumber, pList->pNext);
                    109:        pList = pList->pNext;
                    110:     }
                    111: }
                    112: 
                    113: void * MIDL_user_allocate(size_t len)
                    114: {
                    115:     return(malloc(len));
                    116: }
                    117: 
                    118: void MIDL_user_free(void * ptr)
                    119: {
                    120:     free(ptr);
                    121: }
                    122: 
                    123: /* free the doubly linked list */
                    124: void DOUBLE_LINK_TYPE_free_inst (DOUBLE_LINK_TYPE * pList)
                    125: {
                    126:     while (pList->pNext != NULL)  /* go to end of list */
                    127:        pList = pList->pNext;
                    128:     for (pList = pList->pPrevious; pList != NULL; pList = pList->pPrevious)
                    129:        free(pList->pNext);
                    130: }
                    131: 
                    132: /* free the array structure */
                    133: void DOUBLE_LINK_TYPE_free_xmit (DOUBLE_XMIT_TYPE * pSizedArray)
                    134: {
                    135:     free(pSizedArray);
                    136: }
                    137: 
                    138: 
                    139: /* convert from linked list to array */
                    140: void DOUBLE_LINK_TYPE_to_xmit   (DOUBLE_LINK_TYPE  *  pList,
                    141:                                  DOUBLE_XMIT_TYPE **  ppArray)
                    142: {
                    143:     short cCount = 0;
                    144:     DOUBLE_LINK_TYPE * pHead = pList;  /* save pointer to start */
                    145:     DOUBLE_XMIT_TYPE * pArray;
                    146: 
                    147:     /* count the number of elements to allocate memory */
                    148:     for (; pList != NULL; pList = pList->pNext)
                    149:        cCount++;
                    150: 
                    151:     /* allocate the memory for the array */
                    152:     pArray = (DOUBLE_XMIT_TYPE *) malloc(sizeof(DOUBLE_XMIT_TYPE) + (cCount * sizeof(short)));
                    153:     pArray->sSize = cCount;
                    154: 
                    155:     /* copy the linked list contents into the array */
                    156:     cCount = 0;
                    157:     for (pList = pHead; pList != NULL; pList = pList->pNext)
                    158:        pArray->asNumber[cCount++] = pList->sNumber;
                    159: 
                    160:     /* return the address of the pointer to the array */
                    161:     *ppArray = pArray;
                    162: }
                    163: 
                    164: /* convert from array to linked list */
                    165: void DOUBLE_LINK_TYPE_from_xmit (DOUBLE_XMIT_TYPE * pArray,
                    166:                                 DOUBLE_LINK_TYPE * pDblLinkedList)
                    167: {
                    168:     PDOUBLE_LINK_TYPE pCurrent, pNew;
                    169: 
                    170:     int i;
                    171: 
                    172:     pCurrent = pDblLinkedList;
                    173:     pCurrent->sNumber = pArray->asNumber[0];
                    174: 
                    175:     DOUBLE_LINK_TYPE_free_inst(pCurrent);
                    176:     pCurrent->pNext = NULL;     /* wipe out pointer to old list */
                    177: 
                    178:     for (i = 1; i < pArray->sSize; i++) {
                    179:        pNew = InsertNewNode(pArray->asNumber[i], pCurrent);
                    180:        pCurrent = pNew;
                    181:     }
                    182:     return;
                    183: }
                    184: 
                    185: /* main:  register the interface, start listening for clients */
                    186: void main(int argc, char * argv[])
                    187: {
                    188:     RPC_STATUS status;
                    189:     unsigned char * pszUuid = "12345678-1234-1234-1234-123456789ABC";
                    190:     unsigned char * pszProtocolSequence = "ncacn_np";
                    191:     unsigned char * pszNetworkAddress   = NULL;
                    192:     unsigned char * pszEndpoint        = "\\pipe\\xmit";
                    193:     unsigned char * pszOptions          = NULL;
                    194:     unsigned char * pszStringBinding    = NULL;
                    195:     int i;
                    196: 
                    197:     // allow the user to override settings with command line switches
                    198:     for (i = 1; i < argc; i++) {
                    199:        if ((*argv[i] == '-') || (*argv[i] == '/')) {
                    200:            switch (tolower(*(argv[i]+1))) {
                    201:                case 'p':  // protocol sequence
                    202:                    pszProtocolSequence = argv[++i];
                    203:                    break;
                    204:                case 'n':  // network address
                    205:                    pszNetworkAddress = argv[++i];
                    206:                    break;
                    207:                case 'e':
                    208:                    pszEndpoint = argv[++i];
                    209:                    break;
                    210:                case 'o':
                    211:                    pszOptions = argv[++i];
                    212:                    break;
                    213:                case 'u':
                    214:                    pszUuid = argv[++i];
                    215:                    break;
                    216:                case 'h':
                    217:                case '?':
                    218:                default:
                    219:                    Usage(argv[0]);
                    220:            }
                    221:        }
                    222:        else
                    223:            Usage(argv[0]);
                    224:     }
                    225: 
                    226:     status = RpcServerUseProtseqEp(pszProtocolSequence,
                    227:                                    1, // maximum concurrent calls
                    228:                                    pszEndpoint,
                    229:                                   0);
                    230:     printf("RpcServerUseProtseqEp returned 0x%x\n", status);
                    231:     if (status) {
                    232:         exit(2);
                    233:     }
                    234: 
                    235:     status = RpcServerRegisterIf(xmit_ServerIfHandle, 0, 0);
                    236:     printf("RpcServerRegisterIf returned 0x%x\n", status);
                    237:     if (status) {
                    238:         exit(2);
                    239:     }
                    240: 
                    241:     printf("Calling RpcServerListen\n");
                    242:     status = RpcServerListen(1,
                    243:                             20);
                    244:     printf("RpcServerListen returned: 0x%x\n", status);
                    245:     if (status) {
                    246:         exit(2);
                    247:     }
                    248: 
                    249: } /* end main() */
                    250: 
                    251: /* end xmits.c */

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.