Annotation of libwww/Library/src/HTChannl.c, revision 2.12

2.1       frystyk     1: /*                                                                  HTChannl.c
                      2: **     CONTAINS STREAMS FOR READING AND WRITING TO AND FROM A TRANSPORT
                      3: **
                      4: **     (c) COPYRIGHT MIT 1995.
                      5: **     Please first read the full copyright statement in the file COPYRIGH.
2.12    ! frystyk     6: **     @(#) $Id: HTChannl.c,v 2.11 1996/08/20 04:53:32 frystyk Exp $
2.1       frystyk     7: **
                      8: **
                      9: ** HISTORY:
                     10: **     April 96  HFN   Written
                     11: */
                     12: 
                     13: /* Library Include files */
                     14: #include "sysdep.h"
                     15: #include "WWWUtil.h"
                     16: #include "HTAlert.h"
2.10      frystyk    17: #include "HTHost.h"
2.1       frystyk    18: #include "HTError.h"
                     19: #include "HTChannl.h"                                   /* Implemented here */
                     20: 
                     21: #define HASH_SIZE      67
                     22: #define HASH(s)                ((s) % HASH_SIZE)
                     23: 
                     24: struct _HTChannel {
                     25:     SOCKET             sockfd;                                    /* Socket */
                     26:     FILE *             fp;
                     27:     HTInputStream *    input;                               /* Input stream */
                     28:     HTOutputStream *   output;                             /* Output stream */
                     29:     BOOL               active;                 /* Active or passive channel */
                     30:     int                        semaphore;                         /* On channel use */
2.10      frystyk    31:     HTHost *           host;                          /* Zombie connections */
2.1       frystyk    32: };
                     33: 
                     34: struct _HTInputStream {
                     35:     const HTInputStreamClass * isa;    
                     36: };
                     37: 
                     38: struct _HTOutputStream {
                     39:     const HTOutputStreamClass *        isa;    
                     40: };
                     41: 
                     42: PRIVATE HTList ** channels = NULL;                      /* List of channels */
                     43: 
                     44: /* ------------------------------------------------------------------------- */
                     45: 
                     46: PRIVATE void free_channel (HTChannel * ch)
                     47: {
                     48:     if (ch) {
                     49: 
                     50:        /* Close the input and output stream */
2.11      frystyk    51:        if (ch->input) {
                     52:            (*ch->input->isa->close)(ch->input);
                     53:            ch->input = NULL;
                     54:        }
                     55:        if (ch->output) {
                     56:            (*ch->output->isa->close)(ch->output);
                     57:            ch->output = NULL;
                     58:        }
2.1       frystyk    59: 
                     60:        /* Close the socket */
                     61:        if (ch->sockfd != INVSOC) {
                     62:            NETCLOSE(ch->sockfd);
2.3       eric       63:            HTEvent_unregister(ch->sockfd, (SockOps) FD_ALL);
2.10      frystyk    64:            HTNet_decreaseSocket();
2.1       frystyk    65:            if (PROT_TRACE)
                     66:                HTTrace("Channel..... Deleted %p, socket %d\n", ch,ch->sockfd);
                     67:        }
                     68: 
                     69:        /* Close the file */
                     70:        if (ch->fp) {
                     71:            fclose(ch->fp);
                     72:            if (PROT_TRACE)
                     73:                HTTrace("Channel..... Deleted %p, file %p\n", ch, ch->fp);
                     74:        }
                     75:        HT_FREE(ch);
                     76:     }
                     77: }
                     78: 
                     79: /*
                     80: **     A channel is uniquely identified by a socket.
                     81: **     Note that we don't create the input and output stream - they are 
2.10      frystyk    82: **     created later.
2.1       frystyk    83: **
                     84: **     We only keep a hash on sockfd's as we don't have to look for channels
                     85: **     for ANSI file descriptors.
                     86: */
                     87: PUBLIC HTChannel * HTChannel_new (HTNet * net, BOOL active)
                     88: {
                     89:     if (net) {
                     90:        HTList * list = NULL;
                     91:        HTChannel * ch = NULL;
2.5       frystyk    92:        SOCKET sockfd = HTNet_socket(net);
                     93:        int hash = sockfd < 0 ? 0 : HASH(sockfd);
2.1       frystyk    94:        if (PROT_TRACE) HTTrace("Channel..... Hash value is %d\n", hash);
                     95:        if (!channels) {
                     96:            if (!(channels = (HTList **) HT_CALLOC(HASH_SIZE,sizeof(HTList*))))
                     97:                HT_OUTOFMEM("HTChannel_new");
                     98:        }
                     99:        if (!channels[hash]) channels[hash] = HTList_new();
                    100:        list = channels[hash];
                    101:        if ((ch = (HTChannel *) HT_CALLOC(1, sizeof(HTChannel))) == NULL)
                    102:            HT_OUTOFMEM("HTChannel_new");           
2.5       frystyk   103:        ch->sockfd = sockfd;
2.1       frystyk   104:        ch->active = active;
                    105:        ch->semaphore = 1;
                    106:        HTList_addObject(list, (void *) ch);
                    107:        if (PROT_TRACE) HTTrace("Channel..... Added %p to list %p\n", ch,list);
                    108:        return ch;
                    109:     }
                    110:     return NULL;
                    111: }
                    112: 
                    113: /*
                    114: **     Look for a channel object if we for some reason should have lost it
                    115: **     Returns NULL if nothing found
                    116: */
                    117: PUBLIC HTChannel * HTChannel_find (SOCKET sockfd)
                    118: {
                    119:     if (channels && sockfd != INVSOC) {
                    120:        int hash = HASH(sockfd);
                    121:        HTList * list = channels[hash];
                    122:        if (list) {
                    123:            HTChannel * ch = NULL;
                    124:            while ((ch = (HTChannel *) HTList_nextObject(list)))
                    125:                if (ch->sockfd == sockfd) return ch;
                    126:        }
                    127:     }
                    128:     return NULL;
                    129: }
                    130: 
                    131: /*
                    132: **     When deleting a channel we first look at if there are no more requests
                    133: **     using the channel (the semaphore is <= 0). Then, if the socket supports
                    134: **     persistent connections then we register the channel in the Host cache
                    135: **     and wait until the other end closes it or we get a time out on our side
                    136: */
