Annotation of libwww/Library/src/HTNet.c, revision 2.35

2.23      frystyk     1: /*                                                                  HTNet.c
                      2: **     ASYNCRONOUS SOCKET MANAGEMENT
2.1       frystyk     3: **
2.10      frystyk     4: **     (c) COPYRIGHT MIT 1995.
2.4       frystyk     5: **     Please first read the full copyright statement in the file COPYRIGH.
                      6: **
                      7: **     This is the implementation of the internal library multithreading
2.1       frystyk     8: **     functions. This includes an interrupt handler and a event loop.
                      9: **     
                     10: ** History:
2.14      frystyk    11: **     12 June 94      Written by Henrik Frystyk, frystyk@w3.org
2.17      frystyk    12: **      31 May  95      Charlie Brooks cbrooks@osf.org
                     13: **
2.1       frystyk    14: */
                     15: 
2.9       frystyk    16: /* Implemention dependent include files */
                     17: #include "tcp.h"
                     18: 
2.1       frystyk    19: /* Library include files */
                     20: #include "HTUtils.h"
2.16      frystyk    21: #include "HTProt.h"
2.1       frystyk    22: #include "HTError.h"
2.25      frystyk    23: #include "HTAlert.h"
2.23      frystyk    24: #include "HTReqMan.h"
2.17      frystyk    25: #include "HTEvntrg.h"
2.23      frystyk    26: #include "HTStream.h"
2.24      frystyk    27: #include "HTNetMan.h"                                   /* Implemented here */
2.1       frystyk    28: 
2.9       frystyk    29: #ifdef WIN32
                     30: #include <io.h>
                     31: #endif
                     32: 
2.23      frystyk    33: #ifndef HT_MAX_SOCKETS
                     34: #define HT_MAX_SOCKETS 6
                     35: #endif
                     36: 
2.33      frystyk    37: typedef struct _NetCall {
2.30      frystyk    38:     HTNetCallback *    cbf;
2.23      frystyk    39:     int                status;      /* Status associated with this callback */
2.33      frystyk    40: } NetCall;
2.23      frystyk    41: 
                     42: struct _HTStream {
                     43:     CONST HTStreamClass *      isa;
                     44:     /* ... */
                     45: };
                     46: 
                     47: PRIVATE int    HTMaxActive = HT_MAX_SOCKETS;         /* Max active requests */
2.33      frystyk    48: PRIVATE HTList *HTBefore = NULL;             /* List of call back functions */
                     49: PRIVATE HTList *HTAfter = NULL;                      /* List of call back functions */
2.24      frystyk    50: 
2.23      frystyk    51: PRIVATE HTList *HTNetActive = NULL;               /* List of active requests */
                     52: PRIVATE HTList *HTNetPending = NULL;            /* List of pending requests */
2.24      frystyk    53: PRIVATE HTList *HTNetPersistent = NULL;           /* List of persistent connections */
2.1       frystyk    54: 
                     55: /* ------------------------------------------------------------------------- */
                     56: 
2.23      frystyk    57: /*
                     58: **     Set the max number of simultanous sockets. Default is HT_MAX_SOCKETS
2.1       frystyk    59: */
2.23      frystyk    60: PUBLIC BOOL HTNet_setMaxSocket (int newmax)
                     61: {
                     62:     if (newmax > 0) {
                     63:        HTMaxActive = newmax;
                     64:        return YES;
                     65:     }
                     66:     return NO;
                     67: }
                     68: 
                     69: PUBLIC int HTNet_maxSocket (void)
2.1       frystyk    70: {
2.23      frystyk    71:     return HTMaxActive;
                     72: }
                     73: 
                     74: /* ------------------------------------------------------------------------- */
2.33      frystyk    75: /*                             Call Back Functions                          */
2.23      frystyk    76: /* ------------------------------------------------------------------------- */
                     77: 
2.33      frystyk    78: /*     HTNetCall_add
                     79: **     -------------
                     80: **     Register a call back function that is to be called on every request.
                     81: **     Several call back functions can be registered
2.23      frystyk    82: **     in which case all of them are called in the order of which they
                     83: **     were registered.
                     84: **
                     85: **     The status signifies which call back function to call depending of the 
                     86: **     result of the request. This can be
                     87: **
                     88: **             HT_ERROR        An error occured
                     89: **             HT_LOADED       The document was loaded
                     90: **             HT_NO_DATA      OK, but no data
2.33      frystyk    91: **             HT_REDIRECT     If we received a redirection
2.23      frystyk    92: **             HT_RETRY        Retry request after at a later time
                     93: **             HT_ALL          All of above
                     94: */
