Annotation of libwww/Library/src/HTEvtLst.c, revision 2.18
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.18 ! frystyk 6: ** @(#) $Id: HTEvtLst.c,v 2.17 1997/02/04 16:40:53 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.18 ! frystyk 157: int v = 0;
2.3 eric 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:
2.18 ! frystyk 468: #ifdef WWW_WIN_ASYNC
! 469: if (WSAAsyncSelect(s, HTSocketWin, HTwinMsg, remaining) < 0)
! 470: ret = HT_ERROR;
! 471: #else /* WWW_WIN_ASYNC */
! 472: FD_CLR(s, FdArray+HTEvent_INDEX(type));
! 473: HTTraceData((char*)FdArray+HTEvent_INDEX(type), 8, "HTEventList_unregister: (s:%d)", s);
! 474: #endif /* !WWW_WIN_ASYNC */
! 475:
2.1 frystyk 476: /*
477: ** Check to see if we can delete the action completely. We do this
478: ** if there are no more events registered.
479: */
2.3 eric 480: if ((remaining = EventList_remaining(pres)) == 0) {
2.9 eric 481: HTList * doomed = cur;
2.1 frystyk 482: if (THD_TRACE)
483: HTTrace("Event....... No more events registered for socket %d\n", s);
2.15 frystyk 484:
485:
486: #ifndef WWW_WIN_ASYNC
487: /* Check to see if we have to update MaxSock */
2.18 ! frystyk 488: if(pres->s >= MaxSock) __ResetMaxSock();
2.15 frystyk 489: #endif /* !WWW_WIN_ASYNC */
490:
2.2 eric 491: HT_FREE(pres);
2.9 eric 492: pres = (SockEvents *) HTList_nextObject(cur);
493: HTList_quickRemoveElement(doomed, last);
494: }
495: ret = HT_OK;
2.15 frystyk 496:
2.18 ! frystyk 497: if (THD_TRACE) HTTrace("Event....... Socket %d unregistered for %s\n", s,
! 498: HTEvent_type2str(type));
2.15 frystyk 499:
2.6 eric 500: /*
2.15 frystyk 501: ** We found the socket and can break
2.6 eric 502: */
2.15 frystyk 503: break;
2.9 eric 504: }
505: last = cur;
2.1 frystyk 506: }
2.9 eric 507: if (ret == HT_ERROR && THD_TRACE) HTTrace("Event....... Couldn't find socket %d.\n", s);
508: return ret;
2.1 frystyk 509: }
510:
511: /*
512: ** Unregister all sockets
513: ** N.B. we just remove them for our internal data structures: it is up to the
514: ** application to actually close the socket.
515: */
2.2 eric 516: PUBLIC int HTEventList_unregisterAll (void)
2.1 frystyk 517: {
518: int i;
519: if (THD_TRACE) HTTrace("Unregister.. all sockets\n");
520: for (i = 0 ; i < PRIME_TABLE_SIZE; i++) {
2.2 eric 521: HTList * cur = HashTable[i];
2.3 eric 522: SockEvents * pres;
523: while ((pres = (SockEvents *) HTList_nextObject(cur))) {
524: #ifdef WWW_WIN_ASYNC
525: WSAAsyncSelect(pres->s, HTSocketWin, HTwinMsg, 0);
526: #endif /* WWW_WIN_ASYNC */
2.2 eric 527: HT_FREE(pres);
2.3 eric 528: }
2.2 eric 529: HTList_delete(HashTable[i]);
530: HashTable[i] = NULL;
2.1 frystyk 531: }
2.3 eric 532: #ifndef WWW_WIN_ASYNC
533: MaxSock = 0 ;
2.15 frystyk 534: if (THD_TRACE) HTTrace("Event....... New value for MaxSock is %d\n", MaxSock);
2.1 frystyk 535: FD_ZERO(FdArray+HTEvent_INDEX(HTEvent_READ));
536: FD_ZERO(FdArray+HTEvent_INDEX(HTEvent_WRITE));
537: FD_ZERO(FdArray+HTEvent_INDEX(HTEvent_OOB));
2.3 eric 538: #endif /* !WWW_WIN_ASYNC */
2.2 eric 539: #ifdef HT_EVENT_ORDER
540: EventOrder_deleteAll();
541: #endif /* HT_EVENT_ORDER */
2.1 frystyk 542: return 0;
543: }
544:
545: /*
546: ** Dispatch the event to the appropriate event handler.
547: ** If no event handler is found then just return.
548: */
2.5 eric 549: PUBLIC int HTEventList_dispatch (SOCKET s, HTEventType type, ms_t now)
2.1 frystyk 550: {
2.3 eric 551: SockEvents * sockp = SockEvents_get(s, SockEvents_find);
552: if (sockp) {
553: HTEvent * event = sockp->events[HTEvent_INDEX(type)];
2.1 frystyk 554:
2.5 eric 555: /* Fixup the timeout
556: */
557: #ifdef IN_EVENT
558: if (event->timer)
559: HTTimer_refresh(event->timer, now);
560: #else
561: if (sockp->timeouts[HTEvent_INDEX(type)])
562: HTTimer_refresh(sockp->timeouts[HTEvent_INDEX(type)], now);
563: #endif
2.1 frystyk 564: /*
565: ** If we have found an event object for this event then see
566: ** is we should call it.
567: */
568: if (event && event->priority!=HT_PRIORITY_OFF)
569: return (*event->cbf) (s, event->param, type);
2.3 eric 570: if (THD_TRACE) HTTrace("Dispatch.... Handler %p NOT called\n", sockp);
2.1 frystyk 571: return HT_OK;
572: }
573: if (THD_TRACE) HTTrace("Dispatch.... Bad socket %d\n", s);
574: return NO;
575: }
576:
577: /*
578: ** Stops the (select based) event loop. The function does not guarantee
579: ** that all requests have terminated. This is for the app to do
580: */
581: PUBLIC void HTEventList_stopLoop (void)
582: {
583: HTEndLoop = 1;
584: }
585:
2.3 eric 586: PUBLIC HTEvent * HTEventList_lookup (SOCKET s, HTEventType type)
587: {
588: SockEvents * sockp = NULL;
589: if ((sockp = SockEvents_get(s, SockEvents_find)) == NULL)
590: return NULL;
591: return sockp->events[HTEvent_INDEX(type)];
592: }
593:
594: /* REGISTER DEFULT EVENT MANAGER
595: ** -----------------------------
596: ** Not done automaticly - may be done by application!
597: */
598: PUBLIC BOOL HTEventInit (void)
599: {
2.4 eric 600: #ifdef WWW_WIN_ASYNC
601: /*
602: ** We are here starting a hidden window to take care of events from
603: ** the async select() call in the async version of the event loop in
604: ** the Internal event manager (HTEvtLst.c)
605: */
606: static char className[] = "AsyncWindowClass";
607: WNDCLASS wc;
608: OSVERSIONINFO osInfo;
609:
610: wc.style=0;
611: wc.lpfnWndProc=(WNDPROC)AsyncWindowProc;
612: wc.cbClsExtra=0;
613: wc.cbWndExtra=0;
614: wc.hIcon=0;
615: wc.hCursor=0;
616: wc.hbrBackground=0;
617: wc.lpszMenuName=(LPSTR)0;
618: wc.lpszClassName=className;
619:
620: osInfo.dwOSVersionInfoSize = sizeof(osInfo);
621: GetVersionEx(&osInfo);
622: if (osInfo.dwPlatformId == VER_PLATFORM_WIN32s || osInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
623: wc.hInstance=GetModuleHandle(NULL); /* 95 and non threaded platforms */
624: else
625: wc.hInstance=GetCurrentProcess(); /* NT and hopefully everything following */
626: if (!RegisterClass(&wc)) {
627: HTTrace("HTLibInit.. Can't RegisterClass \"%s\"\n", className);
628: return NO;
629: }
630: if (!(HTSocketWin = CreateWindow(className, "WWW_WIN_ASYNC", WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT,
631: CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, wc.hInstance,0))) {
632: char space[50];
633: HTTrace("HTLibInit.. Can't CreateWindow \"WWW_WIN_ASYNC\" - error:");
634: sprintf(space, "%ld\n", GetLastError());
635: HTTrace(space);
636: return NO;
637: }
638: HTwinMsg = WM_USER; /* use first available message since app uses none */
2.8 frystyk 639:
640: /*
641: ** Register platform specific timer handlers for windows
642: */
643: HTTimer_registerSetTimerCallback(Timer_setWindowsTimer);
644: HTTimer_registerDeleteTimerCallback(Timer_deleteWindowsTimer);
645:
2.4 eric 646: #endif /* WWW_WIN_ASYNC */
647:
648: #ifdef _WINSOCKAPI_
649: /*
650: ** Initialise WinSock DLL. This must also be shut down! PMH
651: */
652: {
653: WSADATA wsadata;
654: if (WSAStartup(DESIRED_WINSOCK_VERSION, &wsadata)) {
655: if (WWWTRACE)
656: HTTrace("HTEventInit. Can't initialize WinSoc\n");
657: WSACleanup();
658: return NO;
659: }
660: if (wsadata.wVersion < MINIMUM_WINSOCK_VERSION) {
661: if (WWWTRACE)
662: HTTrace("HTEventInit. Bad version of WinSoc\n");
663: WSACleanup();
664: return NO;
665: }
666: if (APP_TRACE)
667: HTTrace("HTEventInit. Using WinSoc version \"%s\".\n",
668: wsadata.szDescription);
669: }
670: #endif /* _WINSOCKAPI_ */
671:
2.3 eric 672: HTEvent_setRegisterCallback(HTEventList_register);
673: HTEvent_setUnregisterCallback(HTEventList_unregister);
674: return YES;
675: }
676:
677: PUBLIC BOOL HTEventTerminate (void)
678: {
679: return YES;
680: }
681:
682: #ifdef WWW_WIN_ASYNC
683:
2.4 eric 684: /* HTEventList_get/setWinHandle
2.3 eric 685: ** --------------------------
686: ** Managing the windows handle on Windows
687: */
688: PUBLIC BOOL HTEventList_setWinHandle (HWND window, unsigned long message)
689: {
690: HTSocketWin = window;
691: HTwinMsg = message;
692: return YES;
693: }
694:
695: PUBLIC HWND HTEventList_getWinHandle (unsigned long * pMessage)
696: {
697: if (pMessage)
698: *pMessage = HTwinMsg;
699: return (HTSocketWin);
700: }
701:
702: /* only responsible for WM_TIMER and WSA_AsyncSelect */
703: PUBLIC LRESULT CALLBACK AsyncWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
704: {
705: WORD event;
706: SOCKET sock;
2.4 eric 707: HTEventType type;
2.5 eric 708: ms_t now = HTGetTimeInMillis();
2.3 eric 709:
710: /* timeout stuff */
711: if (uMsg == WM_TIMER) {
2.4 eric 712: HTTimer_dispatch((HTTimer *)wParam);
2.3 eric 713: return (0);
714: }
715:
716: if (uMsg != HTwinMsg) /* not our async message */
717: return (DefWindowProc(hwnd, uMsg, wParam, lParam));
718:
719: event = LOWORD(lParam);
720: sock = (SOCKET)wParam;
2.4 eric 721: switch (event) {
722: case FD_READ: type = HTEvent_READ; break;
723: case FD_WRITE: type = HTEvent_WRITE; break;
724: case FD_ACCEPT: type = HTEvent_ACCEPT; break;
725: case FD_CONNECT: type = HTEvent_CONNECT; break;
726: case FD_OOB: type = HTEvent_OOB; break;
727: case FD_CLOSE: type = HTEvent_CLOSE; break;
728: default: HTDebugBreak();
729: }
2.5 eric 730: if (HTEventList_dispatch((int)sock, type, now) != HT_OK)
2.4 eric 731: HTEndLoop = -1;
2.3 eric 732: return (0);
733: }
734:
2.4 eric 735: PUBLIC int HTEventList_loop (HTRequest * theRequest )
2.3 eric 736: {
737: MSG msg;
738: while (GetMessage(&msg,0,0,0)) {
739: TranslateMessage(&msg);
740: DispatchMessage(&msg);
741: }
742: return (HTEndLoop == 1 ? HT_OK : HT_ERROR);
743: }
744:
745: #else /* WWW_WIN_ASYNC */
746:
2.1 frystyk 747: /*
748: ** We wait for activity from one of our registered
749: ** channels, and dispatch on that.
750: **
751: ** There are now two versions of the event loop. The first is if you want
752: ** to use async I/O on windows, and the other is if you want to use normal
753: ** Unix setup with sockets
754: */
755: PUBLIC int HTEventList_loop (HTRequest * theRequest)
756: {
757: fd_set treadset, twriteset, texceptset;
758: struct timeval waittime, * wt;
759: int active_sockets;
760: int maxfds;
2.5 eric 761: ms_t timeout;
762: ms_t now;
2.1 frystyk 763: SOCKET s;
2.5 eric 764: int status = HT_OK;
2.1 frystyk 765: HTEndLoop = 0;
766:
2.2 eric 767: EventOrderList = HTList_new(); /* is kept around until EventOrder_deleteAll */
768:
2.1 frystyk 769: /* Don't leave this loop until we leave the application */
770: do {
771: treadset = FdArray[HTEvent_INDEX(HTEvent_READ)];
772: twriteset = FdArray[HTEvent_INDEX(HTEvent_WRITE)];
773: texceptset = FdArray[HTEvent_INDEX(HTEvent_OOB)];
774:
775: /*
776: ** Timeval struct copy needed for linux, as it set the value to the
777: ** remaining timeout while exiting the select. (and perhaps for
778: ** other OS). Code borrowed from X server.
779: */
780: wt = NULL;
2.5 eric 781: if ((status = HTTimer_next(&timeout)))
782: return status;
783: if (timeout != 0) {
2.1 frystyk 784: waittime.tv_sec = timeout / MILLI_PER_SECOND;
785: waittime.tv_usec = (timeout % MILLI_PER_SECOND) *
786: (1000000 / MILLI_PER_SECOND);
787: wt = &waittime;
788: }
789:
2.18 ! frystyk 790: maxfds = MaxSock;
! 791: if (THD_TRACE) HTTrace("Event Loop.. calling select: maxfds is %d\n", maxfds);
! 792:
! 793: #ifdef EVENT_TRACE
2.15 frystyk 794: #define HT_FS_BYTES(a) ((((a)/16)+1) * 4)
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:");
2.18 ! frystyk 798: #endif /* EVENT_TRACE */
2.15 frystyk 799:
2.1 frystyk 800: #ifdef __hpux
801: active_sockets = select(maxfds+1, (int *)&treadset, (int *)&twriteset,
802: (int *)&texceptset, wt);
803: #else
804: active_sockets = select(maxfds+1, &treadset, &twriteset, &texceptset, wt);
805: #endif
2.2 eric 806:
2.5 eric 807: now = HTGetTimeInMillis();
2.18 ! frystyk 808:
! 809: #ifdef EVENT_TRACE
2.15 frystyk 810: HTTraceData((char*)&treadset, HT_FS_BYTES(maxfds), "HTEventList_loop post treadset: (active_sockets:%d)", active_sockets);
811: HTTraceData((char*)&twriteset, HT_FS_BYTES(maxfds), "HTEventList_loop post twriteset: (errno:%d)", errno);
812: HTTraceData((char*)&texceptset, HT_FS_BYTES(maxfds), "HTEventList_loop post texceptset:");
2.18 ! frystyk 813: #endif /* EVENT_TRACE */
2.2 eric 814:
2.1 frystyk 815: if (THD_TRACE) HTTrace("Event Loop.. select returns %d\n", active_sockets);
816:
817: if (active_sockets == -1) {
818: HTRequest_addSystemError( theRequest, ERR_FATAL, socerrno, NO, "select");
2.3 eric 819: EventList_dump();
2.1 frystyk 820: return HT_ERROR;
821: }
822:
823: /*
824: ** We had a timeout so now we check and see if we have a timeout
2.5 eric 825: ** handler to call. Let HTTimer_next get it.
2.1 frystyk 826: */
2.5 eric 827: if (active_sockets == 0)
2.1 frystyk 828: continue;
829:
830: /*
831: ** There were active sockets. Determine which fd sets they were in
832: */
2.2 eric 833: #ifdef HT_EVENT_ORDER
2.5 eric 834: #define DISPATCH(socket, type, now) EventOrder_add(socket, type, now)
2.2 eric 835: #else /* HT_EVENT_ORDER */
2.5 eric 836: #define DISPATCH(socket, type, now) HTEventList_dispatch(socket, type, now)
2.2 eric 837: #endif /* !HT_EVENT_ORDER */
2.1 frystyk 838: for (s = 0 ; s <= maxfds ; s++) {
839: if (FD_ISSET(s, &texceptset))
2.5 eric 840: if ((status = DISPATCH(s, HTEvent_OOB, now)) != HT_OK)
2.1 frystyk 841: return status;
842: if (FD_ISSET(s, &twriteset))
2.5 eric 843: if ((status = DISPATCH(s, HTEvent_WRITE, now)) != HT_OK)
2.1 frystyk 844: return status;
845: if (FD_ISSET(s, &treadset))
2.5 eric 846: if ((status = DISPATCH(s, HTEvent_READ, now)) != HT_OK)
2.1 frystyk 847: return status;
848: }
2.2 eric 849: #ifdef HT_EVENT_ORDER
850: if ((status = EventOrder_executeAndDelete()) != HT_OK)
851: return status;
852: #endif /* HT_EVENT_ORDER */
2.1 frystyk 853: } while (!HTEndLoop);
854:
855: return HT_OK;
856: }
857:
2.3 eric 858: #endif /* !WWW_WIN_ASYNC */
2.12 eric 859:
860: PUBLIC void CheckSockEvent(HTTimer * timer, HTTimerCallback * cbf, void * param)
861: {
862: SockEvents * sockp = (SockEvents *)param;
863: if (cbf == EventListTimerHandler &&
864: sockp->timeouts[0] != timer &&
865: sockp->timeouts[1] != timer &&
866: sockp->timeouts[2] != timer) {
2.13 frystyk 867: if (THD_TRACE) HTTrace("Timer....... bad timer %p.\n", timer);
2.12 eric 868: HTDebugBreak();
869: }
870: }
871:
Webmaster