Annotation of libwww/Library/src/HTEvtLst.c, revision 2.10

2.4       eric        1: /*                                                                  HTEvtLst.c
2.1       frystyk     2: **     EVENT MANAGER
                      3: **
                      4: **     (c) COPYRIGHT MIT 1995.
                      5: **     Please first read the full copyright statement in the file COPYRIGH.
2.10    ! eric        6: **     @(#) $Id: HTEvtLst.c,v 2.9 1997/01/03 01:03:37 eric Exp $
2.1       frystyk     7: **
                      8: **     Updated HTEvent module 
                      9: **     This new module combines the functions of the old HTEvent module and 
                     10: **     the HTThread module. We retain the old HTThread module, but it
                     11: **     consists of calls to the HTEvent interfaces
                     12: **
                     13: ** Authors:
                     14: **     HFN     Henrik Frystyk <frystyk@w3.org>
                     15: **     CLB     Charlie Brooks <cbrooks@osf.org>
                     16: ** Bugs
                     17: **
                     18: */
                     19: 
                     20: /*   WSAAsyncSelect and windows app stuff need the following definitions:
                     21:  *   WWW_WIN_ASYNC - enable WSAAsyncSelect instead of select
                     22:  *   _WIN23 - win32 libararies - may be window or console app
                     23:  *   _WINSOCKAPI_ - using WINSOCK.DLL - not necessarily the async routines.
                     24:  *   _CONSOLE - the console app for NT
                     25:  *
                     26:  * first pass: EGP - 10/26/95
                     27:  */
                     28: 
                     29: /* Implementation dependent include files */
                     30: #include "sysdep.h"
                     31: #include "WWWUtil.h"
                     32: #include "WWWCore.h"
                     33: #include "HTReqMan.h"
                     34: #include "HTTimer.h"
2.5       eric       35: #include "HTEvent.h"
2.1       frystyk    36: #include "HTEvtLst.h"                                   /* Implemented here */
                     37: 
                     38: /* Type definitions and global variables etc. local to this module */
                     39: #define PRIME_TABLE_SIZE       67
                     40: #define MILLI_PER_SECOND       1000
                     41: #define HASH(s)                        ((s) % PRIME_TABLE_SIZE) 
2.2       eric       42: #define HT_EVENT_ORDER                           /* use event ordering code */
                     43: #define EVENTS_TO_EXECUTE      5  /* how many to execute in one select loop */
2.1       frystyk    44: 
2.3       eric       45: #ifdef WWW_WIN_ASYNC
                     46: #define TIMEOUT        1 /* WM_TIMER id */
                     47: PRIVATE HWND HTSocketWin;
                     48: PRIVATE unsigned long HTwinMsg;
                     49: #else /* WWW_WIN_ASYNC */
                     50: PRIVATE fd_set FdArray[HTEvent_TYPES];
                     51: PRIVATE SOCKET MaxSock = 0 ;                     /* max socket value in use */
                     52: #endif /* !WWW_WIN_ASYNC */
2.1       frystyk    53: 
2.3       eric       54: typedef struct {
                     55:     SOCKET     s ;                     /* our socket */
                     56:     HTEvent *  events[HTEvent_TYPES];  /* event parameters for read, write, oob */
2.5       eric       57: #ifndef IN_EVENT
                     58:     HTTimer *  timeouts[HTEvent_TYPES];
                     59: #endif
2.3       eric       60: } SockEvents;
                     61: 
                     62: typedef enum {
                     63:     SockEvents_mayCreate,
                     64:     SockEvents_find
                     65: } SockEvents_action;
2.1       frystyk    66: 
2.2       eric       67: HTList * HashTable[PRIME_TABLE_SIZE]; 
2.1       frystyk    68: PRIVATE int HTEndLoop = 0;                    /* If !0 then exit event loop */
                     69: 
                     70: /* ------------------------------------------------------------------------- */
                     71: 
2.8       frystyk    72: #ifdef WWW_WIN_ASYNC
                     73: PRIVATE BOOL Timer_setWindowsTimer (HTTimer * timer)
                     74: {
                     75:     HWND hwnd;
                     76:     UINT id;
                     77:     hwnd = HTEventList_getWinHandle(&id);
2.9       eric       78:     return SetTimer(hwnd, (UINT)timer, (UINT)HTTimer_getTime(timer), NULL) != 0;
2.8       frystyk    79: }
                     80: 
                     81: PRIVATE BOOL Timer_deleteWindowsTimer (HTTimer * timer)
                     82: {
                     83:     HWND hwnd;
                     84:     UINT id;
                     85:     hwnd = HTEventList_getWinHandle(&id);
                     86:     return KillTimer(hwnd, (UINT)timer) != 0;
                     87: }
                     88: #endif /* WWW_WIN_ASYNC */
                     89: 