2.33      frystyk    95: PUBLIC BOOL HTNetCall_add (HTList * list, HTNetCallback *cbf, int status)
2.23      frystyk    96: {
2.33      frystyk    97:     if (WWWTRACE) 
                     98:        TTYPrint(TDEST, "Call Add.... HTNetCallback %p\n", (void *) cbf);
                     99:     if (list && cbf) {
                    100:        NetCall *me = (NetCall *) calloc(1, sizeof(NetCall));
                    101:        if (!me) outofmem(__FILE__, "HTNet_register");
                    102:        me->cbf = cbf;
                    103:        me->status = status;
                    104:        return HTList_addObject(list, (void *) me);
2.23      frystyk   105:     }
                    106:     return NO;
                    107: }
                    108: 
2.33      frystyk   109: /*     HTNetCall_delete
                    110: **     ----------------
                    111: **     Unregister a call back function from a list
2.23      frystyk   112: */
2.33      frystyk   113: PUBLIC BOOL HTNetCall_delete (HTList * list, HTNetCallback *cbf)
2.23      frystyk   114: {
2.33      frystyk   115:     if (WWWTRACE) 
                    116:        TTYPrint(TDEST, "Call delete HTNetCallback %p\n", (void *) cbf);
                    117:     if (list && cbf) {
                    118:        HTList *cur = list;
                    119:        NetCall *pres;
                    120:        while ((pres = (NetCall *) HTList_nextObject(cur))) {
2.23      frystyk   121:            if (pres->cbf == cbf) {
2.33      frystyk   122:                HTList_removeObject(list, (void *) pres);
2.23      frystyk   123:                free(pres);
                    124:                return YES;
                    125:            }
                    126:        }
                    127:     }
                    128:     return NO;
                    129: }
2.1       frystyk   130: 
2.33      frystyk   131: /*     HTNetCall_deleteAll
2.23      frystyk   132: **     -------------------
                    133: **     Unregisters all call back functions
                    134: */
2.33      frystyk   135: PUBLIC BOOL HTNetCall_deleteAll (HTList * list)
2.23      frystyk   136: {
2.33      frystyk   137:     if (WWWTRACE) 
                    138:        TTYPrint(TDEST, "Call delete All callback functions\n");
                    139:     if (list) {
                    140:        HTList *cur = list;
                    141:        NetCall *pres;
                    142:        while ((pres = (NetCall *) HTList_nextObject(cur))) {
                    143:            HTList_removeObject(list, (void *) pres);
2.23      frystyk   144:            free(pres);
                    145:        }
2.33      frystyk   146:        HTList_delete(list);
2.23      frystyk   147:        return YES;
                    148:     }
                    149:     return NO;
                    150: }
                    151: 
