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

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.6     ! eric        6: **     @(#) $Id: HTEvtLst.c,v 2.5 1996/12/13 22:06:06 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;
                    295:        HTTrace("EventList... calling socket %d, request %p handler %p type %s\n",
                    296:                pres->s, (void *) event->request,
                    297:                (void *) event->cbf, HTEvent_type2str(pres->type));
                    298:        ret = (*pres->event->cbf)(pres->s, pres->event->param, pres->type);
2.2       eric      299:        HT_FREE(pres);
                    300:        if (ret != HT_OK)
                    301:            return ret;
                    302:        i++;
                    303:     }
                    304:     return HT_OK;
                    305: }
                    306: 
                    307: PUBLIC BOOL EventOrder_deleteAll (void) 
                    308: {
                    309:     HTList * cur = EventOrderList;
                    310:     EventOrder * pres;
                    311:     if (THD_TRACE) HTTrace("EventOrder.. all ordered events\n");
2.4       eric      312:     if (cur == NULL) return NO;
2.2       eric      313:     while ((pres = (EventOrder *) HTList_nextObject(cur)))
                    314:        HT_FREE(pres);
                    315:     HTList_delete(EventOrderList);
                    316:     EventOrderList = NULL;
                    317:     return YES;
                    318: }
                    319: #endif /* HT_EVENT_ORDER */
                    320: 
                    321: /* ------------------------------------------------------------------------- */
2.1       frystyk   322: 
                    323: /*
                    324: **  For a given socket, reqister a request structure, a set of operations, 
                    325: **  a HTEventCallback function, and a priority. For this implementation, 
                    326: **  we allow only a single HTEventCallback function for all operations.
                    327: **  and the priority field is ignored.
                    328: */
                    329: PUBLIC int HTEventList_register (SOCKET s, HTEventType type, HTEvent * event)
                    330: {
2.3       eric      331:     SockEvents * sockp;
2.1       frystyk   332:     if (THD_TRACE) 
2.5       eric      333:        HTTrace("Event....... Register socket %d, request %p handler %p type %s at priority %d\n",
2.1       frystyk   334:                s, (void *) event->request,
2.5       eric      335:                (void *) event->cbf, HTEvent_type2str(type),
2.1       frystyk   336:                (unsigned) event->priority);
2.4       eric      337:     if (s==INVSOC || HTEvent_INDEX(type) >= HTEvent_TYPES)
2.1       frystyk   338:        return 0;
                    339: 
                    340:     /*
                    341:     ** Insert socket into appropriate file descriptor set. We also make sure
                    342:     ** that it is registered in the global set.
                    343:     */
2.5       eric      344:     if (THD_TRACE) HTTrace("Event....... Registering socket for %s\n", HTEvent_type2str(type));
2.3       eric      345:     sockp = SockEvents_get(s, SockEvents_mayCreate);
                    346:     sockp->s = s;
                    347:     sockp->events[HTEvent_INDEX(type)] = event;
                    348: #ifdef WWW_WIN_ASYNC
2.4       eric      349:     if (WSAAsyncSelect(s, HTSocketWin, HTwinMsg, HTEvent_BITS(type)) < 0)
2.3       eric      350:        return HT_ERROR;
                    351: #else /* WWW_WIN_ASYNC */
2.1       frystyk   352:     FD_SET(s, FdArray+HTEvent_INDEX(type));
2.3       eric      353:     if (s > MaxSock) MaxSock = s ;
                    354: #endif /* !WWW_WIN_ASYNC */
2.1       frystyk   355:     /*
                    356:     ** If the timeout has been set (relative in millis) then we register 
                    357:     ** a new timeout for this event
                    358:     */
                    359:     if (event->millis >= 0) {
2.5       eric      360: #ifdef IN_EVENT
                    361:        event->timer = HTTimer_new(NULL, EventListTimerHandler, sockp, event->millis, YES);
                    362: #else
2.3       eric      363:        sockp->timeouts[HTEvent_INDEX(type)] = HTTimer_new(NULL, EventListTimerHandler, sockp, event->millis, YES);
2.5       eric      364: #endif
2.1       frystyk   365:     }
                    366: 
                    367:     return HT_OK;
                    368: }
                    369: 
