Commit a17bd09dfed67e5ef9f08ca90a9053f5db3382d3
1 parent
83ee50f9
create explicit event done type and reissue event is no handler has returned EVENT_DONE.
Showing
4 changed files
with
43 additions
and
31 deletions
... | ... | @@ -35,7 +35,7 @@ TR_CLASS(TR_EventHandler); |
35 | 35 | |
36 | 36 | #include "tr/event_dispatcher.h" |
37 | 37 | |
38 | -typedef int (* TR_EventMethod_fptr)(TR_EventHandler, TR_Event); | |
38 | +typedef TR_EventDone (* TR_EventMethod_fptr)(TR_EventHandler, TR_Event); | |
39 | 39 | |
40 | 40 | TR_CLASS(TR_EventHandler) { |
41 | 41 | TR_EventDispatcher dispatcher[10]; |
... | ... | @@ -46,9 +46,9 @@ TR_CLASSVARS_DECL(TR_EventHandler) { |
46 | 46 | TR_Hash event_methods; |
47 | 47 | }; |
48 | 48 | |
49 | -void TR_eventHandlerSetDispatcher(TR_EventHandler, TR_EventDispatcher); | |
50 | -void TR_eventHandlerIssueEvent(TR_EventHandler, TR_Event); | |
51 | -int TR_eventHandlerHandleEvent(TR_EventHandler, TR_Event); | |
49 | +void TR_eventHandlerSetDispatcher(TR_EventHandler, TR_EventDispatcher); | |
50 | +void TR_eventHandlerIssueEvent(TR_EventHandler, TR_Event); | |
51 | +TR_EventDone TR_eventHandlerHandleEvent(TR_EventHandler, TR_Event); | |
52 | 52 | |
53 | 53 | #define TR_eventHandlerClassCleanup(cname) \ |
54 | 54 | (TR__eventHandlerClassCleanup(TR_CLASS_BY_NAME(cname))) |
... | ... | @@ -68,7 +68,6 @@ void TR__eventHandlerClassCleanup(TR_class_ptr); |
68 | 68 | sizeof(TR_EventMethod_fptr))); \ |
69 | 69 | } while(0) |
70 | 70 | |
71 | - | |
72 | 71 | #endif // __TR_EVENT_HANDLER_H__ |
73 | 72 | |
74 | 73 | // vim: set ts=4 sw=4: | ... | ... |
... | ... | @@ -38,7 +38,9 @@ TR_eventDispatcherStart(TR_EventDispatcher this) |
38 | 38 | while (this->running || (! TR_queueEmpty(this->events))) { |
39 | 39 | struct timespec tp; |
40 | 40 | int now; // milliseconds |
41 | - TR_Event current = NULL; | |
41 | + TR_Event event; | |
42 | + TR_Queue handler_queue; | |
43 | + TR_HashValue handler_queue_hv; | |
42 | 44 | |
43 | 45 | clock_gettime(CLOCK_REALTIME, &tp); |
44 | 46 | now = tp.tv_sec * 1000 + tp.tv_nsec / 1000000; |
... | ... | @@ -55,43 +57,49 @@ TR_eventDispatcherStart(TR_EventDispatcher this) |
55 | 57 | |
56 | 58 | if (TR_queueEmpty(this->events)) { |
57 | 59 | if (TR_EVD_CLIENT == this->mode) { |
58 | - current = TR_eventSubjectEmit( | |
60 | + event = TR_eventSubjectEmit( | |
59 | 61 | (TR_EventSubject)this, |
60 | 62 | TR_DISPATCHER_EVENT_USER_WAIT, |
61 | 63 | NULL); |
62 | 64 | } else { |
63 | - current = TR_eventSubjectEmit( | |
65 | + event = TR_eventSubjectEmit( | |
64 | 66 | (TR_EventSubject)this, |
65 | 67 | TR_DISPATCHER_EVENT_DATA_WAIT, |
66 | 68 | NULL); |
67 | 69 | } |
68 | 70 | } else { |
69 | - current = TR_queueGet(this->events); | |
71 | + event = TR_queueGet(this->events); | |
70 | 72 | } |
71 | 73 | |
72 | - if (current) { | |
73 | - TR_Queue handler_queue; | |
74 | - TR_HashValue handler_queue_hv = TR_hashGetByVal( | |
75 | - this->handler, | |
76 | - TR_sdbm( | |
77 | - (unsigned char *)&(current->id), | |
78 | - sizeof(current->id))); | |
79 | - | |
80 | - handler_queue = handler_queue_hv | |
81 | - ? *(TR_Queue *)handler_queue_hv->value | |
82 | - : NULL; | |
83 | - | |
84 | - if (handler_queue && ! TR_queueEmpty(handler_queue)) { | |
85 | - TR_Queue queue_node = handler_queue->first; | |
86 | - | |
87 | - while (queue_node) { | |
88 | - TR_EventHandler handler = queue_node->msg; | |
89 | - if (TR_eventHandlerHandleEvent(handler, current)) break; | |
90 | - queue_node = queue_node->next; | |
91 | - } | |
74 | + handler_queue_hv = TR_hashGetByVal( | |
75 | + this->handler, | |
76 | + TR_sdbm( | |
77 | + (unsigned char *)&(event->id), | |
78 | + sizeof(event->id))); | |
79 | + | |
80 | + handler_queue = handler_queue_hv | |
81 | + ? *(TR_Queue *)handler_queue_hv->value | |
82 | + : NULL; | |
83 | + | |
84 | + if (handler_queue && ! TR_queueEmpty(handler_queue)) { | |
85 | + TR_Queue queue_node = handler_queue->first; | |
86 | + TR_EventDone done; | |
87 | + | |
88 | + while (queue_node) { | |
89 | + TR_EventHandler handler = queue_node->msg; | |
90 | + TR_EventDone this_done; | |
91 | + | |
92 | + this_done = TR_eventHandlerHandleEvent(handler, event); | |
93 | + done = TR_EVENT_DONE == done ? done : this_done; | |
94 | + | |
95 | + queue_node = queue_node->next; | |
92 | 96 | } |
93 | 97 | |
94 | - TR_delete(current); | |
98 | + if (TR_EVENT_DONE == done) { | |
99 | + TR_delete(event); | |
100 | + } else { | |
101 | + TR_eventDispatcherEnqueueEvent(this, event); | |
102 | + } | |
95 | 103 | } |
96 | 104 | } |
97 | 105 | } | ... | ... |
Please
register
or
login
to post a comment