2.33      frystyk   152: /*     HTNetCall_execute
                    153: **     -----------------
2.23      frystyk   154: **     Call all the call back functions registered in the list IF not the 
                    155: **     status is HT_IGNORE.
                    156: **     The callback functions are called in the order of which they
                    157: **     were registered. At the moment an application callback function is
                    158: **     called, it can free the request object - it is no longer used by the
                    159: **     Library.
2.33      frystyk   160: **     Returns what the last callback function returns
2.23      frystyk   161: */
2.33      frystyk   162: PUBLIC int HTNetCall_execute (HTList * list, HTRequest * request, int status)
2.23      frystyk   163: {
2.33      frystyk   164:     int ret = HT_OK;
                    165:     if (list && request && status != HT_IGNORE) {      
                    166:        int cnt = HTList_count(list);
2.23      frystyk   167:        while (--cnt >= 0) {
2.33      frystyk   168:            NetCall *pres = (NetCall *) HTList_objectAt(list, cnt);
2.23      frystyk   169:            if (pres && (pres->status == status || pres->status == HT_ALL)) {
2.33      frystyk   170:                if (WWWTRACE)
                    171:                    TTYPrint(TDEST,"Net callback %p (request=%p, status=%d)\n",
2.23      frystyk   172:                            (void *) pres->cbf, request, status);
2.33      frystyk   173:                if ((ret = (*(pres->cbf))(request, status)) != HT_OK) break;
2.23      frystyk   174:            }
                    175:        }
2.1       frystyk   176:     }
2.33      frystyk   177:     return ret;
                    178: }
                    179: 
                    180: /*
                    181: **     Global set of callback functions BEFORE the request is issued
                    182: **     list can be NULL
                    183: */
                    184: PUBLIC BOOL HTNet_setBefore (HTList *list)
                    185: {
                    186:     if (HTBefore) HTNetCall_deleteAll(HTBefore);
                    187:     HTBefore = list;
                    188:     return YES;
                    189: }
                    190: 
                    191: PUBLIC HTList * HTNet_before (void)
                    192: {
                    193:     return HTBefore;
                    194: }
                    195: 
                    196: PUBLIC int HTNet_callBefore (HTRequest *request, int status)
                    197: {
                    198:     return HTNetCall_execute(HTBefore, request, status);
                    199: }
                    200: 
                    201: PUBLIC BOOL HTNetCall_addBefore (HTNetCallback *cbf, int status)
                    202: {
                    203:     if (!HTBefore) HTBefore = HTList_new();
                    204:     return HTNetCall_add(HTBefore, cbf, status);
                    205: }
                    206: 
                    207: /*
                    208: **     Global set of callback functions AFTER the request is issued
                    209: **     list can be NULL
                    210: */
                    211: PUBLIC BOOL HTNet_setAfter (HTList *list)
                    212: {
                    213:     if (HTAfter) HTNetCall_deleteAll(HTAfter);
                    214:     HTAfter = list;
                    215:     return YES;
                    216: }
                    217: 
                    218: PUBLIC HTList * HTNet_after (void)
                    219: {
                    220:     return HTAfter;
                    221: }
                    222: 
                    223: PUBLIC int HTNet_callAfter (HTRequest *request, int status)
                    224: {
                    225:     return HTNetCall_execute(HTAfter, request, status);
                    226: }
                    227: 
                    228: PUBLIC BOOL HTNetCall_addAfter (HTNetCallback *cbf, int status)
                    229: {
                    230:     if (!HTAfter) HTAfter = HTList_new();
                    231:     return HTNetCall_add(HTAfter, cbf, status);
2.1       frystyk   232: }
                    233: 
2.23      frystyk   234: /* ------------------------------------------------------------------------- */
                    235: /*                             Request Queue                                */
                    236: /* ------------------------------------------------------------------------- */
2.1       frystyk   237: 
2.23      frystyk   238: /*     HTNet_activeQueue
                    239: **     -----------------
                    240: **     Returns the list of active requests that are currently having an open
                    241: **     connection.
                    242: **     Returns list of HTNet objects or NULL if error
2.1       frystyk   243: */
2.23      frystyk   244: PUBLIC HTList *HTNet_activeQueue (void)
                    245: {
                    246:     return HTNetActive;
                    247: }
2.17      frystyk   248: 
2.28      frystyk   249: /*     HTNet_activeCount
                    250: **     ----------------
                    251: **     Returns the number of active requests
                    252: */
2.29      frystyk   253: PUBLIC BOOL HTNet_idle (void)
2.28      frystyk   254: {
2.29      frystyk   255:     return HTList_isEmpty(HTNetActive);
2.28      frystyk   256: }
                    257: 
2.23      frystyk   258: /*     HTNet_pendingQueue
                    259: **     ------------------
                    260: **     Returns the list of pending requests that are waiting to become active
                    261: **     Returns list of HTNet objects or NULL if error
                    262: */
                    263: PUBLIC HTList *HTNet_pendingQueue (void)
2.1       frystyk   264: {
2.23      frystyk   265:     return HTNetPending;
2.1       frystyk   266: }
                    267: 
2.23      frystyk   268: /* ------------------------------------------------------------------------- */
                    269: /*                       Creation and deletion methods                      */
                    270: /* ------------------------------------------------------------------------- */
                    271: 