2.3       eric       90: PRIVATE SockEvents * SockEvents_get (SOCKET s, SockEvents_action action)
2.1       frystyk    91: {
                     92:     long v = HASH(s);
2.2       eric       93:     HTList* cur;
2.3       eric       94:     SockEvents * pres;
2.2       eric       95: 
                     96:     if (HashTable[v] == NULL)
                     97:        HashTable[v] = HTList_new();
                     98:     cur = HashTable[v];
2.3       eric       99:     while ((pres = (SockEvents *) HTList_nextObject(cur)))
2.2       eric      100:        if (pres->s == s)
                    101:            return pres;
                    102: 
2.3       eric      103:     if (action == SockEvents_mayCreate) {
                    104:         if ((pres = (SockEvents *) HT_CALLOC(1, sizeof(SockEvents))) == NULL)
2.1       frystyk   105:            HT_OUTOFMEM("HTEventList_register");
2.2       eric      106:        pres->s = s;
                    107:        HTList_addObject(HashTable[v], (void *)pres);
                    108:        return pres;
2.1       frystyk   109:     }
                    110:     return NULL;
                    111: }
                    112: 
2.3       eric      113: PUBLIC void HTEvent_traceHead(void)
                    114: {
                    115:     HTTrace("     event: pri millis  callback   param    request  ");
                    116: }
                    117: PUBLIC void HTEvent_trace(HTEvent * event)
                    118: {
                    119:     if (event == NULL)
                    120:        return;
                    121:     HTTrace("%8p: %3d %6d %8p %8p %8p", event, event->priority, event->millis, event->cbf, event->param, event->request);
                    122: }
                    123: PUBLIC void HTTimer_traceHead(void)
                    124: {
                    125:     HTTrace("     timer: millis expires ?   param   callback  ");
                    126: }
                    127: PRIVATE char * MyTime(unsigned long int time, int len)
                    128: {
                    129: static char space[100];
                    130:     sprintf(space, "1234567");
                    131:     return space;
                    132: }
                    133: struct _HTTimer {
                    134:     HTTimer *  next;           /* The next guy in line */
                    135:     ms_t       millis;         /* Relative value in millis */
                    136:     ms_t       expires;        /* Absolute value in millis */
                    137:     BOOL       relative;
                    138:     void *     param;          /* Client supplied context */
                    139:     HTTimerCallback * cbf;
                    140: };
                    141: 
                    142: PUBLIC void HTTimer_trace(HTTimer * timer)
                    143: {
                    144:     if (timer == NULL)
                    145:        return;
                    146:     HTTrace("%8p: %6d %7s %c %8p %8p", timer, timer->millis, MyTime(timer->expires, 7), 
                    147:            timer->relative == YES ? 'R' : 'A', timer->param, timer->cbf);
                    148: }
2.1       frystyk   149: /*
                    150: **  A simple debug function that dumps all the socket arrays
                    151: **  as trace messages
                    152: */
2.3       eric      153: PRIVATE void EventList_dump (void)
2.1       frystyk   154: {
2.3       eric      155:     long v;
                    156:     HTList* cur;
                    157:     SockEvents * pres;
                    158: 
                    159:     if (HashTable[v] == NULL) {
                    160:        HTTrace("Event....... No sockets registered\n");
                    161:        return;
                    162:     }
                    163:     HTTrace("Event....... Dumping socket events\n");
                    164:     HTTrace("soc ");
                    165:     HTEvent_traceHead();
                    166:     HTTrace(" ");
                    167:     HTTimer_traceHead();
                    168:     HTTrace("\n");
                    169:     for (v = 0; v < PRIME_TABLE_SIZE; v++) {
                    170:        cur = HashTable[v];
                    171:        while ((pres = (SockEvents *) HTList_nextObject(cur))) {
                    172:            int i;
                    173:            HTTrace("%3d \n", pres->s);
                    174:            for (i = 0; i < HTEvent_TYPES; i++)
                    175:                if (pres->events[i]) {
                    176:                    static char * names[HTEvent_TYPES] = {"read", "writ", "xcpt"};
                    177:                    HTTrace("%s", names[i]);
                    178:                    HTEvent_trace(pres->events[i]);
                    179:                    HTTrace(" ");
2.5       eric      180: #ifndef IN_EVENT
2.3       eric      181:                    HTTimer_trace(pres->timeouts[i]);
                    182:                    HTTrace(" ");
2.5       eric      183: #endif
2.3       eric      184:                }
                    185:            HTTrace("\n");
                    186:        }
                    187:     }
2.1       frystyk   188: }
                    189: 
                    190: /* ------------------------------------------------------------------------- */
