Annotation of libwww/Library/src/HTEvtLst.c, revision 2.33
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.33 ! frystyk 6: ** @(#) $Id: HTEvtLst.c,v 2.32 1999/02/07 18:20:31 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) {
2.33 ! frystyk 409: if (THD_TRACE)
! 410: HTTrace("Event....... WSAAsyncSelect returned `%s'!", HTErrnoString(socerrno));
2.3 eric 411: return HT_ERROR;
2.19 frystyk 412: }
2.3 eric 413: #else /* WWW_WIN_ASYNC */
2.1 frystyk 414: FD_SET(s, FdArray+HTEvent_INDEX(type));
2.15 frystyk 415: if (s > MaxSock) {
416: MaxSock = s ;
417: if (THD_TRACE) HTTrace("Event....... New value for MaxSock is %d\n", MaxSock);
418: }
2.3 eric 419: #endif /* !WWW_WIN_ASYNC */
2.11 frystyk 420:
2.1 frystyk 421: /*
2.11 frystyk 422: ** If the timeout has been set (relative in millis) then we register
423: ** a new timeout for this event unless we already have a timer.
2.1 frystyk 424: */
425: if (event->millis >= 0) {
2.5 eric 426: #ifdef IN_EVENT
2.11 frystyk 427: event->timer = HTTimer_new(event->timer, EventListTimerHandler,
428: sockp, event->millis, YES);
2.5 eric 429: #else
2.11 frystyk 430: sockp->timeouts[HTEvent_INDEX(type)] =
431: HTTimer_new(sockp->timeouts[HTEvent_INDEX(type)],
2.22 frystyk 432: EventListTimerHandler, sockp, event->millis, YES, YES);
2.5 eric 433: #endif
2.1 frystyk 434: }
435:
436: return HT_OK;
437: }
438:
439: /*
440: ** Remove the registered information for the specified socket for the actions
441: ** specified in ops. if no actions remain after the unregister, the registered
442: ** info is deleted, and, if the socket has been registered for notification,
443: ** the HTEventCallback will be invoked.
444: */
445: PUBLIC int HTEventList_unregister(SOCKET s, HTEventType type)
446: {
2.3 eric 447: long v = HASH(s);
448: HTList * cur = HashTable[v];
449: HTList * last = cur;
450: SockEvents * pres;
2.9 eric 451: int ret = HT_ERROR;
2.2 eric 452:
2.9 eric 453: while (cur && (pres = (SockEvents *) HTList_nextObject(cur))) {
2.2 eric 454: if (pres->s == s) {
2.19 frystyk 455: int remaining = 0;
2.1 frystyk 456:
457: /*
458: ** Unregister the event from this action
459: */
2.2 eric 460: pres->events[HTEvent_INDEX(type)] = NULL;
2.19 frystyk 461: remaining = EventList_remaining(pres);
2.1 frystyk 462:
463: /*
464: ** Check to see of there was a timeout connected with the event.
465: ** If so then delete the timeout as well.
466: */
467: {
2.5 eric 468: #ifdef IN_EVENT
469: HTTimer * timer = pres->events[HTEvent_INDEX(type)]->timer;
2.19 frystyk 470: if (timer) HTTimer_delete(timer);
471: pres->events[HTEvent_INDEX(type)]->timer = NULL;
2.5 eric 472: #else
2.2 eric 473: HTTimer * timer = pres->timeouts[HTEvent_INDEX(type)];
2.19 frystyk 474: if (timer) HTTimer_delete(timer);
475: pres->timeouts[HTEvent_INDEX(type)] = NULL;
2.5 eric 476: #endif
2.19 frystyk 477:
2.1 frystyk 478: }
479:
2.18 frystyk 480: #ifdef WWW_WIN_ASYNC
481: if (WSAAsyncSelect(s, HTSocketWin, HTwinMsg, remaining) < 0)
482: ret = HT_ERROR;
483: #else /* WWW_WIN_ASYNC */
484: FD_CLR(s, FdArray+HTEvent_INDEX(type));
485: HTTraceData((char*)FdArray+HTEvent_INDEX(type), 8, "HTEventList_unregister: (s:%d)", s);
486: #endif /* !WWW_WIN_ASYNC */
487:
2.1 frystyk 488: /*
489: ** Check to see if we can delete the action completely. We do this
490: ** if there are no more events registered.
491: */
2.19 frystyk 492: if (remaining == 0) {
2.9 eric 493: HTList * doomed = cur;
2.1 frystyk 494: if (THD_TRACE)
495: HTTrace("Event....... No more events registered for socket %d\n", s);
2.15 frystyk 496:
497:
498: #ifndef WWW_WIN_ASYNC
499: /* Check to see if we have to update MaxSock */
2.18 frystyk 500: if(pres->s >= MaxSock) __ResetMaxSock();
2.15 frystyk 501: #endif /* !WWW_WIN_ASYNC */
502:
2.2 eric 503: HT_FREE(pres);
2.9 eric 504: pres = (SockEvents *) HTList_nextObject(cur);
505: HTList_quickRemoveElement(doomed, last);
506: }
507: ret = HT_OK;
2.15 frystyk 508:
2.18 frystyk 509: if (THD_TRACE) HTTrace("Event....... Socket %d unregistered for %s\n", s,
510: HTEvent_type2str(type));
2.15 frystyk 511:
2.6 eric 512: /*
2.15 frystyk 513: ** We found the socket and can break
2.6 eric 514: */
2.15 frystyk 515: break;
2.9 eric 516: }
517: last = cur;
2.1 frystyk 518: }
2.19 frystyk 519: if (ret == HT_ERROR && THD_TRACE) HTTrace("Event....... Couldn't find socket %d. Can't unregister type %s\n",
520: s, HTEvent_type2str(type));
2.9 eric 521: return ret;
2.1 frystyk 522: }
523:
524: /*
525: ** Unregister all sockets
526: ** N.B. we just remove them for our internal data structures: it is up to the
527: ** application to actually close the socket.
528: */
2.2 eric 529: PUBLIC int HTEventList_unregisterAll (void)
2.1 frystyk 530: {
531: int i;
532: if (THD_TRACE) HTTrace("Unregister.. all sockets\n");
2.32 frystyk 533: for (i = 0 ; i < HT_M_HASH_SIZE; i++) {
2.2 eric 534: HTList * cur = HashTable[i];
2.3 eric 535: SockEvents * pres;
536: while ((pres = (SockEvents *) HTList_nextObject(cur))) {
537: #ifdef WWW_WIN_ASYNC
538: WSAAsyncSelect(pres->s, HTSocketWin, HTwinMsg, 0);
539: #endif /* WWW_WIN_ASYNC */
2.2 eric 540: HT_FREE(pres);
2.3 eric 541: }
2.2 eric 542: HTList_delete(HashTable[i]);
543: HashTable[i] = NULL;
2.1 frystyk 544: }
2.3 eric 545: #ifndef WWW_WIN_ASYNC
546: MaxSock = 0 ;
2.15 frystyk 547: if (THD_TRACE) HTTrace("Event....... New value for MaxSock is %d\n", MaxSock);
2.1 frystyk 548: FD_ZERO(FdArray+HTEvent_INDEX(HTEvent_READ));
549: FD_ZERO(FdArray+HTEvent_INDEX(HTEvent_WRITE));
550: FD_ZERO(FdArray+HTEvent_INDEX(HTEvent_OOB));
2.3 eric 551: #endif /* !WWW_WIN_ASYNC */
2.2 eric 552: #ifdef HT_EVENT_ORDER
553: EventOrder_deleteAll();
554: #endif /* HT_EVENT_ORDER */
2.1 frystyk 555: return 0;
556: }
557:
558: /*
559: ** Dispatch the event to the appropriate event handler.
560: ** If no event handler is found then just return.
561: */
2.5 eric 562: PUBLIC int HTEventList_dispatch (SOCKET s, HTEventType type, ms_t now)
2.1 frystyk 563: {
2.3 eric 564: SockEvents * sockp = SockEvents_get(s, SockEvents_find);
565: if (sockp) {
566: HTEvent * event = sockp->events[HTEvent_INDEX(type)];
2.1 frystyk 567:
2.5 eric 568: /* Fixup the timeout
569: */
570: #ifdef IN_EVENT
571: if (event->timer)
572: HTTimer_refresh(event->timer, now);
573: #else
574: if (sockp->timeouts[HTEvent_INDEX(type)])
575: HTTimer_refresh(sockp->timeouts[HTEvent_INDEX(type)], now);
576: #endif
2.1 frystyk 577: /*
578: ** If we have found an event object for this event then see
579: ** is we should call it.
580: */
581: if (event && event->priority!=HT_PRIORITY_OFF)
582: return (*event->cbf) (s, event->param, type);
2.3 eric 583: if (THD_TRACE) HTTrace("Dispatch.... Handler %p NOT called\n", sockp);
2.1 frystyk 584: return HT_OK;
585: }
586: if (THD_TRACE) HTTrace("Dispatch.... Bad socket %d\n", s);
587: return NO;
588: }
589:
590: /*
591: ** Stops the (select based) event loop. The function does not guarantee
592: ** that all requests have terminated. This is for the app to do
593: */
594: PUBLIC void HTEventList_stopLoop (void)
595: {
596: HTEndLoop = 1;
597: }
598:
2.3 eric 599: PUBLIC HTEvent * HTEventList_lookup (SOCKET s, HTEventType type)
600: {
601: SockEvents * sockp = NULL;
602: if ((sockp = SockEvents_get(s, SockEvents_find)) == NULL)
603: return NULL;
604: return sockp->events[HTEvent_INDEX(type)];
605: }
606:
607: /* REGISTER DEFULT EVENT MANAGER
608: ** -----------------------------
609: ** Not done automaticly - may be done by application!
610: */
611: PUBLIC BOOL HTEventInit (void)
612: {
2.4 eric 613: #ifdef WWW_WIN_ASYNC
614: /*
615: ** We are here starting a hidden window to take care of events from
616: ** the async select() call in the async version of the event loop in
617: ** the Internal event manager (HTEvtLst.c)
618: */
619: static char className[] = "AsyncWindowClass";
620: WNDCLASS wc;
621: OSVERSIONINFO osInfo;
622:
623: wc.style=0;
624: wc.lpfnWndProc=(WNDPROC)AsyncWindowProc;
625: wc.cbClsExtra=0;
626: wc.cbWndExtra=0;
627: wc.hIcon=0;
628: wc.hCursor=0;
629: wc.hbrBackground=0;
630: wc.lpszMenuName=(LPSTR)0;
631: wc.lpszClassName=className;
632:
633: osInfo.dwOSVersionInfoSize = sizeof(osInfo);
634: GetVersionEx(&osInfo);
635: if (osInfo.dwPlatformId == VER_PLATFORM_WIN32s || osInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
636: wc.hInstance=GetModuleHandle(NULL); /* 95 and non threaded platforms */
637: else
638: wc.hInstance=GetCurrentProcess(); /* NT and hopefully everything following */
2.31 frystyk 639: HTinstance = wc.hInstance;
640: HTclass = RegisterClass(&wc);
641: if (!HTclass) {
2.4 eric 642: HTTrace("HTLibInit.. Can't RegisterClass \"%s\"\n", className);
643: return NO;
644: }
645: if (!(HTSocketWin = CreateWindow(className, "WWW_WIN_ASYNC", WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT,
646: CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, wc.hInstance,0))) {
647: char space[50];
648: HTTrace("HTLibInit.. Can't CreateWindow \"WWW_WIN_ASYNC\" - error:");
649: sprintf(space, "%ld\n", GetLastError());
650: HTTrace(space);
651: return NO;
652: }
653: HTwinMsg = WM_USER; /* use first available message since app uses none */
2.8 frystyk 654:
655: /*
656: ** Register platform specific timer handlers for windows
657: */
658: HTTimer_registerSetTimerCallback(Timer_setWindowsTimer);
659: HTTimer_registerDeleteTimerCallback(Timer_deleteWindowsTimer);
660:
2.4 eric 661: #endif /* WWW_WIN_ASYNC */
662:
663: #ifdef _WINSOCKAPI_
664: /*
665: ** Initialise WinSock DLL. This must also be shut down! PMH
666: */
667: {
668: WSADATA wsadata;
669: if (WSAStartup(DESIRED_WINSOCK_VERSION, &wsadata)) {
670: if (WWWTRACE)
671: HTTrace("HTEventInit. Can't initialize WinSoc\n");
672: WSACleanup();
673: return NO;
674: }
675: if (wsadata.wVersion < MINIMUM_WINSOCK_VERSION) {
676: if (WWWTRACE)
677: HTTrace("HTEventInit. Bad version of WinSoc\n");
678: WSACleanup();
679: return NO;
680: }
681: if (APP_TRACE)
682: HTTrace("HTEventInit. Using WinSoc version \"%s\".\n",
683: wsadata.szDescription);
684: }
685: #endif /* _WINSOCKAPI_ */
686:
2.3 eric 687: HTEvent_setRegisterCallback(HTEventList_register);
688: HTEvent_setUnregisterCallback(HTEventList_unregister);
689: return YES;
690: }
691:
692: PUBLIC BOOL HTEventTerminate (void)
693: {
2.19 frystyk 694: #ifdef _WINSOCKAPI_
695: WSACleanup();
696: #endif /* _WINSOCKAPI_ */
2.31 frystyk 697:
698: #ifdef WWW_WIN_ASYNC
699: DestroyWindow(HTSocketWin);
700: UnregisterClass((LPCTSTR)HTclass, HTinstance);
701: #endif /* WWW_WIN_ASYNC */
702:
2.3 eric 703: return YES;
704: }
705:
706: #ifdef WWW_WIN_ASYNC
707:
2.4 eric 708: /* HTEventList_get/setWinHandle
2.3 eric 709: ** --------------------------
710: ** Managing the windows handle on Windows
711: */
712: PUBLIC BOOL HTEventList_setWinHandle (HWND window, unsigned long message)
713: {
714: HTSocketWin = window;
715: HTwinMsg = message;
716: return YES;
717: }
718:
719: PUBLIC HWND HTEventList_getWinHandle (unsigned long * pMessage)
720: {
721: if (pMessage)
722: *pMessage = HTwinMsg;
723: return (HTSocketWin);
724: }
725:
726: /* only responsible for WM_TIMER and WSA_AsyncSelect */
727: PUBLIC LRESULT CALLBACK AsyncWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
728: {
729: WORD event;
730: SOCKET sock;
2.4 eric 731: HTEventType type;
2.5 eric 732: ms_t now = HTGetTimeInMillis();
2.3 eric 733:
734: /* timeout stuff */
735: if (uMsg == WM_TIMER) {
2.4 eric 736: HTTimer_dispatch((HTTimer *)wParam);
2.3 eric 737: return (0);
738: }
739:
740: if (uMsg != HTwinMsg) /* not our async message */
741: return (DefWindowProc(hwnd, uMsg, wParam, lParam));
742:
743: event = LOWORD(lParam);
744: sock = (SOCKET)wParam;
2.4 eric 745: switch (event) {
746: case FD_READ: type = HTEvent_READ; break;
747: case FD_WRITE: type = HTEvent_WRITE; break;
748: case FD_ACCEPT: type = HTEvent_ACCEPT; break;
749: case FD_CONNECT: type = HTEvent_CONNECT; break;
750: case FD_OOB: type = HTEvent_OOB; break;
751: case FD_CLOSE: type = HTEvent_CLOSE; break;
2.20 frystyk 752: default: HTDebugBreak(__FILE__, __LINE__, "Unknown event %d\n", event);
2.4 eric 753: }
2.5 eric 754: if (HTEventList_dispatch((int)sock, type, now) != HT_OK)
2.4 eric 755: HTEndLoop = -1;
2.3 eric 756: return (0);
757: }
758:
2.25 frystyk 759: PUBLIC int HTEventList_newLoop (void)
760: {
761: return HTEventList_loop (NULL);
762: }
763:
2.4 eric 764: PUBLIC int HTEventList_loop (HTRequest * theRequest )
2.3 eric 765: {
766: MSG msg;
2.25 frystyk 767: int status;
2.30 frystyk 768: while (!HTEndLoop && GetMessage(&msg,0,0,0)) {
2.3 eric 769: TranslateMessage(&msg);
770: DispatchMessage(&msg);
771: }
2.25 frystyk 772:
773: status = HTEndLoop;
774:
775: /* Reset HTEndLoop in case we want to start again */
776: HTEndLoop = 0;
777:
778: return (status == 1 ? HT_OK : HT_ERROR);
2.3 eric 779: }
780:
781: #else /* WWW_WIN_ASYNC */
782:
2.25 frystyk 783: PUBLIC int HTEventList_newLoop (void)
784: {
785: return HTEventList_loop (NULL);
786: }
787:
2.1 frystyk 788: /*
789: ** We wait for activity from one of our registered
790: ** channels, and dispatch on that.
791: **
792: ** There are now two versions of the event loop. The first is if you want
793: ** to use async I/O on windows, and the other is if you want to use normal
794: ** Unix setup with sockets
795: */
796: PUBLIC int HTEventList_loop (HTRequest * theRequest)
797: {
798: fd_set treadset, twriteset, texceptset;
799: struct timeval waittime, * wt;
800: int active_sockets;
801: int maxfds;
2.5 eric 802: ms_t timeout;
803: ms_t now;
2.1 frystyk 804: SOCKET s;
2.5 eric 805: int status = HT_OK;
2.1 frystyk 806:
2.2 eric 807: EventOrderList = HTList_new(); /* is kept around until EventOrder_deleteAll */
808:
2.1 frystyk 809: /* Don't leave this loop until we leave the application */
2.27 frystyk 810: while (!HTEndLoop) {
2.1 frystyk 811: treadset = FdArray[HTEvent_INDEX(HTEvent_READ)];
812: twriteset = FdArray[HTEvent_INDEX(HTEvent_WRITE)];
813: texceptset = FdArray[HTEvent_INDEX(HTEvent_OOB)];
814:
815: /*
816: ** Timeval struct copy needed for linux, as it set the value to the
817: ** remaining timeout while exiting the select. (and perhaps for
818: ** other OS). Code borrowed from X server.
819: */
820: wt = NULL;
2.5 eric 821: if ((status = HTTimer_next(&timeout)))
822: return status;
823: if (timeout != 0) {
2.1 frystyk 824: waittime.tv_sec = timeout / MILLI_PER_SECOND;
825: waittime.tv_usec = (timeout % MILLI_PER_SECOND) *
826: (1000000 / MILLI_PER_SECOND);
827: wt = &waittime;
828: }
829:
2.18 frystyk 830: maxfds = MaxSock;
831: if (THD_TRACE) HTTrace("Event Loop.. calling select: maxfds is %d\n", maxfds);
832:
833: #ifdef EVENT_TRACE
2.15 frystyk 834: #define HT_FS_BYTES(a) ((((a)/16)+1) * 4)
835: HTTraceData((char*)&treadset, HT_FS_BYTES(maxfds), "HTEventList_loop pre treadset: (maxfd:%d)", maxfds);
836: HTTraceData((char*)&twriteset, HT_FS_BYTES(maxfds), "HTEventList_loop pre twriteset:");
837: HTTraceData((char*)&texceptset, HT_FS_BYTES(maxfds), "HTEventList_loop pre texceptset:");
2.18 frystyk 838: #endif /* EVENT_TRACE */
2.15 frystyk 839:
2.1 frystyk 840: #ifdef __hpux
841: active_sockets = select(maxfds+1, (int *)&treadset, (int *)&twriteset,
842: (int *)&texceptset, wt);
843: #else
844: active_sockets = select(maxfds+1, &treadset, &twriteset, &texceptset, wt);
845: #endif
2.2 eric 846:
2.5 eric 847: now = HTGetTimeInMillis();
2.18 frystyk 848:
849: #ifdef EVENT_TRACE
2.15 frystyk 850: HTTraceData((char*)&treadset, HT_FS_BYTES(maxfds), "HTEventList_loop post treadset: (active_sockets:%d)", active_sockets);
851: HTTraceData((char*)&twriteset, HT_FS_BYTES(maxfds), "HTEventList_loop post twriteset: (errno:%d)", errno);
852: HTTraceData((char*)&texceptset, HT_FS_BYTES(maxfds), "HTEventList_loop post texceptset:");
2.18 frystyk 853: #endif /* EVENT_TRACE */
2.2 eric 854:
2.1 frystyk 855: if (THD_TRACE) HTTrace("Event Loop.. select returns %d\n", active_sockets);
856:
857: if (active_sockets == -1) {
2.20 frystyk 858: #ifdef EINTR
859: if (socerrno == EINTR) {
860: /*
861: ** EINTR The select() function was interrupted before any
862: ** of the selected events occurred and before the
863: ** timeout interval expired.
864: **
865: ** If SA_RESTART has been set for the interrupting
866: ** signal, it is implementation-dependent whether
867: ** select() restarts or returns with EINTR.
868: */
869: if (THD_TRACE)
870: HTTrace("Event Loop.. select was interruted - try again\n");
871: continue;
872: }
873: #endif /* EINTR */
2.28 frystyk 874: #ifdef EBADF
875: if (socerrno == EBADF) {
876: /*
877: ** EBADF One or more of the file descriptor sets specified
878: ** a file descriptor that is not a valid open file
879: ** descriptor.
880: */
2.29 frystyk 881: if (THD_TRACE)
2.28 frystyk 882: HTTrace("Event Loop.. One or more sockets were not through their connect phase - try again\n");
883: continue;
884: }
885: #endif
2.24 frystyk 886: if (THD_TRACE) HTTrace("Event Loop.. select returned error %d\n", socerrno);
2.3 eric 887: EventList_dump();
2.1 frystyk 888: return HT_ERROR;
889: }
890:
891: /*
892: ** We had a timeout so now we check and see if we have a timeout
2.5 eric 893: ** handler to call. Let HTTimer_next get it.
2.1 frystyk 894: */
2.5 eric 895: if (active_sockets == 0)
2.1 frystyk 896: continue;
897:
898: /*
899: ** There were active sockets. Determine which fd sets they were in
900: */
2.2 eric 901: #ifdef HT_EVENT_ORDER
2.5 eric 902: #define DISPATCH(socket, type, now) EventOrder_add(socket, type, now)
2.2 eric 903: #else /* HT_EVENT_ORDER */
2.5 eric 904: #define DISPATCH(socket, type, now) HTEventList_dispatch(socket, type, now)
2.2 eric 905: #endif /* !HT_EVENT_ORDER */
2.1 frystyk 906: for (s = 0 ; s <= maxfds ; s++) {
907: if (FD_ISSET(s, &texceptset))
2.5 eric 908: if ((status = DISPATCH(s, HTEvent_OOB, now)) != HT_OK)
2.1 frystyk 909: return status;
910: if (FD_ISSET(s, &twriteset))
2.5 eric 911: if ((status = DISPATCH(s, HTEvent_WRITE, now)) != HT_OK)
2.1 frystyk 912: return status;
913: if (FD_ISSET(s, &treadset))
2.5 eric 914: if ((status = DISPATCH(s, HTEvent_READ, now)) != HT_OK)
2.1 frystyk 915: return status;
916: }
2.2 eric 917: #ifdef HT_EVENT_ORDER
918: if ((status = EventOrder_executeAndDelete()) != HT_OK)
919: return status;
920: #endif /* HT_EVENT_ORDER */
2.27 frystyk 921: };
2.25 frystyk 922:
923: /* Reset HTEndLoop in case we want to start again */
924: HTEndLoop = 0;
2.1 frystyk 925:
926: return HT_OK;
927: }
928:
2.3 eric 929: #endif /* !WWW_WIN_ASYNC */
2.12 eric 930:
931: PUBLIC void CheckSockEvent(HTTimer * timer, HTTimerCallback * cbf, void * param)
932: {
933: SockEvents * sockp = (SockEvents *)param;
934: if (cbf == EventListTimerHandler &&
935: sockp->timeouts[0] != timer &&
936: sockp->timeouts[1] != timer &&
937: sockp->timeouts[2] != timer) {
2.20 frystyk 938: HTDebugBreak(__FILE__, __LINE__, "Bad timer %p\n", timer);
2.12 eric 939: }
940: }
941:
Webmaster