Annotation of libwww/Library/src/HTEvtLst.c, revision 2.9
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.9 ! eric 6: ** @(#) $Id: HTEvtLst.c,v 2.8 1996/12/27 16:13:16 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];
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: {
194: SockEvents * sockp = (SockEvents *)param;
195: HTEvent * event;
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: }
227: HTTrace("Event....... can't find event for timer %p.\n", timer);
228: return HT_ERROR;
229: }
230:
231: /* ------------------------------------------------------------------------- */
2.2 eric 232: /* E V E N T O R D E R I N G S T U F F */
233: #ifdef HT_EVENT_ORDER
234: typedef struct {
235: HTEvent * event;
236: SOCKET s;
237: HTEventType type;
238: HTPriority skipped;
239: } EventOrder;
2.1 frystyk 240:
2.2 eric 241: HTList * EventOrderList = NULL;
242: #if 0
243: /*
244: ** return -1 if a should be after b
245: */
246: int EventOrderComparer (const void * a, const void * b)
247: {
248: EventOrder * placeMe = (EventOrder *)a;
249: EventOrder * maybeHere = (EventOrder *)b;
250: if (placeMe->event->priority+placeMe->skipped >= maybeHere->event->priority+maybeHere->skipped)
251: return 1;
252: return -1;
253: }
254: #endif
255:
2.5 eric 256: int EventOrder_add (SOCKET s, HTEventType type, ms_t now)
2.2 eric 257: {
258: EventOrder * pres;
259: HTList * cur = EventOrderList;
260: HTList * insertAfter = cur;
2.5 eric 261: SockEvents * sockp = SockEvents_get(s, SockEvents_find);
262: HTEvent * event;
2.2 eric 263:
2.5 eric 264: if (sockp == NULL || (event = sockp->events[HTEvent_INDEX(type)]) == NULL) {
265: HTTrace("EventOrder.. no event found for socket %d, type %s.\n", s, HTEvent_type2str(type));
2.2 eric 266: return HT_ERROR;
267: }
268:
2.5 eric 269: /* Fixup the timeout
270: */
271: #ifdef IN_EVENT
272: if (event->timer)
273: HTTimer_refresh(event->timer, now);
274: #else
275: if (sockp->timeouts[HTEvent_INDEX(type)])
276: HTTimer_refresh(sockp->timeouts[HTEvent_INDEX(type)], now);
277: #endif
278:
2.2 eric 279: /*
280: ** Look to see if it's already here from before
281: */
282: while ((pres = (EventOrder *) HTList_nextObject(cur))) {
283: if (pres->s == s && pres->event == event && pres->type == type) {
284: pres->skipped++;
285: return HT_OK;
286: }
287: if (pres->event->priority+pres->skipped > event->priority)
288: insertAfter = cur;
289: }
290:
291: /*
292: ** No, so create a new element
293: */
294: if ((pres = (EventOrder *) HT_CALLOC(1, sizeof(EventOrder))) == NULL)
295: HT_OUTOFMEM("EventOrder_add");
296: pres->event = event;
297: pres->s = s;
298: pres->type = type;
299: HTList_addObject(insertAfter, (void *)pres);
300: return HT_OK;
301: }
302:
303: PUBLIC int EventOrder_executeAndDelete (void)
304: {
2.4 eric 305: HTList * cur = EventOrderList;
2.2 eric 306: EventOrder * pres;
307: int i = 0;
308: if (THD_TRACE) HTTrace("EventOrder.. execute ordered events\n");
2.4 eric 309: if (cur == NULL) return NO;
2.2 eric 310: while ((pres = (EventOrder *) HTList_removeLastObject(cur)) && i < EVENTS_TO_EXECUTE) {
2.5 eric 311: HTEvent * event = pres->event;
312: int ret;
2.7 frystyk 313: if (THD_TRACE)
2.5 eric 314: HTTrace("EventList... calling socket %d, request %p handler %p type %s\n",
315: pres->s, (void *) event->request,
316: (void *) event->cbf, HTEvent_type2str(pres->type));
317: ret = (*pres->event->cbf)(pres->s, pres->event->param, pres->type);
2.2 eric 318: HT_FREE(pres);
319: if (ret != HT_OK)
320: return ret;
321: i++;
322: }
323: return HT_OK;
324: }
325:
326: PUBLIC BOOL EventOrder_deleteAll (void)
327: {
328: HTList * cur = EventOrderList;
329: EventOrder * pres;
330: if (THD_TRACE) HTTrace("EventOrder.. all ordered events\n");
2.4 eric 331: if (cur == NULL) return NO;
2.2 eric 332: while ((pres = (EventOrder *) HTList_nextObject(cur)))
333: HT_FREE(pres);
334: HTList_delete(EventOrderList);
335: EventOrderList = NULL;
336: return YES;
337: }
338: #endif /* HT_EVENT_ORDER */
339:
340: /* ------------------------------------------------------------------------- */
2.1 frystyk 341:
342: /*
343: ** For a given socket, reqister a request structure, a set of operations,
344: ** a HTEventCallback function, and a priority. For this implementation,
345: ** we allow only a single HTEventCallback function for all operations.
346: ** and the priority field is ignored.
347: */
348: PUBLIC int HTEventList_register (SOCKET s, HTEventType type, HTEvent * event)
349: {
2.3 eric 350: SockEvents * sockp;
2.1 frystyk 351: if (THD_TRACE)
2.5 eric 352: HTTrace("Event....... Register socket %d, request %p handler %p type %s at priority %d\n",
2.1 frystyk 353: s, (void *) event->request,
2.5 eric 354: (void *) event->cbf, HTEvent_type2str(type),
2.1 frystyk 355: (unsigned) event->priority);
2.4 eric 356: if (s==INVSOC || HTEvent_INDEX(type) >= HTEvent_TYPES)
2.1 frystyk 357: return 0;
358:
359: /*
360: ** Insert socket into appropriate file descriptor set. We also make sure
361: ** that it is registered in the global set.
362: */
2.5 eric 363: if (THD_TRACE) HTTrace("Event....... Registering socket for %s\n", HTEvent_type2str(type));
2.3 eric 364: sockp = SockEvents_get(s, SockEvents_mayCreate);
365: sockp->s = s;
366: sockp->events[HTEvent_INDEX(type)] = event;
367: #ifdef WWW_WIN_ASYNC
2.4 eric 368: if (WSAAsyncSelect(s, HTSocketWin, HTwinMsg, HTEvent_BITS(type)) < 0)
2.3 eric 369: return HT_ERROR;
370: #else /* WWW_WIN_ASYNC */
2.1 frystyk 371: FD_SET(s, FdArray+HTEvent_INDEX(type));
2.3 eric 372: if (s > MaxSock) MaxSock = s ;
373: #endif /* !WWW_WIN_ASYNC */
2.1 frystyk 374: /*
375: ** If the timeout has been set (relative in millis) then we register
376: ** a new timeout for this event
377: */
378: if (event->millis >= 0) {
2.5 eric 379: #ifdef IN_EVENT
380: event->timer = HTTimer_new(NULL, EventListTimerHandler, sockp, event->millis, YES);
381: #else
2.3 eric 382: sockp->timeouts[HTEvent_INDEX(type)] = HTTimer_new(NULL, EventListTimerHandler, sockp, event->millis, YES);
2.5 eric 383: #endif
2.1 frystyk 384: }
385:
386: return HT_OK;
387: }
388:
2.3 eric 389: PRIVATE int EventList_remaining(SockEvents * pres)
390: {
391: int ret = 0;
392: int i;
393: for (i = 0; i < HTEvent_TYPES; i++)
394: if (pres->events[i] != NULL)
395: ret |= 1<<i;
396: return ret;
397: }
398:
2.1 frystyk 399: /*
400: ** Remove the registered information for the specified socket for the actions
401: ** specified in ops. if no actions remain after the unregister, the registered
402: ** info is deleted, and, if the socket has been registered for notification,
403: ** the HTEventCallback will be invoked.
404: */
405: PUBLIC int HTEventList_unregister(SOCKET s, HTEventType type)
406: {
2.3 eric 407: long v = HASH(s);
408: HTList * cur = HashTable[v];
409: HTList * last = cur;
410: SockEvents * pres;
2.9 ! eric 411: int ret = HT_ERROR;
2.2 eric 412:
2.9 ! eric 413: #ifndef WWW_WIN_ASYNC
2.6 eric 414: MaxSock = -1; /* re-find maximum in-use socket */
2.9 ! eric 415: #endif /* !WWW_WIN_ASYNC */
! 416: while (cur && (pres = (SockEvents *) HTList_nextObject(cur))) {
2.2 eric 417: if (pres->s == s) {
2.3 eric 418: int remaining;
2.1 frystyk 419:
420: /*
421: ** Unregister the event from this action
422: */
2.2 eric 423: pres->events[HTEvent_INDEX(type)] = NULL;
2.1 frystyk 424:
425: /*
426: ** Check to see of there was a timeout connected with the event.
427: ** If so then delete the timeout as well.
428: */
429: {
2.5 eric 430: #ifdef IN_EVENT
431: HTTimer * timer = pres->events[HTEvent_INDEX(type)]->timer;
432: #else
2.2 eric 433: HTTimer * timer = pres->timeouts[HTEvent_INDEX(type)];
2.5 eric 434: #endif
2.1 frystyk 435: if (timer) HTTimer_delete(timer);
436: }
437:
438: /*
439: ** Check to see if we can delete the action completely. We do this
440: ** if there are no more events registered.
441: */
2.3 eric 442: if ((remaining = EventList_remaining(pres)) == 0) {
2.9 ! eric 443: HTList * doomed = cur;
2.1 frystyk 444: if (THD_TRACE)
445: HTTrace("Event....... No more events registered for socket %d\n", s);
2.2 eric 446: HT_FREE(pres);
2.9 ! eric 447: pres = (SockEvents *) HTList_nextObject(cur);
! 448: HTList_quickRemoveElement(doomed, last);
! 449: }
! 450: ret = HT_OK;
2.3 eric 451: #ifdef WWW_WIN_ASYNC
452: if (WSAAsyncSelect(s, HTSocketWin, HTwinMsg, remaining) < 0)
2.9 ! eric 453: ret = HT_ERROR;
! 454: cur = NULL; /* we don't have to finish list to look for MaxSock in WWW_WIN_ASYNC */
2.3 eric 455: #else /* WWW_WIN_ASYNC */
456: FD_CLR(s, FdArray+HTEvent_INDEX(type));
457: #endif /* !WWW_WIN_ASYNC */
458: if (THD_TRACE)
2.5 eric 459: HTTrace("Event....... Socket %d unregisterd for %s\n", s, HTEvent_type2str(type));
2.6 eric 460: /*
2.9 ! eric 461: ** stay in loop to finish checking list for max socket
2.6 eric 462: */
2.9 ! eric 463: }
! 464: last = cur;
! 465: #ifndef WWW_WIN_ASYNC
! 466: if(pres->s > MaxSock)
2.6 eric 467: MaxSock = pres->s;
2.9 ! eric 468: #endif /* !WWW_WIN_ASYNC */
2.1 frystyk 469: }
2.9 ! eric 470: if (ret == HT_ERROR && THD_TRACE) HTTrace("Event....... Couldn't find socket %d.\n", s);
! 471: #ifndef WWW_WIN_ASYNC
! 472: if (MaxSock <= 0 && THD_TRACE) HTTrace("Event....... MaxSock: %d.\n", MaxSock);
! 473: #endif /* !WWW_WIN_ASYNC */
! 474: return ret;
2.1 frystyk 475: }
476:
477: /*
478: ** Unregister all sockets
479: ** N.B. we just remove them for our internal data structures: it is up to the
480: ** application to actually close the socket.
481: */
2.2 eric 482: PUBLIC int HTEventList_unregisterAll (void)
2.1 frystyk 483: {
484: int i;
485: if (THD_TRACE) HTTrace("Unregister.. all sockets\n");
486: for (i = 0 ; i < PRIME_TABLE_SIZE; i++) {
2.2 eric 487: HTList * cur = HashTable[i];
2.3 eric 488: SockEvents * pres;
489: while ((pres = (SockEvents *) HTList_nextObject(cur))) {
490: #ifdef WWW_WIN_ASYNC
491: WSAAsyncSelect(pres->s, HTSocketWin, HTwinMsg, 0);
492: #endif /* WWW_WIN_ASYNC */
2.2 eric 493: HT_FREE(pres);
2.3 eric 494: }
2.2 eric 495: HTList_delete(HashTable[i]);
496: HashTable[i] = NULL;
2.1 frystyk 497: }
2.3 eric 498: #ifndef WWW_WIN_ASYNC
499: MaxSock = 0 ;
2.1 frystyk 500: FD_ZERO(FdArray+HTEvent_INDEX(HTEvent_READ));
501: FD_ZERO(FdArray+HTEvent_INDEX(HTEvent_WRITE));
502: FD_ZERO(FdArray+HTEvent_INDEX(HTEvent_OOB));
2.3 eric 503: #endif /* !WWW_WIN_ASYNC */
2.2 eric 504: #ifdef HT_EVENT_ORDER
505: EventOrder_deleteAll();
506: #endif /* HT_EVENT_ORDER */
2.1 frystyk 507: return 0;
508: }
509:
510: /*
511: ** Dispatch the event to the appropriate event handler.
512: ** If no event handler is found then just return.
513: */
2.5 eric 514: PUBLIC int HTEventList_dispatch (SOCKET s, HTEventType type, ms_t now)
2.1 frystyk 515: {
2.3 eric 516: SockEvents * sockp = SockEvents_get(s, SockEvents_find);
517: if (sockp) {
518: HTEvent * event = sockp->events[HTEvent_INDEX(type)];
2.1 frystyk 519:
2.5 eric 520: /* Fixup the timeout
521: */
522: #ifdef IN_EVENT
523: if (event->timer)
524: HTTimer_refresh(event->timer, now);
525: #else
526: if (sockp->timeouts[HTEvent_INDEX(type)])
527: HTTimer_refresh(sockp->timeouts[HTEvent_INDEX(type)], now);
528: #endif
2.1 frystyk 529: /*
530: ** If we have found an event object for this event then see
531: ** is we should call it.
532: */
533: if (event && event->priority!=HT_PRIORITY_OFF)
534: return (*event->cbf) (s, event->param, type);
2.3 eric 535: if (THD_TRACE) HTTrace("Dispatch.... Handler %p NOT called\n", sockp);
2.1 frystyk 536: return HT_OK;
537: }
538: if (THD_TRACE) HTTrace("Dispatch.... Bad socket %d\n", s);
539: return NO;
540: }
541:
542: /*
543: ** Stops the (select based) event loop. The function does not guarantee
544: ** that all requests have terminated. This is for the app to do
545: */
546: PUBLIC void HTEventList_stopLoop (void)
547: {
548: HTEndLoop = 1;
549: }
550:
2.3 eric 551: PUBLIC HTEvent * HTEventList_lookup (SOCKET s, HTEventType type)
552: {
553: SockEvents * sockp = NULL;
554: if ((sockp = SockEvents_get(s, SockEvents_find)) == NULL)
555: return NULL;
556: return sockp->events[HTEvent_INDEX(type)];
557: }
558:
559: /* REGISTER DEFULT EVENT MANAGER
560: ** -----------------------------
561: ** Not done automaticly - may be done by application!
562: */
563: PUBLIC BOOL HTEventInit (void)
564: {
2.4 eric 565: #ifdef WWW_WIN_ASYNC
566: /*
567: ** We are here starting a hidden window to take care of events from
568: ** the async select() call in the async version of the event loop in
569: ** the Internal event manager (HTEvtLst.c)
570: */
571: static char className[] = "AsyncWindowClass";
572: WNDCLASS wc;
573: OSVERSIONINFO osInfo;
574:
575: wc.style=0;
576: wc.lpfnWndProc=(WNDPROC)AsyncWindowProc;
577: wc.cbClsExtra=0;
578: wc.cbWndExtra=0;
579: wc.hIcon=0;
580: wc.hCursor=0;
581: wc.hbrBackground=0;
582: wc.lpszMenuName=(LPSTR)0;
583: wc.lpszClassName=className;
584:
585: osInfo.dwOSVersionInfoSize = sizeof(osInfo);
586: GetVersionEx(&osInfo);
587: if (osInfo.dwPlatformId == VER_PLATFORM_WIN32s || osInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
588: wc.hInstance=GetModuleHandle(NULL); /* 95 and non threaded platforms */
589: else
590: wc.hInstance=GetCurrentProcess(); /* NT and hopefully everything following */
591: if (!RegisterClass(&wc)) {
592: HTTrace("HTLibInit.. Can't RegisterClass \"%s\"\n", className);
593: return NO;
594: }
595: if (!(HTSocketWin = CreateWindow(className, "WWW_WIN_ASYNC", WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT,
596: CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, wc.hInstance,0))) {
597: char space[50];
598: HTTrace("HTLibInit.. Can't CreateWindow \"WWW_WIN_ASYNC\" - error:");
599: sprintf(space, "%ld\n", GetLastError());
600: HTTrace(space);
601: return NO;
602: }
603: HTwinMsg = WM_USER; /* use first available message since app uses none */
2.8 frystyk 604:
605: /*
606: ** Register platform specific timer handlers for windows
607: */
608: HTTimer_registerSetTimerCallback(Timer_setWindowsTimer);
609: HTTimer_registerDeleteTimerCallback(Timer_deleteWindowsTimer);
610:
2.4 eric 611: #endif /* WWW_WIN_ASYNC */
612:
613: #ifdef _WINSOCKAPI_
614: /*
615: ** Initialise WinSock DLL. This must also be shut down! PMH
616: */
617: {
618: WSADATA wsadata;
619: if (WSAStartup(DESIRED_WINSOCK_VERSION, &wsadata)) {
620: if (WWWTRACE)
621: HTTrace("HTEventInit. Can't initialize WinSoc\n");
622: WSACleanup();
623: return NO;
624: }
625: if (wsadata.wVersion < MINIMUM_WINSOCK_VERSION) {
626: if (WWWTRACE)
627: HTTrace("HTEventInit. Bad version of WinSoc\n");
628: WSACleanup();
629: return NO;
630: }
631: if (APP_TRACE)
632: HTTrace("HTEventInit. Using WinSoc version \"%s\".\n",
633: wsadata.szDescription);
634: }
635: #endif /* _WINSOCKAPI_ */
636:
2.3 eric 637: HTEvent_setRegisterCallback(HTEventList_register);
638: HTEvent_setUnregisterCallback(HTEventList_unregister);
639: return YES;
640: }
641:
642: PUBLIC BOOL HTEventTerminate (void)
643: {
644: return YES;
645: }
646:
647: #ifdef WWW_WIN_ASYNC
648:
2.4 eric 649: /* HTEventList_get/setWinHandle
2.3 eric 650: ** --------------------------
651: ** Managing the windows handle on Windows
652: */
653: PUBLIC BOOL HTEventList_setWinHandle (HWND window, unsigned long message)
654: {
655: HTSocketWin = window;
656: HTwinMsg = message;
657: return YES;
658: }
659:
660: PUBLIC HWND HTEventList_getWinHandle (unsigned long * pMessage)
661: {
662: if (pMessage)
663: *pMessage = HTwinMsg;
664: return (HTSocketWin);
665: }
666:
667: /* only responsible for WM_TIMER and WSA_AsyncSelect */
668: PUBLIC LRESULT CALLBACK AsyncWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
669: {
670: WORD event;
671: SOCKET sock;
2.4 eric 672: HTEventType type;
2.5 eric 673: ms_t now = HTGetTimeInMillis();
2.3 eric 674:
675: /* timeout stuff */
676: if (uMsg == WM_TIMER) {
2.4 eric 677: HTTimer_dispatch((HTTimer *)wParam);
2.3 eric 678: return (0);
679: }
680:
681: if (uMsg != HTwinMsg) /* not our async message */
682: return (DefWindowProc(hwnd, uMsg, wParam, lParam));
683:
684: event = LOWORD(lParam);
685: sock = (SOCKET)wParam;
2.4 eric 686: switch (event) {
687: case FD_READ: type = HTEvent_READ; break;
688: case FD_WRITE: type = HTEvent_WRITE; break;
689: case FD_ACCEPT: type = HTEvent_ACCEPT; break;
690: case FD_CONNECT: type = HTEvent_CONNECT; break;
691: case FD_OOB: type = HTEvent_OOB; break;
692: case FD_CLOSE: type = HTEvent_CLOSE; break;
693: default: HTDebugBreak();
694: }
2.5 eric 695: if (HTEventList_dispatch((int)sock, type, now) != HT_OK)
2.4 eric 696: HTEndLoop = -1;
2.3 eric 697: return (0);
698: }
699:
2.4 eric 700: PUBLIC int HTEventList_loop (HTRequest * theRequest )
2.3 eric 701: {
702: MSG msg;
703: while (GetMessage(&msg,0,0,0)) {
704: TranslateMessage(&msg);
705: DispatchMessage(&msg);
706: }
707: return (HTEndLoop == 1 ? HT_OK : HT_ERROR);
708: }
709:
710: #else /* WWW_WIN_ASYNC */
711:
2.1 frystyk 712: /*
713: ** We wait for activity from one of our registered
714: ** channels, and dispatch on that.
715: **
716: ** There are now two versions of the event loop. The first is if you want
717: ** to use async I/O on windows, and the other is if you want to use normal
718: ** Unix setup with sockets
719: */
720: PUBLIC int HTEventList_loop (HTRequest * theRequest)
721: {
722: fd_set treadset, twriteset, texceptset;
723: struct timeval waittime, * wt;
724: int active_sockets;
725: int maxfds;
2.5 eric 726: ms_t timeout;
727: ms_t now;
2.1 frystyk 728: SOCKET s;
2.5 eric 729: int status = HT_OK;
2.1 frystyk 730: HTEndLoop = 0;
731:
2.2 eric 732: EventOrderList = HTList_new(); /* is kept around until EventOrder_deleteAll */
733:
2.1 frystyk 734: /* Don't leave this loop until we leave the application */
735: do {
736: treadset = FdArray[HTEvent_INDEX(HTEvent_READ)];
737: twriteset = FdArray[HTEvent_INDEX(HTEvent_WRITE)];
738: texceptset = FdArray[HTEvent_INDEX(HTEvent_OOB)];
2.3 eric 739: maxfds = MaxSock;
2.1 frystyk 740:
741: if (THD_TRACE) HTTrace("Event Loop.. calling select: maxfds is %d\n", maxfds);
742:
743: /*
744: ** Timeval struct copy needed for linux, as it set the value to the
745: ** remaining timeout while exiting the select. (and perhaps for
746: ** other OS). Code borrowed from X server.
747: */
748: wt = NULL;
2.5 eric 749: if ((status = HTTimer_next(&timeout)))
750: return status;
751: if (timeout != 0) {
2.1 frystyk 752: waittime.tv_sec = timeout / MILLI_PER_SECOND;
753: waittime.tv_usec = (timeout % MILLI_PER_SECOND) *
754: (1000000 / MILLI_PER_SECOND);
755: wt = &waittime;
756: }
757:
2.2 eric 758: HTTraceData((char*)&treadset, maxfds/8 + 1, "HTEventList_loop pre treadset: (maxfd:%d)", maxfds);
759: HTTraceData((char*)&twriteset, maxfds/8 + 1, "HTEventList_loop pre twriteset:");
760: HTTraceData((char*)&texceptset, maxfds/8 + 1, "HTEventList_loop pre texceptset:");
761:
2.1 frystyk 762: #ifdef __hpux
763: active_sockets = select(maxfds+1, (int *)&treadset, (int *)&twriteset,
764: (int *)&texceptset, wt);
765: #else
766: active_sockets = select(maxfds+1, &treadset, &twriteset, &texceptset, wt);
767: #endif
2.2 eric 768:
2.5 eric 769: now = HTGetTimeInMillis();
2.2 eric 770: HTTraceData((char*)&treadset, maxfds/8 + 1, "HTEventList_loop post treadset: (active_sockets:%d)", active_sockets);
771: HTTraceData((char*)&twriteset, maxfds/8 + 1, "HTEventList_loop post twriteset: (errno:%d)", errno);
772: HTTraceData((char*)&texceptset, maxfds/8 + 1, "HTEventList_loop post texceptset:");
773:
2.1 frystyk 774: if (THD_TRACE) HTTrace("Event Loop.. select returns %d\n", active_sockets);
775:
776: if (active_sockets == -1) {
777: HTRequest_addSystemError( theRequest, ERR_FATAL, socerrno, NO, "select");
2.3 eric 778: EventList_dump();
779: #if 0
2.1 frystyk 780: __DumpFDSet(FdArray+HTEvent_INDEX(HTEvent_READ), "Read");
781: __DumpFDSet(FdArray+HTEvent_INDEX(HTEvent_WRITE), "Write") ;
782: __DumpFDSet(FdArray+HTEvent_INDEX(HTEvent_OOB), "Exceptions");
2.3 eric 783: #endif /* 0 */
2.1 frystyk 784: return HT_ERROR;
785: }
786:
787: /*
788: ** We had a timeout so now we check and see if we have a timeout
2.5 eric 789: ** handler to call. Let HTTimer_next get it.
2.1 frystyk 790: */
2.5 eric 791: if (active_sockets == 0)
2.1 frystyk 792: continue;
793:
794: /*
795: ** There were active sockets. Determine which fd sets they were in
796: */
2.2 eric 797: #ifdef HT_EVENT_ORDER
2.5 eric 798: #define DISPATCH(socket, type, now) EventOrder_add(socket, type, now)
2.2 eric 799: #else /* HT_EVENT_ORDER */
2.5 eric 800: #define DISPATCH(socket, type, now) HTEventList_dispatch(socket, type, now)
2.2 eric 801: #endif /* !HT_EVENT_ORDER */
2.1 frystyk 802: for (s = 0 ; s <= maxfds ; s++) {
803: if (FD_ISSET(s, &texceptset))
2.5 eric 804: if ((status = DISPATCH(s, HTEvent_OOB, now)) != HT_OK)
2.1 frystyk 805: return status;
806: if (FD_ISSET(s, &twriteset))
2.5 eric 807: if ((status = DISPATCH(s, HTEvent_WRITE, now)) != HT_OK)
2.1 frystyk 808: return status;
809: if (FD_ISSET(s, &treadset))
2.5 eric 810: if ((status = DISPATCH(s, HTEvent_READ, now)) != HT_OK)
2.1 frystyk 811: return status;
812: }
2.2 eric 813: #ifdef HT_EVENT_ORDER
814: if ((status = EventOrder_executeAndDelete()) != HT_OK)
815: return status;
816: #endif /* HT_EVENT_ORDER */
2.1 frystyk 817: } while (!HTEndLoop);
818:
819: return HT_OK;
820: }
821:
2.3 eric 822: #endif /* !WWW_WIN_ASYNC */
Webmaster