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