Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git a/libs/pbd/crossthread.cc b/libs/pbd/crossthread.cc
- index bf21d2befc..84e42e5320 100644
- --- a/libs/pbd/crossthread.cc
- +++ b/libs/pbd/crossthread.cc
- @@ -16,20 +16,20 @@
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
- +#ifndef PLATFORM_WINDOWS
- +#include <unistd.h>
- +#endif
- #include <cstdlib>
- #include <cerrno>
- #include <cstring>
- #include <fcntl.h>
- -#include <unistd.h>
- -#ifdef PLATFORM_WINDOWS
- -#include <winsock2.h>
- -#include <ws2tcpip.h>
- -#endif
- +#include <glibmm.h>
- +#include <giomm.h>
- -#include "pbd/error.h"
- #include "pbd/crossthread.h"
- +#include "pbd/error.h"
- using namespace std;
- using namespace PBD;
- @@ -41,28 +41,14 @@ using namespace Glib;
- #include "crossthread.win.cc"
- #endif
- -#ifndef G_SOURCE_FUNC
- -#define G_SOURCE_FUNC(f) ((GSourceFunc) (void (*)(void)) (f))
- -#endif
- -
- -gboolean
- -cross_thread_channel_call_receive_slot (GIOChannel*, GIOCondition condition, void *data)
- -{
- - CrossThreadChannel* ctc = static_cast<CrossThreadChannel*>(data);
- - return ctc->receive_slot (Glib::IOCondition (condition));
- -}
- -
- -void
- -CrossThreadChannel::set_receive_handler (sigc::slot<bool,Glib::IOCondition> s)
- -{
- - receive_slot = s;
- -}
- -
- void
- CrossThreadChannel::attach (Glib::RefPtr<Glib::MainContext> context)
- {
- - receive_source = g_io_create_watch (receive_channel, GIOCondition(G_IO_IN | G_IO_ERR | G_IO_HUP));
- -
- - g_source_set_callback (receive_source, G_SOURCE_FUNC(cross_thread_channel_call_receive_slot), this, NULL);
- - g_source_attach (receive_source, context->gobj());
- + source = Gio::SocketSource::create (recv_socket,
- + static_cast<Glib::IOCondition>(G_IO_IN | G_IO_ERR | G_IO_HUP));
- + CrossThreadChannel* self = this;
- + source->connect ([self](Glib::IOCondition cond) -> bool {
- + return self->receive_slot (cond);
- + });
- + source->attach (context);
- diff --git a/libs/pbd/crossthread.win.cc b/libs/pbd/crossthread.win.cc
- index 1d0758c911..f117e1ec80 100644
- --- a/libs/pbd/crossthread.win.cc
- +++ b/libs/pbd/crossthread.win.cc
- @@ -18,187 +18,53 @@
- */
- CrossThreadChannel::CrossThreadChannel (bool non_blocking)
- - : receive_channel (0)
- - , receive_source (0)
- - , receive_slot ()
- - , send_socket()
- - , receive_socket()
- - , recv_address()
- + : receive_slot ()
- {
- - struct sockaddr_in send_address;
- + auto inet = Gio::InetAddress::create("127.0.0.1");
- + Gio::SocketFamily family = static_cast<Gio::SocketFamily>(G_SOCKET_FAMILY_IPV4);
- + Gio::Socket::Type type = static_cast<Gio::Socket::Type>(G_SOCKET_TYPE_DATAGRAM);
- + Gio::Socket::Protocol proto = static_cast<Gio::Socket::Protocol>(G_SOCKET_PROTOCOL_UDP);
- - // Create Send Socket
- - send_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
- - send_address.sin_family = AF_INET;
- - send_address.sin_addr.s_addr = inet_addr("127.0.0.1");
- - send_address.sin_port = htons(0);
- - int status = ::bind(send_socket, (SOCKADDR*)&send_address,
- - sizeof(send_address));
- + send_socket = Gio::Socket::create(family, type, proto);
- + recv_socket = Gio::Socket::create(family, type, proto);
- - if (status != 0) {
- - std::cerr << "CrossThreadChannel::CrossThreadChannel() Send socket binding failed with error: " << WSAGetLastError() << std::endl;
- - return;
- - }
- + recv_socket->bind(Gio::InetSocketAddress::create(inet, 0), true);
- - // make the socket non-blockable if required
- - u_long mode = (u_long)non_blocking;
- - int otp_result = 0;
- -
- - otp_result = ioctlsocket(send_socket, FIONBIO, &mode);
- - if (otp_result != NO_ERROR) {
- - std::cerr << "CrossThreadChannel::CrossThreadChannel() Send socket cannot be set to non blocking mode with error: " << WSAGetLastError() << std::endl;
- - }
- -
- - // Create Receive Socket, this socket will be set to unblockable mode by IO channel
- - receive_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
- - recv_address.sin_family = AF_INET;
- - recv_address.sin_addr.s_addr = inet_addr("127.0.0.1");
- - recv_address.sin_port = htons(0);
- - status = ::bind(receive_socket, (SOCKADDR*)&recv_address,
- - sizeof(recv_address));
- -
- - if (status != 0) {
- - std::cerr << "CrossThreadChannel::CrossThreadChannel() Receive socket binding failed with error: " << WSAGetLastError() << std::endl;
- - return;
- - }
- -
- - // recieve socket will be made non-blocking by GSource which will use it
- -
- - // get assigned port number for Receive Socket
- - int recv_addr_len = sizeof(recv_address);
- - status = getsockname(receive_socket, (SOCKADDR*)&recv_address, &recv_addr_len);
- -
- - if (status != 0) {
- - std::cerr << "CrossThreadChannel::CrossThreadChannel() Setting receive socket address to local failed with error: " << WSAGetLastError() << std::endl;
- - return;
- - }
- -
- - // construct IOChannel
- - receive_channel = g_io_channel_win32_new_socket((gint)receive_socket);
- -
- - // set binary data type
- - GIOStatus g_status = g_io_channel_set_encoding (receive_channel, NULL, NULL);
- - if (G_IO_STATUS_NORMAL != g_status ) {
- - std::cerr << "CrossThreadChannel::CrossThreadChannel() Cannot set flag for IOChannel. " << g_status << std::endl;
- - return;
- - }
- -
- - // disable channel buffering
- - g_io_channel_set_buffered (receive_channel, false);
- -}
- -
- -CrossThreadChannel::~CrossThreadChannel ()
- -{
- - if (receive_source) {
- - /* this disconnects it from any main context it was attached in
- - in ::attach(), this prevent its callback from being invoked
- - after the destructor has finished.
- - */
- - g_source_destroy (receive_source);
- - }
- -
- - /* glibmm hack */
- -
- - if (receive_channel) {
- - g_io_channel_unref (receive_channel);
- - }
- -
- - closesocket(send_socket);
- - closesocket(receive_socket);
- + send_socket->set_blocking(!non_blocking);
- + recv_socket->set_blocking(!non_blocking);
- }
- -void
- -CrossThreadChannel::wakeup ()
- +CrossThreadChannel::~CrossThreadChannel()
- {
- - char c = 0;
- -
- - // write one byte to wake up a thread which is listening our IOS
- - sendto(send_socket, &c, sizeof(c), 0, (SOCKADDR*)&recv_address, sizeof(recv_address) );
- + if (source)
- + source->destroy();
- }
- void
- -CrossThreadChannel::drain ()
- +CrossThreadChannel::wakeup()
- {
- - /* flush the buffer - empty the channel from all requests */
- - GError *g_error = 0;
- - gchar buffer[512];
- - gsize read = 0;
- -
- - while (1) {
- - GIOStatus g_status = g_io_channel_read_chars (receive_channel, buffer, sizeof(buffer), &read, &g_error);
- -
- - if (G_IO_STATUS_AGAIN == g_status) {
- - break;
- - }
- -
- - if (G_IO_STATUS_NORMAL != g_status) {
- - std::cerr << "CrossThreadChannel::CrossThreadChannel() Cannot drain from read buffer! " << g_status << std::endl;
- -
- - if (g_error) {
- - std::cerr << "Error is Domain: " << g_error->domain << " Code: " << g_error->code << std::endl;
- - g_clear_error(&g_error);
- - } else {
- - std::cerr << "No error provided\n";
- - }
- - break;
- - }
- - }
- + char c = 0;
- + deliver(c);
- }
- -
- int
- -CrossThreadChannel::deliver (char msg)
- +CrossThreadChannel::deliver(char msg)
- {
- -
- - // write one particular byte to wake up the thread which is listening our IOS
- - int status = sendto(send_socket, &msg, sizeof(msg), 0, (SOCKADDR*)&recv_address, sizeof(recv_address) );
- -
- - if (SOCKET_ERROR == status) {
- - return -1;
- - }
- -
- - return status;
- + 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);
- }
- -bool
- -CrossThreadChannel::poll_for_request()
- +int
- +CrossThreadChannel::receive(char& msg, bool /*wait*/)
- {
- - // windows before Vista has no poll
- - while(true) {
- - fd_set rfds;
- - FD_ZERO(&rfds);
- - FD_SET(receive_socket, &rfds);
- - if ((select(receive_socket+1, &rfds, NULL, NULL, NULL)) < 0) {
- - if (errno == EINTR) {
- - continue;
- - }
- - break;
- - }
- - if(FD_ISSET(receive_socket, &rfds)) {
- - return true;
- - }
- - }
- - return false;
- + gssize r = recv_socket->receive(&msg, sizeof(msg));
- + return (r < 0) ? -1 : static_cast<int>(r);
- }
- -int
- -CrossThreadChannel::receive (char& msg, bool wait)
- +void
- +CrossThreadChannel::drain()
- {
- - gsize read = 0;
- - GError *g_error = 0;
- -
- - if (wait) {
- - if (!poll_for_request ()) {
- - return -1;
- - }
- - }
- -
- - // fetch the message from the channel.
- - GIOStatus g_status = g_io_channel_read_chars (receive_channel, &msg, sizeof(msg), &read, &g_error);
- -
- - if (G_IO_STATUS_NORMAL != g_status) {
- - read = -1;
- - }
- -
- - return read;
- + char buf[512];
- + while (recv_socket->receive(buf, sizeof(buf)) > 0) {}
- }
- diff --git a/libs/pbd/pbd/crossthread.h b/libs/pbd/pbd/crossthread.h
- index 08f61a5b58..2c3b8457ab 100644
- --- a/libs/pbd/pbd/crossthread.h
- +++ b/libs/pbd/pbd/crossthread.h
- @@ -19,18 +19,26 @@
- #pragma once
- -#ifdef check
- -#undef check
- -#endif
- -
- -#include <glibmm/main.h>
- -
- #include "pbd/libpbd_visibility.h"
- +#include <gio/gio.h>
- +
- #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
- @@ -81,7 +89,7 @@ public:
- */
- void drain ();
- - void set_receive_handler (sigc::slot<bool,Glib::IOCondition> s);
- + void set_receive_handler (sigc::slot<bool(Glib::IOCondition)> s);
- void attach (Glib::RefPtr<Glib::MainContext>);
- private:
- @@ -89,17 +97,16 @@ private:
- GIOChannel* receive_channel;
- GSource* receive_source;
- - sigc::slot<bool,Glib::IOCondition> receive_slot;
- + sigc::slot<bool(Glib::IOCondition)> receive_slot;
- bool poll_for_request();
- #ifndef PLATFORM_WINDOWS
- int fds[2]; // current implementation uses a pipe/fifo
- #else
- -
- - SOCKET send_socket;
- - SOCKET receive_socket;
- - struct sockaddr_in recv_address;
- + Glib::RefPtr<Gio::Socket> send_socket;
- + Glib::RefPtr<Gio::Socket> recv_socket;
- + Glib::RefPtr<Gio::SocketSource> source;
- #endif
- };
Advertisement
Add Comment
Please, Sign In to add comment