1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
/*
*Copyright (C) <2017> Alex B
*
*This program is free software: you can redistribute it and/or modify
*it under the terms of the GNU General Public License as published by
*the Free Software Foundation, either version 2 of the License, or
*(at your option) any later version.
*
*This program is distributed in the hope that it will be useful,
*but WITHOUT ANY WARRANTY; without even the implied warranty of
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
*GNU General Public License for more details.
*
*You should have received a copy of the GNU General Public License
*along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#include "avahipoll.h"
#include <QThread>
#include <avahi-common/timeval.h>
static AvahiWatch* avahiWatchNew(const AvahiPoll* /*ap*/, int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void* userdata)
{
return new AvahiWatch(fd, event, callback, userdata);
}
static void avahiWatchUpdate(AvahiWatch* w, AvahiWatchEvent event)
{
w->setEventType(event);
}
static AvahiWatchEvent avahiWatchGetEvents(AvahiWatch* w)
{
return w->previousEvent();
}
static void avahiWatchFree(AvahiWatch* w)
{
(w->thread() == QThread::currentThread()) ? delete w : w->deleteLater();
}
static AvahiTimeout* avahiTimeoutNew(const AvahiPoll* p, const struct timeval* tv, AvahiTimeoutCallback callback, void* userdata)
{
Q_UNUSED(p)
return new AvahiTimeout(tv, callback, userdata);
}
static void avahiTimeoutUpdate(AvahiTimeout* t, const struct timeval* tv)
{
t->updateTimeout(tv);
}
static void avahiTimeoutFree(AvahiTimeout* t)
{
(t->thread() == QThread::currentThread()) ? delete t : t->deleteLater();
}
const AvahiPoll* getAvahiPoll(void)
{
static const AvahiPoll avahiPoll =
{
nullptr,
avahiWatchNew,
avahiWatchUpdate,
avahiWatchGetEvents,
avahiWatchFree,
avahiTimeoutNew,
avahiTimeoutUpdate,
avahiTimeoutFree};
return &avahiPoll;
}
AvahiTimeout::AvahiTimeout(const struct timeval* tv, AvahiTimeoutCallback cb, void* ud)
: callback(cb), userData(ud)
{
connect(&timer, &QTimer::timeout, this, &AvahiTimeout::timeout);
updateTimeout(tv);
}
void AvahiTimeout::updateTimeout(const struct timeval* tv)
{
if (nullptr == tv) {
timer.stop();
return;
}
qint64 msecs = (avahi_age(tv)) / 1000;
msecs = (msecs > 0) ? 0 : -msecs;
timer.setInterval(msecs);
timer.start();
}
void AvahiTimeout::timeout()
{
timer.stop();
callback(this, userData);
}
AvahiWatch::AvahiWatch(int f, AvahiWatchEvent ev, AvahiWatchCallback cb, void* ud)
: notifier(nullptr), callback(cb), event(ev), prevEvent(static_cast<AvahiWatchEvent>(0)), userData(ud), fd(f)
{
setEventType(ev);
}
void AvahiWatch::setEventType(AvahiWatchEvent event)
{
this->event = event;
switch (event) {
case AVAHI_WATCH_IN: {
notifier.reset(new QSocketNotifier(fd, QSocketNotifier::Read, this));
connect(notifier.data(), &QSocketNotifier::activated, this, &AvahiWatch::activated);
break;
}
case AVAHI_WATCH_OUT: {
notifier.reset(new QSocketNotifier(fd, QSocketNotifier::Write, this));
connect(notifier.data(), &QSocketNotifier::activated, this, &AvahiWatch::activated);
break;
}
default:
// should not be reached
break;
}
}
void AvahiWatch::activated(int fd)
{
Q_UNUSED(fd)
prevEvent = event;
callback(this, this->fd, event, userData);
prevEvent = static_cast<AvahiWatchEvent>(0);
}
AvahiWatchEvent AvahiWatch::previousEvent()
{
return prevEvent;
}
#include "moc_avahipoll.cpp"
|