2.5       eric      191: /*             T I M E O U T   H A N D L E R                                */
                    192: PRIVATE int EventListTimerHandler (HTTimer * timer, void * param, HTEventType type)
                    193: {
                    194:     SockEvents * sockp = (SockEvents *)param;
                    195:     HTEvent * event;
2.9       eric      196:     /* HTMemLog_flush(); keep around - very useful for debugging crashes - EGP */
2.5       eric      197: #ifdef IN_EVENT
                    198:     if (sockp->events[HTEvent_INDEX(HTEvent_READ)]->timer == timer)
                    199: #else /* IN_EVENT */
                    200:     if (sockp->timeouts[HTEvent_INDEX(HTEvent_READ)] == timer)
                    201: #endif /* !IN_EVENT */
                    202:     {
                    203:        event = sockp->events[HTEvent_INDEX(HTEvent_READ)];
                    204:        if (THD_TRACE) HTTrace("Event....... READ timed out on %d.\n", sockp->s);
                    205:        return (*event->cbf) (sockp->s, event->param, HTEvent_TIMEOUT);
                    206:     }
                    207: #ifdef IN_EVENT
                    208:     if (sockp->events[HTEvent_INDEX(HTEvent_WRITE)]->timer == timer)
                    209: #else /* IN_EVENT */
                    210:     if (sockp->timeouts[HTEvent_INDEX(HTEvent_WRITE)] == timer)
                    211: #endif /* !IN_EVENT */
                    212:     {
                    213:        event = sockp->events[HTEvent_INDEX(HTEvent_WRITE)];
                    214:        if (THD_TRACE) HTTrace("Event....... WRITE timed out on %d.\n", sockp->s);
                    215:        return (*event->cbf) (sockp->s, event->param, HTEvent_TIMEOUT);
                    216:     }
                    217: #ifdef IN_EVENT
                    218:     if (sockp->events[HTEvent_INDEX(HTEvent_OOB)]->timer == timer)
                    219: #else /* IN_EVENT */
                    220:     if (sockp->timeouts[HTEvent_INDEX(HTEvent_OOB)] == timer)
                    221: #endif /* !IN_EVENT */
                    222:     {
                    223:        event = sockp->events[HTEvent_INDEX(HTEvent_OOB)];
                    224:        if (THD_TRACE) HTTrace("Event....... OOB timed out on %d.\n", sockp->s);
                    225:        return (*event->cbf) (sockp->s, event->param, HTEvent_TIMEOUT);
                    226:     }
                    227:     HTTrace("Event....... can't find event for timer %p.\n", timer);
                    228:     return HT_ERROR;
                    229: }
                    230: 
                    231: /* ------------------------------------------------------------------------- */
2.2       eric      232: /*             E V E N T   O R D E R I N G   S T U F F                      */
                    233: #ifdef HT_EVENT_ORDER
                    234: typedef struct {
                    235:     HTEvent *  event;
                    236:     SOCKET     s;
                    237:     HTEventType        type;
                    238:     HTPriority skipped;
                    239: } EventOrder;
2.1       frystyk   240: 
2.2       eric      241: HTList * EventOrderList = NULL;
                    242: #if 0
                    243: /*
                    244: **     return -1 if a should be after b
                    245:  */
                    246: int EventOrderComparer (const void * a, const void * b)
                    247: {
                    248:     EventOrder * placeMe = (EventOrder *)a;
                    249:     EventOrder * maybeHere = (EventOrder *)b;
                    250:     if (placeMe->event->priority+placeMe->skipped >= maybeHere->event->priority+maybeHere->skipped)
                    251:        return 1;
                    252:     return -1;
                    253: }
                    254: #endif
                    255: 
2.5       eric      256: int EventOrder_add (SOCKET s, HTEventType type, ms_t now)
2.2       eric      257: {
                    258:     EventOrder * pres;
                    259:     HTList * cur = EventOrderList;
                    260:     HTList * insertAfter = cur;
2.5       eric      261:     SockEvents * sockp = SockEvents_get(s, SockEvents_find);
                    262:     HTEvent * event;
2.2       eric      263: 
2.5       eric      264:     if (sockp == NULL || (event = sockp->events[HTEvent_INDEX(type)]) == NULL) {
                    265:        HTTrace("EventOrder.. no event found for socket %d, type %s.\n", s, HTEvent_type2str(type));
2.2       eric      266:        return HT_ERROR;
                    267:     }
                    268: 
2.5       eric      269:     /* Fixup the timeout
                    270:     */
                    271: #ifdef IN_EVENT
                    272:     if (event->timer)
                    273:        HTTimer_refresh(event->timer, now);
                    274: #else
                    275:     if (sockp->timeouts[HTEvent_INDEX(type)])
                    276:        HTTimer_refresh(sockp->timeouts[HTEvent_INDEX(type)], now);
                    277: #endif
                    278: 
2.2       eric      279:     /*
                    280:     ** Look to see if it's already here from before
                    281:     */
                    282:     while ((pres = (EventOrder *) HTList_nextObject(cur))) {
                    283:        if (pres->s == s && pres->event == event && pres->type == type) {
                    284:            pres->skipped++;
                    285:            return HT_OK;
                    286:        }
                    287:        if (pres->event->priority+pres->skipped > event->priority)
                    288:            insertAfter = cur;
                    289:     }
                    290: 
                    291:     /*
                    292:     ** No, so create a new element
                    293:     */
                    294:     if ((pres = (EventOrder *) HT_CALLOC(1, sizeof(EventOrder))) == NULL)
                    295:        HT_OUTOFMEM("EventOrder_add");
                    296:     pres->event = event;
                    297:     pres->s = s;
                    298:     pres->type = type;
                    299:     HTList_addObject(insertAfter, (void *)pres);
                    300:     return HT_OK;
                    301: }
                    302: 
                    303: PUBLIC int EventOrder_executeAndDelete (void) 
                    304: {
2.4       eric      305:     HTList * cur = EventOrderList;
2.2       eric      306:     EventOrder * pres;
                    307:     int i = 0;
                    308:     if (THD_TRACE) HTTrace("EventOrder.. execute ordered events\n");
2.4       eric      309:     if (cur == NULL) return NO;
2.2       eric      310:     while ((pres = (EventOrder *) HTList_removeLastObject(cur)) && i < EVENTS_TO_EXECUTE) {
2.5       eric      311:        HTEvent * event = pres->event;
                    312:        int ret;
2.7       frystyk   313:        if (THD_TRACE)
2.5       eric      314:        HTTrace("EventList... calling socket %d, request %p handler %p type %s\n",
                    315:                pres->s, (void *) event->request,
                    316:                (void *) event->cbf, HTEvent_type2str(pres->type));
                    317:        ret = (*pres->event->cbf)(pres->s, pres->event->param, pres->type);
2.2       eric      318:        HT_FREE(pres);
                    319:        if (ret != HT_OK)
                    320:            return ret;
                    321:        i++;
                    322:     }
                    323:     return HT_OK;
                    324: }
                    325: 
                    326: PUBLIC BOOL EventOrder_deleteAll (void) 
                    327: {
                    328:     HTList * cur = EventOrderList;
                    329:     EventOrder * pres;
                    330:     if (THD_TRACE) HTTrace("EventOrder.. all ordered events\n");
2.4       eric      331:     if (cur == NULL) return NO;
2.2       eric      332:     while ((pres = (EventOrder *) HTList_nextObject(cur)))
                    333:        HT_FREE(pres);
                    334:     HTList_delete(EventOrderList);
                    335:     EventOrderList = NULL;
                    336:     return YES;
                    337: }
                    338: #endif /* HT_EVENT_ORDER */
                    339: 
                    340: /* ------------------------------------------------------------------------- */
