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