2.3       eric      370: PRIVATE int EventList_remaining(SockEvents * pres)
                    371: {
                    372:     int ret = 0;
                    373:     int i;
                    374:     for (i = 0; i < HTEvent_TYPES; i++)
                    375:        if (pres->events[i] != NULL)
                    376:            ret |= 1<<i;
                    377:     return ret;
                    378: }
                    379: 
2.1       frystyk   380: /*
                    381: ** Remove the registered information for the specified socket for the actions 
                    382: ** specified in ops. if no actions remain after the unregister, the registered
                    383: ** info is deleted, and, if the socket has been registered for notification, 
                    384: ** the HTEventCallback will be invoked.
                    385: */
                    386: PUBLIC int HTEventList_unregister(SOCKET s, HTEventType type) 
                    387: {
2.3       eric      388:     long               v = HASH(s);
                    389:     HTList *           cur = HashTable[v];
                    390:     HTList *           last = cur;
                    391:     SockEvents *       pres;
2.2       eric      392: 
2.6     ! eric      393:     MaxSock = -1;      /* re-find maximum in-use socket */
2.3       eric      394:     while ((pres = (SockEvents *) HTList_nextObject(cur))) {
2.2       eric      395:         if (pres->s == s) {
2.3       eric      396:            int         remaining;
2.1       frystyk   397: 
                    398:            /*
                    399:            **  Unregister the event from this action
                    400:            */
2.2       eric      401:            pres->events[HTEvent_INDEX(type)] = NULL;
2.1       frystyk   402: 
                    403:            /*
                    404:            **  Check to see of there was a timeout connected with the event.
                    405:            **  If so then delete the timeout as well.
                    406:            */
                    407:            {
2.5       eric      408: #ifdef IN_EVENT
                    409:                HTTimer * timer = pres->events[HTEvent_INDEX(type)]->timer;
                    410: #else
2.2       eric      411:                HTTimer * timer = pres->timeouts[HTEvent_INDEX(type)];
2.5       eric      412: #endif
2.1       frystyk   413:                if (timer) HTTimer_delete(timer);
                    414:            }
                    415:            
                    416:            /*
                    417:            **  Check to see if we can delete the action completely. We do this
                    418:            **  if there are no more events registered.
                    419:            */
2.3       eric      420:            if ((remaining = EventList_remaining(pres)) == 0) {
2.1       frystyk   421:                if (THD_TRACE)
                    422:                    HTTrace("Event....... No more events registered for socket %d\n", s);
2.2       eric      423:                HT_FREE(pres);
2.3       eric      424:                HTList_quickRemoveElement(cur, last);
2.2       eric      425:            } else
                    426:                last = cur;  /* to set next pointer when creating new */
2.3       eric      427: #ifdef WWW_WIN_ASYNC
                    428:            if (WSAAsyncSelect(s, HTSocketWin, HTwinMsg, remaining) < 0)
                    429:                return HT_ERROR;
                    430: #else /* WWW_WIN_ASYNC */
                    431:            FD_CLR(s, FdArray+HTEvent_INDEX(type));
                    432: #endif /* !WWW_WIN_ASYNC */
                    433:            if (THD_TRACE)
2.5       eric      434:                HTTrace("Event....... Socket %d unregisterd for %s\n", s, HTEvent_type2str(type));
2.6     ! eric      435:            /*
        !           436:            **  finish checking list for max socket
        !           437:            */
        !           438:            while ((pres = (SockEvents *) HTList_nextObject(cur)))
        !           439:                if(pres->s > MaxSock)
        !           440:                    MaxSock = pres->s;
        !           441:            if (MaxSock  <= 0)
        !           442:                HTTrace("Event....... MaxSock: %d.\n", MaxSock);
2.1       frystyk   443:            return HT_OK;
2.6     ! eric      444:        } else if(pres->s > MaxSock)
        !           445:            MaxSock = pres->s;
2.1       frystyk   446:     }
                    447:     if (THD_TRACE) HTTrace("Event....... Couldn't find socket %d.\n", s);
                    448:     return HT_ERROR;
                    449: }
                    450: 
                    451: /*
                    452: ** Unregister all sockets 
                    453: ** N.B. we just remove them for our internal data structures: it is up to the 
                    454: ** application to actually close the socket. 
                    455: */