2.27      frystyk   272: /*     HTNet_duplicate
                    273: **     ---------------
                    274: **     Creates a new HTNet object as a duplicate of the same request.
                    275: **     Returns YES if OK, else NO
                    276: **     BUG: We do not check if we have a socket free!
                    277: */
                    278: PUBLIC BOOL HTNet_dup (HTNet *src, HTNet **dest)
                    279: {
                    280:     *dest = NULL;
                    281:     if (!src) return NO;
                    282:     if ((*dest = (HTNet *) calloc(1, sizeof(HTNet))) == NULL)
                    283:        outofmem(__FILE__, "HTNet_dup");
                    284:     memcpy(*dest, src, sizeof(HTNet));
                    285:     return YES;
                    286: }
                    287: 
2.30      frystyk   288: /*     HTNet_priority
                    289: **     --------------
                    290: **     Get the current priority of the Net object
                    291: */
                    292: PUBLIC HTPriority HTNet_priority (HTNet * net)
                    293: {
                    294:     return (net ? net->priority : -1);
                    295: }
                    296: 
                    297: /*     HTNet_setPriority
                    298: **     -----------------
                    299: **     Set the current priority of the Net object
                    300: **     This will change the priority next time the thread is blocked
                    301: */
                    302: PUBLIC BOOL HTNet_setPriority (HTNet * net, HTPriority priority)
                    303: {
                    304:     if (net) {
                    305:        net->priority = priority;
                    306:        return YES;
                    307:     }
                    308:     return NO;
                    309: }
                    310: 
                    311: /*     HTNet_new
                    312: **     ---------
2.23      frystyk   313: **     Create a new HTNet object as a new request to be handled. If we have
                    314: **     more than HTMaxActive connections already then put this into the
                    315: **     pending queue, else start the request by calling the call back
                    316: **     function registered with this access method. 
                    317: **     Returns YES if OK, else NO
                    318: */
