static gboolean
g_socket_output_stream_pollable_is_writable (GPollableOutputStream *pollable)
{
    GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (pollable);

    return g_socket_condition_check (output_stream->priv->socket, G_IO_OUT);
}
Exemple #2
0
static gboolean
nice_input_stream_is_readable (GPollableInputStream *stream)
{
  NiceInputStreamPrivate *priv = NICE_INPUT_STREAM (stream)->priv;
  Component *component = NULL;
  Stream *_stream = NULL;
  gboolean retval = FALSE;
  GSList *i;
  NiceAgent *agent;  /* owned */

  /* Closed streams are not readable. */
  if (g_input_stream_is_closed (G_INPUT_STREAM (stream)))
    return FALSE;

  /* Has the agent disappeared? */
  agent = g_weak_ref_get (&priv->agent_ref);
  if (agent == NULL)
    return FALSE;

  agent_lock (agent);

  if (!agent_find_component (agent, priv->stream_id, priv->component_id,
          &_stream, &component)) {
    g_warning ("Could not find component %u in stream %u", priv->component_id,
        priv->stream_id);
    goto done;
  }

  /* If it’s a reliable agent, see if there’s any pending data in the pseudo-TCP
   * buffer. */
  if (agent->reliable &&
      pseudo_tcp_socket_get_available_bytes (component->tcp) > 0) {
    retval = TRUE;
    goto done;
  }

  /* Check whether any of the component’s FDs are pollable. */
  for (i = component->socket_sources; i != NULL; i = i->next) {
    SocketSource *socket_source = i->data;
    NiceSocket *nicesock = socket_source->socket;

    if (g_socket_condition_check (nicesock->fileno, G_IO_IN) != 0) {
      retval = TRUE;
      break;
    }
  }

done:
  agent_unlock (agent);

  g_object_unref (agent);

  return retval;
}
Exemple #3
0
/**
 * g_vfs_ftp_connection_is_usable:
 * @conn: a connection
 *
 * Checks if this connection can be used to send new commands. For
 * example, if the connection is in the process of a command sequence or
 * if the connection was closed, this is not possible and this function 
 * will return %FALSE.
 *
 * Returns: %TRUE if the connection is still usable
 **/