2.1       frystyk   341: 
                    342: /*
                    343: **  For a given socket, reqister a request structure, a set of operations, 
                    344: **  a HTEventCallback function, and a priority. For this implementation, 
                    345: **  we allow only a single HTEventCallback function for all operations.
                    346: **  and the priority field is ignored.
                    347: */
                    348: PUBLIC int HTEventList_register (SOCKET s, HTEventType type, HTEvent * event)
                    349: {
2.3       eric      350:     SockEvents * sockp;
2.1       frystyk   351:     if (THD_TRACE) 
2.5       eric      352:        HTTrace("Event....... Register socket %d, request %p handler %p type %s at priority %d\n",
2.1       frystyk   353:                s, (void *) event->request,
2.5       eric      354:                (void *) event->cbf, HTEvent_type2str(type),
2.1       frystyk   355:                (unsigned) event->priority);
2.4       eric      356:     if (s==INVSOC || HTEvent_INDEX(type) >= HTEvent_TYPES)
2.1       frystyk   357:        return 0;
                    358: 
                    359:     /*
                    360:     ** Insert socket into appropriate file descriptor set. We also make sure
                    361:     ** that it is registered in the global set.
                    362:     */
2.5       eric      363:     if (THD_TRACE) HTTrace("Event....... Registering socket for %s\n", HTEvent_type2str(type));
2.3       eric      364:     sockp = SockEvents_get(s, SockEvents_mayCreate);
                    365:     sockp->s = s;
                    366:     sockp->events[HTEvent_INDEX(type)] = event;
                    367: #ifdef WWW_WIN_ASYNC
2.4       eric      368:     if (WSAAsyncSelect(s, HTSocketWin, HTwinMsg, HTEvent_BITS(type)) < 0)
2.3       eric      369:        return HT_ERROR;
                    370: #else /* WWW_WIN_ASYNC */
2.1       frystyk   371:     FD_SET(s, FdArray+HTEvent_INDEX(type));
2.3       eric      372:     if (s > MaxSock) MaxSock = s ;
                    373: #endif /* !WWW_WIN_ASYNC */
2.1       frystyk   374:     /*
                    375:     ** If the timeout has been set (relative in millis) then we register 
                    376:     ** a new timeout for this event
                    377:     */
                    378:     if (event->millis >= 0) {
2.5       eric      379: #ifdef IN_EVENT
                    380:        event->timer = HTTimer_new(NULL, EventListTimerHandler, sockp, event->millis, YES);
                    381: #else
2.3       eric      382:        sockp->timeouts[HTEvent_INDEX(type)] = HTTimer_new(NULL, EventListTimerHandler, sockp, event->millis, YES);
2.5       eric      383: #endif
2.1       frystyk   384:     }
                    385: 
                    386:     return HT_OK;
                    387: }
                    388: 
2.3       eric      389: PRIVATE int EventList_remaining(SockEvents * pres)
                    390: {
                    391:     int ret = 0;
                    392:     int i;
                    393:     for (i = 0; i < HTEvent_TYPES; i++)
                    394:        if (pres->events[i] != NULL)
                    395:            ret |= 1<<i;
                    396:     return ret;
                    397: }
                    398: 
