home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sd386v50.zip / sd386src.zip / ROUTER.C < prev    next >
Text File  |  1996-05-13  |  12KB  |  326 lines

  1. /*****************************************************************************/
  2. /* File:                                             IBM INTERNAL USE ONLY   */
  3. /*   router.c                                                             919*/
  4. /*                                                                           */
  5. /* Description:                                                              */
  6. /*                                                                           */
  7. /*  These functions initialize the connection to a remote esp and            */
  8. /*  routes the remote send/receive calls through that connection.            */
  9. /*                                                                           */
  10. /* History:                                                                  */
  11. /*                                                                           */
  12. /*   07/08/94 Created.                                                       */
  13. /*                                                                           */
  14. /*****************************************************************************/
  15. #include "all.h"
  16.  
  17. static CONNECTION Connection = {BOUND};
  18. static LHANDLE    LsnHandle;
  19. static BOOL       HaltCommunicationsFlag;
  20.  
  21. void  SetHaltCommunicationsFlag( void ) { HaltCommunicationsFlag=TRUE;}
  22.  
  23.  
  24. /*****************************************************************************/
  25. /* - define the connection to the router.                                    */
  26. /*****************************************************************************/
  27. void SendConnectionToRouter( CONNECTION *pConnection )
  28. {
  29.  Connection = *pConnection;
  30. }
  31.  
  32. /*****************************************************************************/
  33. /* - return the local or remote esp connection.                              */
  34. /*****************************************************************************/
  35. BOOL IsEspRemote( void )
  36. {
  37.  BOOL rc = FALSE;
  38.  
  39.  if( Connection.ConnectType != BOUND )
  40.   rc = TRUE;
  41.  return(rc);
  42. }
  43.  
  44. /*****************************************************************************/
  45. /* - return the type of the connection.                                      */
  46. /*****************************************************************************/
  47. int ConnectType( void )
  48. {
  49.  return( Connection.ConnectType );
  50. }
  51.  
  52. /*****************************************************************************/
  53. /* - return serial/parallel.                                                 */
  54. /*****************************************************************************/
  55. int SerialParallel( void )
  56. {
  57.  int type;
  58.  
  59.  type = SERIAL;
  60.  if( Connection.ConnectType != ASYNC )
  61.   type = PARALLEL;
  62.  
  63.  return( type );
  64. }
  65.  
  66.  
  67. /*****************************************************************************/
  68. /* - Initialize a Dbg/Esp connection.                                        */
  69. /*****************************************************************************/
  70. APIRET ConnectInit( int *pRcMoreInfo )
  71. {
  72.  APIRET rc = 0;
  73.  
  74.  HaltCommunicationsFlag = FALSE;
  75.  
  76.  switch( Connection.ConnectType )
  77.  {
  78.   case BOUND:
  79.   default:
  80.    break;
  81.  
  82.   case ASYNC:
  83.    if( IsParent() == TRUE )
  84.     AsyncInit( &Connection );
  85.    LsnHandle = GetComHandle();
  86.    break;
  87.  
  88.   case LOCAL_PIPE:
  89.    rc = PipeInit( &Connection, &LsnHandle );
  90.    break;
  91.  
  92.   case _NETBIOS:
  93.   {
  94.    /**************************************************************************/
  95.    /* - for some strange reason, istink( alias ilink )                       */
  96.    /*   trashes the first character of WaitMsg...so we have to put it back.  */
  97.    /**************************************************************************/
  98.    char WaitMsg[29] = "\nWaiting Netbios Connect...";
  99.  
  100.    WaitMsg[0] = '\n';
  101.    printf( "%s", WaitMsg );fflush(0);
  102.    rc = NetBiosInit( &Connection, &LsnHandle, pRcMoreInfo);
  103.   }
  104.   break;
  105.  
  106.   case SOCKET:
  107.   {
  108.    /**************************************************************************/
  109.    /* - for some strange reason, ilink                                       */
  110.    /*   trashes the first character of WaitMsg...so we have to put it back.  */
  111.    /**************************************************************************/
  112.    char WaitMsg[29] = "\nWaiting Sockets Connect...";
  113.  
  114.    WaitMsg[0] = '\n';
  115.    printf( "%s", WaitMsg );fflush(0);
  116.    rc = SockInit( &Connection, &LsnHandle, pRcMoreInfo);
  117.   }
  118.   break;
  119.  
  120.  }
  121.  return(rc);
  122. }
  123.  
  124. /*****************************************************************************/
  125. /* - Initialize a Dbg/Esp connection.                                        */
  126. /*****************************************************************************/
  127. void ConnectClose( LHANDLE handle )
  128. {
  129.  LHANDLE Lsn_Handle;
  130.  
  131.  
  132.  if( handle == DEFAULT_HANDLE )
  133.   Lsn_Handle = LsnHandle;
  134.  else
  135.   Lsn_Handle = handle;
  136.  
  137.  switch( Connection.ConnectType )
  138.  {
  139.   case BOUND:
  140.   default:
  141.    break;
  142.  
  143.   case ASYNC:
  144.    if( IsParent() == TRUE )
  145.     AsyncClose( &Connection );
  146.    break;
  147.  
  148.   case LOCAL_PIPE:
  149.    PipeClose( &Connection, Lsn_Handle );
  150.    break;
  151.  
  152.   case _NETBIOS:
  153.    NetBiosClose( Lsn_Handle );
  154.    break;
  155.  
  156.   case SOCKET:
  157.    SockClose( Lsn_Handle );
  158.    if( IsParent() && (handle == DEFAULT_HANDLE) )
  159.     SockClose( 0 );
  160.    break;
  161.  }
  162. }
  163.  
  164. /*****************************************************************************/
  165. /* - grab another Dbg/Esp connection.                                        */
  166. /* - resources have already been allocated.                                  */
  167. /*****************************************************************************/
  168. void OpenAnotherConnection( LHANDLE *pLsnHandle )
  169. {
  170.  switch( Connection.ConnectType )
  171.  {
  172.   case BOUND:
  173.   default:
  174.    break;
  175.  
  176.   case ASYNC:
  177.    *pLsnHandle = GetComHandle();
  178.    break;
  179.  
  180.   case LOCAL_PIPE:
  181.    PipeInit( &Connection, pLsnHandle );
  182.    break;
  183.  
  184.   case _NETBIOS:
  185.    NetBiosOriginateSession( &Connection , pLsnHandle );
  186.    break;
  187.  
  188.   case SOCKET:
  189.   {
  190.    int SocketDescriptor;
  191.  
  192.    SocketDescriptor = SockGetAnotherSocket( &Connection );
  193.    *pLsnHandle      = SocketDescriptor;
  194.   }
  195.   break;
  196.  }
  197. }
  198.  
  199. /*****************************************************************************/
  200. /* RmtSend()                                                                 */
  201. /*                                                                           */
  202. /* Description:                                                              */
  203. /*                                                                           */
  204. /*   Sends a buffer of data over a network connection.                       */
  205. /*                                                                           */
  206. /* Parameters:                                                               */
  207. /*                                                                           */
  208. /*   handle     the handle of the connection to send the buffer to if        */
  209. /*              not the default.                                             */
  210. /*   pBuf       -> our transmit buffer.                                      */
  211. /*   Len        number of bytes to transmit.                                 */
  212. /*                                                                           */
  213. /* Return:                                                                   */
  214. /*                                                                           */
  215. /* Assumptions:                                                              */
  216. /*                                                                           */
  217. /*   handle == DEFAULT_HANDLE ==> use the default handle.                    */
  218. /*                                                                           */
  219. /*****************************************************************************/
  220. void RmtSend( LHANDLE handle ,PVOID pBuf, ULONG Len )
  221. {
  222.  LHANDLE Lsn_Handle;
  223.  
  224.  /****************************************************************************/
  225.  /* - When we are terminating the world, we want to halt any further         */
  226.  /*   communications. So, what we do is just sit here and wait for the       */
  227.  /*   operating system to kill this thread.                                  */
  228.  /****************************************************************************/
  229.  if( HaltCommunicationsFlag == TRUE )
  230.  {
  231.    if( IsVerbose() ){ printf("\ncommunications halted");fflush(0);}
  232.    for(;;){ DosSleep(60000); }
  233.  }
  234.  
  235.  Lsn_Handle = LsnHandle;
  236.  if( handle != DEFAULT_HANDLE )
  237.   Lsn_Handle = handle;
  238.  
  239.  switch( Connection.ConnectType )
  240.  {
  241.   case BOUND:
  242.   default:
  243.    break;
  244.  
  245.   case ASYNC:
  246.    AsyncSend( pBuf, Len );
  247.    break;
  248.  
  249.   case LOCAL_PIPE:
  250.    PipeSend( Lsn_Handle, pBuf, Len );
  251.    break;
  252.  
  253.   case _NETBIOS:
  254.    NetBios_Send( Lsn_Handle, pBuf, Len );
  255.    break;
  256.  
  257.   case SOCKET:
  258.    SockSend( Lsn_Handle, pBuf, Len );
  259.    break;
  260.  }
  261. }
  262.  
  263. /*****************************************************************************/
  264. /* RmtRecv()                                                                 */
  265. /*                                                                           */
  266. /* Description:                                                              */
  267. /*                                                                           */
  268. /*   Receive a buffer of data over a network connection.                     */
  269. /*                                                                           */
  270. /* Parameters:                                                               */
  271. /*                                                                           */
  272. /*   handle     the handle of the connection to receive the buffer to if     */
  273. /*              not the default.                                             */
  274. /*   pBuf       -> our transmit buffer.                                      */
  275. /*   Len        number of bytes to transmit.                                 */
  276. /*                                                                           */
  277. /* Return:                                                                   */
  278. /*                                                                           */
  279. /* Assumptions:                                                              */
  280. /*                                                                           */
  281. /*   handle == DEFAULT_HANDLE ==> use the default handle.                    */
  282. /*                                                                           */
  283. /*****************************************************************************/
  284. void RmtRecv( LHANDLE handle, PVOID pBuf, ULONG Len )
  285. {
  286.  LHANDLE Lsn_Handle;
  287.  
  288.  Lsn_Handle = LsnHandle;
  289.  if( handle != DEFAULT_HANDLE )
  290.   Lsn_Handle = handle;
  291.  
  292.  switch( Connection.ConnectType )
  293.  {
  294.   case BOUND:
  295.   default:
  296.    break;
  297.  
  298.   case ASYNC:
  299.    AsyncRecv( pBuf, Len );
  300.    break;
  301.  
  302.   case LOCAL_PIPE:
  303.    PipeRecv( Lsn_Handle, pBuf, Len );
  304.    break;
  305.  
  306.   case _NETBIOS:
  307.    NetBios_Recv( Lsn_Handle, pBuf, Len );
  308.    break;
  309.  
  310.   case SOCKET:
  311.    SockRecv( Lsn_Handle, pBuf, Len );
  312.    break;
  313.  }
  314.  /****************************************************************************/
  315.  /* - When we are terminating the world, we want to halt any further         */
  316.  /*   communications. So, what we do is just sit here and wait for the       */
  317.  /*   operating system to kill this thread.                                  */
  318.  /****************************************************************************/
  319.  if( HaltCommunicationsFlag == TRUE )
  320.  {
  321.    if( IsVerbose() ){ printf("\ncommunications halted");fflush(0);}
  322.    for(;;){ DosSleep(60000); }
  323.  }
  324. }
  325.  
  326.