2.6       eric      137: PUBLIC BOOL HTChannel_delete (HTChannel * channel, int status)
2.1       frystyk   138: {
                    139:     if (channel) {
2.2       frystyk   140:        if (PROT_TRACE) HTTrace("Channel..... Delete %p with semaphore %d\n",
2.1       frystyk   141:                                channel, channel->semaphore);
                    142:        /*
                    143:        **  We call the free methods on both the input stream and the output
                    144:        **  stream so that we can free up the stream pipes. However, note that
                    145:        **  this doesn't mean that we close the input stream and output stream
                    146:        **  them selves - only the generic streams
                    147:        */
2.12    ! frystyk   148:        if (status != HT_IGNORE) {
        !           149:            if (channel->input) 
        !           150:                if (status == HT_INTERRUPTED)
        !           151:                    (*channel->input->isa->abort)(channel->input, NULL);
        !           152:                else
        !           153:                    (*channel->input->isa->_free)(channel->input);
        !           154:            if (channel->output)
        !           155:                if (status == HT_INTERRUPTED)
        !           156:                    (*channel->output->isa->abort)(channel->output, NULL);
        !           157:                else
        !           158:                    (*channel->output->isa->_free)(channel->output);
        !           159:        }
2.1       frystyk   160: 
                    161:        /*
                    162:        **  Check whether this channel is used by other objects or we can
                    163:        **  delete it and free memory.
                    164:        */
                    165:        if (channel->semaphore <= 0 && channels) {
                    166:            int hash = HASH(channel->sockfd);
                    167:            HTList * list = channels[hash];
                    168:            if (list) {
                    169:                HTList_removeObject(list, (void *) channel);
                    170:                free_channel(channel);
                    171:                return YES;
                    172:            }
2.8       frystyk   173:        } else
                    174:            HTChannel_downSemaphore(channel);
2.1       frystyk   175:     }
                    176:     return NO;
                    177: }
                    178: 
                    179: /*     HTChannel_deleteAll
                    180: **     -------------------
                    181: **     Destroys all channels. This is called by HTLibTerminate(0
                    182: */
2.9       frystyk   183: PUBLIC BOOL HTChannel_deleteAll (void)
2.1       frystyk   184: {
                    185:     if (channels) {
                    186:        HTList * cur;
                    187:        int cnt;
                    188:        for (cnt=0; cnt<HASH_SIZE; cnt++) {
                    189:            if ((cur = channels[cnt])) { 
                    190:                HTChannel * pres;
                    191:                while ((pres = (HTChannel *) HTList_nextObject(cur)) != NULL)
                    192:                    free_channel(pres);
                    193:            }
                    194:            HTList_delete(channels[cnt]);
                    195:        }
                    196:        HT_FREE(channels);
                    197:     }
                    198:     return YES;
                    199: }
                    200: 
                    201: /*
2.10      frystyk   202: **     Return the socket associated with this channel
2.1       frystyk   203: */
2.10      frystyk   204: PUBLIC SOCKET HTChannel_socket (HTChannel * channel)
2.1       frystyk   205: {
2.10      frystyk   206:     return channel ? channel->sockfd : INVSOC;
2.1       frystyk   207: }
                    208: 