2.30      frystyk   319: PUBLIC BOOL HTNet_new (HTRequest * request)
2.23      frystyk   320: {
2.33      frystyk   321:     int status;
                    322:     HTNet * me;
2.23      frystyk   323:     HTProtocol *prot;
2.33      frystyk   324:     char * physical;
2.27      frystyk   325:     if (!request) return NO;
2.33      frystyk   326: 
                    327:     /*
                    328:     ** First we do all the "BEFORE" callbacks in order to see if we are to
                    329:     ** continue with this request or not. If we receive a callback status
                    330:     ** that is NOT HT_OK then jump directly to the after callbacks and return
                    331:     */
2.34      frystyk   332:     if ((status = HTNetCall_execute(HTBefore, request, HT_OK)) != HT_OK) {
2.33      frystyk   333:        HTNetCall_execute(HTAfter, request, status);
                    334:        return YES;
                    335:     }
                    336: 
                    337:     if (!(physical = HTAnchor_physical(request->anchor)) || !*physical) {
                    338:        if (WWWTRACE)
                    339:            TTYPrint(TDEST, "HTNet New... NO PHYSICAL ANCHOR ADDRESS. This is needed in order to load a document. Please read the User's Guide on how to set this up\n");
                    340:        return NO;
                    341:     }
                    342: 
2.23      frystyk   343:     if (!HTNetActive) HTNetActive = HTList_new();
2.26      frystyk   344:     prot = (HTProtocol *) HTAnchor_protocol(request->anchor);
2.23      frystyk   345: 
                    346:     /* Create new net object and bind it to the request object */
                    347:     if ((me = (HTNet *) calloc(1, sizeof(HTNet))) == NULL)
                    348:        outofmem(__FILE__, "HTNet_new");
                    349:     me->request = request;
                    350:     request->net = me;
                    351:     me->preemtive = (HTProtocol_preemtive(prot) || request->preemtive);
2.30      frystyk   352:     me->priority = request->priority;
2.23      frystyk   353:     me->sockfd = INVSOC;
                    354:     if (!(me->cbf = HTProtocol_callback(prot))) {
2.33      frystyk   355:        if (WWWTRACE)
2.31      frystyk   356:            TTYPrint(TDEST, "HTNet_new... NO CALL BACK FUNCTION!\n");
2.23      frystyk   357:        free(me);
                    358:        return NO;
                    359:     }
2.25      frystyk   360:     request->retrys++;
2.23      frystyk   361: 
                    362:     /*
                    363:     ** Check if we can start the request, else put it into pending queue
                    364:     ** If so then call the call back function associated with the anchor.
                    365:     ** We use the INVSOC as we don't have a valid socket yet!
                    366:     */
                    367:     if (HTList_count(HTNetActive) < HTMaxActive) {
                    368:        HTList_addObject(HTNetActive, (void *) me);
2.33      frystyk   369:        if (WWWTRACE)
2.31      frystyk   370:            TTYPrint(TDEST, "HTNet_new... starting request %p (retry=%d)\n",
2.25      frystyk   371:                    request, request->retrys);
                    372:        (*(me->cbf))(me->sockfd, request, FD_NONE);
2.23      frystyk   373:     } else {
2.35    ! frystyk   374:        HTAlertCallback *cbf = HTAlert_find(HT_PROG_WAIT);
2.23      frystyk   375:        if (!HTNetPending) HTNetPending = HTList_new();
2.33      frystyk   376:        if (WWWTRACE)
2.31      frystyk   377:            TTYPrint(TDEST, "HTNet_new... request %p registered as pending\n",
2.25      frystyk   378:                    request);
2.35    ! frystyk   379:        if (cbf) (*cbf)(request, HT_PROG_WAIT, HT_MSG_NULL, NULL, NULL, NULL);
2.25      frystyk   380:        HTList_addObject(HTNetPending, (void *) me);    
2.23      frystyk   381:     }
                    382:     return YES;
                    383: }
                    384: 
                    385: /*     delete_object
                    386: **     -------------
                    387: **     Deletes an HTNet object
2.15      frystyk   388: */
2.23      frystyk   389: PRIVATE BOOL delete_object (HTNet *net, int status)
2.15      frystyk   390: {
2.33      frystyk   391:     if (WWWTRACE)
2.31      frystyk   392:        TTYPrint(TDEST, "HTNet_delete Remove net object %p\n", net);
2.23      frystyk   393:     if (net) {
                    394:        int status = 0;
                    395: 
                    396:        /* Free stream with data FROM network to application */
                    397:        if (net->target) {
                    398:            if (status == HT_INTERRUPTED)
                    399:                (*net->target->isa->abort)(net->target, NULL);
                    400:            else
                    401:                (*net->target->isa->_free)(net->target);
                    402:        }
                    403: 
                    404:        /* Close socket */
                    405:        if (net->sockfd != INVSOC) {
2.24      frystyk   406:            if (HTDNS_socket(net->dns) == INVSOC) {
                    407:                if ((status = NETCLOSE(net->sockfd)) < 0)
2.33      frystyk   408:                    HTRequest_addSystemError(net->request, ERR_FATAL,
                    409:                                             socerrno, NO, "NETCLOSE");
                    410:                if (WWWTRACE)
2.31      frystyk   411:                    TTYPrint(TDEST, "HTNet_delete closing %d\n", net->sockfd);
2.25      frystyk   412:                HTEvent_UnRegister(net->sockfd, (SockOps) FD_ALL);
2.24      frystyk   413:            } else {
2.33      frystyk   414:                if (WWWTRACE)
2.31      frystyk   415:                    TTYPrint(TDEST, "HTNet_delete keeping %d\n", net->sockfd);
2.25      frystyk   416:                HTDNS_clearActive(net->dns);
                    417:                /* Here we should probably use a low priority */
2.27      frystyk   418:                HTEvent_UnRegister(net->sockfd, (SockOps) FD_ALL);
2.25      frystyk   419:                HTEvent_Register(net->sockfd, net->request, (SockOps) FD_READ,
                    420:                                 HTDNS_closeSocket, net->priority);
2.24      frystyk   421:            }
2.23      frystyk   422:        }
                    423:        if (net->isoc)
                    424:            HTInputSocket_free(net->isoc);
                    425:        if (net->request)
                    426:            net->request->net = NULL;               /* Break link to request */
                    427:        free(net);
                    428:        return status ? NO : YES;
                    429:     }
                    430:     return NO;
                    431: }
                    432: 
                    433: /*     HTNet_delete
                    434: **     ------------
                    435: **     Deletes the HTNet object from the list of active requests and calls
                    436: **     any registered call back functions IF not the status is HT_IGNORE.
                    437: **     This is used if we have internal requests that the app doesn't know
                    438: **     about. We also see if we have pending requests that can be started
                    439: **     up now when we have a socket free.
                    440: **     The callback functions are called in the reverse order of which they
                    441: **     were registered (last one first)
                    442: */
                    443: PUBLIC BOOL HTNet_delete (HTNet * net, int status)
                    444: {
2.33      frystyk   445:     if (WWWTRACE) 
                    446:        TTYPrint(TDEST,"HTNetDelete. Object and call callback functions\n");
2.23      frystyk   447:     if (HTNetActive && net) {
2.25      frystyk   448:        SOCKFD cs = net->sockfd;                           /* Current sockfd */
2.23      frystyk   449: 
                    450:        /* Remove object and call callback functions */
                    451:        HTRequest *request = net->request;
                    452:        HTList_removeObject(HTNetActive, (void *) net);
                    453:        delete_object(net, status);
2.33      frystyk   454:        HTNetCall_execute(HTAfter, request, status);
2.23      frystyk   455: 
2.25      frystyk   456:        /*
                    457:        ** See first if we have a persistent request queued up for this socket
                    458:        ** If not then see if there is a pending request
                    459:        */
                    460:        if (HTNetPersistent) {
                    461:            HTList *cur = HTNetPersistent;
                    462:            HTNet *next;
                    463:            while ((next = (HTNet *) HTList_nextObject(cur))) {
                    464:                if (next->sockfd == cs) {
2.33      frystyk   465:                    if (WWWTRACE)
2.31      frystyk   466:                        TTYPrint(TDEST, "HTNet delete launch WARM request %p\n",
2.25      frystyk   467:                                next->request);
2.28      frystyk   468:                    HTList_addObject(HTNetActive, (void *) next);
                    469:                    HTList_removeObject(HTNetPersistent, (void *) next);
2.25      frystyk   470:                    (*(next->cbf))(next->sockfd, next->request, FD_NONE);
                    471:                    break;
                    472:                }
                    473:            }
                    474:        } else if (HTList_count(HTNetActive) < HTMaxActive &&
                    475:                   HTList_count(HTNetPending)) {
2.23      frystyk   476:            HTNet *next = (HTNet *) HTList_removeFirstObject(HTNetPending);
                    477:            if (next) {
                    478:                HTList_addObject(HTNetActive, (void *) next);
2.33      frystyk   479:                if (WWWTRACE)
2.31      frystyk   480:                    TTYPrint(TDEST,"HTNet delete launch PENDING request %p\n",
2.23      frystyk   481:                            next->request);
                    482:                (*(next->cbf))(INVSOC, next->request, FD_NONE);
                    483:            }
                    484:        }
                    485:        return YES;
                    486:     }
                    487:     return NO;
                    488: }
                    489: 
                    490: /*     HTNet_deleteAll
                    491: **     ---------------
                    492: **     Deletes all HTNet object that might either be active or pending
2.25      frystyk   493: **     We DO NOT call the call back functions - A crude way of saying goodbye!
2.23      frystyk   494: */
                    495: PUBLIC BOOL HTNet_deleteAll (void)
                    496: {
2.33      frystyk   497:     if (WWWTRACE) 
2.31      frystyk   498:        TTYPrint(TDEST, "HTNetDelete. Remove all Net objects, NO callback\n"); 
2.25      frystyk   499:     if (HTNetPersistent) {
                    500:        HTList *cur = HTNetPersistent;
                    501:        HTNet *pres;
                    502:        while ((pres = (HTNet *) HTList_nextObject(cur))) {
                    503:            pres->sockfd = INVSOC;          /* Don't close it more than once */
                    504:            delete_object(pres, HT_INTERRUPTED);
                    505:        }
                    506:        HTList_delete(HTNetPersistent);
                    507:        HTNetPersistent = NULL;
                    508:     }
2.23      frystyk   509:     if (HTNetPending) {
                    510:        HTList *cur = HTNetPending;
                    511:        HTNet *pres;
                    512:        while ((pres = (HTNet *) HTList_nextObject(cur)))
                    513:            delete_object(pres, HT_INTERRUPTED);
                    514:        HTList_delete(HTNetPending);
                    515:        HTNetPending = NULL;
                    516:     }
                    517:     if (HTNetActive) {
                    518:        HTList *cur = HTNetActive;
                    519:        HTNet *pres;
                    520:        while ((pres = (HTNet *) HTList_nextObject(cur)))
                    521:            delete_object(pres, HT_INTERRUPTED);
                    522:        HTList_delete(HTNetActive);
                    523:        HTNetActive = NULL;
                    524:     }
                    525:     return NO;
2.15      frystyk   526: }
                    527: 
