home *** CD-ROM | disk | FTP | other *** search
- [ 7. Sample TDI Application]
- EMBEDDED LAN SAMPLE TDI APPLICATION
- ═════════════════════════════════════════════════════════════════════════
- The Embedded LAN Transport Driver Interface is easily called in C, just
- like the Physical Driver Interface. The following simple TDI application
- shows how a program can be written that is both a server and client,
- depending on its command line argument.
-
- //
- // PROGRAM NAME: TESTTDI.C.
- //
- // FUNCTIONAL DESCRIPTION.
- // This program is a test program for the TDI interface.
- //
- // MODIFICATION HISTORY.
- // S. E. Jones 92/11/19. Original for Embedded LAN.
- //
- // NOTICE: Copyright (C) 1992 General Software, Inc.
- // All rights reserved.
- //
-
- #include <stdlib.h>
- #include <stdio.h>
- #include <malloc.h>
- #include <string.h>
- #include <ctype.h>
- #include <time.h>
- #include <dos.h>
- #include <conio.h>
-
- #define UCHAR unsigned char
- #define USHORT unsigned short
- #define ULONG unsigned long
- #define BOOLEAN unsigned short
- #define FALSE 0
- #define TRUE (!FALSE)
- #define NULL 0L
- #define VOID void
- #define PVOID void *
- #define IN
- #define OUT
-
- #include "tdi.h" // transport driver interface.
- #include "pdi.h" // physical driver interface.
-
- #define DATA_LEN 2000 // length of datagram data.
-
- typedef struct _PACKET {
- UCHAR Data [DATA_LEN];
- } PACKET, *PPACKET;
-
- static PACKET PacketBuf;
- static TRANSPORT_ADDRESS LocalAddress; // our own transport address.
- static TRANSPORT_ADDRESS RemoteAddress; // target transport address.
- static USHORT ReceiveCount=0;
- static USHORT BytesReceived=0;
- static USHORT OversizeReceived=0;
- static USHORT UndersizeReceived=0;
- static USHORT DataErrors=0;
- static USHORT ReceiveEors=0;
- static USHORT ExpeditedReceives=0;
- static USHORT ConnectCount=0;
- static USHORT DisconnectCount=0;
-
- PIND_TDIRECEIVE TestReceiveHandler (
- PVOID ConnectionContext,
- TDIBITMASK ReceiveFlags,
- PVOID Buffer,
- USHORT BufferLength)
- {
- ReceiveCount++; // count packets received.
- BytesReceived += BufferLength; // count bytes received.
- if (ReceiveFlags & TDI_RECEIVE_OVERSIZE) {
- OversizeReceived++;
- }
- if (ReceiveFlags & TDI_RECEIVE_UNDERSIZE) {
- UndersizeReceived++;
- }
- if (ReceiveFlags & TDI_RECEIVE_DATA_ERROR) {
- DataErrors++;
- }
- if (ReceiveFlags & TDI_RECEIVE_EOR) {
- ReceiveEors++;
- }
- if (ReceiveFlags & TDI_RECEIVE_EXPEDITED) {
- ExpeditedReceives++;
- }
- } // TestReceiveHandler
-
- TDISTATUS TestConnectHandler (
- PVOID EndpointContext,
- PTRANSPORT_ADDRESS RemoteAddress,
- USHORT ConnectionId,
- PVOID * ConnectionContext)
- {
- ConnectCount++;
-
- *ConnectionContext = (ULONG)ConnectCount;
-
- if (ConnectCount & 1) {
- return TDISTATUS_CONNECTION_ACCEPTED;
- } else {
- // return TDISTATUS_CONNECTION_REJECTED;
- return TDISTATUS_CONNECTION_ACCEPTED;
- }
- } // TestConnectHandler
-
- TDISTATUS TestDisconnectHandler (
- PVOID ConnectionContext,
- TDIBITMASK DisconnectFlags)
- {
- DisconnectCount++;
-
- if (DisconnectCount & 1) {
- return TDISTATUS_SUCCESS;
- } else {
- return TDISTATUS_NO_SUCH_CONNECTION;
- }
- } // TestDisconnectHandler
-
- VOID main (argc, argv)
- USHORT argc;
- UCHAR *argv [];
- {
- USHORT i, j, rc, ConnectionId, nreps=0;
- TDIHANDLE Handle;
- TDIBITMASK QualityOfService;
- ULONG run=0L;
- BOOLEAN EarlyBreakout=FALSE;
-
- LocalAddress.Flags = TRANSPORT_ADDRESS_FLAGS_NAME;
- RemoteAddress.Flags = TRANSPORT_ADDRESS_FLAGS_NAME;
-
- strcpy (LocalAddress.AsciizNodeName, "CLIENT");
- strcpy (RemoteAddress.AsciizNodeName, "SERVER");
-
- switch (argc) {
- case 1:
- break; // setup as CLIENT.
- case 2:
- strupr (argv [1]);
- if (!strcmp (argv [1], "SERVER")) {
- strcpy (LocalAddress.AsciizNodeName, "SERVER");
- strcpy (RemoteAddress.AsciizNodeName, "CLIENT");
- } else {
- strcpy (LocalAddress.AsciizNodeName, argv [1]);
- strcpy (RemoteAddress.AsciizNodeName, "SERVER");
- }
- break;
- default:
- printf ("Usage: TESTTDI [CLIENT|SERVER]\n");
- exit (0);
- }
- printf ("This machine is the %s.\n", LocalAddress.AsciizNodeName);
-
- //
- // Open an endpoint to the transport driver interface.
- //
-
- rc = TdiOpenEndpoint (
- "NE2000$",
- &LocalAddress,
- (TDI_QOS_CONNECTION_MODE |
- TDI_QOS_CONNECTIONLESS_MODE |
- TDI_QOS_ERROR_FREE_DELIVERY),
- 0L,
- &Handle);
- if (rc != TDISTATUS_SUCCESS) {
- printf ("TdiOpenEndpoint returned rc=%u.\n", rc);
- exit (1);
- }
-
- rc = TdiSetIndicationHandler (
- Handle,
- TDI_IND_RECEIVE,
- (PTDI_INDICATION)TestReceiveHandler);
-
- if (rc != TDISTATUS_SUCCESS) {
- printf ("TdiSetIndicationHandler returned rc=%u.\n", rc);
- TdiCloseEndpoint (Handle);
- exit (3);
- }
-
- rc = TdiSetIndicationHandler (
- Handle,
- TDI_IND_CONNECT,
- (PTDI_INDICATION)TestConnectHandler);
-
- if (rc != TDISTATUS_SUCCESS) {
- printf ("TdiSetIndicationHandler returned rc=%u.\n", rc);
- TdiCloseEndpoint (Handle);
- exit (3);
- }
-
- rc = TdiSetIndicationHandler (
- Handle,
- TDI_IND_DISCONNECT,
- (PTDI_INDICATION)TestDisconnectHandler);
-
- if (rc != TDISTATUS_SUCCESS) {
- printf ("TdiSetIndicationHandler returned rc=%u.\n", rc);
- TdiCloseEndpoint (Handle);
- exit (3);
- }
-
- //
- // Establish a connection with our remote partner until the
- // operator presses a key.
- //
-
- while (!kbhit ()) {
- if (!strcmp (RemoteAddress.AsciizNodeName,"SERVER")) {
- printf ("\n%lu. Connecting from '%s' to '%s', ",
- run++, LocalAddress.AsciizNodeName,
- RemoteAddress.AsciizNodeName);
-
- //
- // A faster real-time display would be:
- // printf ("\r%lu",run++);
- //
-
- rc = TdiConnect (
- Handle,
- &RemoteAddress,
- NULL, // our context.
- &ConnectionId); // where TDI puts the connection.
-
- if (rc != TDISTATUS_SUCCESS) {
- printf ("[%u] failure.", rc);
- EarlyBreakout = TRUE;
- } else {
- printf ("[%u] success, ID=%u.", rc, ConnectionId);
- rc = TdiDisconnect (Handle, ConnectionId, 0);
- if (rc != TDISTATUS_SUCCESS) {
- printf ("\nTdiDisconnect failed, rc=[%u]",rc);
- break;
- }
-
- printf ("Connection started and stopped.\n");
- nreps++;
- if (nreps == 1000) {
- EarlyBreakout = TRUE;
- break;
- }
- }
- }
- }
-
- //
- // Close the endpoint.
- //
-
- rc = TdiCloseEndpoint (Handle);
- if (rc != TDISTATUS_SUCCESS) {
- printf ("TdiCloseEndpoint returned rc=%u.\n", rc);
- }
-
- //
- // Eat the character and exit the program.
- //
-
- if (!EarlyBreakout) {
- getch (); // eat the character.
- }
- printf ("\nTDI test completed.\n");
- exit (0); // exit to DOS with success.
- } /* testtdi.c */
-