2.1       frystyk   399: /*
                    400: ** Remove the registered information for the specified socket for the actions 
                    401: ** specified in ops. if no actions remain after the unregister, the registered
                    402: ** info is deleted, and, if the socket has been registered for notification, 
                    403: ** the HTEventCallback will be invoked.
                    404: */
                    405: PUBLIC int HTEventList_unregister(SOCKET s, HTEventType type) 
                    406: {
2.3       eric      407:     long               v = HASH(s);
                    408:     HTList *           cur = HashTable[v];
                    409:     HTList *           last = cur;
                    410:     SockEvents *       pres;
2.9       eric      411:     int                        ret = HT_ERROR;
2.2       eric      412: 
2.9       eric      413: #ifndef WWW_WIN_ASYNC
2.6       eric      414:     MaxSock = -1;      /* re-find maximum in-use socket */
2.9       eric      415: #endif /* !WWW_WIN_ASYNC */
                    416:     while (cur && (pres = (SockEvents *) HTList_nextObject(cur))) {
2.2       eric      417:         if (pres->s == s) {
2.3       eric      418:            int         remaining;
2.1       frystyk   419: 
                    420:            /*
                    421:            **  Unregister the event from this action
                    422:            */
2.2       eric      423:            pres->events[HTEvent_INDEX(type)] = NULL;
2.1       frystyk   424: 
                    425:            /*
                    426:            **  Check to see of there was a timeout connected with the event.
                    427:            **  If so then delete the timeout as well.
                    428:            */
                    429:            {
2.5       eric      430: #ifdef IN_EVENT
                    431:                HTTimer * timer = pres->events[HTEvent_INDEX(type)]->timer;
                    432: #else
2.2       eric      433:                HTTimer * timer = pres->timeouts[HTEvent_INDEX(type)];
2.5       eric      434: #endif
2.1       frystyk   435:                if (timer) HTTimer_delete(timer);
                    436:            }
                    437:            
                    438:            /*
                    439:            **  Check to see if we can delete the action completely. We do this
                    440:            **  if there are no more events registered.
                    441:            */
2.3       eric      442:            if ((remaining = EventList_remaining(pres)) == 0) {
2.9       eric      443:                HTList * doomed = cur;
2.1       frystyk   444:                if (THD_TRACE)
                    445:                    HTTrace("Event....... No more events registered for socket %d\n", s);
2.2       eric      446:                HT_FREE(pres);
2.9       eric      447:                pres = (SockEvents *) HTList_nextObject(cur);
                    448:                HTList_quickRemoveElement(doomed, last);
                    449:            }
                    450:            ret = HT_OK;
2.3       eric      451: #ifdef WWW_WIN_ASYNC
                    452:            if (WSAAsyncSelect(s, HTSocketWin, HTwinMsg, remaining) < 0)
2.9       eric      453:                ret = HT_ERROR;
                    454:            cur = NULL; /* we don't have to finish list to look for MaxSock in WWW_WIN_ASYNC */
2.3       eric      455: #else /* WWW_WIN_ASYNC */
                    456:            FD_CLR(s, FdArray+HTEvent_INDEX(type));
                    457: #endif /* !WWW_WIN_ASYNC */
                    458:            if (THD_TRACE)
2.5       eric      459:                HTTrace("Event....... Socket %d unregisterd for %s\n", s, HTEvent_type2str(type));
2.6       eric      460:            /*
2.10    ! eric      461:            **  stay in loop to finish checking list for max socket if not at end
2.6       eric      462:            */
2.10    ! eric      463:            if (!cur) break;
2.9       eric      464:        }
                    465:        last = cur;
                    466: #ifndef WWW_WIN_ASYNC
                    467:        if(pres->s > MaxSock)
2.6       eric      468:            MaxSock = pres->s;
2.9       eric      469: #endif /* !WWW_WIN_ASYNC */
2.1       frystyk   470:     }
2.9       eric      471:     if (ret == HT_ERROR && THD_TRACE) HTTrace("Event....... Couldn't find socket %d.\n", s);
                    472: #ifndef WWW_WIN_ASYNC
                    473:     if (MaxSock <= 0 && THD_TRACE) HTTrace("Event....... MaxSock: %d.\n", MaxSock);
                    474: #endif /* !WWW_WIN_ASYNC */
                    475:     return ret;
2.1       frystyk   476: }
                    477: 
                    478: /*
                    479: ** Unregister all sockets 
                    480: ** N.B. we just remove them for our internal data structures: it is up to the 
                    481: ** application to actually close the socket. 
                    482: */
2.2       eric      483: PUBLIC int HTEventList_unregisterAll (void) 
2.1       frystyk   484: {
                    485:     int i;
                    486:     if (THD_TRACE) HTTrace("Unregister.. all sockets\n");
                    487:     for (i = 0 ; i < PRIME_TABLE_SIZE; i++) {
2.2       eric      488:        HTList * cur = HashTable[i];
2.3       eric      489:        SockEvents * pres;
                    490:        while ((pres = (SockEvents *) HTList_nextObject(cur))) {
                    491: #ifdef WWW_WIN_ASYNC
                    492:            WSAAsyncSelect(pres->s, HTSocketWin, HTwinMsg, 0);
                    493: #endif /* WWW_WIN_ASYNC */
2.2       eric      494:            HT_FREE(pres);
2.3       eric      495:        }
2.2       eric      496:        HTList_delete(HashTable[i]);
                    497:        HashTable[i] = NULL;
2.1       frystyk   498:     }
2.3       eric      499: #ifndef WWW_WIN_ASYNC
                    500:     MaxSock = 0 ;
2.1       frystyk   501:     FD_ZERO(FdArray+HTEvent_INDEX(HTEvent_READ));
                    502:     FD_ZERO(FdArray+HTEvent_INDEX(HTEvent_WRITE));
                    503:     FD_ZERO(FdArray+HTEvent_INDEX(HTEvent_OOB));
2.3       eric      504: #endif /* !WWW_WIN_ASYNC */
2.2       eric      505: #ifdef HT_EVENT_ORDER
                    506:     EventOrder_deleteAll();
                    507: #endif /* HT_EVENT_ORDER */
2.1       frystyk   508:     return 0;
                    509: }
                    510: 
                    511: /*
                    512: **  Dispatch the event to the appropriate event handler.
                    513: **  If no event handler is found then just return.
                    514: */
