Debian Codegraph @

protolib/src/common/protoDispatcher.cpp

raw
1 /*********************************************************************
2 *
3 * AUTHORIZATION TO USE AND DISTRIBUTE
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that:
7 *
8 * (1) source code distributions retain this paragraph in its entirety,
9 *
10 * (2) distributions including binary code include this paragraph in
11 * its entirety in the documentation or other materials provided
12 * with the distribution, and
13 *
14 * (3) all advertising materials mentioning features or use of this
15 * software display the following acknowledgment:
16 *
17 * "This product includes software written and developed
18 * by Brian Adamson and Joe Macker of the Naval Research
19 * Laboratory (NRL)."
20 *
21 * The name of NRL, the name(s) of NRL employee(s), or any entity
22 * of the United States Government may not be used to endorse or
23 * promote products derived from this software, nor does the
24 * inclusion of the NRL written and developed software directly or
25 * indirectly suggest NRL or United States Government endorsement
26 * of this product.
27 *
28 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
30 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
31 ********************************************************************/
32
33 /**
34 * @file protoDispatcher.cpp
35 *
36 * @brief This class provides a core around which Unix and Win32 applications using Protolib can be implemented
37 */
38
39 #include "protoDispatcher.h"
40
41 #include <stdio.h>
42 #include <string.h>
43
44 #ifdef WIN32
45 #include <TCHAR.h>
46 #ifndef _WIN32_WCE
47 #include <process.h> // for _beginthreadex() and _endthreadex()
48 #endif // _WIN32_WCE
49 const ProtoDispatcher::Descriptor ProtoDispatcher::INVALID_DESCRIPTOR = INVALID_HANDLE_VALUE;
50 #else
51 #include <unistd.h>
52 #include <sys/time.h>
53 #include <sys/select.h>
54 #include <fcntl.h>
55 #ifdef HAVE_SCHED
56 #include <sched.h>
57 #else
58 #include <sys/resource.h>
59 #endif // HAVE_SCHED
60 const ProtoDispatcher::Descriptor ProtoDispatcher::INVALID_DESCRIPTOR = -1;
61 #endif // if/else WIN32/UNIX
62
63
64 ProtoDispatcher::Stream::Stream(Type theType)
65 : type(theType)
66 #ifdef WIN32
67 ,index(-1), outdex(-1)
68 #endif // WIN32
69 {
70 }
71
72 ProtoDispatcher::SocketStream::SocketStream(ProtoSocket& theSocket)
73 : Stream(SOCKET), socket(&theSocket)
74 {
75 }
76
77 ProtoDispatcher::ChannelStream::ChannelStream(ProtoChannel& theChannel)
78 : Stream(CHANNEL), channel(&theChannel)
79 {
80 }
81
82 ProtoDispatcher::TimerStream::TimerStream()
83 : Stream(TIMER), descriptor(INVALID_DESCRIPTOR)
84 {
85 }
86
87 ProtoDispatcher::TimerStream::~TimerStream()
88 {
89 #ifdef UNIX
90 if (INVALID_DESCRIPTOR != descriptor)
91 {
92 close(descriptor);
93 descriptor = INVALID_DESCRIPTOR;
94 }
95 #endif // UNIX
96 }
97
98 ProtoDispatcher::EventStream::EventStream(ProtoEvent& theEvent)
99 : Stream(EVENT), event(&theEvent)
100 {
101 }
102
103 ProtoDispatcher::EventStream::~EventStream()
104 {
105 }
106
107 ProtoDispatcher::GenericStream::GenericStream(Descriptor theDescriptor)
108 : Stream(GENERIC), myself(this), descriptor(theDescriptor), callback(NULL), client_data(NULL)
109 {
110 }
111
112 ProtoDispatcher::ProtoDispatcher()
113 : run(false), wait_status(-1), exit_code(0), timer_delay(-1), precise_timing(false),
114 thread_id((ThreadId)(NULL)), external_thread(false), priority_boost(false),
115 thread_started(false), thread_signaled(false), thread_master((ThreadId)(NULL)),
116 suspend_count(0), signal_count(0), controller(NULL),
117 prompt_set(false), prompt_callback(NULL), prompt_client_data(NULL),
118 break_stream(break_event)
119 #ifdef WIN32
120 ,stream_handles_array(NULL), stream_ptrs_array(NULL),
121 stream_array_size(0), stream_count(0), msg_window(NULL),
122 actual_thread_handle(NULL)
123 #ifdef USE_WAITABLE_TIMER
124 ,timer_active(false)
125 #endif // USE_WAITABLE_TIMER
126 #else // UNIX
127 #ifdef USE_TIMERFD
128 ,timer_fd(-1)
129 #endif // USE_TIMERFD
130 #if defined(USE_SELECT)
131 // no special initialization required
132 #elif defined(USE_KQUEUE)
133 ,kevent_queue(-1)
134 #elif defined(USE_EPOLL)
135 ,epoll_fd(-1)
136 #else
137 #error "undefined async i/o mechanism" // to make sure we implement something
138 #endif // if/else USE_SELECT / USE_KQUEUE / USE_EPOLL
139 #endif // if/else WIN32/UNIX
140 {
141 }
142
143 ProtoDispatcher::~ProtoDispatcher()
144 {
145 Destroy();
146 }
147
148 void ProtoDispatcher::Destroy()
149 {
150 Stop();
151
152 // Iterate through stream_table and disable notification
153 // (TBD - could we skip this and just call stream_list.Destroy()?)
154 Stream* stream;
155 StreamTable::Iterator iterator(stream_table);
156 while (NULL != (stream = iterator.GetNextItem()))
157 {
158 switch (stream->GetType())
159 {
160 case Stream::CHANNEL:
161 static_cast<ChannelStream*>(stream)->GetChannel().SetNotifier(NULL);
162 break;
163 case Stream::SOCKET:
164 static_cast<SocketStream*>(stream)->GetSocket().SetNotifier(NULL);
165 break;
166 case Stream::GENERIC:
167 ReleaseGenericStream(static_cast<GenericStream&>(*stream));
168 break;
169 case Stream::TIMER:
170 // No timer streams are put in the stream_table (yet)
171
172 break;
173 case Stream::EVENT:
174 ReleaseEventStream(static_cast<EventStream&>(*stream));
175 break;
176 }
177 }
178 ASSERT(stream_table.IsEmpty());
179 channel_stream_pool.Destroy();
180 socket_stream_pool.Destroy();
181 generic_stream_pool.Destroy();
182
183 #ifdef WIN32
184 if (NULL != stream_handles_array)
185 {
186 delete[] stream_handles_array;
187 delete[] stream_ptrs_array;
188 stream_array_size = 0;
189 }
190 stream_count = 0;
191 Win32Cleanup();
192 #endif // WIN32
193 } // end ProtoDispatcher::Destroy()
194
195 bool ProtoDispatcher::UpdateChannelNotification(ProtoChannel& theChannel,
196 int notifyFlags)
197 {
198 SignalThread();
199
200 // Find stream in our "wait" list, or add it to the list ...
201 ChannelStream* channelStream = GetChannelStream(theChannel);
202
203 if (NULL != channelStream)
204 {
205
206 if ((channelStream->GetNotifyFlags() == notifyFlags) && (0 != notifyFlags))
207 {
208 // no notification change needed, but maybe input/output ready change below
209 ASSERT(0 != notifyFlags);
210 }
211 else if (0 != notifyFlags)
212 {
213 // Determine what notification changes are needed for this stream and make them
214 if (0 != (notifyFlags & ProtoChannel::NOTIFY_INPUT))
215 {
216 if (!channelStream->IsInput()) // Enable system-specific input notification
217 {
218 if (!UpdateStreamNotification(*channelStream, ENABLE_INPUT))
219 {
220 PLOG(PL_ERROR, "ProtoDispatcher::UpdateChannelNotification() error: unable to ENABLE_INPUT!\n");
221 UnsignalThread();
222 return false;
223 }
224 }
225 }
226 else
227 {
228 if (channelStream->IsInput()) // Disable system-specific input notification
229 {
230 if (!UpdateStreamNotification(*channelStream, DISABLE_INPUT))
231 {
232 PLOG(PL_ERROR, "ProtoDispatcher::UpdateChannelNotification() error: unable to DISABLE_INPUT!\n");
233 UnsignalThread();
234 return false;
235 }
236 }
237 }
238 if (0 != (notifyFlags & ProtoChannel::NOTIFY_OUTPUT))
239 {
240 if (!channelStream->IsOutput()) // Enable system-specific input notification
241 {
242 if (!UpdateStreamNotification(*channelStream, ENABLE_OUTPUT))
243 {
244 PLOG(PL_ERROR, "ProtoDispatcher::UpdateChannelNotification() error: unable to ENABLE_OUTPUT!\n");
245 UnsignalThread();
246 return false;
247 }
248 }
249 }
250 else
251 {
252 if (channelStream->IsOutput()) // Disable system-specific output notification
253 {
254 if (!UpdateStreamNotification(*channelStream, DISABLE_OUTPUT))
255 {
256 PLOG(PL_ERROR, "ProtoDispatcher::UpdateChannelNotification() error: unable to DISABLE_OUTPUT!\n");
257 UnsignalThread();
258 return false;
259 }
260 }
261 }
262 // TBD - support exception notification ???
263 }
264 else // 0 == notifyFlags
265 {
266 if (channelStream->HasNotifyFlags())
267 {
268 if (!UpdateStreamNotification(*channelStream, DISABLE_ALL))
269 {
270 PLOG(PL_ERROR, "ProtoDispatcher::UpdateChannelNotification() error: unable to DISABLE_ALL!\n");
271 UnsignalThread();
272 return false;
273 }
274 }
275 ReleaseChannelStream(*channelStream);
276 UnsignalThread();
277 return true;
278 } // end if/else 0 != notifyFlags
279
280 #ifdef WIN32
281 // Add or remove channelStream from "ready_stream_list" as appropriate
282 // Note input/output readiness may have changed even if notification did not
283 if ((theChannel.IsInputReady() && channelStream->IsInput()) ||
284 (theChannel.IsOutputReady() && channelStream->IsOutput()))
285 {
286 // Make sure our "ready" socket is in the "ready_stream_list"
287 if (!ready_stream_list.Contains(*channelStream))
288 {
289 if (!ready_stream_list.Append(*channelStream))
290 {
291 ReleaseChannelStream(*channelStream);
292 PLOG(PL_ERROR, "ProtoDispatcher::UpdateChannelNotification() error: unable to append ready channel!\n");
293 UnsignalThread();
294 return false;
295
296 }
297 }
298 }
299 else if (ready_stream_list.Contains(*channelStream))
300 {
301 // Remove from "ready_socket_list"
302 ready_stream_list.Remove(*channelStream);
303 }
304 #endif // WIN32
305
306 UnsignalThread();
307 return true;
308 }
309 else
310 {
311 PLOG(PL_ERROR, "ProtoDispatcher::UpdateChannelNotification() new ChannelStream error: %s\n",
312 GetErrorString());
313 UnsignalThread();
314 return false;
315 }
316 } // end ProtoDispatcher::UpdateChannelNotification()
317
318 bool ProtoDispatcher::UpdateEventNotification(ProtoEvent& theEvent,
319 int notifyFlags)
320 {
321 SignalThread(); // TBD- check result?
322 EventStream* eventStream = GetEventStream(theEvent);
323 if (NULL != eventStream)
324 {
325 if (eventStream->GetNotifyFlags() == notifyFlags)
326 {
327 if (0 == notifyFlags)
328 {
329 ReleaseEventStream(*eventStream);
330 UnsignalThread();
331 return true;
332 }
333 }
334 else if (0 != notifyFlags)
335 {
336 // ProtoEvents only have NOTIFY_INPUT
337 ASSERT(!eventStream->NotifyFlagIsSet(EventStream::NOTIFY_INPUT));
338 if (!UpdateStreamNotification(*eventStream, ENABLE_INPUT))
339 {
340 PLOG(PL_ERROR, "ProtoDispatcher::UpdateEventNotification() error: unable to ENABLE_INPUT!\n");
341 UnsignalThread();
342 return false;
343 }
344 }
345 else // 0 == notifyFlags
346 {
347 if (!UpdateStreamNotification(*eventStream, DISABLE_INPUT))
348 {
349 PLOG(PL_ERROR, "ProtoDispatcher::UpdateEventNotification() error: unable to DISABLE_INPUT!\n");
350 UnsignalThread();
351 return false;
352 }
353 ASSERT(0 == eventStream->GetNotifyFlags());
354 ReleaseEventStream(*eventStream);
355 }
356 }
357 else
358 {
359 PLOG(PL_ERROR, "ProtoDispatcher::UpdateEventNotification() new EventStream error: %s\n",
360 GetErrorString());
361 UnsignalThread();
362 return false;
363 } // end if/else (NULL != eventStream)
364 UnsignalThread();
365 return true;
366 } // end ProtoDispatcher::UpdateEventNotification()
367
368 bool ProtoDispatcher::UpdateSocketNotification(ProtoSocket& theSocket,
369 int notifyFlags)
370 {
371 SignalThread(); // TBD - check result?
372 SocketStream* socketStream = GetSocketStream(theSocket);
373 if (NULL != socketStream)
374 {
375 if (socketStream->GetNotifyFlags() == notifyFlags)
376 {
377 // nothing to do up here, notification unchanged
378 // (but for WIN32, socket "readiness" may have changed)
379 // _unless_ it's a new (closed) socket or idle tcp socket
380 // TBD - should prob not be calling "UpdateSocketNotification()" in this case
381 if (0 == notifyFlags)
382 {
383
384 ReleaseSocketStream(*socketStream);
385 UnsignalThread();
386 return true;
387 }
388 }
389 else if (0 != notifyFlags)
390 {
391 #ifdef WIN32
392 // WIN32 Sockets are handled differently
393 int index = socketStream->GetIndex();
394 if (index < 0)
395 {
396 if ((index = Win32AddStream(*socketStream, theSocket.GetInputEventHandle())) < 0)
397 {
398 PLOG(PL_ERROR, "ProtoDispatcher::UpdateSocketNotification() error adding handle\n");
399 ReleaseSocketStream(*socketStream);
400 UnsignalThread();
401 return false;
402 }
403 socketStream->SetIndex(index);
404 }
405 long eventMask = 0;
406 if (0 != (notifyFlags & ProtoSocket::NOTIFY_INPUT))
407 eventMask |= (FD_READ | FD_ACCEPT | FD_CLOSE);
408 if (0 != (notifyFlags & ProtoSocket::NOTIFY_OUTPUT))
409 {
410 eventMask |= (FD_WRITE | FD_CONNECT | FD_CLOSE);
411 }
412 if (0 != (notifyFlags & ProtoSocket::NOTIFY_EXCEPTION))
413 eventMask |= FD_ADDRESS_LIST_CHANGE;
414 // Note for IPv4/IPv6 sockets, the "input event handle" is used for both input & output
415 if (0 != WSAEventSelect(theSocket.GetHandle(), theSocket.GetInputEventHandle(), eventMask))
416 {
417 ReleaseSocketStream(*socketStream);
418 PLOG(PL_ERROR, "ProtoDispatcher::UpdateSocketNotification() WSAEventSelect(0x%x) error: %s\n",
419 eventMask, ProtoSocket::GetErrorString());
420 UnsignalThread();
421 return false;
422 }
423 socketStream->SetNotifyFlags(notifyFlags);
424 #else // UNIX
425 // Determine what notification changes are needed for this stream and make them
426 if (0 != (notifyFlags & ProtoSocket::NOTIFY_INPUT))
427 {
428 if (!socketStream->IsInput()) // Enable system-specific input notification
429 {
430 if (!UpdateStreamNotification(*socketStream, ENABLE_INPUT))
431 {
432 PLOG(PL_ERROR, "ProtoDispatcher::UpdateSocketNotification() error: unable to ENABLE_INPUT!\n");
433 UnsignalThread();
434 return false;
435 }
436 }
437 }
438 else
439 {
440 if (socketStream->IsInput()) // Disable system-specific input notification
441 {
442 if (!UpdateStreamNotification(*socketStream, DISABLE_INPUT))
443 {
444 PLOG(PL_ERROR, "ProtoDispatcher::UpdateSocketNotification() error: unable to DISABLE_INPUT!\n");
445 UnsignalThread();
446 return false;
447 }
448 }
449 }
450 if (0 != (notifyFlags & ProtoSocket::NOTIFY_OUTPUT))
451 {
452 if (!socketStream->IsOutput()) // Enable system-specific input notification
453 {
454 if (!UpdateStreamNotification(*socketStream, ENABLE_OUTPUT))
455 {
456 PLOG(PL_ERROR, "ProtoDispatcher::UpdateSocketNotification() error: unable to ENABLE_OUTPUT!\n");
457 UnsignalThread();
458 return false;
459 }
460 }
461 }
462 else
463 {
464 if (socketStream->IsOutput()) // Disable system-specific output notification
465 {
466 if (!UpdateStreamNotification(*socketStream, DISABLE_OUTPUT))
467 {
468 PLOG(PL_ERROR, "ProtoDispatcher::UpdateSocketNotification() error: unable to DISABLE_OUTPUT!\n");
469 UnsignalThread();
470 return false;
471 }
472 }
473 }
474 // TBD - support exception notification ???
475 #endif // if/else WIN32/UNIX
476 }
477 else // 0 == notifyFlags
478 {
479 ASSERT(socketStream->HasNotifyFlags());
480 #ifdef WIN32
481 if (0 != WSAEventSelect(theSocket.GetHandle(), theSocket.GetInputEventHandle(), 0))
482 PLOG(PL_WARN, "ProtoDispatcher::UpdateSocketNotification() WSAEventSelect(0) warning: %s\n",
483 ProtoSocket::GetErrorString());
484 #endif // WIN32
485 if (!UpdateStreamNotification(*socketStream, DISABLE_ALL))
486 {
487 PLOG(PL_ERROR, "ProtoDispatcher::UpdateSocketNotification() error: unable to DISABLE_ALL!\n");
488 UnsignalThread();
489 return false;
490 }
491 ReleaseSocketStream(*socketStream);
492 UnsignalThread();
493 return true;
494 } // end if/else 0 != notifyFlags
495 #ifdef WIN32
496 // Add or remove socketStream from "ready_socket_list" as appropriate
497 // Note input/output readiness may have changed even if notification did not
498 if ((theSocket.IsInputReady() && socketStream->IsInput()) ||
499 (theSocket.IsOutputReady() && socketStream->IsOutput()))
500 {
501 // Make sure our "ready" socket is in the "ready_stream_list"
502 if (!ready_stream_list.Contains(*socketStream))
503 {
504 if (!ready_stream_list.Append(*socketStream))
505 {
506 ReleaseSocketStream(*socketStream);
507 PLOG(PL_ERROR, "ProtoDispatcher::UpdateSocketNotification() error: unable to append ready socket!\n");
508 UnsignalThread();
509 return false;
510
511 }
512 }
513 }
514 else if (ready_stream_list.Contains(*socketStream))
515 {
516 // Remove from "ready_stream_list"
517 ready_stream_list.Remove(*socketStream);
518 }
519 #endif // WIN32
520 UnsignalThread();
521 return true;
522 }
523 else
524 {
525 PLOG(PL_ERROR, "ProtoDispatcher::UpdateSocketNotification() new SocketStream error: %s\n",
526 GetErrorString());
527 UnsignalThread();
528 return false;
529 }
530 } // end ProtoDispatcher::UpdateSocketNotification()
531
532 ProtoDispatcher::SocketStream* ProtoDispatcher::GetSocketStream(ProtoSocket& theSocket)
533 {
534 // First, search our list of active sockets
535 ProtoSocket* socketPtr = &theSocket;
536 SocketStream* socketStream =
537 static_cast<SocketStream*>(stream_table.Find((const char*)&socketPtr, sizeof(ProtoSocket*) << 3));
538 if (NULL == socketStream)
539 {
540 // Get one from the pool or create a new one
541 socketStream = socket_stream_pool.Get();
542 if (NULL != socketStream)
543 {
544 socketStream->ClearNotifyFlags();
545 socketStream->SetSocket(theSocket);
546 }
547 else
548 {
549 if (!(socketStream = new SocketStream(theSocket)))
550 {
551 PLOG(PL_ERROR, "ProtoDispatcher::GetSocketStream() new SocketStream error: %s\n", GetErrorString());
552 return NULL;
553 }
554 }
555 // Insert into "active" stream list
556 stream_table.Insert(*socketStream);
557 }
558 return socketStream;
559 } // end ProtoDispatcher::GetSocketStream()
560
561 void ProtoDispatcher::ReleaseSocketStream(SocketStream& socketStream)
562 {
563 #ifdef WIN32
564 if (ready_stream_list.Contains(socketStream))
565 ready_stream_list.Remove(socketStream);
566 // Makes sure the channel input/output event HANDLE is removed from array
567 // (This removes it from list passed to MsgWaitForMultipleObjectsEx() call)
568 if (socketStream.GetIndex() >= 0)
569 {
570 Win32RemoveStream(socketStream.GetIndex());
571 socketStream.SetIndex(-1);
572 }
573 if (socketStream.GetOutdex() >= 0)
574 {
575 Win32RemoveStream(socketStream.GetOutdex());
576 socketStream.SetOutdex(-1);
577 }
578 #endif // WIN32
579 if (socketStream.HasNotifyFlags())
580 {
581 if (!UpdateStreamNotification(socketStream, DISABLE_ALL))
582 PLOG(PL_ERROR, "ProtoDispatcher::ReleaseSocketStream() error: UpdateStreamNotification(DISABLE_ALL) failure!\n");
583 socketStream.ClearNotifyFlags();
584 }
585 stream_table.Remove(socketStream);
586 socket_stream_pool.Put(socketStream);
587 } // end ProtoDispatcher::ReleaseSocketStream()
588
589
590 ProtoDispatcher::ChannelStream* ProtoDispatcher::GetChannelStream(ProtoChannel& theChannel)
591 {
592
593 // First, search our list of active channels
594 ProtoChannel* channelPtr = &theChannel;
595 ChannelStream* channelStream =
596 static_cast<ChannelStream*>(stream_table.Find((const char*)&channelPtr, sizeof(ProtoChannel*) << 3));
597
598 if (NULL == channelStream)
599 {
600 // Get one from the pool or create a new one
601 channelStream = channel_stream_pool.Get();
602 if (NULL != channelStream)
603 {
604 channelStream->ClearNotifyFlags();
605 channelStream->SetChannel(theChannel);
606 }
607 else
608 {
609 if (NULL == (channelStream = new ChannelStream(theChannel)))
610 {
611 PLOG(PL_ERROR, "ProtoDispatcher::GetChannelStream() new ChannelStream error: %s\n", GetErrorString());
612 return NULL;
613 }
614 }
615 // Insert into "active" stream table
616 stream_table.Insert(*channelStream);
617 }
618 return channelStream;
619 } // end ProtoDispatcher::GetChannelStream()
620
621 void ProtoDispatcher::ReleaseChannelStream(ChannelStream& channelStream)
622 {
623 #ifdef WIN32
624 if (ready_stream_list.Contains(channelStream))
625 ready_stream_list.Remove(channelStream);
626 // Makes sure the channel input/output event HANDLE is removed from array
627 // (This removes it from list passed to MsgWaitForMultipleObjectsEx() call)
628 if (channelStream.GetIndex() >= 0)
629 {
630 Win32RemoveStream(channelStream.GetIndex());
631 channelStream.SetIndex(-1);
632 }
633 if (channelStream.GetOutdex() >= 0)
634 {
635 Win32RemoveStream(channelStream.GetOutdex());
636 channelStream.SetOutdex(-1);
637 }
638 #endif // WIN32
639 stream_table.Remove(channelStream);
640 channelStream.ClearNotifyFlags();
641 channel_stream_pool.Put(channelStream);
642 } // end ProtoDispatcher::ReleaseChannelStream()
643
644 ProtoDispatcher::EventStream* ProtoDispatcher::GetEventStream(ProtoEvent& theEvent)
645 {
646 // First, search our list of active events
647 ProtoEvent* eventPtr = &theEvent;
648 EventStream* eventStream =
649 static_cast<EventStream*>(stream_table.Find((const char*)&eventPtr, sizeof(ProtoEvent*) << 3));
650 if (NULL == eventStream)
651 {
652 // Get one from the pool or create a new one
653 eventStream = event_stream_pool.Get();
654 if (NULL != eventStream)
655 {
656 eventStream->ClearNotifyFlags();
657 eventStream->SetEvent(theEvent);
658 }
659 else
660 {
661 if (NULL == (eventStream = new EventStream(theEvent)))
662 {
663 PLOG(PL_ERROR, "ProtoDispatcher::GetEventStream() new EventStream error: %s\n", GetErrorString());
664 return NULL;
665 }
666 }
667 // Insert into "active" stream table
668 stream_table.Insert(*eventStream);
669 }
670 return eventStream;
671 } // end ProtoDispatcher::GetEventStream()
672
673 void ProtoDispatcher::ReleaseEventStream(EventStream& eventStream)
674 {
675 /*
676 #ifdef WIN32
677 if (ready_stream_list.Contains(eventStream))
678 ready_stream_list.Remove(eventStream);
679 // Makes sure the event input/output event HANDLE is removed from array
680 // (This removes it from list passed to MsgWaitForMultipleObjectsEx() call)
681 if (eventStream.GetIndex() >= 0)
682 {
683 Win32RemoveStream(eventStream.GetIndex());
684 eventStream.SetIndex(-1);
685 }
686 if (eventStream.GetOutdex() >= 0)
687 {
688 Win32RemoveStream(eventStream.GetOutdex());
689 eventStream.SetOutdex(-1);
690 }
691 #endif // WIN32
692 */
693 stream_table.Remove(eventStream);
694 eventStream.ClearNotifyFlags();
695 event_stream_pool.Put(eventStream);
696 } // end ProtoDispatcher::ReleaseEventStream()
697
698
699 bool ProtoDispatcher::InstallGenericInput(ProtoDispatcher::Descriptor descriptor,
700 ProtoDispatcher::Callback* callback,
701 const void* clientData)
702 {
703 SignalThread();
704 GenericStream* stream = GetGenericStream(descriptor);
705 if (NULL != stream)
706 {
707 if (!stream->IsInput()) // Enable system-specific input notification
708 {
709 if (!UpdateStreamNotification(*stream, ENABLE_INPUT))
710 {
711 PLOG(PL_ERROR, "ProtoDispatcher::InstallGenericInput() error: unable to ENABLE_INPUT!\n");
712 if (!stream->HasNotifyFlags()) ReleaseGenericStream(*stream);
713 UnsignalThread();
714 return false;
715 }
716 }
717 }
718 stream->SetNotifyFlag(Stream::NOTIFY_INPUT);
719 stream->SetCallback(callback, clientData);
720 UnsignalThread();
721 return true;;
722 } // end ProtoDispatcher::InstallGenericInput()
723
724 void ProtoDispatcher::RemoveGenericInput(Descriptor descriptor)
725 {
726 SignalThread();
727 GenericStream* stream = FindGenericStream(descriptor);
728 if (NULL != stream)
729 {
730 if (stream->IsInput())
731 {
732 if (!UpdateStreamNotification(*stream, DISABLE_INPUT))
733 PLOG(PL_ERROR, "ProtoDispatcher::RemoveGenericInput() error: UpdateStreamNotification(DISABLE_INPUT) failure!\n");
734 stream->UnsetNotifyFlag(Stream::NOTIFY_INPUT);
735 }
736 if (!stream->HasNotifyFlags()) ReleaseGenericStream(*stream);
737 }
738 UnsignalThread();
739 } // end ProtoDispatcher::RemoveGenericInput()
740
741 bool ProtoDispatcher::InstallGenericOutput(ProtoDispatcher::Descriptor descriptor,
742 ProtoDispatcher::Callback* callback,
743 const void* clientData)
744 {
745 SignalThread();
746 GenericStream* stream = FindGenericStream(descriptor);
747 if (NULL != stream)
748 {
749 if (!stream->IsOutput()) // Enable system-specific output notification
750 {
751 if (!UpdateStreamNotification(*stream, ENABLE_OUTPUT))
752 {
753 PLOG(PL_ERROR, "ProtoDispatcher::InstallGenericOutput() error: unable to ENABLE_OUTPUT!\n");
754 if (!stream->HasNotifyFlags()) ReleaseGenericStream(*stream);
755 UnsignalThread();
756 return false;
757 }
758 }
759 }
760 stream->SetNotifyFlag(Stream::NOTIFY_OUTPUT);
761 stream->SetCallback(callback, clientData);
762 UnsignalThread();
763 return true;
764 } // end ProtoDispatcher::InstallGenericOutput()
765
766 void ProtoDispatcher::RemoveGenericOutput(Descriptor descriptor)
767 {
768 SignalThread();
769 GenericStream* stream = FindGenericStream(descriptor);
770 if (NULL != stream)
771 {
772 if (stream->IsOutput())
773 {
774 if (!UpdateStreamNotification(*stream, DISABLE_OUTPUT))
775 PLOG(PL_ERROR, "ProtoDispatcher::RemoveGenericOutput() error: UpdateStreamNotification(DISABLE_OUTPUT) failure!\n");
776 stream->UnsetNotifyFlag(Stream::NOTIFY_OUTPUT);
777 }
778 if (!stream->HasNotifyFlags()) ReleaseGenericStream(*stream);
779 }
780 UnsignalThread();
781 } // end ProtoDispatcher::RemoveGenericOutput()
782
783 ProtoDispatcher::GenericStream* ProtoDispatcher::GetGenericStream(Descriptor descriptor)
784 {
785 // First, search our list of active generic streams
786 GenericStream* genericStream = generic_stream_table.FindByDescriptor(descriptor);
787 if (NULL == genericStream)
788 {
789 // Get one from the pool or create a new one
790 genericStream = generic_stream_pool.Get();
791 if (NULL != genericStream)
792 {
793 genericStream->ClearNotifyFlags();
794 genericStream->SetDescriptor(descriptor);
795 }
796 else
797 {
798 if (NULL == (genericStream = new GenericStream(descriptor)))
799 {
800 PLOG(PL_ERROR, "ProtoDispatcher::GetGenericStream() new GenericStream error: %s\n", GetErrorString());
801 return NULL;
802 }
803 }
804 // Insert into "active" stream table
805 stream_table.Insert(*genericStream);
806 // Insert into generic_stream_table (indexed by descriptor)
807 if (!generic_stream_table.Insert(*genericStream))
808 {
809 PLOG(PL_ERROR, "ProtoDispatcher::GetGenericStream() error: unable to add to table: %s\n", GetErrorString());
810 ReleaseGenericStream(*genericStream);
811 return NULL;
812 }
813 }
814 return genericStream;
815 } // end ProtoDispatcher::GetGenericStream()
816
817 void ProtoDispatcher::ReleaseGenericStream(GenericStream& genericStream)
818 {
819 // First, disable system-specific notifications for stream
820 if (genericStream.IsInput())
821 {
822 if (!UpdateStreamNotification(genericStream, DISABLE_INPUT))
823 PLOG(PL_ERROR, "ProtoDispatcher::ReleaseGenericStream() error: UpdateStreamNotification(DISABLE_INPUT) failure!\n");
824 }
825 if (genericStream.IsOutput())
826 {
827 if (!UpdateStreamNotification(genericStream, DISABLE_OUTPUT))
828 PLOG(PL_ERROR, "ProtoDispatcher::ReleaseGenericStream() error: UpdateStreamNotification(DISABLE_OUTPUT) failure!\n");
829 }
830 genericStream.ClearNotifyFlags();
831 stream_table.Remove(genericStream);
832 generic_stream_table.Remove(genericStream);
833 generic_stream_pool.Put(genericStream);
834 } // end ProtoDispatcher::ReleaseGenericStream()
835
836
837 /**
838 * @brief Boost process priority for real-time operation
839 */
840 bool ProtoDispatcher::BoostPriority()
841 {
842 #ifdef WINCE
843 PLOG(PL_ERROR, "ProtoDispatcher::BoostPriority() not supported on WinCE");
844 return false;
845 #elif defined(WIN32)
846 ThreadId threadId = IsThreaded() ? thread_id : ::GetCurrentThreadId();
847 HANDLE threadHandle = OpenThread(THREAD_SET_INFORMATION, FALSE, threadId);
848 if (NULL == threadHandle)
849 {
850 PLOG(PL_ERROR, "ProtoDispatcher::BoostPriority() error: OpenThread() error: %s\n", GetErrorString());
851 return false;
852 }
853 if (!SetThreadPriority(threadHandle, THREAD_PRIORITY_TIME_CRITICAL))
854 {
855 PLOG(PL_ERROR, "ProtoDispatcher::BoostPriority() error: SetThreadPriority() error: %s\n", GetErrorString());
856 CloseHandle(threadHandle);
857 return false;
858 }
859 CloseHandle(threadHandle);
860 #else
861 //int who = IsThreaded() ? thread_id : 0;
862 #ifdef HAVE_SCHED
863 // (This _may_ work on Linux-only at this point)
864 // (TBD) We should probably look into pthread-setschedparam() instead
865 struct sched_param schp;
866 memset(&schp, 0, sizeof(schp));
867 schp.sched_priority = sched_get_priority_max(SCHED_FIFO);
868 if (0 != sched_setscheduler(0, SCHED_FIFO, &schp))
869 {
870 schp.sched_priority = sched_get_priority_max(SCHED_OTHER);
871 if (0 != sched_setscheduler(0, SCHED_OTHER, &schp))
872 {
873 PLOG(PL_ERROR, "ProtoDispatcher::BoostPriority() error: sched_setscheduler() error: %s\n", GetErrorString());
874 return false;
875 }
876 else
877 {
878 PLOG(PL_WARN, "ProtoDispatcher::BoostPriority() warning: unable to set SCHED_FIFO boost, SCHED_OTHER set.\n"
879 " (run as root or sudofor full SCHED_FIFO priority boost)\n");
880 }
881 }
882 #else
883 // (TBD) Do something differently if "pthread sched param"?
884 if (0 != setpriority(PRIO_PROCESS, getpid(), -20))
885 {
886 PLOG(PL_ERROR, "ProtoDispatcher::BoostPriority() error: setpriority() error: %s\n", GetErrorString());
887 return false;
888 }
889 #endif // HAVE_SCHED
890 #endif // if/else WIN32/UNIX
891 return true;
892 } // end ProtoDispatcher::BoostPriority()
893
894
895 /**
896 * A ProtoDispatcher "main loop"
897 */
898 int ProtoDispatcher::Run(bool oneShot)
899 {
900 exit_code = 0;
901 wait_status = -1;
902 if (priority_boost) BoostPriority();
903
904 #ifdef USE_TIMERFD // LINUX-only
905 int tfd = timerfd_create(CLOCK_MONOTONIC, 0);
906 if (tfd < 0)
907 {
908 PLOG(PL_ERROR, "ProtoDispatcher::Run() timerfd_create() error: %s\n", GetErrorString());
909 return -1; // TBD - is there a more specific exitCode we should use instead???
910 }
911 #ifdef USE_EPOLL
912 if (!EpollChange(tfd, EPOLLIN, EPOLL_CTL_ADD, &timer_stream))
913 {
914 PLOG(PL_ERROR, "ProtoDispatcher::Run() error EpolLChange(timer_fd) failed!\n");
915 close(tfd);
916 return -1; // TBD - is there a more specific exitCode we should use instead???
917 }
918 #endif
919 timer_stream.SetDescriptor(tfd);
920 #endif // USE_TIMERFD
921
922 #ifdef USE_WAITABLE_TIMER // WIN32-only
923 HANDLE wtm = CreateWaitableTimer(NULL, TRUE, NULL);
924 if (INVALID_HANDLE_VALUE == wtm)
925 {
926 PLOG(PL_ERROR, "ProtoDispatcher::Run() CreateWaitableTimer() error: %s\n", GetErrorString());
927 return -1;
928 }
929 timer_stream.SetDescriptor(wtm);
930 timer_stream.SetIndex(-1); // the timer_stream will added when activated
931 timer_active = false;
932 #endif // USE_WAITABLE_TIMER
933
934 // TBD - should we keep "run" true while in the do loop so "IsRunning()"
935 // can be interpreted properly (or just deprecate IsRunning() ???
936 run = oneShot ? false : true;
937 do
938 {
939 if (IsPending())
940 {
941 // Here we "latch" the next timeout _before_
942 // we open the suspend window to be safe
943 timer_delay = ProtoTimerMgr::GetTimeRemaining();
944 if (IsThreaded())
945 {
946 Lock(signal_mutex);
947 Unlock(suspend_mutex);
948 Wait();
949 Unlock(signal_mutex);
950
951 // <-- this is where a dispatcher rests after SignalThread() call
952
953 // what if another thread calls SignalThread() at this exact moment?
954 // and gets the suspend and signal mutexes ...
955 // a) WasSignaled() will be false on Unix (break_pipe fd not set)
956 // b) then Dispatch() will be called ... but only valid descriptors, etc are checked
957 // as Wait resets/builds the FD_SET
958 // c) next time WasSignaled() will be true, but no big deal
959
960 Lock(suspend_mutex);
961
962 if (prompt_set)
963 {
964
965 if (NULL != prompt_callback)
966 prompt_callback(prompt_client_data);
967 prompt_set = false;
968 }
969 /*
970 if (WasSignaled()) // true" if was signaled via ProtoDispatcher::SignalThread()
971 {
972 // We "continue" here in case i/o or timeout status
973 // was changed during signal suspend
974 continue;
975 }
976 else */
977 if (NULL != controller)
978 {
979 // Relinquish to controller thread
980 // Note this assumes the controller thread is the _only_
981 // other thread vying for control of this dispatcher thread
982 Unlock(suspend_mutex);
983 controller->DoDispatch();
984 Lock(suspend_mutex);
985 }
986 else
987 {
988 // Do our own event dispatching
989 Dispatch();
990 }
991 }
992 else
993 {
994 Wait();
995 Dispatch();
996 }
997 }
998 else
999 {
1000 PLOG(PL_DEBUG, "ProtoDispatcher::Run() would be stuck with infinite timeout & no inputs!\n");
1001 break;
1002 }
1003 } while (run);
1004 #ifdef USE_TIMERFD
1005 // Note if USE_EPOLL, closing the "timer_fd" automatically deletes the event
1006 close(timer_stream.GetDescriptor());
1007 timer_stream.SetDescriptor(INVALID_DESCRIPTOR);
1008 #endif // USE_TIMERFD
1009 #ifdef USE_WAITABLE_TIMER
1010 if (timer_active)
1011 {
1012 CancelWaitableTimer(timer_stream.GetDescriptor());
1013 timer_active = false;
1014 }
1015 if (timer_stream.GetIndex() >= 0)
1016 {
1017 Win32RemoveStream(timer_stream.GetIndex());
1018 timer_stream.SetIndex(-1);
1019 }
1020 CloseHandle(timer_stream.GetDescriptor());
1021 timer_stream.SetDescriptor(INVALID_DESCRIPTOR);
1022 #endif // USE_WAITABLE_TIMER
1023 return exit_code;
1024 } // end ProtoDispatcher::Run()
1025
1026 void ProtoDispatcher::Stop(int exitCode)
1027 {
1028 if (controller)
1029 {
1030 controller->OnThreadStop();
1031 controller = NULL;
1032 }
1033 SignalThread();
1034 exit_code = run ? exitCode : exit_code;
1035 run = false;
1036 #ifdef WIN32
1037 if (msg_window)
1038 PostMessage(msg_window, WM_DESTROY, NULL, NULL);
1039 #endif // WIN32
1040 UnsignalThread();
1041 DestroyThread();
1042 } // end ProtoDispatcher::Stop();
1043
1044
1045 #ifdef WIN32
1046 #ifdef _WIN32_WCE
1047 DWORD WINAPI ProtoDispatcher::DoThreadStart(LPVOID param)
1048 #else
1049 unsigned int __stdcall ProtoDispatcher::DoThreadStart(void* param)
1050 #endif // if/else _WIN32_WCE
1051 #else
1052 void* ProtoDispatcher::DoThreadStart(void* param)
1053 #endif // if/else WIN32/UNIX
1054 {
1055 ProtoDispatcher* dp = reinterpret_cast<ProtoDispatcher*>(param);
1056 ASSERT(NULL != dp);
1057 if (NULL != dp->controller) Lock(dp->controller->lock_b);
1058 Lock(dp->suspend_mutex);
1059 dp->thread_started = true;
1060 dp->exit_status = dp->Run(); // TBD - should have Run() set exit_status internally instead???
1061 Unlock(dp->suspend_mutex);
1062 DoThreadExit(dp->GetExitStatus());
1063 return (dp->GetExitStatus());
1064 } // end ProtoDispatcher::DoThreadStart()
1065
1066
1067 bool ProtoDispatcher::StartThread(bool priorityBoost,
1068 ProtoDispatcher::Controller* theController,
1069 ThreadId threadId)
1070 {
1071 if (IsThreaded())
1072 {
1073 PLOG(PL_ERROR, "ProtoDispatcher::StartThread() error: thread already started\n");
1074 return false;
1075 }
1076 priority_boost = priorityBoost;
1077 if (!InstallBreak())
1078 {
1079 PLOG(PL_ERROR, "ProtoDispatcher::StartThread() error: InstallBreak() failed\n");
1080 return false;
1081 }
1082 controller = theController;
1083 Init(suspend_mutex);
1084 Init(signal_mutex);
1085 Lock(suspend_mutex);
1086
1087 if ((ThreadId)NULL == threadId)
1088 {
1089 #ifdef WIN32
1090 // Note: For WinCE, we need to use CreateThread() instead of _beginthreadex()
1091 #ifdef _WIN32_WCE
1092 if (!(actual_thread_handle = CreateThread(NULL, 0, DoThreadStart, this, 0, &thread_id)))
1093 #else
1094 if (!(actual_thread_handle = (HANDLE)_beginthreadex(NULL, 0, DoThreadStart, this, 0, (unsigned*)&thread_id)))
1095 #endif // if/else _WIN32_WCE
1096 #else
1097 if (0 != pthread_create(&thread_id, NULL, DoThreadStart, this))
1098 #endif // if/else WIN32/UNIX
1099 {
1100 PLOG(PL_ERROR, "ProtoDispatcher::StartThread() create thread error: %s\n", GetErrorString());
1101 RemoveBreak();
1102 Unlock(suspend_mutex); // will be relocked by DoThreadStart()
1103 thread_id = (ThreadId)NULL;
1104 controller = NULL;
1105 return false;
1106 }
1107 external_thread = false;
1108 Unlock(suspend_mutex);
1109 }
1110 else
1111 {
1112 // Starting within externally created thread
1113 ASSERT(threadId == GetCurrentThread());
1114 thread_id = threadId; // externally created thread
1115 external_thread = true;
1116 // So must exec DoThreadStart() steps here
1117 thread_started = true;
1118 exit_status = Run();
1119 Unlock(suspend_mutex);
1120 }
1121 return true;
1122 } // end ProtoDispatcher::StartThread()
1123
1124 /**
1125 * This wakes up the thread, makes a call to the installed prompt_callback
1126 * and the thread goes back to waiting. This is the way to have a ProtoDispatcher
1127 * thread perform some "work" function. It currently does not have a means to
1128 * signal when the work is completed other than this call (due to SuspendThread() call)
1129 * will block until the work is done. TBD - provide a method to access/monitor/wait on
1130 * the "suspend_mutex" that will be unlocked when the thread goes back to "sleep"
1131 */
1132 bool ProtoDispatcher::PromptThread()
1133 {
1134 // Make sure thread is asleep before setting prompt
1135 if (SuspendThread())
1136 {
1137 prompt_set = true; // indicate prompt_callback should be called
1138 // Now explicitly wake the thread that the prompt is set
1139 if (!SignalThread())
1140 {
1141 ResumeThread();
1142 return false; // suspend/signal thread
1143 }
1144 UnsignalThread();
1145 ResumeThread();
1146 return true;
1147 }
1148 else
1149 {
1150 return false;
1151 }
1152 } // end ProtoDispatcher::PromptThread()
1153
1154
1155 /**
1156 * This brings the thread to a suspended state _outside_
1157 * of _its_ ProtoDispatcher::Wait() state. A call to
1158 * SignalThread() followed by UnsignalThread() will make
1159 * the thread break from waiting and dispatch timers, pending I/O, etc
1160 * The caller can safely do things in between signal/unsignal like
1161 * add/remove descriptors, etc and this is used by internal ProtoDispatcher
1162 * code to do this safely for threaded dispatcher instances. Any call to SignalThread()
1163 * MUST be mirrored with an UnsignalThread() call and it is OK for it to be re-entrantly
1164 * called (that is what the "signal_count" is for).
1165 */
1166 bool ProtoDispatcher::SignalThread()
1167 {
1168 SuspendThread();
1169 ThreadId currentThread = GetCurrentThread();
1170 if (IsThreaded() && (currentThread != thread_id))
1171 {
1172 if (signal_count > 0)
1173 {
1174 signal_count++;
1175 return true;
1176 }
1177 else
1178 {
1179 if (!SetBreak())
1180 {
1181 PLOG(PL_ERROR, "ProtoDispatcher::SignalThread() error: SetBreak() failed!\n");
1182 ResumeThread();
1183 return false;
1184 }
1185 // Attempting to lock the "signal_mutex" blocks until the thread
1186 // is "outside" its "ProtoDispatcher::Wait()" stage
1187 // (thread can't reenter wait stage until "UnsignalThread()" is
1188 // called and the "signal_mutex" is unlocked)
1189 Lock(signal_mutex);
1190 signal_count = 1;
1191 thread_signaled = true;
1192 }
1193 }
1194 return true;
1195 } // end ProtoDispatcher::SignalThread()
1196
1197 void ProtoDispatcher::UnsignalThread()
1198 {
1199 ThreadId currentThread = GetCurrentThread();
1200 if (IsThreaded() && (currentThread != thread_id) && (thread_master == currentThread))
1201 {
1202 ASSERT(0 != signal_count);
1203 signal_count--;
1204 if (0 == signal_count)
1205 Unlock(signal_mutex);
1206 }
1207 ResumeThread();
1208 } // end ProtoDispatcher::UnsignalThread()
1209
1210 /*
1211 * This makes sure the thread is asleep (i.e. waiting) or stopped
1212 * until ResumeThread() is called (unlocks the suspend_mutex so
1213 * thread can proceed) Note that SuspendThread() will block
1214 * until the thread gets to the waiting state. Any call to SuspendThread()
1215 * MUST be mirrored with an ResumeThread() call and it is OK for it to
1216 * be re-entrantly called (that is what the "suspend_count" is for).
1217 *
1218 */
1219 bool ProtoDispatcher::SuspendThread()
1220 {
1221 ThreadId currentThread = GetCurrentThread();
1222 if (IsThreaded() && (currentThread != thread_id))
1223 {
1224 if (currentThread == thread_master)
1225 {
1226 suspend_count++;
1227 return true;
1228 }
1229 // We use while() here to make sure the thread started
1230 // (TBD) use a spin_count to limit iterations as safeguard
1231 while (!thread_started);
1232 Lock(suspend_mutex); // TBD - check result of "Lock()"
1233 thread_master = currentThread;
1234 suspend_count = 1;
1235 }
1236 return true;
1237 } // end ProtoDispatcher::SuspendThread()
1238
1239 void ProtoDispatcher::ResumeThread()
1240 {
1241 ThreadId currentThread = GetCurrentThread();
1242 if (IsThreaded() && (currentThread != thread_id))
1243 {
1244 if (currentThread == thread_master)
1245 {
1246 if (suspend_count > 1)
1247 {
1248 suspend_count--;
1249 }
1250 else
1251 {
1252 thread_master = (ThreadId)NULL;
1253 suspend_count = 0;
1254 Unlock(suspend_mutex);
1255 }
1256 }
1257 }
1258 } // end ProtoDispatcher::ResumeThread()
1259
1260 void ProtoDispatcher::DestroyThread()
1261 {
1262 if (IsThreaded())
1263 {
1264 controller = NULL;
1265 if (!external_thread)
1266 {
1267 #ifdef WIN32
1268 if (!IsMyself()) WaitForSingleObject(actual_thread_handle, INFINITE);
1269 #ifdef _WIN32_WCE
1270 CloseHandle(actual_thread_handle);
1271 actual_thread_handle = NULL;
1272 #endif // _WIN32_WCE
1273 #else
1274 if (!IsMyself()) pthread_join(thread_id, NULL);
1275 #endif // if/else WIN32/UNIX
1276 }
1277 thread_started = false;
1278 thread_id = (ThreadId)NULL;
1279 external_thread = false;
1280 RemoveBreak();
1281 Destroy(suspend_mutex);
1282 Destroy(signal_mutex);
1283 }
1284 } // end ProtoDispatcher::DestroyThread()
1285
1286 /////////////////////////////////////////////////////////////////////////
1287 // Unix-specific methods and implementation
1288 #ifdef UNIX
1289
1290
1291
1292 #if defined(USE_SELECT)
1293 // USE_SELECT implementation of ProtoDispatcher::UpdateStreamNotification()
1294 bool ProtoDispatcher::UpdateStreamNotification(Stream& stream, NotificationCommand cmd)
1295 {
1296 // ProtoDispatcher currently builds its "struct fdsets" by iterating through
1297 // the "stream_list" each time ProtoDispatcher::Wait() is called
1298 // TBD - we should use FD_CLR here to make sure our current input_set and
1299 // output_set are in the proper state for when a stream's notification
1300 // is disabled (e.g. due to deletion or other)
1301
1302 switch (cmd)
1303 {
1304 case ENABLE_INPUT:
1305 stream.SetNotifyFlag(Stream::NOTIFY_INPUT);
1306 break;
1307 case DISABLE_INPUT:
1308 stream.UnsetNotifyFlag(Stream::NOTIFY_INPUT);
1309 if (INVALID_DESCRIPTOR != stream.GetInputHandle())
1310 FD_CLR(stream.GetInputHandle(), &input_set);
1311 break;
1312 case ENABLE_OUTPUT:
1313 stream.SetNotifyFlag(Stream::NOTIFY_OUTPUT);
1314 break;
1315 case DISABLE_OUTPUT:
1316 stream.UnsetNotifyFlag(Stream::NOTIFY_OUTPUT);
1317 if (INVALID_DESCRIPTOR != stream.GetOutputHandle())
1318 FD_CLR(stream.GetOutputHandle(), &output_set);
1319 break;
1320 case DISABLE_ALL:
1321 stream.ClearNotifyFlags();
1322 if (INVALID_DESCRIPTOR != stream.GetInputHandle())
1323 FD_CLR(stream.GetInputHandle(), &input_set);
1324 if (INVALID_DESCRIPTOR != stream.GetOutputHandle())
1325 FD_CLR(stream.GetOutputHandle(), &output_set);
1326 break;
1327 }
1328
1329 return true;
1330 } // end ProtoDispatcher::UpdateStreamNotification() [USE_SELECT]
1331
1332 #elif defined(USE_KQUEUE)
1333
1334 // USE_KQUEUE implementation of ProtoDispatcher::UpdateStreamNotification()
1335 bool ProtoDispatcher::UpdateStreamNotification(Stream& stream, NotificationCommand cmd)
1336 {
1337 switch (cmd)
1338 {
1339 case ENABLE_INPUT:
1340 if (!KeventChange(stream.GetInputHandle(), EVFILT_READ, EV_ADD | EV_ENABLE, &stream))
1341 {
1342 PLOG(PL_ERROR, "ProtoDispatcher::UpdateStreamNotification(ENABLE_INPUT) KeventChange() error!\n");
1343 return false;
1344 }
1345 stream.SetNotifyFlag(Stream::NOTIFY_INPUT);
1346 break;
1347
1348 case DISABLE_INPUT:
1349 if (!KeventChange(stream.GetInputHandle(), EVFILT_READ, EV_DISABLE, &stream))
1350 {
1351 PLOG(PL_ERROR, "ProtoDispatcher::UpdateStreamNotification(DISABLE_INPUT) KeventChange() error!\n");
1352 return false;
1353 }
1354 stream.UnsetNotifyFlag(Stream::NOTIFY_INPUT);
1355 break;
1356
1357 case ENABLE_OUTPUT:
1358 if (!KeventChange(stream.GetOutputHandle(), EVFILT_WRITE, EV_ADD | EV_ENABLE, &stream))
1359 {
1360 PLOG(PL_ERROR, "ProtoDispatcher::UpdateStreamNotification(DISABLE_INPUT) KeventChange() error!\n");
1361 return false;
1362 }
1363 stream.SetNotifyFlag(Stream::NOTIFY_OUTPUT);
1364 break;
1365
1366 case DISABLE_OUTPUT:
1367 if (!KeventChange(stream.GetOutputHandle(), EVFILT_WRITE, EV_DISABLE, &stream))
1368 {
1369 PLOG(PL_ERROR, "ProtoDispatcher::UpdateStreamNotification(DISABLE_INPUT) KeventChange() error!\n");
1370 return false;
1371 }
1372 stream.UnsetNotifyFlag(Stream::NOTIFY_OUTPUT);
1373 break;
1374
1375 case DISABLE_ALL:
1376 if (stream.IsInput() && !KeventChange(stream.GetInputHandle(), EVFILT_READ, EV_DISABLE, &stream))
1377 {
1378 PLOG(PL_WARN, "ProtoDispatcher::UpdateStreamNotification(DISABLE_ALL) KeventChange(EVFILT_READ) error!\n");
1379 //return false;
1380 }
1381 if (stream.IsOutput() && !KeventChange(stream.GetOutputHandle(), EVFILT_WRITE, EV_DISABLE, &stream))
1382 {
1383 PLOG(PL_WARN, "ProtoDispatcher::UpdateStreamNotification(DISABLE_ALL) KeventChange(EVFILT_WRITE) error!\n");
1384 //return false;
1385 }
1386 stream.ClearNotifyFlags();
1387 // TBD - support exceptions ??
1388 break;
1389 }
1390 return true;
1391 } // end ProtoDispatcher::UpdateStreamNotification() [USE_KQUEUE]
1392
1393 bool ProtoDispatcher::KeventChange(uintptr_t ident, int16_t filter, uint16_t flags, void* udata)
1394 {
1395 // This simply sets a new "struct kevent" in our "kevent_array"
1396 if (-1 == kevent_queue)
1397 {
1398 if (-1 == (kevent_queue = kqueue()))
1399 {
1400 PLOG(PL_ERROR, "ProtoDispatcher::KeventChange() kqueue() error: %s\n", GetErrorString());
1401 return false;
1402 }
1403 }
1404 struct kevent kvt;
1405 kvt.ident = ident;
1406 kvt.filter = filter;
1407 kvt.flags = flags;
1408 kvt.fflags = 0;
1409 kvt.data = 0;
1410 kvt.udata = udata;
1411
1412 // On EV_DISABLE, go through the current kevent_array and make
1413 // sure this "ident::filter" is nullified to avoid undesired
1414 // notification attempts (i.e. disable current, pending notification, if any)
1415 // (i.e., set the "filter" to harmless EVFILT_USER value)
1416 if (0 != (flags & EV_DISABLE))
1417 {
1418 struct kevent* kep = kevent_array;
1419 for (int i = 0; i < wait_status; i++)
1420 {
1421 if ((ident == kep->ident) && (filter == kep->filter))
1422 {
1423 kep->ident = 0; // different than our "break" EVFILT_USER if ever needed
1424 kep->filter = EVFILT_USER;
1425 }
1426 }
1427 }
1428 // This call enables/disables future notifications as desired
1429 if (kevent(kevent_queue, &kvt, 1, NULL, 0, NULL) < 0)
1430 {
1431 PLOG(PL_ERROR, "ProtoDispatcher::KeventChange() kevent() error: %s\n", GetErrorString());
1432 return false;
1433 }
1434 else
1435 {
1436 return true;
1437 }
1438 } // end ProtoDispatcher::KeventChange()
1439
1440 #elif USE_EPOLL
1441
1442 // USE_EPOLL implementation of ProtoDispatcher::UpdateStreamNotification()
1443 bool ProtoDispatcher::UpdateStreamNotification(Stream& stream, NotificationCommand cmd)
1444 {
1445 // Note for ProtoChannel it is possible that separate input and output descriptors may
1446 // be used and so the code below checks for this condition
1447 switch (cmd)
1448 {
1449 case ENABLE_INPUT:
1450 ASSERT(!stream.IsInput());
1451 if (!((stream.IsOutput() && (stream.GetInputHandle() == stream.GetOutputHandle())) ?
1452 EpollChange(stream.GetInputHandle(), EPOLLIN | EPOLLOUT, EPOLL_CTL_MOD, &stream) :
1453 EpollChange(stream.GetInputHandle(), EPOLLIN, EPOLL_CTL_ADD, &stream)))
1454 {
1455 PLOG(PL_ERROR, "ProtoDispatcher::UpdateStreamNotification(ENABLE_INPUT) error: EpollChange() failed!\n");
1456 return false;
1457 }
1458 stream.SetNotifyFlag(Stream::NOTIFY_INPUT);
1459 break;
1460
1461 case DISABLE_INPUT:
1462 ASSERT(stream.IsInput());
1463 if (!((stream.IsOutput() && (stream.GetInputHandle() == stream.GetOutputHandle())) ?
1464 EpollChange(stream.GetInputHandle(), EPOLLOUT, EPOLL_CTL_MOD, &stream) :
1465 EpollChange(stream.GetInputHandle(), 0, EPOLL_CTL_DEL, NULL)))
1466 {
1467 PLOG(PL_ERROR, "ProtoDispatcher::UpdateStreamNotification(DISABLE_INPUT) error: EpollChange() failed!\n");
1468 return false;
1469 }
1470 stream.UnsetNotifyFlag(Stream::NOTIFY_INPUT);
1471 break;
1472
1473 case ENABLE_OUTPUT:
1474 ASSERT(!stream.IsOutput());
1475 if (!((stream.IsInput() && (stream.GetInputHandle() == stream.GetOutputHandle())) ?
1476 EpollChange(stream.GetOutputHandle(), EPOLLIN | EPOLLOUT, EPOLL_CTL_MOD, &stream) :
1477 EpollChange(stream.GetOutputHandle(), EPOLLOUT, EPOLL_CTL_ADD, &stream)))
1478 {
1479 PLOG(PL_ERROR, "ProtoDispatcher::UpdateStreamNotification(ENABLE_OUTPUT) error: EpollChange() failed!\n");
1480 return false;
1481 }
1482 stream.SetNotifyFlag(Stream::NOTIFY_OUTPUT);
1483 break;
1484
1485 case DISABLE_OUTPUT:
1486 ASSERT(stream.IsOutput());
1487 if (!((stream.IsInput() && (stream.GetInputHandle() == stream.GetOutputHandle())) ?
1488 EpollChange(stream.GetOutputHandle(), EPOLLIN, EPOLL_CTL_MOD, &stream) :
1489 EpollChange(stream.GetOutputHandle(), 0, EPOLL_CTL_DEL, NULL)))
1490 {
1491 PLOG(PL_ERROR, "ProtoDispatcher::UpdateStreamNotification(DISABLE_OUTPUT) error: EpollChange() failed!\n");
1492 return false;
1493 }
1494 stream.UnsetNotifyFlag(Stream::NOTIFY_OUTPUT);
1495 break;
1496
1497 case DISABLE_ALL:
1498 ASSERT(stream.IsInput() || stream.IsOutput());
1499 if (stream.GetInputHandle() == stream.GetOutputHandle())
1500 {
1501 if (!EpollChange(stream.GetInputHandle(), 0, EPOLL_CTL_DEL, NULL))
1502 {
1503 PLOG(PL_ERROR, "ProtoDispatcher::UpdateStreamNotification(DISABLE_ALL) error: EpollChange() failed!\n");
1504 return false;
1505 }
1506 }
1507 else
1508 {
1509 if (stream.IsInput())
1510 {
1511 if (!EpollChange(stream.GetInputHandle(), 0, EPOLL_CTL_DEL, NULL))
1512 {
1513 PLOG(PL_ERROR, "ProtoDispatcher::UpdateStreamNotification(DISABLE_ALL) error: EpollChange(input) failed!\n");
1514 return false;
1515 }
1516 }
1517 if (stream.IsOutput())
1518 {
1519 if (!EpollChange(stream.GetOutputHandle(), 0, EPOLL_CTL_DEL, NULL))
1520 {
1521 PLOG(PL_ERROR, "ProtoDispatcher::UpdateStreamNotification(DISABLE_ALL) error: EpollChange(output) failed!\n");
1522 return false;
1523 }
1524 }
1525 }
1526 stream.ClearNotifyFlags();
1527 // TBD - support exceptions ??
1528 break;
1529 }
1530 return true;
1531 } // end ProtoDispatcher::UpdateStreamNotification() [USE_EPOLL]
1532
1533 bool ProtoDispatcher::EpollChange(int fd, int events, int op, void* udata)
1534 {
1535 if (-1 == epoll_fd)
1536 {
1537 if (-1 == (epoll_fd = epoll_create1(0)))
1538 {
1539 PLOG(PL_ERROR, "ProtoDispatcher::UpdateStreamNotification() epoll_create() error: %s\n",
1540 GetErrorString());
1541 return false;
1542 }
1543 }
1544 // Go through the current "epoll_event_array" and trim down or nullify
1545 // applicable current pending events for this "fd"
1546 // (So we don't get undesired notification dispatches)
1547 switch (op)
1548 {
1549 case EPOLL_CTL_MOD:
1550 {
1551 struct epoll_event* evp = epoll_event_array;
1552 for (int i = 0; i < wait_status; i++)
1553 {
1554 if (evp->data.ptr == udata)
1555 evp->events &= events; // mask out events no longer wanted
1556 evp++;
1557 }
1558 break;
1559 }
1560 case EPOLL_CTL_DEL:
1561 {
1562 struct epoll_event* evp = epoll_event_array;
1563 for (int i = 0; i < wait_status; i++)
1564 {
1565 if (evp->data.ptr == udata)
1566 {
1567 evp->events = 0; // no events wanted
1568 evp->data.ptr = NULL; // make sure no dereference is attempted
1569 }
1570 evp++;
1571 }
1572 break;
1573 }
1574 default:
1575 break;
1576 } // end switch(op)
1577 // Set or disable future events for this descriptor
1578 struct epoll_event event;
1579 event.events = events;
1580 event.data.ptr = udata;
1581 if (-1 == epoll_ctl(epoll_fd, op, fd, &event))
1582 {
1583 #ifdef USE_TIMERFD
1584 PLOG(PL_ERROR, "ProtoDispatcher::UpdateStreamNotification() epoll_ctl() error: %s (timer_fd:%d epoll_fd:%d)\n",
1585 GetErrorString(), timer_stream.GetDescriptor(), epoll_fd);
1586 #else
1587 PLOG(PL_ERROR, "ProtoDispatcher::UpdateStreamNotification() epoll_ctl() error: %s (epoll_fd:%d)\n",
1588 GetErrorString(), epoll_fd);
1589 #endif
1590 return false;
1591 }
1592 return true;
1593 } // end ProtoDispatcher::EpollChange()
1594
1595 #else
1596 #error "undefined async i/o mechanism" // to make sure we implement something
1597 #endif // !USE_SELECT && !USE_KQUEUE
1598
1599 bool ProtoDispatcher::InstallBreak()
1600 {
1601 #ifndef USE_KQUEUE
1602 // ProtoEvent uses eventfd(), kevent(), or pipe under the hood for us
1603 if (!break_event.Open())
1604 {
1605 PLOG(PL_ERROR, "ProtoDispatcher::InstallBreak() break_event.Open() error: %s\n", GetErrorString());
1606 return false;
1607 }
1608 #ifdef USE_EPOLL
1609 if (!EpollChange(break_event.GetDescriptor(), EPOLLIN, EPOLL_CTL_ADD, &break_stream))
1610 {
1611 PLOG(PL_ERROR, "ProtoDispatcher::InstallBreak() error: EpollChange() failed!\n");
1612 break_event.Close();
1613 return false;
1614 }
1615 #endif // USE_EPOLL
1616 #else
1617 // TBD - use ProtoEvent here, too
1618 if (!KeventChange(1, EVFILT_USER, EV_ADD | EV_CLEAR, NULL))
1619 {
1620 PLOG(PL_ERROR, "ProtoDispatcher::InstallBreak() KeventChange(EVFILT_USER) error: %s\n", GetErrorString());
1621 return false;
1622 }
1623 #endif // if/else !KQUEUE
1624 return true;
1625 } // end ProtoDispatcher::InstallBreak()
1626
1627 bool ProtoDispatcher::SetBreak()
1628 {
1629 #ifndef USE_KQUEUE
1630 if (!break_event.Set())
1631 {
1632 PLOG(PL_ERROR, "ProtoDispatcher::SetBreak() break_event.Set() error: %s\n", GetErrorString());
1633 return false;
1634 }
1635 #else
1636 // TBD - use ProtoEvent here, too
1637 struct kevent kev;
1638 EV_SET(&kev, 1, EVFILT_USER, 0, NOTE_TRIGGER, 0, NULL);
1639 if (-1 == kevent(kevent_queue, &kev, 1, NULL, 0, NULL))
1640 {
1641 PLOG(PL_ERROR, "ProtoDispatcher::SetBreak() kevent() error: %s\n", GetErrorString());
1642 return false;
1643 }
1644 #endif // if/else !KQUEUE
1645 return true;
1646 } // end ProtoDispatcher::SetBreak()
1647
1648 void ProtoDispatcher::RemoveBreak()
1649 {
1650 #ifndef USE_KQUEUE
1651 if (INVALID_DESCRIPTOR != break_stream.GetDescriptor())
1652 {
1653 #ifdef USE_EPOLL
1654 if (!EpollChange(break_stream.GetDescriptor(), EPOLLIN, EPOLL_CTL_DEL, &break_stream))
1655 {
1656 PLOG(PL_ERROR, "ProtoDispatcher::RemoveBreak() error: EpollChange() failed!\n");
1657 }
1658 #endif // USE_EPOLL
1659 // Close down the break_stream ProtoEvent
1660 break_event.Close();
1661 }
1662 #else // USE_KQUEUE
1663 struct kevent kev;
1664 EV_SET(&kev, 1, EVFILT_USER, EV_DELETE, 0, 0, NULL);
1665 if (-1 == kevent(kevent_queue, &kev, 1, NULL, 0, NULL))
1666 PLOG(PL_ERROR, "ProtoDispatcher::RemoveBreak() kevent() error: %s\n", GetErrorString());
1667 #endif // if/else !KQUEUE
1668 } // end ProtoDispatcher::RemoveBreak()
1669
1670
1671 /**
1672 * Warning! This may block indefinitely if !IsPending() ...
1673 */
1674 void ProtoDispatcher::Wait()
1675 {
1676 // (TBD) We could put some code here to protect this from
1677 // being called by the wrong thread?
1678
1679 #if defined(USE_KQUEUE) || defined(HAVE_PSELECT) || defined(USE_TIMERFD)
1680 #define USE_TIMESPEC 1 // so we can use the "struct timespec" created here
1681 #endif // USE_KQUEUE || HAVE_PSELECT || USE_TIMERFD
1682
1683
1684 #ifdef USE_SELECT
1685 int maxDescriptor = -1;
1686 FD_ZERO(&input_set);
1687 FD_ZERO(&output_set);
1688 #endif // USE_SELECT
1689
1690 #ifdef USE_TIMESPEC
1691 struct timespec timeout;
1692 struct timespec* timeoutPtr = NULL;
1693 #else
1694 struct timeval timeout;
1695 struct timeval* timeoutPtr = NULL;
1696 #endif // if/else USE_TIMESPEC
1697 //double timerDelay = ProtoTimerMgr::GetTimeRemaining();
1698 double timerDelay = timer_delay;
1699 if (timerDelay < 0.0)
1700 {
1701 #ifdef USE_TIMERFD
1702 timeout.tv_sec = 0;
1703 timeout.tv_nsec = 0;
1704 #endif //USE_TIMERFD
1705
1706 }
1707 else
1708 {
1709 // We have observed some different thresholds on "precise timing" for
1710 // for different systems and APIs
1711 #if defined(MACOSX)
1712 #define PRECISE_THRESHOLD 1.0e-05 // about 10 microseconds for MacOS whether select(), kevent(), etc
1713 #elif defined(LINUX)
1714 #ifdef USE_TIMERFD
1715 #define PRECISE_THRESHOLD 2.0e-05 // about 20 microseconds for Linux timerfd usage
1716 #else
1717 #define PRECISE_THRESHOLD 2.0e-03 // about 2 msec for Linux select()
1718 #endif // if/else USE_TIMERFD
1719 #else
1720 #define PRECISE_THRESHOLD 2.0e-03 // assume 2 msec for other Unix
1721 #endif // if/else MACOSX / LINUX / OTHER
1722
1723 // If (true == precise_timing) essentially force polling for small delays
1724 // (Note this will consume CPU resources)
1725 if (precise_timing && (timerDelay < PRECISE_THRESHOLD)) timerDelay = 0.0;
1726 timeout.tv_sec = (unsigned long)timerDelay;
1727 #ifdef USE_TIMESPEC
1728 timeout.tv_nsec =
1729 (unsigned long)(1.0e+09 * (timerDelay - (double)timeout.tv_sec));
1730 #else
1731 timeout.tv_usec =
1732 (suseconds_t)(1.0e+06 * (timerDelay - (double)timeout.tv_sec));
1733 #endif // if/else USE_TIMESPEC
1734 timeoutPtr = &timeout;
1735 #ifdef USE_TIMERFD
1736 if ((0 != timeout.tv_nsec) || (0 != timeout.tv_sec))
1737 {
1738 // Install the timerfd descriptor to our "input_set"
1739 // configured with an appropriate one-shot timeout
1740 struct itimerspec timerSpec;
1741 timerSpec.it_interval.tv_sec = timerSpec.it_interval.tv_nsec = 0; // non-repeating timeout?
1742 timerSpec.it_value = timeout;
1743 if (0 == timerfd_settime(timer_stream.GetDescriptor(), 0, &timerSpec, 0))
1744 {
1745 #ifdef USE_SELECT
1746 FD_SET(timer_stream.GetDescriptor(), &input_set);
1747 if (timer_stream.GetDescriptor() > maxDescriptor)
1748 maxDescriptor = timer_stream.GetDescriptor();
1749 #endif // USE_SELECT
1750 timeoutPtr = NULL; // select() (or pselect()) will be using "timer_fd" instead
1751 }
1752 else
1753 {
1754 PLOG(PL_ERROR, "ProtoDispatcher::Wait() timerfd_settime() error: %s\n", GetErrorString());
1755
1756 }
1757 }
1758 #endif // USE_TIMERFD
1759 }
1760
1761 #if defined(USE_SELECT)
1762 // Monitor "break_pipe" if we are a threaded dispatcher
1763 if (IsThreaded())
1764 {
1765 FD_SET(break_event.GetDescriptor(), &input_set);
1766 if (break_event.GetDescriptor() > maxDescriptor)
1767 maxDescriptor = break_event.GetDescriptor();
1768 }
1769
1770 // Iterate through and add streams to our FD_SETs
1771 StreamTable::Iterator iterator(stream_table);
1772 Stream* stream;
1773 while (NULL != (stream = iterator.GetNextItem()))
1774 {
1775 if (stream->IsInput())
1776 {
1777 Descriptor descriptor = stream->GetInputHandle();
1778 FD_SET(descriptor, &input_set);
1779 if (descriptor > maxDescriptor) maxDescriptor = descriptor;
1780 }
1781 if (stream->IsOutput())
1782 {
1783 Descriptor descriptor = stream->GetOutputHandle();
1784 FD_SET(descriptor, &output_set);
1785 if (descriptor > maxDescriptor) maxDescriptor = descriptor;
1786 }
1787 // TBD - handle exception notification ???
1788 }
1789 #ifdef HAVE_PSELECT
1790 wait_status = pselect(maxDescriptor+1,
1791 (fd_set*)&input_set,
1792 (fd_set*)&output_set,
1793 (fd_set*) NULL,
1794 timeoutPtr,
1795 (sigset_t*)NULL);
1796 #else
1797 wait_status = select(maxDescriptor+1,
1798 (fd_set*)&input_set,
1799 (fd_set*)&output_set,
1800 (fd_set*) NULL,
1801 (timeval*)timeoutPtr);
1802 #endif // if/else HAVE_PSELECT
1803
1804 #elif defined(USE_EPOLL)
1805
1806 // If no sockets were installed yet, create epoll_fd early
1807 if (-1 == epoll_fd)
1808 {
1809 if (-1 == (epoll_fd = epoll_create1(0)))
1810 {
1811 PLOG(PL_ERROR, "ProtoDispatcher::Wait() epoll_create() error: %s\n",
1812 GetErrorString());
1813 return;
1814 }
1815 }
1816
1817 // Note if (NULL == timeoutPtr), then the timer_fd has been set up with the proper timeout value
1818 // (otherwise we convert "timerDelay" to milliseconds)
1819 wait_status = epoll_wait(epoll_fd, epoll_event_array, EPOLL_ARRAY_SIZE, (NULL != timeoutPtr) ? (int)(timerDelay*1000.0) : -1);
1820
1821 #elif defined(USE_KQUEUE)
1822 if (-1 == kevent_queue)
1823 {
1824 // Need to create our kqueue instance
1825 if (-1 == (kevent_queue = kqueue()))
1826 {
1827 PLOG(PL_ERROR, "ProtoDispatcher::Wait() kqueue() error: %s\n", GetErrorString());
1828 // TBD - should we set "run" to false here???
1829 wait_status = -1;
1830 return;
1831 }
1832 }
1833 wait_status = kevent(kevent_queue, NULL, 0, kevent_array, KEVENT_ARRAY_SIZE, timeoutPtr);
1834 // TBD - should we print a message here on error? (Dispatch() does this for us)
1835 #else
1836 #error "undefined async i/o mechanism" // to make sure we implement something
1837 #endif // !USE_SELECT && !USE_KQUEUE
1838
1839 } // end ProtoDispatcher::Wait()
1840
1841 void ProtoDispatcher::Dispatch()
1842 {
1843 #if defined(USE_SELECT)
1844 // Here the "wait_status" is the return value from the select() call
1845 switch (wait_status)
1846 {
1847 case -1:
1848 if (EINTR != errno)
1849 PLOG(PL_ERROR, "ProtoDispatcher::Dispatch() select() error: %s\n", GetErrorString());
1850 //OnSystemTimeout();
1851 break;
1852
1853 case 0:
1854 // timeout only ?
1855 OnSystemTimeout();
1856 break;
1857
1858 default:
1859 // Iterate through and check/dispatch streams
1860 StreamTable::Iterator iterator(stream_table);
1861 Stream* stream;
1862 while (NULL != (stream = iterator.GetNextItem()))
1863 {
1864 switch (stream->GetType())
1865 {
1866 case Stream::CHANNEL:
1867 {
1868 ProtoChannel& theChannel = static_cast<ChannelStream*>(stream)->GetChannel();
1869 // A channel _might_ be implemented with separate input/output descriptors
1870 if (stream->IsInput())
1871 {
1872 if (FD_ISSET(theChannel.GetInputEventHandle(), &input_set))
1873 theChannel.OnNotify(ProtoChannel::NOTIFY_INPUT);
1874 }
1875 // Note that if the input notification handling caused the
1876 // channel to even be deleted, because the stream is "pooled"
1877 // and its flags zero, we're safe. If the channel is removed
1878 // and the stream repurposed, we could throw a false notification
1879 // here, but no big deal (same is true for socket handling
1880 // immediately below). TBD - use FD_CLR in "UpdateChannelNotification()"
1881 // to avoid this possibility
1882 if (stream->IsOutput())
1883 {
1884 if (FD_ISSET(theChannel.GetOutputEventHandle(), &output_set))
1885 theChannel.OnNotify(ProtoChannel::NOTIFY_OUTPUT);
1886 }
1887 break;
1888 }
1889 case Stream::SOCKET:
1890 {
1891 // A socket has a single input/output descriptor
1892 ProtoSocket& theSocket = static_cast<SocketStream*>(stream)->GetSocket();
1893 Descriptor descriptor = theSocket.GetHandle();
1894 if (stream->IsInput() && FD_ISSET(descriptor, &input_set))
1895 theSocket.OnNotify(ProtoSocket::NOTIFY_INPUT);
1896 // TBD - what if stream and/or theSocket was deleted?
1897 if (stream->IsOutput() && FD_ISSET(descriptor, &output_set))
1898 theSocket.OnNotify(ProtoSocket::NOTIFY_OUTPUT);
1899 break;
1900 }
1901 case Stream::GENERIC:
1902 {
1903 // A generic stream has a single input/output descriptor
1904 Descriptor descriptor = static_cast<GenericStream*>(stream)->GetDescriptor();
1905 if (stream->IsInput() && FD_ISSET(descriptor, &input_set))
1906 static_cast<GenericStream*>(stream)->OnEvent(EVENT_INPUT);
1907 if (stream->IsOutput() && FD_ISSET(descriptor, &output_set))
1908 static_cast<GenericStream*>(stream)->OnEvent(EVENT_OUTPUT);
1909 break;
1910 }
1911 case Stream::TIMER:
1912 break;
1913 case Stream::EVENT:
1914 {
1915 EventStream* eventStream = static_cast<EventStream*>(stream);
1916 if (FD_ISSET(eventStream->GetDescriptor(), &input_set))
1917 {
1918 ProtoEvent& theEvent = eventStream->GetEvent();
1919 if (theEvent.GetAutoReset()) theEvent.Reset();
1920 theEvent.OnNotify();
1921 }
1922 break;
1923 }
1924 } // end switch stream->GetType())
1925 }
1926 if ((INVALID_DESCRIPTOR != break_stream.GetDescriptor()) &&
1927 FD_ISSET(break_stream.GetDescriptor(), &input_set))
1928 {
1929 // We were signaled so reset our break_event
1930 break_event.Reset(); // break_event is auto reset
1931 }
1932 #ifdef USE_TIMERFD
1933 // TBD - should we put this code up at the top as the first check
1934 // instead of checking all descriptors, all the time (maybe "epoll()" will help?)
1935 if (timer_stream.GetDescriptor() != INVALID_DESCRIPTOR &&
1936 FD_ISSET(timer_stream.GetDescriptor(), &input_set))
1937 {
1938 uint64_t expirations = 0;
1939 if (read(timer_stream.GetDescriptor(), &expirations, sizeof(expirations)) < 0)
1940 PLOG(PL_ERROR, "ProtoDispatcher::Dispatch() read(timer_fd) error: %s\n", GetErrorString());
1941 }
1942 #endif // USE_TIMERFD
1943 OnSystemTimeout();
1944 break;
1945 } // end switch(wait_status) [USE_SELECT]
1946
1947 #elif defined(USE_EPOLL)
1948 switch(wait_status)
1949 {
1950 case -1:
1951 if (EINTR != errno)
1952 PLOG(PL_ERROR, "ProtoDispatcher::Dispatch() epoll_wait() error: %s\n", GetErrorString());
1953 break;
1954
1955 case 0:
1956 // timeout only
1957 OnSystemTimeout();
1958 break;
1959
1960 default:
1961 struct epoll_event* evp = epoll_event_array;
1962 for (int i = 0; i < wait_status; i++)
1963 {
1964 // First check for an error condition
1965 /*if (0 != (EPOLLERR & evp->events))
1966 {
1967 // We don't check for EPOLLHUP since ProtoSocket handles that
1968 PLOG(PL_ERROR, "ProtoDispatcher::Dispatch() epoll event error: %s\n", GetErrorString());
1969 }
1970 else */
1971 if (NULL != evp->data.ptr)
1972 {
1973 // TBD - process EPOLLHUP events?
1974 Stream* stream = (Stream*)evp->data.ptr;
1975 switch (stream->GetType())
1976 {
1977 case Stream::CHANNEL:
1978 {
1979 ProtoChannel& channel = static_cast<ChannelStream*>(stream)->GetChannel();
1980 if (0 != (EPOLLIN & evp->events))
1981 {
1982 if (stream->IsInput())
1983 channel.OnNotify(ProtoChannel::NOTIFY_INPUT);
1984 }
1985 if (0 != (EPOLLOUT & evp->events))
1986 {
1987 if (stream->IsOutput())
1988 channel.OnNotify(ProtoChannel::NOTIFY_OUTPUT);
1989 }
1990 if (0 != (EPOLLERR & evp->events))
1991 {
1992 PLOG(PL_ERROR, "ProtoDispatcher::Dispatch() ProtoChannel epoll event error: %s\n", GetErrorString());
1993 // Throw a notification so error will be detected by app???
1994 if (stream->IsInput())
1995 channel.OnNotify(ProtoChannel::NOTIFY_INPUT);
1996 else if (stream->IsOutput())
1997 channel.OnNotify(ProtoChannel::NOTIFY_OUTPUT);
1998 }
1999 break;
2000 }
2001 case Stream::SOCKET:
2002 {
2003 ProtoSocket& socket = static_cast<SocketStream*>(stream)->GetSocket();
2004 if (0 != (EPOLLIN & evp->events))
2005 {
2006 if (stream->IsInput())
2007 socket.OnNotify(ProtoSocket::NOTIFY_INPUT);
2008 }
2009 if (0 != (EPOLLOUT & evp->events))
2010 {
2011 if (stream->IsOutput())
2012 socket.OnNotify(ProtoSocket::NOTIFY_OUTPUT);
2013 }
2014 if (0 != (EPOLLERR & evp->events))
2015 {
2016 socket.OnNotify(ProtoSocket::NOTIFY_ERROR);
2017 // Alternatively throw an input or output notification so app gets notified?
2018 //if (stream->IsInput())
2019 // socket.OnNotify(ProtoSocket::NOTIFY_INPUT);
2020 //else if (stream->IsOutput())
2021 // socket.OnNotify(ProtoSocket::NOTIFY_OUTPUT);
2022 }
2023 break;
2024 }
2025 case Stream::GENERIC:
2026 {
2027 if (0 != (EPOLLIN & evp->events))
2028 {
2029 if (stream->IsInput())
2030 static_cast<GenericStream*>(stream)->OnEvent(EVENT_INPUT);
2031 }
2032 if (0 != (EPOLLOUT & evp->events))
2033 {
2034 if (stream->IsOutput())
2035 static_cast<GenericStream*>(stream)->OnEvent(EVENT_OUTPUT);
2036 }
2037 if (0 != (EPOLLERR & evp->events))
2038 {
2039 // Throw an input or output notification so app gets notified?
2040 if (stream->IsInput())
2041 static_cast<GenericStream*>(stream)->OnEvent(EVENT_INPUT);
2042 else if (stream->IsOutput())
2043 static_cast<GenericStream*>(stream)->OnEvent(EVENT_OUTPUT);
2044 }
2045 break;
2046 }
2047 case Stream::TIMER:
2048 {
2049 #ifdef USE_TIMERFD
2050 // This _should_ only be our ProtoDispatcher::timer_stream
2051 // Clear the timerfd with a read() call (timeout dispatched below)
2052 uint64_t expirations = 0;
2053 if (read(static_cast<TimerStream*>(stream)->GetDescriptor(), &expirations, sizeof(expirations)) < 0)
2054 PLOG(PL_ERROR, "ProtoDispatcher::Dispatch() read(timer_fd) error: %s\n", GetErrorString());
2055 #endif // USE_TIMERFD
2056 break;
2057 }
2058 case Stream::EVENT:
2059 {
2060 if (0 != (EPOLLIN & evp->events))
2061 {
2062 EventStream* eventStream = static_cast<EventStream*>(stream);
2063 ProtoEvent& event = eventStream->GetEvent();
2064 if (event.GetAutoReset()) event.Reset();
2065 event.OnNotify();
2066 }
2067 break;
2068 }
2069 } // end switch(stream->GetType()) [USE_EPOLL]
2070 }
2071 else
2072 {
2073 // This must be a nullified event so do nothing
2074 }
2075 evp++;
2076 } // end for (i = 0..wait_status)
2077 OnSystemTimeout();
2078 break;
2079 } // end switch(wait_status) [USE_EPOLL]
2080
2081 #elif defined(USE_KQUEUE)
2082 // Here the "wait_status" is the return value from the kevent() call
2083 switch (wait_status)
2084 {
2085 case -1:
2086 if (EINTR != errno)
2087 PLOG(PL_ERROR, "ProtoDispatcher::Dispatch() kevent() error: %s\n", GetErrorString());
2088 //OnSystemTimeout();
2089 break;
2090
2091 case 0:
2092 // timeout only
2093 OnSystemTimeout();
2094 break;
2095
2096 default:
2097 {
2098 // The return value is the number of events available
2099 // (Note "wait_status" may change on the fly, here if
2100 // notification results in stream removal)
2101 struct kevent* kep = kevent_array;
2102 for (int i = 0; i < wait_status; i++)
2103 {
2104 // First check if there was an error for the event
2105 if (0 != (EV_ERROR & kep->flags))
2106 {
2107 PLOG(PL_ERROR, "ProtoDispatcher::Dispatch() kqueue event error: %s\n", GetErrorString((int)(kep->data)));
2108 kep++;
2109 continue;
2110 }
2111 Stream* stream = (Stream*)kep->udata;
2112 switch (kep->filter)
2113 {
2114 case EVFILT_READ:
2115 ASSERT(NULL != stream);
2116 if (!stream->IsInput()) break;
2117 switch (stream->GetType())
2118 {
2119 case Stream::CHANNEL:
2120 static_cast<ChannelStream*>(stream)->GetChannel().OnNotify(ProtoChannel::NOTIFY_INPUT);
2121 break;
2122 case Stream::SOCKET:
2123 static_cast<SocketStream*>(stream)->GetSocket().OnNotify(ProtoSocket::NOTIFY_INPUT);
2124 break;
2125 case Stream::GENERIC:
2126 static_cast<GenericStream*>(stream)->OnEvent(EVENT_INPUT);
2127 break;
2128 case Stream::TIMER:
2129 // No Stream::TIMER or Stream::EVENT is used eith EVFILT_READ for now
2130 break;
2131 case Stream::EVENT:
2132 EventStream* eventStream = static_cast<EventStream*>(stream);
2133 ProtoEvent& theEvent = eventStream->GetEvent();
2134 if (theEvent.GetAutoReset()) theEvent.Reset();
2135 theEvent.OnNotify();
2136 break;
2137 }
2138 break;
2139
2140 case EVFILT_WRITE:
2141 ASSERT(NULL != stream);
2142 if (!stream->IsOutput()) break;
2143 switch (stream->GetType())
2144 {
2145 case Stream::CHANNEL:
2146 static_cast<ChannelStream*>(stream)->GetChannel().OnNotify(ProtoChannel::NOTIFY_OUTPUT);
2147 break;
2148 case Stream::SOCKET:
2149 static_cast<SocketStream*>(stream)->GetSocket().OnNotify(ProtoSocket::NOTIFY_OUTPUT);
2150 break;
2151 case Stream::GENERIC:
2152 static_cast<GenericStream*>(stream)->OnEvent(EVENT_OUTPUT);
2153 break;
2154 case Stream::TIMER:
2155 case Stream::EVENT:
2156 // No Stream::TIMER or Stream::EVENT is used with EVFILT_WRITE
2157 break;
2158 }
2159 break;
2160
2161 case EVFILT_USER:
2162 // Nothing to be done since EVFILT_USER w/ EV_CLEAR resets itself
2163 // (or was nullified event)
2164 break;
2165
2166 case EVFILT_TIMER:
2167 // We found that "EVFILT_TIMER" was not as precise as the kevent() timeout, so we don't use it
2168 default:
2169 PLOG(PL_ERROR, "ProtoDispatcher::Dispatch() error: unexpected event filter type %d\n", kep->filter);
2170 break;
2171
2172 } // switch(kep->filter)
2173 kep++;
2174 }
2175 OnSystemTimeout(); // called to service timers in case one or more is ready
2176 break;
2177 }
2178 } // end switch(wait_status) [USE_KQUEUE]
2179 #else // !USE_SELECT && !USE_KQUEUE
2180 #error "undefined async i/o mechanism" // to make sure we implement something
2181 #endif // end if/else USE_SELECT, USE_KQUEUE, ...
2182 wait_status = 0; // reset "wait_status" since we're done
2183 } // end ProtoDispatcher::Dispatch()
2184
2185 #endif // UNIX
2186
2187
2188 #ifdef WIN32
2189
2190 bool ProtoDispatcher::InstallBreak()
2191 {
2192 ASSERT(INVALID_DESCRIPTOR == break_event.GetDescriptor());
2193 if (!break_event.Open())
2194 {
2195 PLOG(PL_ERROR, "ProtoDispatcher::InstallBreak() CreateEvent() error\n");
2196 return false;
2197 }
2198 int index = Win32AddStream(break_stream, break_event.GetDescriptor());
2199 if (index < 0)
2200 {
2201 PLOG(PL_ERROR, "ProtoDispatcher::InstallBreak() error: add break_stream failed!\n");
2202 break_event.Close();
2203 return false;
2204 }
2205 break_stream.SetIndex(index);
2206 return true;
2207 } // end ProtoDispatcher::InstallBreak()
2208
2209 bool ProtoDispatcher::SetBreak()
2210 {
2211 if (!break_event.Set())
2212 {
2213 PLOG(PL_ERROR, "ProtoDispatcher::SetBreak() break_event.Set() error: %s\n", GetErrorString());
2214 return false;
2215 }
2216 return true;
2217 } // end ProtoDispatcher::SetBreak()
2218
2219 void ProtoDispatcher::RemoveBreak()
2220 {
2221 if (INVALID_DESCRIPTOR != break_stream.GetDescriptor())
2222 {
2223 Win32RemoveStream(break_stream.GetIndex());
2224 break_stream.SetIndex(-1);
2225 break_event.Close();
2226 }
2227 } // end ProtoDispatcher::RemoveBreak()
2228
2229 bool ProtoDispatcher::Win32Init()
2230 {
2231 HINSTANCE theInstance = GetModuleHandle(NULL);
2232 // Register our msg_window class
2233 WNDCLASS cl;
2234 cl.style = CS_HREDRAW | CS_VREDRAW;
2235 cl.lpfnWndProc = MessageHandler;
2236 cl.cbClsExtra = 0;
2237 cl.cbWndExtra = 0;
2238 cl.hInstance = theInstance;
2239 cl.hIcon = NULL;
2240 cl.hCursor = NULL;
2241 cl.hbrBackground = NULL;
2242 cl.lpszMenuName = NULL;
2243 cl.lpszClassName = _T("ProtoDispatcher");
2244 if (!RegisterClass(&cl))
2245 {
2246 LPVOID lpMsgBuf;
2247 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
2248 FORMAT_MESSAGE_FROM_SYSTEM |
2249 FORMAT_MESSAGE_IGNORE_INSERTS,
2250 NULL, GetLastError(),
2251 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
2252 (LPTSTR) &lpMsgBuf, 0, NULL );
2253 // Display the string.
2254 MessageBox( NULL, (LPCTSTR)lpMsgBuf, (LPCTSTR)"Error", MB_OK | MB_ICONINFORMATION );
2255 // Free the buffer.
2256 LocalFree(lpMsgBuf);
2257 PLOG(PL_ERROR, "ProtoDispatcher::Win32Init() Error registering message window class!\n");
2258 return false;
2259 }
2260
2261 // Create hidden "msg_window" to receive event window messages
2262 HWND parent = NULL;
2263 #ifdef HWND_MESSAGE
2264 parent = HWND_MESSAGE;
2265 #endif // HWND_MESSAGE
2266 msg_window = CreateWindowEx(0, _T("ProtoDispatcher"), // registered class name
2267 _T("ProtoDispatcher"), // window name
2268 WS_OVERLAPPED, // window style
2269 CW_USEDEFAULT, // horizontal position of window
2270 CW_USEDEFAULT, // vertical position of window
2271 0, // window width
2272 0, // window height
2273 parent, // handle to parent or owner window
2274 NULL, // menu handle or child identifier
2275 theInstance, // handle to application instance
2276 this); // window-creation data
2277 if (NULL != msg_window)
2278 {
2279 ShowWindow(msg_window, SW_HIDE);
2280 return true;
2281 }
2282 else
2283 {
2284 PLOG(PL_ERROR, "ProtoDispatcher::Win32Init() Error creating message window: %s (%d)\n",
2285 GetErrorString(), GetLastError());
2286 return false;
2287 }
2288 } // end ProtoDispatcher::Win32Init()
2289
2290 void ProtoDispatcher::Win32Cleanup()
2291 {
2292 if (NULL != msg_window)
2293 {
2294 DestroyWindow(msg_window);
2295 msg_window = NULL;
2296 }
2297 } // end Win32Cleanup()
2298
2299
2300 // WIN32 implementation of ProtoDispatcher::UpdateStreamNotification()
2301 bool ProtoDispatcher::UpdateStreamNotification(Stream& stream, NotificationCommand cmd)
2302 {
2303 switch (cmd)
2304 {
2305 case ENABLE_INPUT:
2306 {
2307 ASSERT(stream.GetIndex() < 0); // input already enabled?!
2308 int index = Win32AddStream(stream, stream.GetInputHandle());
2309 if (index < 0)
2310 {
2311 PLOG(PL_ERROR, "ProtoDispatcher::UpdateStreamNotification() error adding input stream\n");
2312 return false;
2313 }
2314 stream.SetIndex(index);
2315 stream.SetNotifyFlag(Stream::NOTIFY_INPUT);
2316 break;
2317 }
2318 case DISABLE_INPUT:
2319 {
2320 ASSERT(stream.GetIndex() >= 0);
2321 Win32RemoveStream(stream.GetIndex());
2322 stream.SetIndex(-1);
2323 stream.UnsetNotifyFlag(Stream::NOTIFY_INPUT);
2324 break;
2325 }
2326 case ENABLE_OUTPUT:
2327 {
2328 ASSERT(stream.GetOutdex() < 0); // output already enabled?!
2329 int outdex = Win32AddStream(stream, stream.GetOutputHandle());
2330 if (outdex < 0)
2331 {
2332 PLOG(PL_ERROR, "ProtoDispatcher::UpdateStreamNotification() error adding output stream\n");
2333 return false;
2334 }
2335 stream.SetOutdex(outdex);
2336 stream.SetNotifyFlag(Stream::NOTIFY_OUTPUT);
2337 break;
2338 }
2339 case DISABLE_OUTPUT:
2340 {
2341 ASSERT(stream.GetOutdex() >= 0);
2342 Win32RemoveStream(stream.GetOutdex());
2343 stream.SetOutdex(-1);
2344 stream.UnsetNotifyFlag(Stream::NOTIFY_OUTPUT);
2345 break;
2346 }
2347 case DISABLE_ALL:
2348 {
2349 if (stream.GetIndex() >= 0)
2350 {
2351 Win32RemoveStream(stream.GetIndex());
2352 stream.SetIndex(-1);
2353 }
2354 if (stream.GetOutdex() >= 0)
2355 {
2356 Win32RemoveStream(stream.GetOutdex());
2357 stream.SetIndex(-1);
2358 }
2359 stream.ClearNotifyFlags();
2360 break;
2361 }
2362 }
2363 return true;
2364 } // end ProtoDispatcher::UpdateStreamNotification() [WIN32]
2365
2366 int ProtoDispatcher::Win32AddStream(Stream& stream, HANDLE handle)
2367 {
2368 DWORD index = stream_count;
2369 if (index >= stream_array_size)
2370 {
2371 if (!Win32IncreaseStreamArraySize())
2372 {
2373 PLOG(PL_ERROR, "ProtoDispatcher::AddStream() error increasing array size\n");
2374 return -1;
2375 }
2376 }
2377 stream_handles_array[index] = handle;
2378 stream_ptrs_array[index] = &stream;
2379 stream_count++;
2380 return index;
2381 } // end ProtoDispatcher::Win32AddStream()
2382
2383 void ProtoDispatcher::Win32RemoveStream(int index)
2384 {
2385 ASSERT((index >= 0) && (index < (int)stream_count));
2386 unsigned int moveCount = stream_count - index - 1;
2387 memmove(stream_handles_array+index, stream_handles_array+index+1,
2388 moveCount*sizeof(HANDLE));
2389 memmove(stream_ptrs_array+index, stream_ptrs_array+index+1,
2390 moveCount*sizeof(Stream*));
2391 stream_count--;
2392 for (; index < (int)stream_count; index++)
2393 stream_ptrs_array[index]->SetIndex(index);
2394 } // end ProtoDispatcher::Win32RemoveStream
2395
2396 bool ProtoDispatcher::Win32IncreaseStreamArraySize()
2397 {
2398 unsigned int newSize = (0 != stream_array_size) ? (2 * stream_array_size) : DEFAULT_STREAM_ARRAY_SIZE;
2399 HANDLE* hPtr = new HANDLE[newSize];
2400 Stream** iPtr = new Stream*[newSize];
2401 if ((NULL != hPtr) && (NULL != iPtr))
2402 {
2403 if (0 != stream_count)
2404 {
2405 memcpy(hPtr, stream_handles_array, stream_count*sizeof(HANDLE));
2406 memcpy(iPtr, stream_ptrs_array, stream_count*sizeof(Stream*));
2407 }
2408 if (NULL != stream_handles_array)
2409 {
2410 delete[] stream_handles_array;
2411 delete[] stream_ptrs_array;
2412 }
2413 stream_handles_array = hPtr;
2414 stream_ptrs_array = iPtr;
2415 return true;
2416 }
2417 else
2418 {
2419 PLOG(PL_ERROR, "ProtoDispatcher::Win32IncreaseStreamArraySize() new stream_array error: %s\n",
2420 GetErrorString());
2421 if (NULL != hPtr) delete[] hPtr;
2422 if (NULL != iPtr) delete[] iPtr;
2423 return false;
2424 }
2425 } // end ProtoDispatcher::Win32IncreaseStreamArraySize()
2426
2427 void ProtoDispatcher::Wait()
2428 {
2429 double timerDelay = timer_delay;
2430 // Don't wait if we have "ready" streams pending
2431 if (!ready_stream_list.IsEmpty()) timerDelay = 0.0;
2432
2433 #ifdef USE_WAITABLE_TIMER
2434 DWORD msec = INFINITE;
2435 if (timerDelay < 0.0)
2436 {
2437 if (timer_active)
2438 {
2439 CancelWaitableTimer(timer_stream.GetDescriptor());
2440 timer_active = false;
2441 }
2442 if (timer_stream.GetIndex() >= 0)
2443 {
2444 Win32RemoveStream(timer_stream.GetIndex());
2445 timer_stream.SetIndex(-1);
2446 }
2447 }
2448 else if (0.0 == timerDelay)
2449 {
2450 if (timer_active)
2451 {
2452 CancelWaitableTimer(timer_stream.GetDescriptor());
2453 timer_active = false;
2454 }
2455 if (timer_stream.GetIndex() >= 0)
2456 {
2457 Win32RemoveStream(timer_stream.GetIndex());
2458 timer_stream.SetIndex(-1);
2459 }
2460 msec = 0;
2461 }
2462 else
2463 {
2464 // The "negative" dueTime makes a "relative" waitable timer
2465 LARGE_INTEGER dueTime;
2466 dueTime.QuadPart = (LONGLONG)(-(timerDelay * 1.0e+07)); // convert to 100 nsec ticks
2467 LONG period = 0;
2468 if (0 == SetWaitableTimer(timer_stream.GetDescriptor(), &dueTime, 0, NULL, NULL, FALSE))
2469 {
2470 PLOG(PL_ERROR, "ProtoDispatcher::Wait() SetWaitableTimer() error: %s\n", GetErrorString());
2471 msec = (DWORD)(1000.0 * timerDelay);
2472 if ((timerDelay > 0.0) && (0 == msec) && !precise_timing) msec = 1;
2473 timer_active = false;
2474 }
2475 else
2476 {
2477 if (timer_stream.GetIndex() < 0)
2478 {
2479 int index = Win32AddStream(timer_stream, timer_stream.GetDescriptor());
2480 if (index < 0)
2481 {
2482 PLOG(PL_ERROR, "ProtoDispatcher::Wait() error: unable to insert timer_stream\n");
2483 msec = (DWORD)(1000.0 * timerDelay);
2484 if ((timerDelay > 0.0) && (0 == msec) && !precise_timing) msec = 1;
2485 timer_active = false;
2486 }
2487 else
2488 {
2489 timer_stream.SetIndex(index);
2490 timer_active = true;
2491 }
2492 }
2493 else
2494 {
2495 timer_active = true;
2496 }
2497 }
2498 }
2499 #else
2500 DWORD msec = (timerDelay < 0.0) ? INFINITE : ((DWORD)(1000.0 * timerDelay));
2501 // If (false == precise_timing) enforce minimum delay of 1 msec
2502 // (Note the busy-wait "precise_timing" will consume additional CPU resources)
2503 if ((timerDelay > 0.0) && (0 == msec) && !precise_timing) msec = 1;
2504 #endif // else/if USE_WAITABLE_TIMER
2505
2506 // Set some waitFlags
2507 DWORD waitFlags = 0;
2508 // on WinNT 4.0, getaddrinfo() doesn't work, so we check the OS version
2509 // to decide what to do. Try "gethostbyaddr()" if it's an old OS (e.g. NT 4.0 or earlier)
2510 OSVERSIONINFO vinfo;
2511 vinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
2512 GetVersionEx(&vinfo);
2513 if ((VER_PLATFORM_WIN32_NT == vinfo.dwPlatformId) &&
2514 ((vinfo.dwMajorVersion > 4) ||
2515 ((vinfo.dwMajorVersion == 4) && (vinfo.dwMinorVersion > 0))))
2516 waitFlags |= MWMO_INPUTAVAILABLE; // it's a modern OS where MWMO_INPUTAVAILABLE should work!
2517 // (TBD) should MWMO_INPUTAVAILABLE be set fo Win98 type OS's???
2518 #ifndef _WIN32_WCE
2519 waitFlags |= MWMO_ALERTABLE;
2520 #endif // !_WIN32_WCE
2521 wait_status = MsgWaitForMultipleObjectsEx(stream_count, // number of handles in array
2522 stream_handles_array, // object-handle array
2523 msec, // time-out interval
2524 QS_ALLINPUT, // input-event type
2525 waitFlags);
2526 } // end ProtoDispatcher::Wait()
2527
2528 void ProtoDispatcher::Dispatch()
2529 {
2530 switch (wait_status)
2531 {
2532 case -1: // error status
2533 {
2534 char errorString[256];
2535 errorString[255] = '\0';
2536 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
2537 FORMAT_MESSAGE_IGNORE_INSERTS,
2538 NULL,
2539 GetLastError(),
2540 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
2541 (LPTSTR) errorString, 255, NULL);
2542 PLOG(PL_ERROR, "ProtoDispatcher::Dispatch() MsgWaitForMultipleObjectsEx() error: %s\n", errorString);
2543 break;
2544 }
2545 default:
2546 {
2547 // Handle any "ready" sockets that are "io_pending"
2548 // (e.g., WIN32 only does "edge triggering" on sockets)
2549 // TBD - keep a separate list of io_pending socket streams?
2550 StreamList::Iterator sit(ready_stream_list);
2551 Stream* nextStream;
2552 while (NULL != (nextStream = sit.GetNextItem()))
2553 {
2554 switch (nextStream->GetType())
2555 {
2556 case Stream::CHANNEL:
2557 {
2558 ProtoChannel& theChannel = static_cast<ChannelStream*>(nextStream)->GetChannel();
2559 // (TBD) Make this safer (i.e. if notification destroys channel)
2560 if (theChannel.InputNotification())
2561 theChannel.OnNotify(ProtoChannel::NOTIFY_INPUT);
2562 if (theChannel.OutputNotification())
2563 theChannel.OnNotify(ProtoChannel::NOTIFY_OUTPUT);
2564 break;
2565 }
2566 case Stream::SOCKET:
2567 {
2568 ProtoSocket& theSocket = static_cast<SocketStream*>(nextStream)->GetSocket();
2569 // (TBD) Make this safer (i.e. if notification destroys socket)
2570 if (theSocket.InputNotification())
2571 theSocket.OnNotify(ProtoSocket::NOTIFY_INPUT);
2572 if (theSocket.OutputNotification())
2573 theSocket.OnNotify(ProtoSocket::NOTIFY_OUTPUT);
2574 break;
2575 }
2576 default:
2577 {
2578 ASSERT(0);
2579 break;
2580 }
2581 }
2582 }
2583 if (WAIT_TIMEOUT == wait_status)
2584 {
2585 break;
2586 }
2587 else if ((WAIT_OBJECT_0 <= wait_status) && (wait_status < (WAIT_OBJECT_0 + stream_count)))
2588 {
2589 unsigned int index = wait_status - WAIT_OBJECT_0;
2590 Stream* stream = stream_ptrs_array[index];
2591 switch (stream->GetType())
2592 {
2593 case Stream::SOCKET:
2594 {
2595 ProtoSocket& theSocket = static_cast<SocketStream*>(stream)->GetSocket();
2596 WSANETWORKEVENTS event;
2597 if (0 == WSAEnumNetworkEvents(theSocket.GetHandle(), stream_handles_array[index], &event))
2598 {
2599 if (0 != (event.lNetworkEvents & (FD_READ | FD_ACCEPT)))
2600 theSocket.OnNotify(ProtoSocket::NOTIFY_INPUT);
2601
2602 if (0 != (event.lNetworkEvents & FD_WRITE))
2603 theSocket.OnNotify(ProtoSocket::NOTIFY_OUTPUT);
2604
2605 if (0 != (event.lNetworkEvents & FD_CLOSE))
2606 {
2607 theSocket.SetClosing(true);
2608 if (0 == event.iErrorCode[FD_CLOSE_BIT])
2609 theSocket.OnNotify(ProtoSocket::NOTIFY_INPUT);
2610 else
2611 theSocket.OnNotify(ProtoSocket::NOTIFY_ERROR);
2612 }
2613 if (0 != (event.lNetworkEvents & FD_CONNECT))
2614 {
2615 if (0 == event.iErrorCode[FD_CONNECT_BIT])
2616 theSocket.OnNotify(ProtoSocket::NOTIFY_OUTPUT);
2617 else
2618 theSocket.OnNotify(ProtoSocket::NOTIFY_ERROR);
2619 }
2620 if (0 != (event.lNetworkEvents & FD_ADDRESS_LIST_CHANGE))
2621 {
2622 if (0 == event.iErrorCode[FD_ADDRESS_LIST_CHANGE_BIT])
2623 theSocket.OnNotify(ProtoSocket::NOTIFY_EXCEPTION);
2624 else
2625 theSocket.OnNotify(ProtoSocket::NOTIFY_ERROR);
2626 }
2627 }
2628 else
2629 {
2630 PLOG(PL_ERROR, "ProtoDispatcher::Dispatch() WSAEnumNetworkEvents() error\n");
2631 }
2632 break;
2633 } // end case Stream::SOCKET
2634 case Stream::CHANNEL:
2635 {
2636 ProtoChannel& theChannel = static_cast<ChannelStream*>(stream)->GetChannel();
2637 if (index == (unsigned int)stream->GetIndex())
2638 theChannel.OnNotify(ProtoChannel::NOTIFY_INPUT);
2639 else // (index == stream->GetOutdex())
2640 theChannel.OnNotify(ProtoChannel::NOTIFY_OUTPUT);
2641 break;
2642 }
2643 case Stream::TIMER:
2644 {
2645 #ifdef USE_WAITABLE_TIMER
2646 // Our one and only "timer_stream"
2647 ResetEvent(timer_stream.GetDescriptor());
2648 timer_active = false;
2649 #endif // USE_WAITABLE_TIMER
2650 break;
2651 }
2652 case Stream::EVENT:
2653 {
2654 EventStream* eventStream = static_cast<EventStream*>(stream);
2655 ProtoEvent& event = eventStream->GetEvent();
2656 if (event.GetAutoReset()) event.Reset();
2657 // Note the "break_stream" has no listener, but others might
2658 if (&break_stream != eventStream) event.OnNotify();
2659 break;
2660 }
2661 case Stream::GENERIC:
2662 {
2663 // (TBD) Can we test the handle for input/output readiness?
2664 if (stream->IsInput())
2665 static_cast<GenericStream*>(stream)->OnEvent(EVENT_INPUT);
2666 if (stream->IsOutput())
2667 static_cast<GenericStream*>(stream)->OnEvent(EVENT_OUTPUT);
2668 break;
2669 }
2670 } // end switch (stream->GetType())
2671 }
2672 else if ((WAIT_OBJECT_0 + stream_count) == wait_status)
2673 {
2674 // Windows GUI message event
2675 MSG msg;
2676 // Use PeekMessage to make sure it's not a false alarm
2677 if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
2678 {
2679 //if (GetMessage(&msg, NULL, 0, 0 ))
2680 if (WM_QUIT != msg.message)
2681 {
2682 TranslateMessage(&msg);
2683 DispatchMessage(&msg);
2684 }
2685 else
2686 {
2687 exit_code = msg.wParam;
2688 run = false;
2689 break; // we're done
2690 }
2691 }
2692 }
2693 else
2694 {
2695 // (TBD) handle these other conditions when needed
2696 // WAIT_ABANDONED or WAIT_IO_COMPLETION
2697 }
2698 break;
2699 } // end case default
2700 } // end switch(status)
2701 OnSystemTimeout();
2702 } // end ProtoDispatcher::Dispatch()
2703
2704 LRESULT CALLBACK ProtoDispatcher::MessageHandler(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
2705 {
2706 switch (message)
2707 {
2708 case WM_CREATE:
2709 {
2710 CREATESTRUCT *info = (CREATESTRUCT *) lParam;
2711 ProtoDispatcher* dp = (ProtoDispatcher*)info->lpCreateParams;
2712 SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)dp);
2713 return 0;
2714 }
2715 case WM_DESTROY:
2716 {
2717 ProtoDispatcher* dp = (ProtoDispatcher*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
2718 dp->msg_window = NULL;
2719 PostQuitMessage(0);
2720 return 0;
2721 }
2722 case WM_QUIT:
2723 {
2724 ProtoDispatcher* dp = (ProtoDispatcher*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
2725 dp->run = false;
2726 return 0;
2727 }
2728 default:
2729 break;
2730 }
2731 return DefWindowProc(hwnd, message, wParam, lParam);
2732 } // end ProtoDispatcher::MessageHandler()
2733
2734 #endif // WIN32
2735
2736 /**
2737 * ProtoDispatcher::Controller implementation
2738 */
2739 ProtoDispatcher::Controller::Controller(ProtoDispatcher& theDispatcher)
2740 : dispatcher(theDispatcher), use_lock_a(true)
2741 {
2742 ProtoDispatcher::Init(lock_a);
2743 ProtoDispatcher::Init(lock_b);
2744 ProtoDispatcher::Lock(lock_a);
2745 // Note: "lock_b" gets an initial Lock() by the controlled
2746 // thread during "ProtoDispatcher::DoThreadStart()"
2747 }
2748
2749 ProtoDispatcher::Controller::~Controller()
2750 {
2751 Unlock(lock_a);
2752 Unlock(lock_b);
2753 Destroy(lock_a);
2754 Destroy(lock_b);
2755 }
2756
2757 bool ProtoDispatcher::Controller::DoDispatch()
2758 {
2759 // (TBD) make sure !IsMyself() ???
2760 if (use_lock_a)
2761 ProtoDispatcher::Unlock(lock_b);
2762 else
2763 ProtoDispatcher::Unlock(lock_a);
2764 if (!SignalDispatchReady())
2765 {
2766 PLOG(PL_ERROR, "ProtoDispatcher::Controller::DoDispatch()) SignalDispatchReady() error\n");
2767 return false;
2768 }
2769 if (use_lock_a)
2770 {
2771 ProtoDispatcher::Lock(lock_a);
2772 use_lock_a = false;
2773 }
2774 else
2775 {
2776 ProtoDispatcher::Lock(lock_b);
2777 use_lock_a = true;
2778 }
2779 return true;
2780 } // end ProtoDispatcher::Controller::DoDispatch()
2781
2782 void ProtoDispatcher::Controller::OnDispatch()
2783 {
2784 dispatcher.SuspendThread();
2785 dispatcher.Dispatch();
2786 if (use_lock_a)
2787 {
2788 ProtoDispatcher::Lock(lock_b);
2789 ProtoDispatcher::Unlock(lock_a);
2790 }
2791 else
2792 {
2793 ProtoDispatcher::Lock(lock_a);
2794 ProtoDispatcher::Unlock(lock_b);
2795 }
2796 dispatcher.ResumeThread();
2797 } // end ProtoDispatcher::Controller::OnDispatch()
2798