home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / comm / tcp / amitcp / src / l / inet-handler / handler / readevent.c < prev    next >
Encoding:
Text File  |  1994-04-20  |  2.9 KB  |  116 lines

  1. /* $Id: readevent.c,v 1.2 1994/04/20 15:47:46 too Exp $
  2.  *
  3.  * Copyright (c) 1993 AmiTCP/IP Group <amitcp-group@hut.fi>
  4.  *
  5.  * Created: Wed Nov 10 13:55:43 1993 too
  6.  * Last modified: Wed Apr 13 15:51:59 1994 too
  7.  */
  8. {
  9.   struct DosPacket * packet;
  10.   LONG genlocvar1;
  11. #define len genlocvar1
  12. #define sd  genlocvar1
  13.  
  14.  do_this_again:
  15.   packet = lastappl->ap_Packet;
  16.   /*
  17.    * if there is no packets in, set message arrived flag (Disb)
  18.    * and disable detection of this socket at WaitSelect
  19.    */
  20.   if (packet == NULL) {
  21.     FD_CLR(lastsd, &readfds);
  22.     lastappl->ap_Disb = TRUE;
  23.   }
  24.   else {
  25.     switch (packet->dp_Type) {
  26.     case ACTION_READ:
  27.       /*
  28.        * Fill data in given ACTION_READ packet.
  29.        */
  30.       len = recv(lastsd, (char *)packet->dp_Arg2, packet->dp_Arg3, 0);
  31.       /*
  32.        * EOF?. Disable event waiting to this socket.
  33.        */
  34.       if (len == 0) {
  35.     FD_CLR(lastsd, &readfds);
  36.     lastappl->ap_Disb = TRUE;
  37.       }
  38.       ReplyPkt(packet, len, 0);
  39.       break;
  40.     case ACTION_WAIT_CHAR:
  41.       /*
  42.        * use ioctl() to determine how many bytes are in socket buffer
  43.        */
  44.       Abort_Timeout(&lastappl->ap_Tr);
  45.       IoctlSocket(lastsd, FIONREAD, (char *)&len);
  46.       /*
  47.        * Data stays in socket buffer so disable event waiting
  48.        * to this socket.
  49.        */
  50.       FD_CLR(lastsd, &readfds);
  51.       lastappl->ap_Disb = TRUE;
  52.       
  53.       ReplyPkt(packet, len, 0);
  54.       break;
  55.     case ACTION_FINDINPUT:
  56.     case ACTION_FINDOUTPUT:
  57.     case ACTION_FINDUPDATE:
  58.       /*
  59.        * Still holding Open() packet. Do accept()
  60.        */
  61.       sd = accept(lastsd, NULL, NULL);
  62.       
  63.       if (sd == -1) {
  64.     ReplyPkt(packet, DOSFALSE, ERROR_OBJECT_NOT_FOUND);
  65.     AddTail((struct List *)&freeappllist, (struct Node *)lastappl);
  66.     lastmask = 0; /* applport no longer exists */
  67.     sd = lastsd;
  68.     nfds--;
  69.     if (sd == nfds) {
  70.       CloseSocket(sd);
  71.       FD_CLR(nfds, &readfds);
  72.       goto skip;
  73.     }
  74.     else {
  75.       Dup2Socket(nfds, sd);
  76.       CloseSocket(nfds);
  77.       lastappl = applarray[sd] = applarray[nfds];
  78.       lastappl->ap_Sd = sd;
  79.       if (! FD_ISSET(nfds, &readfds))
  80.         FD_CLR(sd, &readfds);
  81.       else
  82.         FD_CLR(nfds, &readfds);
  83.  
  84.       if (FD_ISSET(nfds, &rfds))
  85.         goto do_this_again;
  86.     }
  87.       }
  88.       else {
  89.     struct FileHandle * fh =
  90.       (struct FileHandle *)BADDR(packet->dp_Arg1);
  91.  
  92.     /*
  93.      * Dup newly connected socket to the place of listening socket
  94.      * (closing it). Then, close another reference of the new socket.
  95.      */
  96.     Dup2Socket(sd, lastsd);
  97.     CloseSocket(sd);
  98.     DP("accepted socket: %ld - new socket: %ld", sd, lastsd);
  99.     sd = lastsd;
  100.     /*
  101.      * Set MsgPort ptr of Open() FileHandle to point the
  102.      *  messageport allocated for this connection.
  103.      */
  104.     fh->fh_Port = (struct MsgPort *)DOSTRUE; /* IsInteractive() */
  105.     fh->fh_Type = &lastappl->ap_AMP.amp_Msgport;
  106.     IoctlSocket(sd, FIONBIO, (char *)&one);
  107.     setsockopt(sd, SOL_SOCKET, SO_OOBINLINE, (char *)&one, sizeof one);
  108.     ReplyPkt(packet, DOSTRUE, packet->dp_Res2);
  109.     break;
  110.       }
  111.     }
  112.     lastappl->ap_Packet = NULL;
  113.   skip:
  114.   }
  115. }
  116.