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