/*
- Copyright (C) 2009 Paul Davis
+ Copyright (C) 2009 Paul Davis
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
*/
CrossThreadChannel::CrossThreadChannel (bool non_blocking)
- : receive_channel (0)
+ : receive_channel (0)
, receive_source (0)
, receive_slot ()
, send_socket()
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,
+ int status = bind(send_socket, (SOCKADDR*)&send_address,
sizeof(send_address));
if (status != 0) {
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,
+ 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;
// 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;
return status;
}
-int
-CrossThreadChannel::receive (char& msg)
+bool
+CrossThreadChannel::poll_for_request()
+{
+ // 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;
+}
+
+int
+CrossThreadChannel::receive (char& msg, bool wait)
{
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);