gboolean
g_vfs_ftp_connection_is_usable (GVfsFtpConnection *conn)
{
  GIOCondition cond;

  g_return_val_if_fail (conn != NULL, FALSE);

  if (conn->waiting_for_reply)
    return FALSE;

  cond = G_IO_ERR | G_IO_HUP;
  cond = g_socket_condition_check (g_socket_connection_get_socket (G_SOCKET_CONNECTION (conn->commands)), cond);
  if (cond)
    return FALSE;

  return TRUE;
}
static gboolean
nice_output_stream_is_writable (GPollableOutputStream *stream)
{
  NiceOutputStreamPrivate *priv = NICE_OUTPUT_STREAM (stream)->priv;
  NiceComponent *component = NULL;
  NiceStream *_stream = NULL;
  gboolean retval = FALSE;
  NiceAgent *agent;  /* owned */

  /* Closed streams are not writeable. */
  if (g_output_stream_is_closed (G_OUTPUT_STREAM (stream)))
    return FALSE;

  /* Has the agent disappeared? */
  agent = g_weak_ref_get (&priv->agent_ref);
  if (agent == NULL)
    return FALSE;

  agent_lock ();

  if (!agent_find_component (agent, priv->stream_id, priv->component_id,
          &_stream, &component)) {
    g_warning ("Could not find component %u in stream %u", priv->component_id,
        priv->stream_id);
    goto done;
  }
  if (component->selected_pair.local != NULL) {
    NiceSocket *sockptr = component->selected_pair.local->sockptr;

    /* If it’s a reliable agent, see if there’s any space in the pseudo-TCP
     * output buffer. */
    if (!nice_socket_is_reliable (sockptr)) {
      retval = pseudo_tcp_socket_can_send (component->tcp);
    } else {
      retval = (g_socket_condition_check (sockptr->fileno, G_IO_OUT) != 0);
    }
  }

done:
  agent_unlock ();

  g_object_unref (agent);

  return retval;
}
Exemple #5
0
GInetAddress *interface_getConfiguredAddress(struct node *n)
{
    GInetAddress *ret = NULL;
    GSocket *s = n->ubnetd;

    if( s != NULL ){
        if( g_socket_condition_check(s, G_IO_IN) & G_IO_IN ){
            gchar buf[1];
            g_socket_receive(s,buf,1,NULL,NULL);
            if( buf[0] == 'A' )
                ret = n->netadr;
            g_socket_close(s, NULL);
            g_object_unref(s);
            n->ubnetd = NULL;
        }
    }
    return ret;
}
KmsSCTPResult
kms_sctp_connection_receive (KmsSCTPConnection * conn, KmsSCTPMessage * message,
    GCancellable * cancellable, GError ** err)
{
  GIOCondition condition;
  guint streamid;

  g_return_val_if_fail (conn != NULL, KMS_SCTP_ERROR);
  g_return_val_if_fail (conn->socket != NULL, KMS_SCTP_ERROR);

  if (!g_socket_condition_wait (conn->socket,
          G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP, cancellable, err)) {
    return KMS_SCTP_ERROR;
  }

  condition = g_socket_condition_check (conn->socket,
      G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP);

  if ((condition & G_IO_ERR)) {
    g_set_error (err, KMS_SCTP_CONNECTION_ERROR, KMS_CONNECTION_READ_ERROR,
        "Socket in error state");
    return KMS_SCTP_ERROR;
  } else if ((condition & G_IO_HUP)) {
    g_set_error (err, KMS_SCTP_CONNECTION_ERROR, KMS_CONNECTION_READ_ERROR,
        "Connection closed");
    return KMS_SCTP_EOF;
  }

  message->used = sctp_socket_receive (conn->socket, message->buf,
      message->size, cancellable, &streamid, err);

  GST_LOG ("Receive data on stream id %d", streamid);

  if (message->used == 0)
    return KMS_SCTP_EOF;
  else if (message->used < 0)
    return KMS_SCTP_ERROR;
  else
    return KMS_SCTP_OK;
}
/* coroutine context */
static gboolean spice_channel_iterate(SpiceChannel *channel)
{
    spice_channel *c = channel->priv;
    GIOCondition ret;

    do {
        while (c->state == SPICE_CHANNEL_STATE_MIGRATING) {
            /* freeze coroutine */
            coroutine_yield(NULL);
            g_return_val_if_fail(c->state != SPICE_CHANNEL_STATE_MIGRATING, FALSE);
        }

        if (c->has_error) {
            SPICE_DEBUG("channel has error, breaking loop");
            return FALSE;
        }

        SPICE_CHANNEL_GET_CLASS(channel)->iterate_write(channel);
	/*FIXME: this calling will fall in deadloop because coroutine_swap() returns NULL;
	 * So I force reading.
	 * */
        ret = G_IO_IN;//g_io_wait_interruptable(&c->wait, c->sock, G_IO_IN);
#ifdef WIN32
        /* FIXME: windows gsocket is buggy, it doesn't return correct condition... */
        ret = g_socket_condition_check(c->sock, G_IO_IN);
#endif
    } while (ret == 0); /* ret == 0 means no IO condition, but woken up */

    if (ret & (G_IO_ERR|G_IO_HUP)) {
        SPICE_DEBUG("got socket error before read(): %d", ret);
        c->has_error = TRUE;
        return FALSE;
    }

    SPICE_CHANNEL_GET_CLASS(channel)->iterate_read(channel);

    return TRUE;
}
Exemple #8
0
/**
 * g_vfs_ftp_connection_is_usable:
 * @conn: a connection
 *
 * Checks if this connection can be used to send new commands. For
 * example, if the connection is in the process of a command sequence or
 * if the connection was closed, this is not possible and this function 
 * will return %FALSE.
 *
 * Returns: %TRUE if the connection is still usable
 **/
