home *** CD-ROM | disk | FTP | other *** search
/ C/C++ User's Journal & Wi…eveloper's Journal Tools / C-C__Users_Journal_and_Windows_Developers_Journal_Tools_1997.iso / sysembed / samptdi.elh < prev    next >
Encoding:
Text File  |  1995-03-30  |  7.6 KB  |  268 lines

  1. [ 7. Sample TDI Application]
  2.                  EMBEDDED LAN SAMPLE TDI APPLICATION
  3. ═════════════════════════════════════════════════════════════════════════
  4. The Embedded LAN Transport Driver Interface is easily called in C, just
  5. like the Physical Driver Interface.  The following simple TDI application
  6. shows how a program can be written that is both a server and client,
  7. depending on its command line argument.
  8.  
  9. //
  10. // PROGRAM NAME:  TESTTDI.C.
  11. //
  12. // FUNCTIONAL DESCRIPTION.
  13. //      This program is a test program for the TDI interface.
  14. //
  15. // MODIFICATION HISTORY.
  16. //      S. E. Jones     92/11/19.       Original for Embedded LAN.
  17. //
  18. // NOTICE:  Copyright (C) 1992 General Software, Inc.
  19. //          All rights reserved.
  20. //
  21.  
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <malloc.h>
  25. #include <string.h>
  26. #include <ctype.h>
  27. #include <time.h>
  28. #include <dos.h>
  29. #include <conio.h>
  30.  
  31. #define UCHAR unsigned char
  32. #define USHORT unsigned short
  33. #define ULONG unsigned long
  34. #define BOOLEAN unsigned short
  35. #define FALSE 0
  36. #define TRUE (!FALSE)
  37. #define NULL 0L
  38. #define VOID void
  39. #define PVOID void *
  40. #define IN
  41. #define OUT
  42.  
  43. #include "tdi.h"                        // transport driver interface.
  44. #include "pdi.h"                        // physical driver interface.
  45.  
  46. #define DATA_LEN        2000            // length of datagram data.
  47.  
  48. typedef struct _PACKET {
  49.     UCHAR Data [DATA_LEN];
  50. } PACKET, *PPACKET;
  51.  
  52. static PACKET PacketBuf;
  53. static TRANSPORT_ADDRESS LocalAddress;  // our own transport address.
  54. static TRANSPORT_ADDRESS RemoteAddress; // target transport address.
  55. static USHORT ReceiveCount=0;
  56. static USHORT BytesReceived=0;
  57. static USHORT OversizeReceived=0;
  58. static USHORT UndersizeReceived=0;
  59. static USHORT DataErrors=0;
  60. static USHORT ReceiveEors=0;
  61. static USHORT ExpeditedReceives=0;
  62. static USHORT ConnectCount=0;
  63. static USHORT DisconnectCount=0;
  64.  
  65. PIND_TDIRECEIVE TestReceiveHandler (
  66.     PVOID ConnectionContext,
  67.     TDIBITMASK ReceiveFlags,
  68.     PVOID Buffer,
  69.     USHORT BufferLength)
  70. {
  71.     ReceiveCount++;                     // count packets received.
  72.     BytesReceived += BufferLength;      // count bytes received.
  73.     if (ReceiveFlags & TDI_RECEIVE_OVERSIZE) {
  74.         OversizeReceived++;
  75.     }
  76.     if (ReceiveFlags & TDI_RECEIVE_UNDERSIZE) {
  77.         UndersizeReceived++;
  78.     }
  79.     if (ReceiveFlags & TDI_RECEIVE_DATA_ERROR) {
  80.         DataErrors++;
  81.     }
  82.     if (ReceiveFlags & TDI_RECEIVE_EOR) {
  83.         ReceiveEors++;
  84.     }
  85.     if (ReceiveFlags & TDI_RECEIVE_EXPEDITED) {
  86.         ExpeditedReceives++;
  87.     }
  88. } // TestReceiveHandler
  89.  
  90. TDISTATUS TestConnectHandler (
  91.     PVOID EndpointContext,
  92.     PTRANSPORT_ADDRESS RemoteAddress,
  93.     USHORT ConnectionId,
  94.     PVOID * ConnectionContext)
  95. {
  96.     ConnectCount++;
  97.  
  98.     *ConnectionContext = (ULONG)ConnectCount;
  99.  
  100.     if (ConnectCount & 1) {
  101.         return TDISTATUS_CONNECTION_ACCEPTED;
  102.     } else {
  103. //        return TDISTATUS_CONNECTION_REJECTED;
  104.         return TDISTATUS_CONNECTION_ACCEPTED;
  105.     }
  106. } // TestConnectHandler
  107.  
  108. TDISTATUS TestDisconnectHandler (
  109.     PVOID ConnectionContext,
  110.     TDIBITMASK DisconnectFlags)
  111. {
  112.     DisconnectCount++;
  113.  
  114.     if (DisconnectCount & 1) {
  115.         return TDISTATUS_SUCCESS;
  116.     } else {
  117.         return TDISTATUS_NO_SUCH_CONNECTION;
  118.     }
  119. } // TestDisconnectHandler
  120.  
  121. VOID main (argc, argv)
  122.     USHORT argc;
  123.     UCHAR *argv [];
  124. {
  125.     USHORT i, j, rc, ConnectionId, nreps=0;
  126.     TDIHANDLE Handle;
  127.     TDIBITMASK QualityOfService;
  128.     ULONG run=0L;
  129.     BOOLEAN EarlyBreakout=FALSE;
  130.  
  131.     LocalAddress.Flags = TRANSPORT_ADDRESS_FLAGS_NAME;
  132.     RemoteAddress.Flags = TRANSPORT_ADDRESS_FLAGS_NAME;
  133.  
  134.     strcpy (LocalAddress.AsciizNodeName, "CLIENT");
  135.     strcpy (RemoteAddress.AsciizNodeName, "SERVER");
  136.  
  137.     switch (argc) {
  138.         case 1:
  139.             break;                      // setup as CLIENT.
  140.         case 2:
  141.             strupr (argv [1]);
  142.             if (!strcmp (argv [1], "SERVER")) {
  143.                 strcpy (LocalAddress.AsciizNodeName, "SERVER");
  144.                 strcpy (RemoteAddress.AsciizNodeName, "CLIENT");
  145.             } else {
  146.                 strcpy (LocalAddress.AsciizNodeName, argv [1]);
  147.                 strcpy (RemoteAddress.AsciizNodeName, "SERVER");
  148.             }
  149.             break;
  150.         default:
  151.             printf ("Usage: TESTTDI  [CLIENT|SERVER]\n");
  152.             exit (0);
  153.     }
  154.     printf ("This machine is the %s.\n", LocalAddress.AsciizNodeName);
  155.  
  156.     //
  157.     // Open an endpoint to the transport driver interface.
  158.     //
  159.  
  160.     rc = TdiOpenEndpoint (
  161.             "NE2000$",
  162.             &LocalAddress,
  163.             (TDI_QOS_CONNECTION_MODE |
  164.              TDI_QOS_CONNECTIONLESS_MODE |
  165.              TDI_QOS_ERROR_FREE_DELIVERY),
  166.             0L,
  167.             &Handle);
  168.     if (rc != TDISTATUS_SUCCESS) {
  169.         printf ("TdiOpenEndpoint returned rc=%u.\n", rc);
  170.         exit (1);
  171.     }
  172.  
  173.     rc = TdiSetIndicationHandler (
  174.             Handle,
  175.             TDI_IND_RECEIVE,
  176.             (PTDI_INDICATION)TestReceiveHandler);
  177.  
  178.     if (rc != TDISTATUS_SUCCESS) {
  179.         printf ("TdiSetIndicationHandler returned rc=%u.\n", rc);
  180.         TdiCloseEndpoint (Handle);
  181.         exit (3);
  182.     }
  183.  
  184.     rc = TdiSetIndicationHandler (
  185.             Handle,
  186.             TDI_IND_CONNECT,
  187.             (PTDI_INDICATION)TestConnectHandler);
  188.  
  189.     if (rc != TDISTATUS_SUCCESS) {
  190.         printf ("TdiSetIndicationHandler returned rc=%u.\n", rc);
  191.         TdiCloseEndpoint (Handle);
  192.         exit (3);
  193.     }
  194.  
  195.     rc = TdiSetIndicationHandler (
  196.             Handle,
  197.             TDI_IND_DISCONNECT,
  198.             (PTDI_INDICATION)TestDisconnectHandler);
  199.  
  200.     if (rc != TDISTATUS_SUCCESS) {
  201.         printf ("TdiSetIndicationHandler returned rc=%u.\n", rc);
  202.         TdiCloseEndpoint (Handle);
  203.         exit (3);
  204.     }
  205.  
  206.     //
  207.     // Establish a connection with our remote partner until the
  208.     // operator presses a key.
  209.     //
  210.  
  211.     while (!kbhit ()) {
  212.         if (!strcmp (RemoteAddress.AsciizNodeName,"SERVER")) {
  213.             printf ("\n%lu. Connecting from '%s' to '%s', ",
  214.                     run++, LocalAddress.AsciizNodeName,
  215.                     RemoteAddress.AsciizNodeName);
  216.  
  217.             //
  218.             // A faster real-time display would be:
  219.             // printf ("\r%lu",run++);
  220.             //
  221.  
  222.             rc = TdiConnect (
  223.                     Handle,
  224.                     &RemoteAddress,
  225.                     NULL,                           // our context.
  226.                     &ConnectionId);                 // where TDI puts the connection.
  227.  
  228.             if (rc != TDISTATUS_SUCCESS) {
  229.                 printf ("[%u] failure.", rc);
  230.                 EarlyBreakout = TRUE;
  231.             } else {
  232.                 printf ("[%u] success, ID=%u.", rc, ConnectionId);
  233.                 rc = TdiDisconnect (Handle, ConnectionId, 0);
  234.                 if (rc != TDISTATUS_SUCCESS) {
  235.                     printf ("\nTdiDisconnect failed, rc=[%u]",rc);
  236.                     break;
  237.                 }
  238.  
  239.                 printf ("Connection started and stopped.\n");
  240.                 nreps++;
  241.                 if (nreps == 1000) {
  242.                     EarlyBreakout = TRUE;
  243.                     break;
  244.                 }
  245.             }
  246.         }
  247.     }
  248.  
  249.     //
  250.     // Close the endpoint.
  251.     //
  252.  
  253.     rc = TdiCloseEndpoint (Handle);
  254.     if (rc != TDISTATUS_SUCCESS) {
  255.         printf ("TdiCloseEndpoint returned rc=%u.\n", rc);
  256.     }
  257.  
  258.     //
  259.     // Eat the character and exit the program.
  260.     //
  261.  
  262.     if (!EarlyBreakout) {
  263.         getch ();                       // eat the character.
  264.     }
  265.     printf ("\nTDI test completed.\n");
  266.     exit (0);                           // exit to DOS with success.
  267. } /* testtdi.c */
  268.