home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / comm / tcp / amitcp / src / l / inet-handler / handler.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-17  |  3.9 KB  |  168 lines

  1. /*
  2.  * handler.c
  3.  *
  4.  * Author: Tomi Ollila <too@cs.hut.fi>
  5.  *
  6.  * Copyright (c) 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
  7.  *              All rights reserved.
  8.  *
  9.  * Created: Wed Sep  1 18:05:18 1993 too
  10.  * Last modified: Thu Apr 14 13:19:04 1994 too
  11.  *
  12.  * $Id: handler.c,v 3.2 1994/04/17 11:52:13 too Exp $
  13.  *
  14.  */
  15.  
  16. #if 0 && (__GNUC__ < 2) && (__SASC < 6) || !defined (AMIGA)
  17. #error this program compiles only with amiga, gcc v2.x or sas/c version 6.x
  18. #endif
  19.  
  20. /*
  21.  * These Globals are constant after initialization
  22.  */
  23. #ifdef __GNUC__
  24. struct ExecBase * SysBase = 0;
  25. #endif
  26. struct DosLibrary * DOSBase = 0;
  27.  
  28. const int one = 1;
  29.  
  30. #include "handler/begin.c"
  31. #define RFI .
  32.  
  33. #ifdef __SASC
  34. #undef SocketBase
  35. struct Library * SocketBase;
  36. #endif
  37.  
  38. static ULONG dtablesizetaglist[] = {
  39.   SBTM_SETVAL(SBTC_DTABLESIZE), FD_SETSIZE, TAG_DONE };
  40.  
  41. int start()
  42. {
  43.   struct CleanUp    CU /* = { 0 } */;
  44.   struct ApplShared    AS;
  45.   struct ApplPort *    applarray[FD_SETSIZE];
  46.   struct MinList    writelist;    /* list of apps having pending wrts */
  47.   struct MinList    freewritelist;    /* free pending write structures */
  48.   struct MinList    freeappllist;
  49.   struct MsgPort *    mymsgport;
  50.   ULONG         myportflag;
  51.   ULONG         timerflag;
  52.   struct DosList *    mydev;
  53.   int            nfds;
  54.   fd_set        readfds, writefds;
  55.   ULONG         sigmask; /* Exec signals to be catched */
  56.   int            writespending = 0; /* counter */
  57.   struct ApplPort *    lastappl = NULL;
  58.   LONG            lastmask = 0;
  59.   int            lastsd = 0;
  60.   int            notDone = TRUE;
  61.  
  62. #include "handler/startup.c"
  63.  
  64.   while (notDone) {
  65.     fd_set        rfds;
  66.     fd_set        wfds;
  67.     fd_set *        wfdsp;
  68.     ULONG        smask;
  69.     int            n;
  70.     
  71.     rfds = readfds;
  72.     if (writespending) {
  73.        DP("WRITESPENDING: writefds %lx", *(LONG *)&writefds);
  74.       wfds = writefds;
  75.       wfdsp = &wfds;
  76.     }
  77.     else
  78.       wfdsp = NULL;
  79.     smask = sigmask;
  80.  
  81.     /*
  82.      * All Input events are waited here.
  83.      */
  84. /*    n = WaitSelect2(nfds, &rfds, wfdsp, NULL, NULL, &smask); */
  85.  
  86.     DP("BWS nfds: %lx readfds: %lx", nfds, *(LONG *)&readfds);
  87.     n = WaitSelect2(nfds, &rfds, wfdsp, &smask);
  88.     DP("AWS n %ld, rfds %lx, wfds %lx smask %lx", n, *(LONG *)&rfds, 
  89.     writespending? *(LONG *)wfdsp: 0, smask);
  90.  
  91.     switch (n) {
  92.     case -1:
  93.       notDone = FALSE;
  94.       break;
  95.     case 0:
  96.       /*
  97.        * Since there is no timeout used the only possibility is there
  98.        * perhaps is packet arrived. since the case where something has
  99.        * arrived to sockets there is a possibisity a packet has arrived
  100.        * -- the packet handling is done last under 'default' case.
  101.        */
  102.       goto packet;
  103.     default:
  104.       if (writespending) {
  105.     DP("WRITESPENDING: wfds %lx", *(LONG *)wfdsp);
  106. #    include "handler/pendingwrites.c"
  107.  
  108.       if (*(LONG *)&rfds == 0)
  109.     goto packet;
  110.       }
  111.       /*
  112.        * First look if data has arrived to the socket where it last arrived.
  113.        * this speeds up net burps. If not, scan whole application array
  114.        * through
  115.        */
  116.       {
  117.     int i;
  118.  
  119. /*     DP("lastmask %ld, rfds %ld\n", lastmask, *(LONG *)&rfds); */
  120.  
  121.     if (lastmask == *(LONG*)&rfds) {
  122.       i = nfds;
  123.       goto readit;
  124.     }
  125.     i = 0;
  126.       loop:
  127.     if (FD_ISSET(i, &rfds)) {
  128.       lastappl = applarray[i];
  129.       lastsd = i;
  130.     readit:
  131. #      include "handler/readevent.c"
  132.     }
  133.     i++;
  134.     if (i < nfds)
  135.       goto loop;
  136.     lastmask = 1 << lastsd;
  137.       }
  138.     packet:
  139.       /*
  140.        * First see if one or more of the applications have sent packet
  141.        * to their private port (other than an open/reopen -packet).
  142.        */
  143.       if (smask & applportflag) {
  144. #    include "handler/applmsg.c"
  145.       }
  146.       /*
  147.        * See if some of the waitforchar requests are expired
  148.        */
  149.       if (smask & timerflag) {
  150. #    include "handler/timermsg.c"
  151.       }
  152.       /*
  153.        * See if someone has sent message to my message port.
  154.        */
  155.       if (smask & myportflag) {
  156. #    include "handler/mymsg.c"
  157.       }
  158.     } /* WaitSelect() */
  159.   }  /* while */
  160.  
  161.   /*
  162.    * Program received break signal. drop out all connections.
  163.    */
  164. # include "handler/cleanup.c"
  165.  
  166.   return clean(&CU, 0);
  167. }
  168.