2.2       eric      456: PUBLIC int HTEventList_unregisterAll (void) 
2.1       frystyk   457: {
                    458:     int i;
                    459:     if (THD_TRACE) HTTrace("Unregister.. all sockets\n");
                    460:     for (i = 0 ; i < PRIME_TABLE_SIZE; i++) {
2.2       eric      461:        HTList * cur = HashTable[i];
2.3       eric      462:        SockEvents * pres;
                    463:        while ((pres = (SockEvents *) HTList_nextObject(cur))) {
                    464: #ifdef WWW_WIN_ASYNC
                    465:            WSAAsyncSelect(pres->s, HTSocketWin, HTwinMsg, 0);
                    466: #endif /* WWW_WIN_ASYNC */
2.2       eric      467:            HT_FREE(pres);
2.3       eric      468:        }
2.2       eric      469:        HTList_delete(HashTable[i]);
                    470:        HashTable[i] = NULL;
2.1       frystyk   471:     }
2.3       eric      472: #ifndef WWW_WIN_ASYNC
                    473:     MaxSock = 0 ;
2.1       frystyk   474:     FD_ZERO(FdArray+HTEvent_INDEX(HTEvent_READ));
                    475:     FD_ZERO(FdArray+HTEvent_INDEX(HTEvent_WRITE));
                    476:     FD_ZERO(FdArray+HTEvent_INDEX(HTEvent_OOB));
2.3       eric      477: #endif /* !WWW_WIN_ASYNC */
2.2       eric      478: #ifdef HT_EVENT_ORDER
                    479:     EventOrder_deleteAll();
                    480: #endif /* HT_EVENT_ORDER */
2.1       frystyk   481:     return 0;
                    482: }
                    483: 
                    484: /*
                    485: **  Dispatch the event to the appropriate event handler.
                    486: **  If no event handler is found then just return.
                    487: */
2.5       eric      488: PUBLIC int HTEventList_dispatch (SOCKET s, HTEventType type, ms_t now)
2.1       frystyk   489: {
2.3       eric      490:     SockEvents * sockp = SockEvents_get(s, SockEvents_find);
                    491:     if (sockp) {
                    492:        HTEvent * event = sockp->events[HTEvent_INDEX(type)];
2.1       frystyk   493: 
2.5       eric      494:        /*      Fixup the timeout
                    495:        */
                    496: #ifdef IN_EVENT
                    497:        if (event->timer)
                    498:            HTTimer_refresh(event->timer, now);
                    499: #else
                    500:        if (sockp->timeouts[HTEvent_INDEX(type)])
                    501:            HTTimer_refresh(sockp->timeouts[HTEvent_INDEX(type)], now);
                    502: #endif
2.1       frystyk   503:        /*
                    504:        **  If we have found an event object for this event then see
                    505:        **  is we should call it.
                    506:        */
                    507:        if (event && event->priority!=HT_PRIORITY_OFF)
                    508:            return (*event->cbf) (s, event->param, type);
2.3       eric      509:        if (THD_TRACE) HTTrace("Dispatch.... Handler %p NOT called\n", sockp);
2.1       frystyk   510:        return HT_OK;
                    511:     }
                    512:     if (THD_TRACE) HTTrace("Dispatch.... Bad socket %d\n", s);
                    513:     return NO;
                    514: }
                    515: 
                    516: /*
                    517: **  Stops the (select based) event loop. The function does not guarantee
                    518: **  that all requests have terminated. This is for the app to do
                    519: */
                    520: PUBLIC void HTEventList_stopLoop (void)
                    521: {
                    522:     HTEndLoop = 1;
                    523: }
                    524: 