2.5       eric      515: PUBLIC int HTEventList_dispatch (SOCKET s, HTEventType type, ms_t now)
2.1       frystyk   516: {
2.3       eric      517:     SockEvents * sockp = SockEvents_get(s, SockEvents_find);
                    518:     if (sockp) {
                    519:        HTEvent * event = sockp->events[HTEvent_INDEX(type)];
2.1       frystyk   520: 
2.5       eric      521:        /*      Fixup the timeout
                    522:        */
                    523: #ifdef IN_EVENT
                    524:        if (event->timer)
                    525:            HTTimer_refresh(event->timer, now);
                    526: #else
                    527:        if (sockp->timeouts[HTEvent_INDEX(type)])
                    528:            HTTimer_refresh(sockp->timeouts[HTEvent_INDEX(type)], now);
                    529: #endif
2.1       frystyk   530:        /*
                    531:        **  If we have found an event object for this event then see
                    532:        **  is we should call it.
                    533:        */
                    534:        if (event && event->priority!=HT_PRIORITY_OFF)
                    535:            return (*event->cbf) (s, event->param, type);
2.3       eric      536:        if (THD_TRACE) HTTrace("Dispatch.... Handler %p NOT called\n", sockp);
2.1       frystyk   537:        return HT_OK;
                    538:     }
                    539:     if (THD_TRACE) HTTrace("Dispatch.... Bad socket %d\n", s);
                    540:     return NO;
                    541: }
                    542: 
                    543: /*
                    544: **  Stops the (select based) event loop. The function does not guarantee
                    545: **  that all requests have terminated. This is for the app to do
                    546: */
                    547: PUBLIC void HTEventList_stopLoop (void)
                    548: {
                    549:     HTEndLoop = 1;
                    550: }
                    551: 