gboolean
g_vfs_ftp_connection_is_usable (GVfsFtpConnection *conn)
{
  GIOCondition cond;

  g_return_val_if_fail (conn != NULL, FALSE);

  if (conn->waiting_for_reply)
    return FALSE;

  cond = G_IO_ERR | G_IO_HUP | G_IO_IN;
  cond = g_socket_condition_check (g_socket_connection_get_socket (conn->connection), cond);
  if (cond)
    {
      g_debug ("##%2d ##  connection unusuable: %s%s%s\r\n", conn->debug_id,
                                                             cond & G_IO_IN ? "IN " : "",
                                                             cond & G_IO_HUP ? "HUP " : "",
                                                             cond & G_IO_ERR ? "ERR " : "");
      return FALSE;
    }

  return TRUE;
}
Exemple #9
0
static GstFlowReturn
gst_tcp_mix_src_pad_read (GstTCPMixSrcPad * pad, GstBuffer ** outbuf)
{
  GstTCPMixSrc *src = GST_TCP_MIX_SRC (GST_PAD_PARENT (pad));
  gssize avail, receivedBytes;
  GstMapInfo map;
  GError *err = NULL;

  /* if we have a client, wait for read */
  GST_LOG_OBJECT (pad, "asked for a buffer");

  if (!pad->client) {
    if (src->mode == MODE_LOOP)
      goto loop_read;
    else
      goto no_client;
  }

  /* read the buffer header */

read_available_bytes:
  avail = g_socket_get_available_bytes (pad->client);
  if (avail < 0) {
    goto socket_get_available_bytes_error;
  } else if (avail == 0) {
    GIOCondition condition;

    if (!g_socket_condition_wait (pad->client,
            G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP, pad->cancellable, &err))
      goto socket_condition_wait_error;

    condition = g_socket_condition_check (pad->client,
        G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP);

    if ((condition & G_IO_ERR))
      goto socket_condition_error;
    else if ((condition & G_IO_HUP))
      goto socket_condition_hup;

    avail = g_socket_get_available_bytes (pad->client);
    if (avail < 0)
      goto socket_get_available_bytes_error;
  }

  if (0 < avail) {
    gsize readBytes = MIN (avail, MAX_READ_SIZE);
    *outbuf = gst_buffer_new_and_alloc (readBytes);
    gst_buffer_map (*outbuf, &map, GST_MAP_READWRITE);
    receivedBytes = g_socket_receive (pad->client, (gchar *) map.data,
        readBytes, pad->cancellable, &err);
  } else {
    /* Connection closed */
    receivedBytes = 0;
    *outbuf = NULL;
  }

  if (receivedBytes == 0)
    goto socket_connection_closed;
  else if (receivedBytes < 0)
    goto socket_receive_error;

  gst_buffer_unmap (*outbuf, &map);
  gst_buffer_resize (*outbuf, 0, receivedBytes);

#if 0
  GST_LOG_OBJECT (pad,
      "Returning buffer from _get of size %" G_GSIZE_FORMAT
      ", ts %" GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT
      ", offset %" G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT,
      gst_buffer_get_size (*outbuf),
      GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (*outbuf)),
      GST_TIME_ARGS (GST_BUFFER_DURATION (*outbuf)),
      GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf));
#endif

  g_clear_error (&err);

  return GST_FLOW_OK;

  /* Handling Errors */
no_client:
  {
    GST_ELEMENT_ERROR (pad, RESOURCE, READ, (NULL),
        ("No client socket (%s)", GST_PAD_NAME (pad)));

    if (src->mode == MODE_LOOP)
      goto loop_read;
    return GST_FLOW_ERROR;
  }

socket_get_available_bytes_error:
  {
    GST_ELEMENT_ERROR (pad, RESOURCE, READ, (NULL),
        ("Failed to get available bytes from socket"));

    gst_tcp_mix_src_pad_reset (pad);

    if (src->mode == MODE_LOOP)
      goto loop_read;
    return GST_FLOW_ERROR;
  }

socket_condition_wait_error:
  {
    GST_ELEMENT_ERROR (pad, RESOURCE, READ, (NULL),
        ("Select failed: %s", err->message));
    g_clear_error (&err);

    gst_tcp_mix_src_pad_reset (pad);

    if (src->mode == MODE_LOOP)
      goto loop_read;
    return GST_FLOW_ERROR;
  }

socket_condition_error:
  {
    GST_ELEMENT_ERROR (pad, RESOURCE, READ, (NULL), ("Socket in error state"));
    *outbuf = NULL;

    gst_tcp_mix_src_pad_reset (pad);

    if (src->mode == MODE_LOOP)
      goto loop_read;
    return GST_FLOW_ERROR;
  }

socket_condition_hup:
  {
    GST_DEBUG_OBJECT (pad, "Connection closed");
    *outbuf = NULL;

    gst_tcp_mix_src_pad_reset (pad);

    if (src->mode == MODE_LOOP)
      goto loop_read;
    return GST_FLOW_EOS;
  }

socket_connection_closed:
  {
    GST_DEBUG_OBJECT (pad, "Connection closed");
    if (*outbuf) {
      gst_buffer_unmap (*outbuf, &map);
      gst_buffer_unref (*outbuf);
    }
    *outbuf = NULL;

    gst_tcp_mix_src_pad_reset (pad);

    if (src->mode == MODE_LOOP)
      goto loop_read;
    return GST_FLOW_EOS;
  }

socket_receive_error:
  {
    gst_buffer_unmap (*outbuf, &map);
    gst_buffer_unref (*outbuf);
    *outbuf = NULL;

    if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
      GST_DEBUG_OBJECT (pad, "Cancelled reading from socket");

      if (src->mode == MODE_LOOP)
        goto loop_read;
      return GST_FLOW_FLUSHING;
    } else {
      GST_ELEMENT_ERROR (pad, RESOURCE, READ, (NULL),
          ("Failed to read from socket: %s", err->message));

      if (src->mode == MODE_LOOP)
        goto loop_read;
      return GST_FLOW_ERROR;
    }
  }

