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