Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright (C) 2009-2015 Paul Davis <[email protected]>
- *
- * 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, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
- #ifdef PLATFORM_WINDOWS
- #define WIN32_LEAN_AND_MEAN
- #define NOMINMAX
- #include <winsock2.h>
- #include <windows.h>
- #undef check
- #undef read
- #undef write
- #undef close
- #undef seek
- #undef flush
- #undef interface
- #undef min
- #undef max
- #else
- #include <unistd.h>
- #endif
- #include <cstdlib>
- #include <cerrno>
- #include <cstring>
- #include <fcntl.h>
- #include <glibmm.h>
- #include <giomm.h>
- #include "pbd/crossthread.h"
- #include "pbd/error.h"
- using namespace std;
- using namespace PBD;
- using namespace Glib;
- #ifndef PLATFORM_WINDOWS
- #include "crossthread.posix.cc"
- #else
- #include "crossthread.win.cc"
- #endif
- void
- CrossThreadChannel::attach (Glib::RefPtr<Glib::MainContext> context)
- {
- source = Gio::SocketSource::create (recv_socket,
- Glib::IO_IN | Glib::IO_ERR | Glib::IO_HUP);
- source->connect ([this](const Glib::IOCondition& cond) {
- return receive_slot (cond);
- });
- source->attach (context);
- }
- ______________________________________________________________________________________________________
- /*
- * Copyright (C) 2006-2015 Paul Davis <[email protected]>
- * Copyright (C) 2015 Robin Gareus <[email protected]>
- *
- * 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, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
- #pragma once
- #ifdef PLATFORM_WINDOWS
- #include <winsock2.h>
- #include <windows.h>
- #undef check
- #undef read
- #undef write
- #undef close
- #undef seek
- #undef flush
- #undef interface
- #undef min
- #undef max
- #endif // PLATFORM_WINDOWS
- #include <glibmm/main.h>
- #include <giomm.h>
- /** A simple abstraction of a mechanism of signalling one thread from another.
- * The signaller calls \ref wakeup() to tell the signalled thread to check for
- * work to be done.
- *
- * This implementation provides both selectable() for use in direct
- * poll/select-based event loops, and a Glib::IOSource via ios() for use
- * in Glib main loop based situations.
- */
- class LIBPBD_API CrossThreadChannel {
- public:
- /** if @a non_blocking is true, the channel will not cause blocking
- * when used in an event loop based on poll/select or the glib main
- * loop.
- */
- CrossThreadChannel(bool non_blocking);
- ~CrossThreadChannel();
- /** Tell the listening thread that is has work to do.
- */
- void wakeup();
- /* if the listening thread cares about the precise message
- * it is being sent, then \ref deliver() can be used to send
- * a single byte message rather than a simple wakeup. These
- * two mechanisms should not be used on the same CrossThreadChannel
- * because there is no way to know which byte value will be used
- * for ::wakeup()
- */
- int deliver (char msg);
- /** if using \ref deliver() to wakeup the listening thread, then
- * the listener should call \ref receive() to fetch the message
- * type from the channel.
- *
- * wait = true only make sense for non_blocking channels,
- * it polls for data to become available.
- */
- int receive (char& msg, bool wait = false);
- /** empty the channel of all requests.
- * Typically this is done as soon as input
- * is noticed on the channel, because the
- * handler will look at a separately managed work
- * queue. The actual number of queued "wakeups"
- * in the channel will not be important.
- */
- void drain ();
- void set_receive_handler (sigc::slot<bool(Glib::IOCondition)> s);
- void attach (Glib::RefPtr<Glib::MainContext>);
- private:
- friend gboolean cross_thread_channel_call_receive_slot (GIOChannel*, GIOCondition condition, void *data);
- GIOChannel* receive_channel;
- GSource* receive_source;
- sigc::slot<bool(Glib::IOCondition)> receive_slot;
- bool poll_for_request();
- #ifndef PLATFORM_WINDOWS
- int fds[2]; // current implementation uses a pipe/fifo
- #else
- Glib::RefPtr<Gio::Socket> send_socket;
- Glib::RefPtr<Gio::Socket> recv_socket;
- Glib::RefPtr<Gio::SocketSource> source;
- #endif
- };
- ________________________________________________________________________________________________________________
- /*
- * Copyright (C) 2014-2016 Paul Davis <[email protected]>
- * Copyright (C) 2015 Robin Gareus <[email protected]>
- *
- * 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, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
- CrossThreadChannel::CrossThreadChannel (bool non_blocking)
- : receive_slot ()
- {
- auto inet = Gio::InetAddress::create("127.0.0.1");
- auto family = Gio::SOCKET_FAMILY_IPV4;
- auto type = Gio::SOCKET_TYPE_DATAGRAM;
- auto proto = Gio::SOCKET_PROTOCOL_UDP;
- send_socket = Gio::Socket::create(family, type, proto);
- recv_socket = Gio::Socket::create(family, type, proto);
- recv_socket->bind(Gio::InetSocketAddress::create(inet, 0), true);
- send_socket->set_blocking(!non_blocking);
- recv_socket->set_blocking(!non_blocking);
- }
- CrossThreadChannel::~CrossThreadChannel()
- {
- if (source)
- source->destroy();
- }
- void
- CrossThreadChannel::wakeup()
- {
- char c = 0;
- deliver(c);
- }
- int
- CrossThreadChannel::deliver(char msg)
- {
- auto addr = recv_socket->get_local_address();
- gssize sent = send_socket->send_to(addr, &msg, sizeof(msg));
- return (sent < 0) ? -1 : static_cast<int>(sent);
- }
- int
- CrossThreadChannel::receive(char& msg, bool /*wait*/)
- {
- gssize r = recv_socket->receive(&msg, sizeof(msg));
- return (r < 0) ? -1 : static_cast<int>(r);
- }
- void
- CrossThreadChannel::drain()
- {
- char buf[512];
- while (recv_socket->receive(buf, sizeof(buf)) > 0) {}
- }
- ________________________________________________________________________________________________________________________________
Advertisement
Add Comment
Please, Sign In to add comment