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