2.25      frystyk   528: /*     HTNet_wait
                    529: **     ----------
                    530: **     Let a net object wait for a persistent socket. It will be launched
                    531: **     from the HTNet_delete() function
                    532: */
                    533: PUBLIC BOOL HTNet_wait (HTNet *net)
                    534: {
                    535:     if (net) {
2.33      frystyk   536:        if (WWWTRACE)
2.31      frystyk   537:            TTYPrint(TDEST,"HTNet_wait.. request %p is waiting for socket %d\n",
2.25      frystyk   538:                    net->request, net->sockfd);
                    539:        if (!HTNetPersistent) HTNetPersistent = HTList_new();
                    540:        HTList_addObject(HTNetPersistent, (void *) net);        
                    541:        return YES;
                    542:     }
                    543:     return NO;
                    544: }
                    545: 
2.23      frystyk   546: /* ------------------------------------------------------------------------- */
                    547: /*                             Killing requests                             */
                    548: /* ------------------------------------------------------------------------- */
                    549: 
                    550: /*     HTNet_kill
                    551: **     ----------
                    552: **     Kill the request by calling the call back function with a request for 
                    553: **     closing the connection. Does not remove the object. This is done by
                    554: **     HTNet_delete() function which is called by the load routine.
                    555: **     Returns OK if success, NO on error
                    556: */
                    557: PUBLIC BOOL HTNet_kill (HTNet * me)
                    558: {
                    559:     if (HTNetActive && me) {
2.25      frystyk   560:        HTList *cur = HTNetActive;
2.23      frystyk   561:        HTNet *pres;
2.25      frystyk   562:        while ((pres = (HTNet *) HTList_nextObject(cur))) {
2.23      frystyk   563:            if (pres == me) {
2.25      frystyk   564:                (*(pres->cbf))(pres->sockfd, pres->request, FD_CLOSE);
2.23      frystyk   565:                return YES;
                    566:            }
                    567:        }
                    568:     }
2.33      frystyk   569:     if (WWWTRACE)
2.31      frystyk   570:        TTYPrint(TDEST, "HTNet_kill.. object %p is not registered\n", me);
2.23      frystyk   571:     return NO;
                    572: }
                    573: 
                    574: /*     HTNet_killAll
                    575: **     -------------
                    576: **     Kills all registered (active+pending) requests by calling the call
                    577: **     back function with a request for closing the connection. We do not
                    578: **     remove the HTNet object as it is done by HTNet_delete().
                    579: **     Returns OK if success, NO on error
                    580: */
                    581: PUBLIC BOOL HTNet_killAll (void)
                    582: {
                    583:     HTNet *pres;
2.33      frystyk   584:     if (WWWTRACE)
2.31      frystyk   585:        TTYPrint(TDEST, "HTNet_kill.. ALL registered requests!!!\n");
2.23      frystyk   586: 
2.25      frystyk   587:     /* We start off in persistent queue so we avoid racing */
                    588:     if (HTNetPersistent) {
                    589:        while ((pres = (HTNet *) HTList_lastObject(HTNetPersistent)) != NULL) {
                    590:            pres->sockfd = INVSOC;
                    591:            (*(pres->cbf))(pres->sockfd, pres->request, FD_CLOSE);
                    592:            HTList_removeObject(HTNetPersistent, pres);
                    593:        }
                    594:     }
2.23      frystyk   595:     if (HTNetPending) {
                    596:        while ((pres = (HTNet *) HTList_lastObject(HTNetPending)) != NULL) {
2.25      frystyk   597:            (*(pres->cbf))(pres->sockfd, pres->request, FD_CLOSE);
2.23      frystyk   598:            HTList_removeObject(HTNetPending, pres);
                    599:        }
                    600:     }
                    601:     if (HTNetActive) {
                    602:        while ((pres = (HTNet *) HTList_lastObject(HTNetActive)) != NULL)
2.25      frystyk   603:            (*(pres->cbf))(pres->sockfd, pres->request, FD_CLOSE);
2.23      frystyk   604:     }
                    605:     return YES;
                    606: }

Webmaster