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

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

Webmaster