예제 #1
0
/**
 * g_socket_listener_add_socket:
 * @listener: a #GSocketListener
 * @socket: a listening #GSocket
 * @source_object: (allow-none): Optional #GObject identifying this source
 * @error: #GError for error reporting, or %NULL to ignore.
 *
 * Adds @socket to the set of sockets that we try to accept
 * new clients from. The socket must be bound to a local
 * address and listened to.
 *
 * @source_object will be passed out in the various calls
 * to accept to identify this particular source, which is
 * useful if you're listening on multiple addresses and do
 * different things depending on what address is connected to.
 *
 * Returns: %TRUE on success, %FALSE on error.
 *
 * Since: 2.22
 */
gboolean
g_socket_listener_add_socket (GSocketListener  *listener,
			      GSocket          *socket,
			      GObject          *source_object,
			      GError          **error)
{
  if (!check_listener (listener, error))
    return FALSE;

  /* TODO: Check that socket it is bound & not closed? */

  if (g_socket_is_closed (socket))
    {
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
			   _("Added socket is closed"));
      return FALSE;
    }

  g_object_ref (socket);
  g_ptr_array_add (listener->priv->sockets, socket);

  if (source_object)
    g_object_set_qdata_full (G_OBJECT (socket), source_quark,
			     g_object_ref (source_object), g_object_unref);


  if (G_SOCKET_LISTENER_GET_CLASS (listener)->changed)
    G_SOCKET_LISTENER_GET_CLASS (listener)->changed (listener);

  return TRUE;
}
예제 #2
0
void
kms_sctp_connection_close (KmsSCTPConnection * conn)
{
  GError *err = NULL;

  g_return_if_fail (conn != NULL);

  if (conn->socket == NULL)
    return;

  if (g_socket_is_closed (conn->socket)) {
    GST_DEBUG ("Socket is already closed");
    return;
  }

  if (!g_socket_shutdown (conn->socket, TRUE, TRUE, &err)) {
    GST_DEBUG ("%s", err->message);
    g_clear_error (&err);
  }

  GST_DEBUG ("Closing socket");

  if (!g_socket_close (conn->socket, &err)) {
    GST_ERROR ("Failed to close socket %p: %s", conn->socket, err->message);
    g_clear_error (&err);
  }
}
예제 #3
0
파일: dasom-im.c 프로젝트: namwonkeun/dasom
gboolean dasom_im_get_surrounding (DasomIM  *im,
                                   gchar   **text,
                                   gint     *cursor_index)
{
  g_debug (G_STRLOC ": %s", G_STRFUNC);

  g_return_val_if_fail (DASOM_IS_IM (im), FALSE);

  GSocket *socket = g_socket_connection_get_socket (im->connection);
  if (!socket || g_socket_is_closed (socket))
  {
    if (text)
      *text = g_strdup ("");

    if (cursor_index)
      *cursor_index = 0;

    g_warning ("socket is closed");

    return FALSE;
  }

  dasom_send_message (socket, DASOM_MESSAGE_GET_SURROUNDING, NULL, 0, NULL);
  dasom_result_iteration_until (im->result,
                                dasom_im_sockets_context,
                                DASOM_MESSAGE_GET_SURROUNDING_REPLY);

  if (im->result->reply == NULL)
  {
    if (text)
      *text = g_strdup ("");

    if (cursor_index)
      *cursor_index = 0;

    return FALSE;
  }

  if (text)
    *text = g_strndup (im->result->reply->data,
                       im->result->reply->header->data_len - 1 -
                       sizeof (gint) - sizeof (gboolean));

  if (cursor_index)
  {
    *cursor_index = *(gint *) (im->result->reply->data +
                               im->result->reply->header->data_len -
                               sizeof (gint) - sizeof (gboolean));
  }

  return *(gboolean *) (im->result->reply->data - sizeof (gboolean));
}
예제 #4
0
static gssize
sctp_socket_receive_with_blocking (GSocket * socket, gchar * buffer, gsize size,
    gboolean blocking, GCancellable * cancellable, guint * streamid,
    GError ** error)
{
  gssize ret;

  if (g_socket_is_closed (socket)) {
    g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
        "Socket is already closed");
    return -1;
  }

  if (g_cancellable_set_error_if_cancelled (cancellable, error))
    return -1;

  while (TRUE) {
    struct sctp_sndrcvinfo sndrcvinfo;
    int flags = 0;

    if (blocking &&
        !g_socket_condition_wait (socket, G_IO_IN, cancellable, error))
      return -1;

    if ((ret = sctp_recvmsg (g_socket_get_fd (socket), buffer, size, NULL, 0,
                &sndrcvinfo, &flags)) < 0) {
      if (errno == EINTR)
        continue;

      if (blocking) {
        if (errno == EWOULDBLOCK || errno == EAGAIN)
          continue;
      }

      g_set_error (error, G_IO_ERROR, errno, "Error receiving data: %s",
          strerror (errno));

      return -1;
    }

    *streamid = sndrcvinfo.sinfo_stream;
    break;
  }

  return ret;
}
예제 #5
0
파일: dasom-im.c 프로젝트: namwonkeun/dasom
void dasom_im_reset (DasomIM *im)
{
  g_debug (G_STRLOC ": %s", G_STRFUNC);

  g_return_if_fail (DASOM_IS_IM (im));

  GSocket *socket = g_socket_connection_get_socket (im->connection);
  if (!socket || g_socket_is_closed (socket))
  {
    g_warning ("socket is closed");
    return;
  }

  dasom_send_message (socket, DASOM_MESSAGE_RESET, NULL, 0, NULL);
  dasom_result_iteration_until (im->result,
                                dasom_im_sockets_context,
                                DASOM_MESSAGE_RESET_REPLY);
}
예제 #6
0
파일: dasom-im.c 프로젝트: namwonkeun/dasom
void dasom_im_set_use_preedit (DasomIM  *im,
                               gboolean  use_preedit)
{
  g_debug (G_STRLOC ": %s", G_STRFUNC);

  g_return_if_fail (DASOM_IS_IM (im));

  GSocket *socket = g_socket_connection_get_socket (im->connection);
  if (!socket || g_socket_is_closed (socket))
  {
    g_warning ("socket is closed");
    return;
  }

  dasom_send_message (socket, DASOM_MESSAGE_SET_USE_PREEDIT,
                      (gchar *) &use_preedit, sizeof (gboolean), NULL);
  dasom_result_iteration_until (im->result,
                                dasom_im_sockets_context,
                                DASOM_MESSAGE_SET_USE_PREEDIT_REPLY);
}
예제 #7
0
파일: dasom-im.c 프로젝트: namwonkeun/dasom
void dasom_im_set_cursor_location (DasomIM              *im,
                                   const DasomRectangle *area)
{
  g_debug (G_STRLOC ": %s", G_STRFUNC);

  g_return_if_fail (DASOM_IS_IM (im));

  GSocket *socket = g_socket_connection_get_socket (im->connection);
  if (!socket || g_socket_is_closed (socket))
  {
    g_warning ("socket is closed");
    return;
  }

  dasom_send_message (socket, DASOM_MESSAGE_SET_CURSOR_LOCATION,
                      (gchar *) area, sizeof (DasomRectangle), NULL);
  dasom_result_iteration_until (im->result,
                                dasom_im_sockets_context,
                                DASOM_MESSAGE_SET_CURSOR_LOCATION_REPLY);
}
예제 #8
0
static gssize
sctp_socket_send_with_blocking (GSocket * socket, guint streamid,
    guint32 timetolive, const gchar * buffer, gsize size, gboolean blocking,
    GCancellable * cancellable, GError ** error)
{
  gssize ret;

  if (g_socket_is_closed (socket)) {
    g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
        "Socket is already closed");
    return -1;
  }

  if (g_cancellable_set_error_if_cancelled (cancellable, error))
    return -1;

  while (TRUE) {
    if (blocking &&
        !g_socket_condition_wait (socket, G_IO_OUT, cancellable, error))
      return -1;

    if ((ret = sctp_sendmsg (g_socket_get_fd (socket), buffer, size, NULL, 0,
                0, 0, streamid, timetolive, 0)) < 0) {
      if (errno == EINTR)
        continue;

      if (blocking) {
        if (errno == EWOULDBLOCK || errno == EAGAIN)
          continue;
      }

      g_set_error (error, G_IO_ERROR, errno, "Error sending data: %s",
          strerror (errno));
      return -1;
    }
    break;
  }

  return ret;
}
예제 #9
0
파일: dasom-im.c 프로젝트: namwonkeun/dasom
gboolean dasom_im_filter_event (DasomIM *im, DasomEvent *event)
{
  g_debug (G_STRLOC ":%s", G_STRFUNC);

  g_return_val_if_fail (DASOM_IS_IM (im), FALSE);

  GSocket *socket = g_socket_connection_get_socket (im->connection);
  if (!socket || g_socket_is_closed (socket))
  {
    g_warning ("socket is closed");
    return dasom_im_filter_event_fallback (im, event);
  }

  dasom_send_message (socket, DASOM_MESSAGE_FILTER_EVENT, event,
                      sizeof (DasomEvent), NULL);
  dasom_result_iteration_until (im->result,
                                dasom_im_sockets_context,
                                DASOM_MESSAGE_FILTER_EVENT_REPLY);

  if (im->result->reply == NULL)
    return dasom_im_filter_event_fallback (im, event);

  return *(gboolean *) (im->result->reply->data);
}
예제 #10
0
파일: dasom-im.c 프로젝트: namwonkeun/dasom
void dasom_im_set_surrounding (DasomIM    *im,
                               const char *text,
                               gint        len,
                               gint        cursor_index)
{
  g_debug (G_STRLOC ": %s", G_STRFUNC);

  g_return_if_fail (DASOM_IS_IM (im));

  GSocket *socket = g_socket_connection_get_socket (im->connection);
  if (!socket || g_socket_is_closed (socket))
  {
    g_warning ("socket is closed");
    return;
  }

  gchar *data = NULL;
  gint   str_len;

  if (len == -1)
    str_len = strlen (text);
  else
    str_len = len;

  data = g_strndup (text, str_len);
  data = g_realloc (data, str_len + 1 + 2 * sizeof (gint));

  *(gint *) (data + str_len + 1) = len;
  *(gint *) (data + str_len + 1 + sizeof (gint)) = cursor_index;

  dasom_send_message (socket, DASOM_MESSAGE_SET_SURROUNDING, data,
                      str_len + 1 + 2 * sizeof (gint), g_free);
  dasom_result_iteration_until (im->result,
                                dasom_im_sockets_context,
                                DASOM_MESSAGE_SET_SURROUNDING_REPLY);
}
예제 #11
0
파일: dasom-im.c 프로젝트: namwonkeun/dasom
static gboolean
on_incoming_message (GSocket      *socket,
                     GIOCondition  condition,
                     gpointer      user_data)
{
  g_debug (G_STRLOC ": %s: socket fd:%d", G_STRFUNC, g_socket_get_fd (socket));

  DasomIM *im = DASOM_IM (user_data);
  dasom_message_unref (im->result->reply);
  im->result->is_dispatched = TRUE;

  if (condition & (G_IO_HUP | G_IO_ERR))
  {
    /* Because two GSource is created over one socket,
     * when G_IO_HUP | G_IO_ERR, callback can run two times.
     * the following code avoid that callback runs two times. */
    GSource *source = g_main_current_source ();

    if (source == im->default_context_source)
      g_source_destroy (im->sockets_context_source);
    else if (source == im->sockets_context_source)
      g_source_destroy (im->default_context_source);

    if (!g_socket_is_closed (socket))
      g_socket_close (socket, NULL);

    im->result->reply    = NULL;

    g_critical (G_STRLOC ": %s: G_IO_HUP | G_IO_ERR", G_STRFUNC);

    return G_SOURCE_REMOVE;
  }

  DasomMessage *message;
  message = dasom_recv_message (socket);
  im->result->reply = message;
  gboolean retval;

  if (G_UNLIKELY (message == NULL))
  {
    g_critical (G_STRLOC ": NULL message");
    return G_SOURCE_CONTINUE;
  }

  switch (message->header->type)
  {
    /* reply */
    case DASOM_MESSAGE_FILTER_EVENT_REPLY:
    case DASOM_MESSAGE_RESET_REPLY:
    case DASOM_MESSAGE_FOCUS_IN_REPLY:
    case DASOM_MESSAGE_FOCUS_OUT_REPLY:
    case DASOM_MESSAGE_SET_SURROUNDING_REPLY:
    case DASOM_MESSAGE_GET_SURROUNDING_REPLY:
    case DASOM_MESSAGE_SET_CURSOR_LOCATION_REPLY:
    case DASOM_MESSAGE_SET_USE_PREEDIT_REPLY:
      break;
    /* signals */
    case DASOM_MESSAGE_PREEDIT_START:
      g_signal_emit_by_name (im, "preedit-start");
      dasom_send_message (socket, DASOM_MESSAGE_PREEDIT_START_REPLY, NULL, 0, NULL);
      break;
    case DASOM_MESSAGE_PREEDIT_END:
      g_signal_emit_by_name (im, "preedit-end");
      dasom_send_message (socket, DASOM_MESSAGE_PREEDIT_END_REPLY, NULL, 0, NULL);
      break;
    case DASOM_MESSAGE_PREEDIT_CHANGED:
      g_free (im->preedit_string);
      im->preedit_string = g_strndup (message->data,
                                      message->header->data_len - 1 - sizeof (gint));
      im->cursor_pos = *(gint *) (message->data +
                                  message->header->data_len - sizeof (gint));
      g_signal_emit_by_name (im, "preedit-changed");
      dasom_send_message (socket,
                          DASOM_MESSAGE_PREEDIT_CHANGED_REPLY,
                          NULL, 0, NULL);
      break;
    case DASOM_MESSAGE_COMMIT:
      dasom_message_ref (message);
      g_signal_emit_by_name (im, "commit", (const gchar *) message->data);
      dasom_message_unref (message);
      dasom_send_message (socket, DASOM_MESSAGE_COMMIT_REPLY, NULL, 0, NULL);
      break;
    case DASOM_MESSAGE_RETRIEVE_SURROUNDING:
      g_signal_emit_by_name (im, "retrieve-surrounding", &retval);
      dasom_send_message (socket, DASOM_MESSAGE_RETRIEVE_SURROUNDING_REPLY,
                          &retval, sizeof (gboolean), NULL);
      break;
    case DASOM_MESSAGE_DELETE_SURROUNDING:
      dasom_message_ref (message);
      g_signal_emit_by_name (im, "delete-surrounding",
                             ((gint *) message->data)[0],
                             ((gint *) message->data)[1], &retval);
      dasom_message_unref (message);
      dasom_send_message (socket, DASOM_MESSAGE_DELETE_SURROUNDING_REPLY,
                          &retval, sizeof (gboolean), NULL);
      break;
    default:
      g_warning (G_STRLOC ": %s: Unknown message type: %d", G_STRFUNC, message->header->type);
      break;
  }

  return G_SOURCE_CONTINUE;
}
예제 #12
0
gssize fusion_tls_connection_send (FusionTLSConnection* self, guint8* buffer, int buffer_length1, GCancellable* cancellable, GError** error) {
	gssize result = 0L;
	gssize ret = 0L;
	GSocket* _tmp0_ = NULL;
	gboolean _tmp1_ = FALSE;
	GCancellable* _tmp3_ = NULL;
	GError * _inner_error_ = NULL;
	g_return_val_if_fail (self != NULL, 0L);
	g_warning ("TLSConnection.vala:123: SEND");
	_tmp0_ = self->priv->socket;
	_tmp1_ = g_socket_is_closed (_tmp0_);
	if (_tmp1_) {
		GError* _tmp2_ = NULL;
		_tmp2_ = g_error_new_literal (G_IO_ERROR, G_IO_ERROR_CLOSED, "Socket is already closed");
		_inner_error_ = _tmp2_;
		if (_inner_error_->domain == G_IO_ERROR) {
			g_propagate_error (error, _inner_error_);
			return 0L;
		} else {
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
			g_clear_error (&_inner_error_);
			return 0L;
		}
	}
	_tmp3_ = cancellable;
	g_cancellable_set_error_if_cancelled (_tmp3_, &_inner_error_);
	if (_inner_error_ != NULL) {
		if (_inner_error_->domain == G_IO_ERROR) {
			g_propagate_error (error, _inner_error_);
			return 0L;
		} else {
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
			g_clear_error (&_inner_error_);
			return 0L;
		}
	}
	while (TRUE) {
		gboolean _tmp4_ = FALSE;
		GSocket* _tmp5_ = NULL;
		gboolean _tmp6_ = FALSE;
		gboolean _tmp11_ = FALSE;
		struct gnutls_session_int* _tmp12_ = NULL;
		guint8* _tmp13_ = NULL;
		gint _tmp13__length1 = 0;
		guint8* _tmp14_ = NULL;
		gint _tmp14__length1 = 0;
		gssize _tmp15_ = 0L;
		gssize _tmp16_ = 0L;
		_tmp5_ = self->priv->socket;
		_tmp6_ = g_socket_get_blocking (_tmp5_);
		if (_tmp6_) {
			gboolean _tmp7_ = FALSE;
			GSocket* _tmp8_ = NULL;
			GCancellable* _tmp9_ = NULL;
			gboolean _tmp10_ = FALSE;
			_tmp8_ = self->priv->socket;
			_tmp9_ = cancellable;
			_tmp10_ = g_socket_condition_wait (_tmp8_, G_IO_OUT, _tmp9_, &_inner_error_);
			_tmp7_ = _tmp10_;
			if (_inner_error_ != NULL) {
				if (_inner_error_->domain == G_IO_ERROR) {
					g_propagate_error (error, _inner_error_);
					return 0L;
				} else {
					g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
					g_clear_error (&_inner_error_);
					return 0L;
				}
			}
			_tmp4_ = !_tmp7_;
		} else {
			_tmp4_ = FALSE;
		}
		_tmp11_ = _tmp4_;
		if (_tmp11_) {
			result = (gssize) (-1);
			return result;
		}
		_tmp12_ = self->tls_session;
		_tmp13_ = buffer;
		_tmp13__length1 = buffer_length1;
		_tmp14_ = buffer;
		_tmp14__length1 = buffer_length1;
		_tmp15_ = gnutls_record_send (_tmp12_, _tmp13_, (gsize) _tmp14__length1);
		ret = _tmp15_;
		_tmp16_ = ret;
		if (_tmp16_ < ((gssize) 0)) {
			int ecode = 0;
			gssize _tmp17_ = 0L;
			int _tmp18_ = 0;
			gboolean _tmp19_ = FALSE;
			GSocket* _tmp20_ = NULL;
			gboolean _tmp21_ = FALSE;
			gboolean _tmp23_ = FALSE;
			int _tmp24_ = 0;
			_tmp17_ = ret;
			ecode = (int) _tmp17_;
			_tmp18_ = ecode;
			if (_tmp18_ == GNUTLS_E_INTERRUPTED) {
				continue;
			}
			_tmp20_ = self->priv->socket;
			_tmp21_ = g_socket_get_blocking (_tmp20_);
			if (_tmp21_) {
				int _tmp22_ = 0;
				_tmp22_ = ecode;
				_tmp19_ = _tmp22_ == GNUTLS_E_AGAIN;
			} else {
				_tmp19_ = FALSE;
			}
			_tmp23_ = _tmp19_;
			if (_tmp23_) {
				continue;
			}
			_tmp24_ = ecode;
			fusion_tls_connection_handle_error (self, _tmp24_, 2, &_inner_error_);
			if (_inner_error_ != NULL) {
				if (_inner_error_->domain == G_IO_ERROR) {
					g_propagate_error (error, _inner_error_);
					return 0L;
				} else {
					g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
					g_clear_error (&_inner_error_);
					return 0L;
				}
			}
			continue;
		}
		break;
	}
	result = ret;
	return result;
}