Annotation of libwww/Library/src/HTEvtLst.c, revision 2.23
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.23 ! frystyk 6: ** @(#) $Id: HTEvtLst.c,v 2.22 1998/05/19 16:49:23 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 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:
2.19 frystyk 368: PRIVATE int EventList_remaining(SockEvents * pres)
369: {
370: int ret = 0;
371: int i;
372: for (i = 0; i < HTEvent_TYPES; i++)
373: if (pres->events[i] != NULL)
374: ret |= 1<<i;
375: return ret;
376: }
377:
2.15 frystyk 378: /*
2.1 frystyk 379: ** For a given socket, reqister a request structure, a set of operations,
380: ** a HTEventCallback function, and a priority. For this implementation,
381: ** we allow only a single HTEventCallback function for all operations.
382: ** and the priority field is ignored.
383: */
384: PUBLIC int HTEventList_register (SOCKET s, HTEventType type, HTEvent * event)
385: {
2.19 frystyk 386: int newset = 0;
2.3 eric 387: SockEvents * sockp;
2.1 frystyk 388: if (THD_TRACE)
2.5 eric 389: HTTrace("Event....... Register socket %d, request %p handler %p type %s at priority %d\n",
2.1 frystyk 390: s, (void *) event->request,
2.5 eric 391: (void *) event->cbf, HTEvent_type2str(type),
2.1 frystyk 392: (unsigned) event->priority);
2.4 eric 393: if (s==INVSOC || HTEvent_INDEX(type) >= HTEvent_TYPES)
2.1 frystyk 394: return 0;
395:
396: /*
397: ** Insert socket into appropriate file descriptor set. We also make sure
398: ** that it is registered in the global set.
399: */
2.5 eric 400: if (THD_TRACE) HTTrace("Event....... Registering socket for %s\n", HTEvent_type2str(type));
2.3 eric 401: sockp = SockEvents_get(s, SockEvents_mayCreate);
402: sockp->s = s;
403: sockp->events[HTEvent_INDEX(type)] = event;
2.19 frystyk 404: newset = EventList_remaining(sockp);
2.3 eric 405: #ifdef WWW_WIN_ASYNC
2.19 frystyk 406: if (WSAAsyncSelect(s, HTSocketWin, HTwinMsg, HTEvent_BITS(newset)) < 0) {
407: if (THD_TRACE) HTTrace("Event....... WSAAsyncSelect returned error!");
2.3 eric 408: return HT_ERROR;
2.19 frystyk 409: }
2.3 eric 410: #else /* WWW_WIN_ASYNC */
2.1 frystyk 411: FD_SET(s, FdArray+HTEvent_INDEX(type));
2.15 frystyk 412: if (s > MaxSock) {
413: MaxSock = s ;
414: if (THD_TRACE) HTTrace("Event....... New value for MaxSock is %d\n", MaxSock);
415: }
2.3 eric 416: #endif /* !WWW_WIN_ASYNC */
2.11 frystyk 417:
2.1 frystyk 418: /*
2.11 frystyk 419: ** If the timeout has been set (relative in millis) then we register
420: ** a new timeout for this event unless we already have a timer.
2.1 frystyk 421: */
422: if (event->millis >= 0) {
2.5 eric 423: #ifdef IN_EVENT
2.11 frystyk 424: event->timer = HTTimer_new(event->timer, EventListTimerHandler,
425: sockp, event->millis, YES);
2.5 eric 426: #else
2.11 frystyk 427: sockp->timeouts[HTEvent_INDEX(type)] =
428: HTTimer_new(sockp->timeouts[HTEvent_INDEX(type)],
2.22 frystyk 429: EventListTimerHandler, sockp, event->millis, YES, YES);
2.5 eric 430: #endif
2.1 frystyk 431: }
432:
433: return HT_OK;
434: }
435:
436: /*
437: ** Remove the registered information for the specified socket for the actions
438: ** specified in ops. if no actions remain after the unregister, the registered
439: ** info is deleted, and, if the socket has been registered for notification,
440: ** the HTEventCallback will be invoked.
441: */
442: PUBLIC int HTEventList_unregister(SOCKET s, HTEventType type)
443: {
2.3 eric 444: long v = HASH(s);
445: HTList * cur = HashTable[v];
446: HTList * last = cur;
447: SockEvents * pres;
2.9 eric 448: int ret = HT_ERROR;
2.2 eric 449:
2.9 eric 450: while (cur && (pres = (SockEvents *) HTList_nextObject(cur))) {
2.2 eric 451: if (pres->s == s) {
2.19 frystyk 452: int remaining = 0;
2.1 frystyk 453:
454: /*
455: ** Unregister the event from this action
456: */
2.2 eric 457: pres->events[HTEvent_INDEX(type)] = NULL;
2.19 frystyk 458: remaining = EventList_remaining(pres);
2.1 frystyk 459:
460: /*
461: ** Check to see of there was a timeout connected with the event.
462: ** If so then delete the timeout as well.
463: */
464: {
2.5 eric 465: #ifdef IN_EVENT
466: HTTimer * timer = pres->events[HTEvent_INDEX(type)]->timer;
2.19 frystyk 467: if (timer) HTTimer_delete(timer);
468: pres->events[HTEvent_INDEX(type)]->timer = NULL;
2.5 eric 469: #else
2.2 eric 470: HTTimer * timer = pres->timeouts[HTEvent_INDEX(type)];
2.19 frystyk 471: if (timer) HTTimer_delete(timer);
472: pres->timeouts[HTEvent_INDEX(type)] = NULL;
2.5 eric 473: #endif
2.19 frystyk 474:
2.1 frystyk 475: }
476:
2.18 frystyk 477: #ifdef WWW_WIN_ASYNC
478: if (WSAAsyncSelect(s, HTSocketWin, HTwinMsg, remaining) < 0)
479: ret = HT_ERROR;
480: #else /* WWW_WIN_ASYNC */
481: FD_CLR(s, FdArray+HTEvent_INDEX(type));
482: HTTraceData((char*)FdArray+HTEvent_INDEX(type), 8, "HTEventList_unregister: (s:%d)", s);
483: #endif /* !WWW_WIN_ASYNC */
484:
2.1 frystyk 485: /*
486: ** Check to see if we can delete the action completely. We do this
487: ** if there are no more events registered.
488: */
2.19 frystyk 489: if (remaining == 0) {
2.9 eric 490: HTList * doomed = cur;
2.1 frystyk 491: if (THD_TRACE)
492: HTTrace("Event....... No more events registered for socket %d\n", s);
2.15 frystyk 493:
494:
495: #ifndef WWW_WIN_ASYNC
496: /* Check to see if we have to update MaxSock */
2.18 frystyk 497: if(pres->s >= MaxSock) __ResetMaxSock();
2.15 frystyk 498: #endif /* !WWW_WIN_ASYNC */
499:
2.2 eric 500: HT_FREE(pres);
2.9 eric 501: pres = (SockEvents *) HTList_nextObject(cur);
502: HTList_quickRemoveElement(doomed, last);
503: }
504: ret = HT_OK;
2.15 frystyk 505:
2.18 frystyk 506: if (THD_TRACE) HTTrace("Event....... Socket %d unregistered for %s\n", s,
507: HTEvent_type2str(type));
2.15 frystyk 508:
2.6 eric 509: /*
2.15 frystyk 510: ** We found the socket and can break
2.6 eric 511: */
2.15 frystyk 512: break;
2.9 eric 513: }
514: last = cur;
2.1 frystyk 515: }
2.19 frystyk 516: if (ret == HT_ERROR && THD_TRACE) HTTrace("Event....... Couldn't find socket %d. Can't unregister type %s\n",
517: s, HTEvent_type2str(type));
2.9 eric 518: return ret;
2.1 frystyk 519: }
520:
521: /*
522: ** Unregister all sockets
523: ** N.B. we just remove them for our internal data structures: it is up to the
524: ** application to actually close the socket.
525: */
2.2 eric 526: PUBLIC int HTEventList_unregisterAll (void)
2.1 frystyk 527: {
528: int i;
529: if (THD_TRACE) HTTrace("Unregister.. all sockets\n");
530: for (i = 0 ; i < PRIME_TABLE_SIZE; i++) {
2.2 eric 531: HTList * cur = HashTable[i];
2.3 eric 532: SockEvents * pres;
533: while ((pres = (SockEvents *) HTList_nextObject(cur))) {
534: #ifdef WWW_WIN_ASYNC
535: WSAAsyncSelect(pres->s, HTSocketWin, HTwinMsg, 0);
536: #endif /* WWW_WIN_ASYNC */
2.2 eric 537: HT_FREE(pres);
2.3 eric 538: }
2.2 eric 539: HTList_delete(HashTable[i]);
540: HashTable[i] = NULL;
2.1 frystyk 541: }
2.3 eric 542: #ifndef WWW_WIN_ASYNC
543: MaxSock = 0 ;
2.15 frystyk 544: if (THD_TRACE) HTTrace("Event....... New value for MaxSock is %d\n", MaxSock);
2.1 frystyk 545: FD_ZERO(FdArray+HTEvent_INDEX(HTEvent_READ));
546: FD_ZERO(FdArray+HTEvent_INDEX(HTEvent_WRITE));
547: FD_ZERO(FdArray+HTEvent_INDEX(HTEvent_OOB));
2.3 eric 548: #endif /* !WWW_WIN_ASYNC */
2.2 eric 549: #ifdef HT_EVENT_ORDER
550: EventOrder_deleteAll();
551: #endif /* HT_EVENT_ORDER */
2.1 frystyk 552: return 0;
553: }
554:
555: /*
556: ** Dispatch the event to the appropriate event handler.
557: ** If no event handler is found then just return.
558: */
2.5 eric 559: PUBLIC int HTEventList_dispatch (SOCKET s, HTEventType type, ms_t now)
2.1 frystyk 560: {
2.3 eric 561: SockEvents * sockp = SockEvents_get(s, SockEvents_find);
562: if (sockp) {
563: HTEvent * event = sockp->events[HTEvent_INDEX(type)];
2.1 frystyk 564:
2.5 eric 565: /* Fixup the timeout
566: */
567: #ifdef IN_EVENT
568: if (event->timer)
569: HTTimer_refresh(event->timer, now);
570: #else
571: if (sockp->timeouts[HTEvent_INDEX(type)])
572: HTTimer_refresh(sockp->timeouts[HTEvent_INDEX(type)], now);
573: #endif
2.1 frystyk 574: /*
575: ** If we have found an event object for this event then see
576: ** is we should call it.
577: */
578: if (event && event->priority!=HT_PRIORITY_OFF)
579: return (*event->cbf) (s, event->param, type);
2.3 eric 580: if (THD_TRACE) HTTrace("Dispatch.... Handler %p NOT called\n", sockp);
2.1 frystyk 581: return HT_OK;
582: }
583: if (THD_TRACE) HTTrace("Dispatch.... Bad socket %d\n", s);
584: return NO;
585: }
586:
587: /*
588: ** Stops the (select based) event loop. The function does not guarantee
589: ** that all requests have terminated. This is for the app to do
590: */
591: PUBLIC void HTEventList_stopLoop (void)
592: {
593: HTEndLoop = 1;
594: }
595:
2.3 eric 596: PUBLIC HTEvent * HTEventList_lookup (SOCKET s, HTEventType type)
597: {
598: SockEvents * sockp = NULL;
599: if ((sockp = SockEvents_get(s, SockEvents_find)) == NULL)
600: return NULL;
601: return sockp->events[HTEvent_INDEX(type)];
602: }
603:
604: /* REGISTER DEFULT EVENT MANAGER
605: ** -----------------------------
606: ** Not done automaticly - may be done by application!
607: */
608: PUBLIC BOOL HTEventInit (void)
609: {
2.4 eric 610: #ifdef WWW_WIN_ASYNC
611: /*
612: ** We are here starting a hidden window to take care of events from
613: ** the async select() call in the async version of the event loop in
614: ** the Internal event manager (HTEvtLst.c)
615: */
616: static char className[] = "AsyncWindowClass";
617: WNDCLASS wc;
618: OSVERSIONINFO osInfo;
619:
620: wc.style=0;
621: wc.lpfnWndProc=(WNDPROC)AsyncWindowProc;
622: wc.cbClsExtra=0;
623: wc.cbWndExtra=0;
624: wc.hIcon=0;
625: wc.hCursor=0;
626: wc.hbrBackground=0;
627: wc.lpszMenuName=(LPSTR)0;
628: wc.lpszClassName=className;
629:
630: osInfo.dwOSVersionInfoSize = sizeof(osInfo);
631: GetVersionEx(&osInfo);
632: if (osInfo.dwPlatformId == VER_PLATFORM_WIN32s || osInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
633: wc.hInstance=GetModuleHandle(NULL); /* 95 and non threaded platforms */
634: else
635: wc.hInstance=GetCurrentProcess(); /* NT and hopefully everything following */
636: if (!RegisterClass(&wc)) {
637: HTTrace("HTLibInit.. Can't RegisterClass \"%s\"\n", className);
638: return NO;
639: }
640: if (!(HTSocketWin = CreateWindow(className, "WWW_WIN_ASYNC", WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT,
641: CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, wc.hInstance,0))) {
642: char space[50];
643: HTTrace("HTLibInit.. Can't CreateWindow \"WWW_WIN_ASYNC\" - error:");
644: sprintf(space, "%ld\n", GetLastError());
645: HTTrace(space);
646: return NO;
647: }
648: HTwinMsg = WM_USER; /* use first available message since app uses none */
2.8 frystyk 649:
650: /*
651: ** Register platform specific timer handlers for windows
652: */
653: HTTimer_registerSetTimerCallback(Timer_setWindowsTimer);
654: HTTimer_registerDeleteTimerCallback(Timer_deleteWindowsTimer);
655:
2.4 eric 656: #endif /* WWW_WIN_ASYNC */
657:
658: #ifdef _WINSOCKAPI_
659: /*
660: ** Initialise WinSock DLL. This must also be shut down! PMH
661: */
662: {
663: WSADATA wsadata;
664: if (WSAStartup(DESIRED_WINSOCK_VERSION, &wsadata)) {
665: if (WWWTRACE)
666: HTTrace("HTEventInit. Can't initialize WinSoc\n");
667: WSACleanup();
668: return NO;
669: }
670: if (wsadata.wVersion < MINIMUM_WINSOCK_VERSION) {
671: if (WWWTRACE)
672: HTTrace("HTEventInit. Bad version of WinSoc\n");
673: WSACleanup();
674: return NO;
675: }
676: if (APP_TRACE)
677: HTTrace("HTEventInit. Using WinSoc version \"%s\".\n",
678: wsadata.szDescription);
679: }
680: #endif /* _WINSOCKAPI_ */
681:
2.3 eric 682: HTEvent_setRegisterCallback(HTEventList_register);
683: HTEvent_setUnregisterCallback(HTEventList_unregister);
684: return YES;
685: }
686:
687: PUBLIC BOOL HTEventTerminate (void)
688: {
2.19 frystyk 689: #ifdef _WINSOCKAPI_
690: WSACleanup();
691: #endif /* _WINSOCKAPI_ */
2.3 eric 692: return YES;
693: }
694:
695: #ifdef WWW_WIN_ASYNC
696:
2.4 eric 697: /* HTEventList_get/setWinHandle
2.3 eric 698: ** --------------------------
699: ** Managing the windows handle on Windows
700: */
701: PUBLIC BOOL HTEventList_setWinHandle (HWND window, unsigned long message)
702: {
703: HTSocketWin = window;
704: HTwinMsg = message;
705: return YES;
706: }
707:
708: PUBLIC HWND HTEventList_getWinHandle (unsigned long * pMessage)
709: {
710: if (pMessage)
711: *pMessage = HTwinMsg;
712: return (HTSocketWin);
713: }
714:
715: /* only responsible for WM_TIMER and WSA_AsyncSelect */
716: PUBLIC LRESULT CALLBACK AsyncWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
717: {
718: WORD event;
719: SOCKET sock;
2.4 eric 720: HTEventType type;
2.5 eric 721: ms_t now = HTGetTimeInMillis();
2.3 eric 722:
723: /* timeout stuff */
724: if (uMsg == WM_TIMER) {
2.4 eric 725: HTTimer_dispatch((HTTimer *)wParam);
2.3 eric 726: return (0);
727: }
728:
729: if (uMsg != HTwinMsg) /* not our async message */
730: return (DefWindowProc(hwnd, uMsg, wParam, lParam));
731:
732: event = LOWORD(lParam);
733: sock = (SOCKET)wParam;
2.4 eric 734: switch (event) {
735: case FD_READ: type = HTEvent_READ; break;
736: case FD_WRITE: type = HTEvent_WRITE; break;
737: case FD_ACCEPT: type = HTEvent_ACCEPT; break;
738: case FD_CONNECT: type = HTEvent_CONNECT; break;
739: case FD_OOB: type = HTEvent_OOB; break;
740: case FD_CLOSE: type = HTEvent_CLOSE; break;
2.20 frystyk 741: default: HTDebugBreak(__FILE__, __LINE__, "Unknown event %d\n", event);
2.4 eric 742: }
2.5 eric 743: if (HTEventList_dispatch((int)sock, type, now) != HT_OK)
2.4 eric 744: HTEndLoop = -1;
2.3 eric 745: return (0);
746: }
747:
2.4 eric 748: PUBLIC int HTEventList_loop (HTRequest * theRequest )
2.3 eric 749: {
750: MSG msg;
751: while (GetMessage(&msg,0,0,0)) {
752: TranslateMessage(&msg);
753: DispatchMessage(&msg);
754: }
755: return (HTEndLoop == 1 ? HT_OK : HT_ERROR);
756: }
757:
758: #else /* WWW_WIN_ASYNC */
759:
2.1 frystyk 760: /*
761: ** We wait for activity from one of our registered
762: ** channels, and dispatch on that.
763: **
764: ** There are now two versions of the event loop. The first is if you want
765: ** to use async I/O on windows, and the other is if you want to use normal
766: ** Unix setup with sockets
767: */
768: PUBLIC int HTEventList_loop (HTRequest * theRequest)
769: {
770: fd_set treadset, twriteset, texceptset;
771: struct timeval waittime, * wt;
772: int active_sockets;
773: int maxfds;
2.5 eric 774: ms_t timeout;
775: ms_t now;
2.1 frystyk 776: SOCKET s;
2.5 eric 777: int status = HT_OK;
2.1 frystyk 778:
2.2 eric 779: EventOrderList = HTList_new(); /* is kept around until EventOrder_deleteAll */
780:
2.1 frystyk 781: /* Don't leave this loop until we leave the application */
782: do {
783: treadset = FdArray[HTEvent_INDEX(HTEvent_READ)];
784: twriteset = FdArray[HTEvent_INDEX(HTEvent_WRITE)];
785: texceptset = FdArray[HTEvent_INDEX(HTEvent_OOB)];
786:
787: /*
788: ** Timeval struct copy needed for linux, as it set the value to the
789: ** remaining timeout while exiting the select. (and perhaps for
790: ** other OS). Code borrowed from X server.
791: */
792: wt = NULL;
2.5 eric 793: if ((status = HTTimer_next(&timeout)))
794: return status;
795: if (timeout != 0) {
2.1 frystyk 796: waittime.tv_sec = timeout / MILLI_PER_SECOND;
797: waittime.tv_usec = (timeout % MILLI_PER_SECOND) *
798: (1000000 / MILLI_PER_SECOND);
799: wt = &waittime;
800: }
801:
2.18 frystyk 802: maxfds = MaxSock;
803: if (THD_TRACE) HTTrace("Event Loop.. calling select: maxfds is %d\n", maxfds);
804:
805: #ifdef EVENT_TRACE
2.15 frystyk 806: #define HT_FS_BYTES(a) ((((a)/16)+1) * 4)
807: HTTraceData((char*)&treadset, HT_FS_BYTES(maxfds), "HTEventList_loop pre treadset: (maxfd:%d)", maxfds);
808: HTTraceData((char*)&twriteset, HT_FS_BYTES(maxfds), "HTEventList_loop pre twriteset:");
809: HTTraceData((char*)&texceptset, HT_FS_BYTES(maxfds), "HTEventList_loop pre texceptset:");
2.18 frystyk 810: #endif /* EVENT_TRACE */
2.15 frystyk 811:
2.1 frystyk 812: #ifdef __hpux
813: active_sockets = select(maxfds+1, (int *)&treadset, (int *)&twriteset,
814: (int *)&texceptset, wt);
815: #else
816: active_sockets = select(maxfds+1, &treadset, &twriteset, &texceptset, wt);
817: #endif
2.2 eric 818:
2.5 eric 819: now = HTGetTimeInMillis();
2.18 frystyk 820:
821: #ifdef EVENT_TRACE
2.15 frystyk 822: HTTraceData((char*)&treadset, HT_FS_BYTES(maxfds), "HTEventList_loop post treadset: (active_sockets:%d)", active_sockets);
823: HTTraceData((char*)&twriteset, HT_FS_BYTES(maxfds), "HTEventList_loop post twriteset: (errno:%d)", errno);
824: HTTraceData((char*)&texceptset, HT_FS_BYTES(maxfds), "HTEventList_loop post texceptset:");
2.18 frystyk 825: #endif /* EVENT_TRACE */
2.2 eric 826:
2.1 frystyk 827: if (THD_TRACE) HTTrace("Event Loop.. select returns %d\n", active_sockets);
828:
829: if (active_sockets == -1) {
2.20 frystyk 830: #ifdef EINTR
831: if (socerrno == EINTR) {
832: /*
833: ** EINTR The select() function was interrupted before any
834: ** of the selected events occurred and before the
835: ** timeout interval expired.
836: **
837: ** If SA_RESTART has been set for the interrupting
838: ** signal, it is implementation-dependent whether
839: ** select() restarts or returns with EINTR.
840: */
841: if (THD_TRACE)
842: HTTrace("Event Loop.. select was interruted - try again\n");
843: continue;
844: }
845: #endif /* EINTR */
846: HTRequest_addSystemError(theRequest, ERR_FATAL, socerrno, NO, "select");
2.3 eric 847: EventList_dump();
2.1 frystyk 848: return HT_ERROR;
849: }
850:
851: /*
852: ** We had a timeout so now we check and see if we have a timeout
2.5 eric 853: ** handler to call. Let HTTimer_next get it.
2.1 frystyk 854: */
2.5 eric 855: if (active_sockets == 0)
2.1 frystyk 856: continue;
857:
858: /*
859: ** There were active sockets. Determine which fd sets they were in
860: */
2.2 eric 861: #ifdef HT_EVENT_ORDER
2.5 eric 862: #define DISPATCH(socket, type, now) EventOrder_add(socket, type, now)
2.2 eric 863: #else /* HT_EVENT_ORDER */
2.5 eric 864: #define DISPATCH(socket, type, now) HTEventList_dispatch(socket, type, now)
2.2 eric 865: #endif /* !HT_EVENT_ORDER */
2.1 frystyk 866: for (s = 0 ; s <= maxfds ; s++) {
867: if (FD_ISSET(s, &texceptset))
2.5 eric 868: if ((status = DISPATCH(s, HTEvent_OOB, now)) != HT_OK)
2.1 frystyk 869: return status;
870: if (FD_ISSET(s, &twriteset))
2.5 eric 871: if ((status = DISPATCH(s, HTEvent_WRITE, now)) != HT_OK)
2.1 frystyk 872: return status;
873: if (FD_ISSET(s, &treadset))
2.5 eric 874: if ((status = DISPATCH(s, HTEvent_READ, now)) != HT_OK)
2.1 frystyk 875: return status;
876: }
2.2 eric 877: #ifdef HT_EVENT_ORDER
878: if ((status = EventOrder_executeAndDelete()) != HT_OK)
879: return status;
880: #endif /* HT_EVENT_ORDER */
2.1 frystyk 881: } while (!HTEndLoop);
882:
883: return HT_OK;
884: }
885:
2.3 eric 886: #endif /* !WWW_WIN_ASYNC */
2.12 eric 887:
888: PUBLIC void CheckSockEvent(HTTimer * timer, HTTimerCallback * cbf, void * param)
889: {
890: SockEvents * sockp = (SockEvents *)param;
891: if (cbf == EventListTimerHandler &&
892: sockp->timeouts[0] != timer &&
893: sockp->timeouts[1] != timer &&
894: sockp->timeouts[2] != timer) {
2.20 frystyk 895: HTDebugBreak(__FILE__, __LINE__, "Bad timer %p\n", timer);
2.12 eric 896: }
897: }
898:
Webmaster