2.3       eric      525: PUBLIC HTEvent * HTEventList_lookup (SOCKET s, HTEventType type)
                    526: {
                    527:     SockEvents * sockp = NULL;
                    528:     if ((sockp = SockEvents_get(s, SockEvents_find)) == NULL)
                    529:        return NULL;
                    530:     return sockp->events[HTEvent_INDEX(type)];
                    531: }
                    532: 
                    533: /*     REGISTER DEFULT EVENT MANAGER
                    534: **     -----------------------------
                    535: **     Not done automaticly - may be done by application!
                    536: */
                    537: PUBLIC BOOL HTEventInit (void)
                    538: {
2.4       eric      539: #ifdef WWW_WIN_ASYNC
                    540:     /*
                    541:     ** We are here starting a hidden window to take care of events from
                    542:     **  the async select() call in the async version of the event loop in
                    543:     ** the Internal event manager (HTEvtLst.c)
                    544:     */
                    545:     static char className[] = "AsyncWindowClass";
                    546:     WNDCLASS wc;
                    547:     OSVERSIONINFO osInfo;
                    548:     
                    549:     wc.style=0;
                    550:     wc.lpfnWndProc=(WNDPROC)AsyncWindowProc;
                    551:     wc.cbClsExtra=0;
                    552:     wc.cbWndExtra=0;
                    553:     wc.hIcon=0;
                    554:     wc.hCursor=0;
                    555:     wc.hbrBackground=0;
                    556:     wc.lpszMenuName=(LPSTR)0;
                    557:     wc.lpszClassName=className;
                    558: 
                    559:     osInfo.dwOSVersionInfoSize = sizeof(osInfo);
                    560:     GetVersionEx(&osInfo);
                    561:     if (osInfo.dwPlatformId == VER_PLATFORM_WIN32s || osInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
                    562:        wc.hInstance=GetModuleHandle(NULL); /* 95 and non threaded platforms */
                    563:     else
                    564:        wc.hInstance=GetCurrentProcess(); /* NT and hopefully everything following */
                    565:     if (!RegisterClass(&wc)) {
                    566:        HTTrace("HTLibInit.. Can't RegisterClass \"%s\"\n", className);
                    567:            return NO;
                    568:     }
                    569:     if (!(HTSocketWin = CreateWindow(className, "WWW_WIN_ASYNC", WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT, 
                    570:                                      CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, wc.hInstance,0))) {
                    571:        char space[50];
                    572:                HTTrace("HTLibInit.. Can't CreateWindow \"WWW_WIN_ASYNC\" - error:");
                    573:        sprintf(space, "%ld\n", GetLastError());
                    574:        HTTrace(space);
                    575:        return NO;
                    576:     }
                    577:     HTwinMsg = WM_USER;  /* use first available message since app uses none */
                    578: #endif /* WWW_WIN_ASYNC */
                    579: 
                    580: #ifdef _WINSOCKAPI_
                    581:     /*
                    582:     ** Initialise WinSock DLL. This must also be shut down! PMH
                    583:     */
                    584:     {
                    585:         WSADATA            wsadata;
                    586:        if (WSAStartup(DESIRED_WINSOCK_VERSION, &wsadata)) {
                    587:            if (WWWTRACE)
                    588:                HTTrace("HTEventInit. Can't initialize WinSoc\n");
                    589:             WSACleanup();
                    590:             return NO;
                    591:         }
                    592:         if (wsadata.wVersion < MINIMUM_WINSOCK_VERSION) {
                    593:             if (WWWTRACE)
                    594:                HTTrace("HTEventInit. Bad version of WinSoc\n");
                    595:             WSACleanup();
                    596:             return NO;
                    597:         }
                    598:        if (APP_TRACE)
                    599:            HTTrace("HTEventInit. Using WinSoc version \"%s\".\n", 
                    600:                    wsadata.szDescription);
                    601:     }
                    602: #endif /* _WINSOCKAPI_ */
                    603: 
2.3       eric      604:     HTEvent_setRegisterCallback(HTEventList_register);
                    605:     HTEvent_setUnregisterCallback(HTEventList_unregister);
                    606:     return YES;
                    607: }
                    608: 
                    609: PUBLIC BOOL HTEventTerminate (void)
                    610: {
                    611:     return YES;
                    612: }
                    613: 
                    614: #ifdef WWW_WIN_ASYNC
                    615: 
2.4       eric      616: /*     HTEventList_get/setWinHandle
2.3       eric      617: **     --------------------------
                    618: **     Managing the windows handle on Windows
                    619: */
                    620: PUBLIC BOOL HTEventList_setWinHandle (HWND window, unsigned long message)
                    621: {
                    622:     HTSocketWin = window;
                    623:     HTwinMsg = message;
                    624:     return YES;
                    625: }
                    626: 
                    627: PUBLIC HWND HTEventList_getWinHandle (unsigned long * pMessage)
                    628: {
                    629:     if (pMessage)
                    630:         *pMessage = HTwinMsg;
                    631:     return (HTSocketWin);
                    632: }
                    633: 
                    634: /* only responsible for WM_TIMER and WSA_AsyncSelect */        
                    635: PUBLIC LRESULT CALLBACK AsyncWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
                    636: {
                    637:     WORD event;
                    638:     SOCKET sock;
2.4       eric      639:     HTEventType type;
2.5       eric      640:     ms_t now = HTGetTimeInMillis();
2.3       eric      641: 
                    642:     /* timeout stuff */
                    643:     if (uMsg == WM_TIMER) {
2.4       eric      644:        HTTimer_dispatch((HTTimer *)wParam);
2.3       eric      645:        return (0);
                    646:     }
                    647: 
                    648:     if (uMsg != HTwinMsg)      /* not our async message */
                    649:        return (DefWindowProc(hwnd, uMsg, wParam, lParam));
                    650: 
                    651:     event = LOWORD(lParam);
                    652:     sock = (SOCKET)wParam;
2.4       eric      653:     switch (event) {
                    654:     case FD_READ: type = HTEvent_READ; break;
                    655:     case FD_WRITE: type = HTEvent_WRITE; break;
                    656:     case FD_ACCEPT: type = HTEvent_ACCEPT; break;
                    657:     case FD_CONNECT: type = HTEvent_CONNECT; break;
                    658:     case FD_OOB: type = HTEvent_OOB; break;
                    659:     case FD_CLOSE: type = HTEvent_CLOSE; break;
                    660:     default: HTDebugBreak();
                    661:     }
2.5       eric      662:     if (HTEventList_dispatch((int)sock, type, now) != HT_OK)
2.4       eric      663:        HTEndLoop = -1;
2.3       eric      664:     return (0);
                    665: }
                    666: 
2.4       eric      667: PUBLIC int HTEventList_loop (HTRequest * theRequest )
2.3       eric      668: {
                    669:     MSG msg;
                    670:     while (GetMessage(&msg,0,0,0)) {
                    671:            TranslateMessage(&msg);
                    672:            DispatchMessage(&msg);
                    673:     }
                    674:     return (HTEndLoop == 1 ? HT_OK : HT_ERROR);
                    675: }
                    676: 
                    677: #else /* WWW_WIN_ASYNC */
                    678: 
2.1       frystyk   679: /*
                    680: **  We wait for activity from one of our registered 
                    681: **  channels, and dispatch on that.
                    682: **
                    683: **  There are now two versions of the event loop. The first is if you want
                    684: **  to use async I/O on windows, and the other is if you want to use normal
                    685: **  Unix setup with sockets
                    686: */
                    687: PUBLIC int HTEventList_loop (HTRequest * theRequest) 
                    688: {
                    689:     fd_set treadset, twriteset, texceptset;
                    690:     struct timeval waittime, * wt;
                    691:     int active_sockets;
                    692:     int maxfds;
2.5       eric      693:     ms_t timeout;
                    694:     ms_t now;
2.1       frystyk   695:     SOCKET s;
2.5       eric      696:     int status = HT_OK;
2.1       frystyk   697:     HTEndLoop = 0;
                    698: 
2.2       eric      699:     EventOrderList = HTList_new();     /* is kept around until EventOrder_deleteAll */
                    700: 
2.1       frystyk   701:     /* Don't leave this loop until we leave the application */
                    702:     do {
                    703:         treadset = FdArray[HTEvent_INDEX(HTEvent_READ)];
                    704:         twriteset = FdArray[HTEvent_INDEX(HTEvent_WRITE)];
                    705:         texceptset = FdArray[HTEvent_INDEX(HTEvent_OOB)];
2.3       eric      706:         maxfds = MaxSock; 
2.1       frystyk   707: 
                    708:        if (THD_TRACE) HTTrace("Event Loop.. calling select: maxfds is %d\n", maxfds);
                    709: 
                    710:         /*
                    711:        **  Timeval struct copy needed for linux, as it set the value to the
                    712:        **  remaining timeout while exiting the select. (and perhaps for
                    713:        **  other OS). Code borrowed from X server.
                    714:        */
                    715:        wt = NULL;
2.5       eric      716:        if ((status = HTTimer_next(&timeout)))
                    717:            return status;
                    718:        if (timeout != 0) {
2.1       frystyk   719:            waittime.tv_sec = timeout / MILLI_PER_SECOND;
                    720:            waittime.tv_usec = (timeout % MILLI_PER_SECOND) *
                    721:                (1000000 / MILLI_PER_SECOND);
                    722:            wt = &waittime;
                    723:        }
                    724: 
2.2       eric      725:        HTTraceData((char*)&treadset, maxfds/8 + 1, "HTEventList_loop pre treadset: (maxfd:%d)", maxfds);
                    726:        HTTraceData((char*)&twriteset, maxfds/8 + 1, "HTEventList_loop pre twriteset:");
                    727:        HTTraceData((char*)&texceptset, maxfds/8 + 1, "HTEventList_loop pre texceptset:");
                    728: 
2.1       frystyk   729: #ifdef __hpux 
                    730:         active_sockets = select(maxfds+1, (int *)&treadset, (int *)&twriteset,
                    731:                                (int *)&texceptset, wt);
                    732: #else
                    733:         active_sockets = select(maxfds+1, &treadset, &twriteset, &texceptset, wt);
                    734: #endif
2.2       eric      735: 
2.5       eric      736:        now = HTGetTimeInMillis();
2.2       eric      737:        HTTraceData((char*)&treadset, maxfds/8 + 1, "HTEventList_loop post treadset: (active_sockets:%d)", active_sockets);
                    738:        HTTraceData((char*)&twriteset, maxfds/8 + 1, "HTEventList_loop post twriteset: (errno:%d)", errno);
                    739:        HTTraceData((char*)&texceptset, maxfds/8 + 1, "HTEventList_loop post texceptset:");
                    740: 
2.1       frystyk   741:        if (THD_TRACE) HTTrace("Event Loop.. select returns %d\n", active_sockets);
                    742: 
                    743:         if (active_sockets == -1) {
                    744:            HTRequest_addSystemError( theRequest, ERR_FATAL, socerrno, NO, "select");
2.3       eric      745:            EventList_dump();
                    746: #if 0
2.1       frystyk   747:            __DumpFDSet(FdArray+HTEvent_INDEX(HTEvent_READ), "Read");
                    748:            __DumpFDSet(FdArray+HTEvent_INDEX(HTEvent_WRITE), "Write") ;
                    749:            __DumpFDSet(FdArray+HTEvent_INDEX(HTEvent_OOB), "Exceptions");
2.3       eric      750: #endif /* 0 */
2.1       frystyk   751:            return HT_ERROR;
                    752:         }
                    753: 
                    754:        /*
                    755:        **  We had a timeout so now we check and see if we have a timeout
2.5       eric      756:        **  handler to call. Let HTTimer_next get it.
2.1       frystyk   757:        */ 
2.5       eric      758:        if (active_sockets == 0)
2.1       frystyk   759:            continue;
                    760: 
                    761:        /*
                    762:        **  There were active sockets. Determine which fd sets they were in
                    763:        */
2.2       eric      764: #ifdef HT_EVENT_ORDER
2.5       eric      765: #define DISPATCH(socket, type, now) EventOrder_add(socket, type, now)
2.2       eric      766: #else /* HT_EVENT_ORDER */
2.5       eric      767: #define DISPATCH(socket, type, now) HTEventList_dispatch(socket, type, now)
2.2       eric      768: #endif /* !HT_EVENT_ORDER */
2.1       frystyk   769:        for (s = 0 ; s <= maxfds ; s++) { 
                    770:            if (FD_ISSET(s, &texceptset))
2.5       eric      771:                if ((status = DISPATCH(s, HTEvent_OOB, now)) != HT_OK)
2.1       frystyk   772:                    return status;
                    773:            if (FD_ISSET(s, &twriteset))
2.5       eric      774:                if ((status = DISPATCH(s, HTEvent_WRITE, now)) != HT_OK)
2.1       frystyk   775:                    return status;
                    776:            if (FD_ISSET(s, &treadset))
2.5       eric      777:                if ((status = DISPATCH(s, HTEvent_READ, now)) != HT_OK)
2.1       frystyk   778:                    return status;
                    779:        }
2.2       eric      780: #ifdef HT_EVENT_ORDER
                    781:        if ((status = EventOrder_executeAndDelete()) != HT_OK)
                    782:            return status;
                    783: #endif /* HT_EVENT_ORDER */
2.1       frystyk   784:     } while (!HTEndLoop);
                    785: 
                    786:     return HT_OK;
                    787: }
                    788: 
2.3       eric      789: #endif /* !WWW_WIN_ASYNC */

Webmaster