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