loop_read:
  {
#if 0
    GstEvent *event = gst_event_new_flush_start ();

    if (!gst_pad_push_event (pad, event)) {
      GST_ERROR_OBJECT (src, "Failed to flush data on %s.%s",
          GST_ELEMENT_NAME (src), GST_PAD_NAME (pad));
    }
#endif

#if 0
    GST_DEBUG_OBJECT (pad, "Looping");
#endif

    if (src->fill == FILL_NONE) {
      gst_tcp_mix_src_pad_wait_for_client (pad);
      goto read_available_bytes;
    }

    enum
    { buffer_size = 1024 };
    *outbuf = gst_buffer_new_and_alloc (buffer_size);

    switch (src->fill) {
      case FILL_ZERO:
        break;
      case FILL_RAND:
      {
        guchar *p;
        gst_buffer_map (*outbuf, &map, GST_MAP_READWRITE);
        for (p = map.data; p < map.data + buffer_size; p += 4) {
          *((int *) p) = rand ();
        }
      } break;
    }
    return GST_FLOW_OK;
  }
}
static gboolean fusion_tls_output_stream_real_write_async_co (FusionTlsOutputStreamWriteAsyncData* _data_) {
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->_tmp0_ = NULL;
	_data_->_tmp0_ = _data_->self->priv->socket;
	_data_->_tmp1_ = 0;
	_data_->_tmp1_ = g_socket_condition_check (_data_->_tmp0_, G_IO_OUT);
	if (_data_->_tmp1_ == 0) {
		_data_->_tmp2_ = NULL;
		_data_->_tmp2_ = _data_->self->priv->socket;
		_data_->_tmp3_ = NULL;
		_data_->_tmp3_ = _data_->cancellable;
		_data_->_tmp4_ = NULL;
		_data_->_tmp4_ = g_socket_create_source (_data_->_tmp2_, (G_IO_OUT | G_IO_HUP) | G_IO_ERR, _data_->_tmp3_);
		_data_->source = _data_->_tmp4_;
		_data_->_tmp5_ = NULL;
		_data_->_tmp5_ = _data_->source;
		g_source_set_callback (_data_->_tmp5_, (GSourceFunc) _fusion_tls_output_stream_real_write_async_co_gsocket_source_func, _data_, NULL);
		_data_->_tmp6_ = NULL;
		_data_->_tmp6_ = _data_->source;
		_data_->_tmp7_ = NULL;
		_data_->_tmp7_ = g_main_context_get_thread_default ();
		g_source_attach (_data_->_tmp6_, _data_->_tmp7_);
		_data_->_state_ = 1;
		return FALSE;
		_state_1:
		;
		_g_source_unref0 (_data_->source);
	}
	_data_->_tmp9_ = NULL;
	_data_->_tmp9_ = _data_->self->priv->connection;
	_data_->_tmp10_ = NULL;
	_data_->_tmp10__length1 = 0;
	_data_->_tmp10_ = _data_->buffer;
	_data_->_tmp10__length1 = _data_->buffer_length1;
	_data_->_tmp11_ = NULL;
	_data_->_tmp11_ = _data_->cancellable;
	_data_->_tmp12_ = 0L;
	_data_->_tmp12_ = fusion_tls_connection_send (_data_->_tmp9_, _data_->_tmp10_, _data_->_tmp10__length1, _data_->_tmp11_, &_data_->_inner_error_);
	_data_->_tmp8_ = _data_->_tmp12_;
	if (_data_->_inner_error_ != NULL) {
		if (_data_->_inner_error_->domain == G_IO_ERROR) {
			g_simple_async_result_set_from_error (_data_->_async_result, _data_->_inner_error_);
			g_error_free (_data_->_inner_error_);
			if (_data_->_state_ == 0) {
				g_simple_async_result_complete_in_idle (_data_->_async_result);
			} else {
				g_simple_async_result_complete (_data_->_async_result);
			}
			g_object_unref (_data_->_async_result);
			return FALSE;
		} else {
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _data_->_inner_error_->message, g_quark_to_string (_data_->_inner_error_->domain), _data_->_inner_error_->code);
			g_clear_error (&_data_->_inner_error_);
			return FALSE;
		}
	}
	_data_->result = _data_->_tmp8_;
	if (_data_->_state_ == 0) {
		g_simple_async_result_complete_in_idle (_data_->_async_result);
	} else {
		g_simple_async_result_complete (_data_->_async_result);
	}
	g_object_unref (_data_->_async_result);
	return FALSE;
	if (_data_->_state_ == 0) {
		g_simple_async_result_complete_in_idle (_data_->_async_result);
	} else {
		g_simple_async_result_complete (_data_->_async_result);
	}
	g_object_unref (_data_->_async_result);
	return FALSE;
}
Exemple #11
0
static GstFlowReturn
gst_tcp_server_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
{
  GstTCPServerSrc *src;
  GstFlowReturn ret = GST_FLOW_OK;
  gssize rret, avail;
  gsize read;
  GError *err = NULL;
  GstMapInfo map;

  src = GST_TCP_SERVER_SRC (psrc);

  if (!GST_OBJECT_FLAG_IS_SET (src, GST_TCP_SERVER_SRC_OPEN))
    goto wrong_state;

  if (!src->client_socket) {
    /* wait on server socket for connections */
    src->client_socket =
        g_socket_accept (src->server_socket, src->cancellable, &err);
    if (!src->client_socket)
      goto accept_error;
    /* now read from the socket. */
  }

  /* if we have a client, wait for read */
  GST_LOG_OBJECT (src, "asked for a buffer");

  /* read the buffer header */
  avail = g_socket_get_available_bytes (src->client_socket);
  if (avail < 0) {
    goto get_available_error;
  } else if (avail == 0) {
    GIOCondition condition;

    if (!g_socket_condition_wait (src->client_socket,
            G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP, src->cancellable, &err))
      goto select_error;

    condition =
        g_socket_condition_check (src->client_socket,
        G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP);

    if ((condition & G_IO_ERR)) {
      GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
          ("Socket in error state"));
      *outbuf = NULL;
      ret = GST_FLOW_ERROR;
      goto done;
    } else if ((condition & G_IO_HUP)) {
      GST_DEBUG_OBJECT (src, "Connection closed");
      *outbuf = NULL;
      ret = GST_FLOW_EOS;
      goto done;
    }
    avail = g_socket_get_available_bytes (src->client_socket);
    if (avail < 0)
      goto get_available_error;
  }

  if (avail > 0) {
    read = MIN (avail, MAX_READ_SIZE);
    *outbuf = gst_buffer_new_and_alloc (read);
    gst_buffer_map (*outbuf, &map, GST_MAP_READWRITE);
    rret =
        g_socket_receive (src->client_socket, (gchar *) map.data, read,
        src->cancellable, &err);
  } else {
    /* Connection closed */
    rret = 0;
    *outbuf = NULL;
    read = 0;
  }

  if (rret == 0) {
    GST_DEBUG_OBJECT (src, "Connection closed");
    ret = GST_FLOW_EOS;
    if (*outbuf) {
      gst_buffer_unmap (*outbuf, &map);
      gst_buffer_unref (*outbuf);
    }
    *outbuf = NULL;
  } else if (rret < 0) {
    if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
      ret = GST_FLOW_FLUSHING;
      GST_DEBUG_OBJECT (src, "Cancelled reading from socket");
    } else {
      ret = GST_FLOW_ERROR;
      GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
          ("Failed to read from socket: %s", err->message));
    }
    gst_buffer_unmap (*outbuf, &map);
    gst_buffer_unref (*outbuf);
    *outbuf = NULL;
  } else {
    ret = GST_FLOW_OK;
    gst_buffer_unmap (*outbuf, &map);
    gst_buffer_resize (*outbuf, 0, rret);

    GST_LOG_OBJECT (src,
        "Returning buffer from _get of size %" G_GSIZE_FORMAT ", ts %"
        GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT
        ", offset %" G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT,
        gst_buffer_get_size (*outbuf),
        GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (*outbuf)),
        GST_TIME_ARGS (GST_BUFFER_DURATION (*outbuf)),
        GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf));
  }
  g_clear_error (&err);

done:
  return ret;

wrong_state:
  {
    GST_DEBUG_OBJECT (src, "connection to closed, cannot read data");
    return GST_FLOW_FLUSHING;
  }
accept_error:
  {
    if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
      GST_DEBUG_OBJECT (src, "Cancelled accepting of client");
    } else {
      GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
          ("Failed to accept client: %s", err->message));
    }
    g_clear_error (&err);
    return GST_FLOW_ERROR;
  }
select_error:
  {
    GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
        ("Select failed: %s", err->message));
    g_clear_error (&err);
    return GST_FLOW_ERROR;
  }
get_available_error:
  {
    GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
        ("Failed to get available bytes from socket"));
    return GST_FLOW_ERROR;
  }
}