2.10      frystyk   209: /*
                    210: **     Return the file descriptor associated with this channel
                    211: */
                    212: PUBLIC FILE * HTChannel_file (HTChannel * channel)
2.1       frystyk   213: {
2.10      frystyk   214:     return channel ? channel->fp : NULL;
2.1       frystyk   215: }
                    216: 
                    217: /*
2.10      frystyk   218: **     We keep the associated Host object in case we have a
                    219: **     sleeping connection. 
2.1       frystyk   220: */
2.10      frystyk   221: PUBLIC BOOL HTChannel_setHost (HTChannel * ch, HTHost * host)
2.1       frystyk   222: {
2.10      frystyk   223:     if (ch && host) {
                    224:        ch->host = host;
                    225:        return YES;
                    226:     }
                    227:     return NO;
2.4       frystyk   228: }
                    229: 
2.10      frystyk   230: PUBLIC HTHost * HTChannel_host (HTChannel * ch)
2.4       frystyk   231: {
2.10      frystyk   232:     return (ch ? ch->host : NULL);
2.1       frystyk   233: }
                    234: 
                    235: /*
                    236: **     Increase the semaphore for this channel
                    237: */
                    238: PUBLIC void HTChannel_upSemaphore (HTChannel * channel)
                    239: {
                    240:     if (channel) {
                    241:        channel->semaphore++;
                    242:        if (PROT_TRACE)
                    243:            HTTrace("Channel..... Semaphore increased to %d for channel %p\n",
                    244:                    channel->semaphore, channel);
                    245:     }
                    246: }
                    247: 
                    248: /*
                    249: **     Decrease the semaphore for this channel
                    250: */
                    251: PUBLIC void HTChannel_downSemaphore (HTChannel * channel)
                    252: {
                    253:     if (channel) {
                    254:        channel->semaphore--;
                    255:        if (channel->semaphore <= 0) channel->semaphore = 0;
                    256:        if (PROT_TRACE)
                    257:            HTTrace("Channel..... Semaphore decreased to %d for channel %p\n",
2.7       frystyk   258:                    channel->semaphore, channel);
                    259:     }
                    260: }
                    261: 
                    262: /*
                    263: **     Explicitly set the semaphore for this channel
                    264: */
                    265: PUBLIC void HTChannel_setSemaphore (HTChannel * channel, int semaphore)
                    266: {
                    267:     if (channel) {
                    268:        channel->semaphore = semaphore;
                    269:        if (channel->semaphore <= 0) channel->semaphore = 0;
                    270:        if (PROT_TRACE)
                    271:            HTTrace("Channel..... Semaphore set to %d for channel %p\n",
2.1       frystyk   272:                    channel->semaphore, channel);
                    273:     }
                    274: }
                    275: 
                    276: /*
                    277: **     Create the input stream and bind it to the channel
                    278: **     Please read the description in the HTIOStream module on the parameters
                    279: */
2.10      frystyk   280: PUBLIC BOOL HTChannel_setInput (HTChannel * ch, HTInputStream * input)
2.1       frystyk   281: {
                    282:     if (ch) {
                    283:        ch->input = input;
                    284:        return YES;
                    285:     }
                    286:     return NO;
                    287: }
                    288: 
                    289: PUBLIC HTInputStream * HTChannel_input (HTChannel * ch)
                    290: {
                    291:     return ch ? ch->input : NULL;
                    292: }
                    293: 
                    294: /*
                    295: **     Create the output stream and bind it to the channel
                    296: **     Please read the description in the HTIOStream module on the parameters
                    297: */
2.10      frystyk   298: PUBLIC BOOL HTChannel_setOutput (HTChannel * ch, HTOutputStream * output)
2.1       frystyk   299: {
                    300:     if (ch) {
                    301:        ch->output = output;
                    302:        return YES;
                    303:     }
                    304:     return NO;
                    305: }
                    306: 
                    307: PUBLIC HTOutputStream * HTChannel_output (HTChannel * ch)
                    308: {
                    309:     return ch ? ch->output : NULL;
                    310: }
                    311: 

Webmaster