2.3       eric      552: PUBLIC HTEvent * HTEventList_lookup (SOCKET s, HTEventType type)
                    553: {
                    554:     SockEvents * sockp = NULL;
                    555:     if ((sockp = SockEvents_get(s, SockEvents_find)) == NULL)
                    556:        return NULL;
                    557:     return sockp->events[HTEvent_INDEX(type)];
                    558: }
                    559: 
                    560: /*     REGISTER DEFULT EVENT MANAGER
                    561: **     -----------------------------
                    562: **     Not done automaticly - may be done by application!
                    563: */
                    564: PUBLIC BOOL HTEventInit (void)
                    565: {
2.4       eric      566: #ifdef WWW_WIN_ASYNC
                    567:     /*
                    568:     ** We are here starting a hidden window to take care of events from
                    569:     **  the async select() call in the async version of the event loop in
                    570:     ** the Internal event manager (HTEvtLst.c)
                    571:     */
                    572:     static char className[] = "AsyncWindowClass";
                    573:     WNDCLASS wc;
                    574:     OSVERSIONINFO osInfo;
                    575:     
                    576:     wc.style=0;
                    577:     wc.lpfnWndProc=(WNDPROC)AsyncWindowProc;
                    578:     wc.cbClsExtra=0;
                    579:     wc.cbWndExtra=0;
                    580:     wc.hIcon=0;
                    581:     wc.hCursor=0;
                    582:     wc.hbrBackground=0;
                    583:     wc.lpszMenuName=(LPSTR)0;
                    584:     wc.lpszClassName=className;
                    585: 
                    586:     osInfo.dwOSVersionInfoSize = sizeof(osInfo);
                    587:     GetVersionEx(&osInfo);
                    588:     if (osInfo.dwPlatformId == VER_PLATFORM_WIN32s || osInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
                    589:        wc.hInstance=GetModuleHandle(NULL); /* 95 and non threaded platforms */
                    590:     else
                    591:        wc.hInstance=GetCurrentProcess(); /* NT and hopefully everything following */
                    592:     if (!RegisterClass(&wc)) {
                    593:        HTTrace("HTLibInit.. Can't RegisterClass \"%s\"\n", className);
                    594:            return NO;
                    595:     }
                    596:     if (!(HTSocketWin = CreateWindow(className, "WWW_WIN_ASYNC", WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT, 
                    597:                                      CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, wc.hInstance,0))) {
                    598:        char space[50];
                    599:                HTTrace("HTLibInit.. Can't CreateWindow \"WWW_WIN_ASYNC\" - error:");
                    600:        sprintf(space, "%ld\n", GetLastError());
                    601:        HTTrace(space);
                    602:        return NO;
                    603:     }
                    604:     HTwinMsg = WM_USER;  /* use first available message since app uses none */
2.8       frystyk   605: 
                    606:     /*
                    607:     **  Register platform specific timer handlers for windows
                    608:     */
                    609:     HTTimer_registerSetTimerCallback(Timer_setWindowsTimer);
                    610:     HTTimer_registerDeleteTimerCallback(Timer_deleteWindowsTimer);
                    611: 
2.4       eric      612: #endif /* WWW_WIN_ASYNC */
                    613: 
                    614: #ifdef _WINSOCKAPI_
                    615:     /*
                    616:     ** Initialise WinSock DLL. This must also be shut down! PMH
                    617:     */
                    618:     {
                    619:         WSADATA            wsadata;
                    620:        if (WSAStartup(DESIRED_WINSOCK_VERSION, &wsadata)) {
                    621:            if (WWWTRACE)
                    622:                HTTrace("HTEventInit. Can't initialize WinSoc\n");
                    623:             WSACleanup();
                    624:             return NO;
                    625:         }
                    626:         if (wsadata.wVersion < MINIMUM_WINSOCK_VERSION) {
                    627:             if (WWWTRACE)
                    628:                HTTrace("HTEventInit. Bad version of WinSoc\n");
                    629:             WSACleanup();
                    630:             return NO;
                    631:         }
                    632:        if (APP_TRACE)
                    633:            HTTrace("HTEventInit. Using WinSoc version \"%s\".\n", 
                    634:                    wsadata.szDescription);
                    635:     }
                    636: #endif /* _WINSOCKAPI_ */
                    637: 
2.3       eric      638:     HTEvent_setRegisterCallback(HTEventList_register);
                    639:     HTEvent_setUnregisterCallback(HTEventList_unregister);
                    640:     return YES;
                    641: }
                    642: 
                    643: PUBLIC BOOL HTEventTerminate (void)
                    644: {
                    645:     return YES;
                    646: }
                    647: 
                    648: #ifdef WWW_WIN_ASYNC
                    649: 
2.4       eric      650: /*     HTEventList_get/setWinHandle
2.3       eric      651: **     --------------------------
                    652: **     Managing the windows handle on Windows
                    653: */
                    654: PUBLIC BOOL HTEventList_setWinHandle (HWND window, unsigned long message)
                    655: {
                    656:     HTSocketWin = window;
                    657:     HTwinMsg = message;
                    658:     return YES;
                    659: }
                    660: 
                    661: PUBLIC HWND HTEventList_getWinHandle (unsigned long * pMessage)
                    662: {
                    663:     if (pMessage)
                    664:         *pMessage = HTwinMsg;
                    665:     return (HTSocketWin);
                    666: }
                    667: 
                    668: /* only responsible for WM_TIMER and WSA_AsyncSelect */        
                    669: PUBLIC LRESULT CALLBACK AsyncWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
                    670: {
                    671:     WORD event;
                    672:     SOCKET sock;
2.4       eric      673:     HTEventType type;
2.5       eric      674:     ms_t now = HTGetTimeInMillis();
2.3       eric      675: 
                    676:     /* timeout stuff */
                    677:     if (uMsg == WM_TIMER) {
2.4       eric      678:        HTTimer_dispatch((HTTimer *)wParam);
2.3       eric      679:        return (0);
                    680:     }
                    681: 
                    682:     if (uMsg != HTwinMsg)      /* not our async message */
                    683:        return (DefWindowProc(hwnd, uMsg, wParam, lParam));
                    684: 
                    685:     event = LOWORD(lParam);
                    686:     sock = (SOCKET)wParam;
2.4       eric      687:     switch (event) {
                    688:     case FD_READ: type = HTEvent_READ; break;
                    689:     case FD_WRITE: type = HTEvent_WRITE; break;
                    690:     case FD_ACCEPT: type = HTEvent_ACCEPT; break;
                    691:     case FD_CONNECT: type = HTEvent_CONNECT; break;
                    692:     case FD_OOB: type = HTEvent_OOB; break;
                    693:     case FD_CLOSE: type = HTEvent_CLOSE; break;
                    694:     default: HTDebugBreak();
                    695:     }
2.5       eric      696:     if (HTEventList_dispatch((int)sock, type, now) != HT_OK)
2.4       eric      697:        HTEndLoop = -1;
2.3       eric      698:     return (0);
                    699: }
                    700: 
2.4       eric      701: PUBLIC int HTEventList_loop (HTRequest * theRequest )
2.3       eric      702: {
                    703:     MSG msg;
                    704:     while (GetMessage(&msg,0,0,0)) {
                    705:            TranslateMessage(&msg);
                    706:            DispatchMessage(&msg);
                    707:     }
                    708:     return (HTEndLoop == 1 ? HT_OK : HT_ERROR);
                    709: }
                    710: 
                    711: #else /* WWW_WIN_ASYNC */
                    712: 
2.1       frystyk   713: /*
                    714: **  We wait for activity from one of our registered 
                    715: **  channels, and dispatch on that.
                    716: **
                    717: **  There are now two versions of the event loop. The first is if you want
                    718: **  to use async I/O on windows, and the other is if you want to use normal
                    719: **  Unix setup with sockets
                    720: */
                    721: PUBLIC int HTEventList_loop (HTRequest * theRequest) 
                    722: {
                    723:     fd_set treadset, twriteset, texceptset;
                    724:     struct timeval waittime, * wt;
                    725:     int active_sockets;
                    726:     int maxfds;
2.5       eric      727:     ms_t timeout;
                    728:     ms_t now;
2.1       frystyk   729:     SOCKET s;
2.5       eric      730:     int status = HT_OK;
2.1       frystyk   731:     HTEndLoop = 0;
                    732: 
2.2       eric      733:     EventOrderList = HTList_new();     /* is kept around until EventOrder_deleteAll */
                    734: 
2.1       frystyk   735:     /* Don't leave this loop until we leave the application */
                    736:     do {
                    737:         treadset = FdArray[HTEvent_INDEX(HTEvent_READ)];
                    738:         twriteset = FdArray[HTEvent_INDEX(HTEvent_WRITE)];
                    739:         texceptset = FdArray[HTEvent_INDEX(HTEvent_OOB)];
2.3       eric      740:         maxfds = MaxSock; 
2.1       frystyk   741: 
                    742:        if (THD_TRACE) HTTrace("Event Loop.. calling select: maxfds is %d\n", maxfds);
                    743: 
                    744:         /*
                    745:        **  Timeval struct copy needed for linux, as it set the value to the
                    746:        **  remaining timeout while exiting the select. (and perhaps for
                    747:        **  other OS). Code borrowed from X server.
                    748:        */
                    749:        wt = NULL;
2.5       eric      750:        if ((status = HTTimer_next(&timeout)))
                    751:            return status;
                    752:        if (timeout != 0) {
2.1       frystyk   753:            waittime.tv_sec = timeout / MILLI_PER_SECOND;
                    754:            waittime.tv_usec = (timeout % MILLI_PER_SECOND) *
                    755:                (1000000 / MILLI_PER_SECOND);
                    756:            wt = &waittime;
                    757:        }
                    758: 
2.2       eric      759:        HTTraceData((char*)&treadset, maxfds/8 + 1, "HTEventList_loop pre treadset: (maxfd:%d)", maxfds);
                    760:        HTTraceData((char*)&twriteset, maxfds/8 + 1, "HTEventList_loop pre twriteset:");
                    761:        HTTraceData((char*)&texceptset, maxfds/8 + 1, "HTEventList_loop pre texceptset:");
                    762: 
2.1       frystyk   763: #ifdef __hpux 
                    764:         active_sockets = select(maxfds+1, (int *)&treadset, (int *)&twriteset,
                    765:                                (int *)&texceptset, wt);
                    766: #else
                    767:         active_sockets = select(maxfds+1, &treadset, &twriteset, &texceptset, wt);
                    768: #endif
2.2       eric      769: 
2.5       eric      770:        now = HTGetTimeInMillis();
2.2       eric      771:        HTTraceData((char*)&treadset, maxfds/8 + 1, "HTEventList_loop post treadset: (active_sockets:%d)", active_sockets);
                    772:        HTTraceData((char*)&twriteset, maxfds/8 + 1, "HTEventList_loop post twriteset: (errno:%d)", errno);
                    773:        HTTraceData((char*)&texceptset, maxfds/8 + 1, "HTEventList_loop post texceptset:");
                    774: 
2.1       frystyk   775:        if (THD_TRACE) HTTrace("Event Loop.. select returns %d\n", active_sockets);
                    776: 
                    777:         if (active_sockets == -1) {
                    778:            HTRequest_addSystemError( theRequest, ERR_FATAL, socerrno, NO, "select");
2.3       eric      779:            EventList_dump();
                    780: #if 0
2.1       frystyk   781:            __DumpFDSet(FdArray+HTEvent_INDEX(HTEvent_READ), "Read");
                    782:            __DumpFDSet(FdArray+HTEvent_INDEX(HTEvent_WRITE), "Write") ;
                    783:            __DumpFDSet(FdArray+HTEvent_INDEX(HTEvent_OOB), "Exceptions");
2.3       eric      784: #endif /* 0 */
2.1       frystyk   785:            return HT_ERROR;
                    786:         }
                    787: 
                    788:        /*
                    789:        **  We had a timeout so now we check and see if we have a timeout
2.5       eric      790:        **  handler to call. Let HTTimer_next get it.
2.1       frystyk   791:        */ 
2.5       eric      792:        if (active_sockets == 0)
2.1       frystyk   793:            continue;
                    794: 
                    795:        /*
                    796:        **  There were active sockets. Determine which fd sets they were in
                    797:        */
2.2       eric      798: #ifdef HT_EVENT_ORDER
2.5       eric      799: #define DISPATCH(socket, type, now) EventOrder_add(socket, type, now)
2.2       eric      800: #else /* HT_EVENT_ORDER */
2.5       eric      801: #define DISPATCH(socket, type, now) HTEventList_dispatch(socket, type, now)
2.2       eric      802: #endif /* !HT_EVENT_ORDER */
2.1       frystyk   803:        for (s = 0 ; s <= maxfds ; s++) { 
                    804:            if (FD_ISSET(s, &texceptset))
2.5       eric      805:                if ((status = DISPATCH(s, HTEvent_OOB, now)) != HT_OK)
2.1       frystyk   806:                    return status;
                    807:            if (FD_ISSET(s, &twriteset))
2.5       eric      808:                if ((status = DISPATCH(s, HTEvent_WRITE, now)) != HT_OK)
2.1       frystyk   809:                    return status;
                    810:            if (FD_ISSET(s, &treadset))
2.5       eric      811:                if ((status = DISPATCH(s, HTEvent_READ, now)) != HT_OK)
2.1       frystyk   812:                    return status;
                    813:        }
2.2       eric      814: #ifdef HT_EVENT_ORDER
                    815:        if ((status = EventOrder_executeAndDelete()) != HT_OK)
                    816:            return status;
                    817: #endif /* HT_EVENT_ORDER */
2.1       frystyk   818:     } while (!HTEndLoop);
                    819: 
                    820:     return HT_OK;
                    821: }
                    822: 
2.3       eric      823: #endif /* !WWW_WIN_ASYNC */

Webmaster