Line data Source code
1 :
2 : /* Copyright (c) 2014, Daniel Nachbaur <danielnachbaur@gmail.com>
3 : *
4 : * This library is free software; you can redistribute it and/or modify it under
5 : * the terms of the GNU Lesser General Public License version 2.1 as published
6 : * by the Free Software Foundation.
7 : *
8 : * This library is distributed in the hope that it will be useful, but WITHOUT
9 : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 : * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11 : * details.
12 : *
13 : * You should have received a copy of the GNU Lesser General Public License
14 : * along with this library; if not, write to the Free Software Foundation, Inc.,
15 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 : */
17 :
18 : #include "messagePump.h"
19 :
20 : #ifdef EQUALIZER_USE_DEFLECT
21 : # include "../deflect/eventHandler.h"
22 : # include "../deflect/proxy.h"
23 : #endif
24 :
25 : #include <lunchbox/clock.h>
26 : #include <lunchbox/sleep.h>
27 : #include <QEventLoop>
28 :
29 : namespace eq
30 : {
31 : namespace qt
32 : {
33 0 : MessagePump::MessagePump()
34 : {
35 0 : }
36 :
37 0 : MessagePump::~MessagePump()
38 : {
39 0 : }
40 :
41 0 : void MessagePump::postWakeup()
42 : {
43 0 : _wakeup = 1;
44 0 : }
45 :
46 0 : void MessagePump::dispatchOne( const uint32_t timeout )
47 : {
48 : // dispatchOne / wakeup semantics are not implementable. poll.
49 : // * QEventLoop::wakeup does not wakup processEvents( WaitForMoreEvents )
50 : // * processEvents( timeout ) returns as soon as there are no events
51 :
52 0 : QEventLoop eventLoop;
53 0 : lunchbox::Clock _clock;
54 0 : if( eventLoop.processEvents( QEventLoop::AllEvents ))
55 0 : return;
56 :
57 0 : uint32_t timeLeft = std::max( 0u, timeout - uint32_t( _clock.getTimef( )));
58 0 : while( timeLeft )
59 : {
60 0 : if( _wakeup == 1 )
61 : {
62 0 : _wakeup = 0;
63 0 : return;
64 : }
65 0 : lunchbox::sleep( std::min( 10u, timeLeft ));
66 0 : if( eventLoop.processEvents( QEventLoop::AllEvents ))
67 0 : return;
68 0 : timeLeft = std::max( 0u, timeout - uint32_t( _clock.getTimef( )));
69 0 : }
70 : }
71 :
72 0 : void MessagePump::dispatchAll()
73 : {
74 0 : QEventLoop eventLoop;
75 0 : eventLoop.processEvents();
76 0 : }
77 :
78 0 : void MessagePump::register_( deflect::Proxy* proxy LB_UNUSED )
79 : {
80 : #ifdef EQUALIZER_USE_DEFLECT
81 : QSocketNotifier* notifier =
82 0 : new QSocketNotifier( proxy->getSocketDescriptor(),
83 0 : QSocketNotifier::Read );
84 0 : _notifiers[proxy].reset( notifier );
85 : notifier->connect( notifier, &QSocketNotifier::activated,
86 0 : [proxy]{ deflect::EventHandler::processEvents( proxy ); });
87 :
88 : // QSocketNotifier sometimes does not fire, help with a timer
89 0 : if( !_timer )
90 : {
91 0 : _timer.reset( new QTimer( ));
92 0 : _timer->start( 20 );
93 : }
94 :
95 0 : _connections[proxy] = _timer->connect( _timer.get(), &QTimer::timeout,
96 0 : [proxy] { deflect::EventHandler::processEvents( proxy ); });
97 : #endif
98 0 : }
99 :
100 0 : void MessagePump::deregister( deflect::Proxy* proxy LB_UNUSED )
101 : {
102 : #ifdef EQUALIZER_USE_DEFLECT
103 0 : _notifiers.erase( proxy );
104 :
105 0 : _timer->disconnect( _connections[proxy] );
106 0 : _connections.erase( proxy );
107 0 : if( _connections.empty( ))
108 0 : _timer.reset();
109 : #endif
110 0 : }
111 :
112 : }
113 42 : }
|