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 <QEventLoop>
26 : #include <lunchbox/clock.h>
27 : #include <lunchbox/sleep.h>
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 wakeup 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 : }
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] =
96 0 : _timer->connect(_timer.get(), &QTimer::timeout, [proxy] {
97 0 : deflect::EventHandler::processEvents(proxy);
98 0 : });
99 : #endif
100 0 : }
101 :
102 0 : void MessagePump::deregister(deflect::Proxy* proxy LB_UNUSED)
103 : {
104 : #ifdef EQUALIZER_USE_DEFLECT
105 0 : _notifiers.erase(proxy);
106 :
107 0 : _timer->disconnect(_connections[proxy]);
108 0 : _connections.erase(proxy);
109 0 : if (_connections.empty())
110 0 : _timer.reset();
111 : #endif
112 0 : }
113 : }
114 30 : }
|