static void soup_input_stream_read_async (GInputStream *stream, void *buffer, gsize count, int io_priority, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data) { SoupInputStreamPrivate *priv = SOUP_INPUT_STREAM_GET_PRIVATE (stream); GSimpleAsyncResult *result; /* If the session uses the default GMainContext, then we can do * async I/O directly. But if it has its own main context, we fall * back to the async-via-sync-in-another-thread implementation. */ if (soup_session_get_async_context (priv->session)) { G_INPUT_STREAM_CLASS (soup_input_stream_parent_class)-> read_async (stream, buffer, count, io_priority, cancellable, callback, user_data); return; } result = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, soup_input_stream_read_async); if (priv->finished) { g_simple_async_result_set_op_res_gssize (result, 0); g_simple_async_result_complete_in_idle (result); g_object_unref (result); return; } if (priv->leftover_bufsize) { gsize nread = read_from_leftover (priv, buffer, count); g_simple_async_result_set_op_res_gssize (result, nread); g_simple_async_result_complete_in_idle (result); g_object_unref (result); return; } priv->result = result; priv->got_chunk_cb = read_async_done; priv->finished_cb = read_async_done; priv->cancelled_cb = read_async_done; soup_input_stream_prepare_for_io (stream, cancellable, buffer, count); }
void nm_connectivity_check_async (NMConnectivity *self, GAsyncReadyCallback callback, gpointer user_data) { NMConnectivityPrivate *priv; #if WITH_CONCHECK SoupMessage *msg; #endif GSimpleAsyncResult *simple; g_return_if_fail (NM_IS_CONNECTIVITY (self)); priv = NM_CONNECTIVITY_GET_PRIVATE (self); simple = g_simple_async_result_new (G_OBJECT (self), callback, user_data, nm_connectivity_check_async); #if WITH_CONCHECK if (priv->uri && priv->interval) { msg = soup_message_new ("GET", priv->uri); soup_message_set_flags (msg, SOUP_MESSAGE_NO_REDIRECT); soup_session_queue_message (priv->soup_session, msg, nm_connectivity_check_cb, simple); return; } #endif g_simple_async_result_set_op_res_gssize (simple, priv->state); g_simple_async_result_complete_in_idle (simple); }
static void read_async_done (GInputStream *stream) { SoupInputStreamPrivate *priv = SOUP_INPUT_STREAM_GET_PRIVATE (stream); GSimpleAsyncResult *result; GError *error = NULL; result = priv->result; priv->result = NULL; if (g_cancellable_set_error_if_cancelled (priv->cancellable, &error) || set_error_if_http_failed (priv->msg, &error)) { g_simple_async_result_set_from_error (result, error); g_error_free (error); } else g_simple_async_result_set_op_res_gssize (result, priv->caller_nread); priv->got_chunk_cb = NULL; priv->finished_cb = NULL; priv->cancelled_cb = NULL; soup_input_stream_done_io (stream); g_simple_async_result_complete (result); g_object_unref (result); }
static void soup_output_stream_write_async (GOutputStream *stream, const void *buffer, gsize count, int io_priority, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data) { SoupOutputStreamPrivate *priv = SOUP_OUTPUT_STREAM_GET_PRIVATE (stream); GSimpleAsyncResult *result; result = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, soup_output_stream_write_async); if (priv->size > 0 && priv->offset + count > priv->size) { GError *error; error = g_error_new (G_IO_ERROR, G_IO_ERROR_NO_SPACE, "Write would exceed caller-defined file size"); g_simple_async_result_set_from_error (result, error); g_error_free (error); } else { g_byte_array_append (priv->ba, buffer, count); priv->offset += count; g_simple_async_result_set_op_res_gssize (result, count); } g_simple_async_result_complete_in_idle (result); g_object_unref (result); }
void e_reap_trash_directory (GFile *trash_directory, gint expiry_in_days, gint io_priority, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data) { GSimpleAsyncResult *simple; g_return_if_fail (G_IS_FILE (trash_directory)); g_return_if_fail (expiry_in_days > 0); simple = g_simple_async_result_new ( G_OBJECT (trash_directory), callback, user_data, e_reap_trash_directory); g_simple_async_result_set_check_cancellable (simple, cancellable); g_simple_async_result_set_op_res_gssize (simple, expiry_in_days); g_simple_async_result_run_in_thread ( simple, reap_trash_directory_thread, io_priority, cancellable); g_object_unref (simple); }
static VALUE rg_set_op_res_gssize(VALUE self, VALUE op_res) { g_simple_async_result_set_op_res_gssize(_SELF(self), RVAL2GSSIZE(op_res)); return self; }
static void g_tls_output_stream_gnutls_write_async (GOutputStream *stream, const void *buffer, gsize count, gint io_priority, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data) { GTlsOutputStreamGnutls *tls_stream = G_TLS_OUTPUT_STREAM_GNUTLS (stream); GSimpleAsyncResult *simple; gssize nwrote; GError *error = NULL; GSource *source; g_return_if_fail (tls_stream->priv->conn != NULL); simple = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_tls_output_stream_gnutls_write_async); nwrote = g_tls_connection_gnutls_write (tls_stream->priv->conn, buffer, count, FALSE, cancellable, &error); if (nwrote >= 0 || !g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK)) { if (error) { g_simple_async_result_set_from_error (simple, error); g_error_free (error); } else g_simple_async_result_set_op_res_gssize (simple, nwrote); g_simple_async_result_complete_in_idle (simple); g_object_unref (simple); return; } if (error) g_error_free (error); tls_stream->priv->cancellable = cancellable ? g_object_ref (cancellable) : NULL; tls_stream->priv->buffer = buffer; tls_stream->priv->count = count; source = g_tls_connection_gnutls_create_source (tls_stream->priv->conn, G_IO_OUT, tls_stream->priv->cancellable); g_source_set_callback (source, (GSourceFunc) g_tls_output_stream_gnutls_write_ready, simple, NULL); g_source_attach (source, g_main_context_get_thread_default ()); g_source_unref (source); }
static void apply_complete_one (GSimpleAsyncResult *result) { guint count; count = g_simple_async_result_get_op_res_gssize (result); count--; g_simple_async_result_set_op_res_gssize (result, count); if (count == 0) g_simple_async_result_complete (result); }
static gboolean read_async_cb (ReadAsyncData *data, GIOCondition condition, int fd) { GSimpleAsyncResult *simple; GError *error = NULL; gssize count_read; /* We know that we can read from fd once without blocking */ while (1) { if (g_cancellable_set_error_if_cancelled (data->cancellable, &error)) { count_read = -1; break; } count_read = read (data->stream->priv->fd, data->buffer, data->count); if (count_read == -1) { int errsv = errno; if (errsv == EINTR) continue; g_set_error (&error, G_IO_ERROR, g_io_error_from_errno (errsv), _("Error reading from unix: %s"), g_strerror (errsv)); } break; } simple = g_simple_async_result_new (G_OBJECT (data->stream), data->callback, data->user_data, g_unix_input_stream_read_async); g_simple_async_result_set_op_res_gssize (simple, count_read); if (count_read == -1) { g_simple_async_result_set_from_error (simple, error); g_error_free (error); } /* Complete immediately, not in idle, since we're already in a mainloop callout */ g_simple_async_result_complete (simple); g_object_unref (simple); return FALSE; }
void nm_connectivity_check_async (NMConnectivity *self, GAsyncReadyCallback callback, gpointer user_data) { NMConnectivityPrivate *priv; GSimpleAsyncResult *simple; g_return_if_fail (NM_IS_CONNECTIVITY (self)); priv = NM_CONNECTIVITY_GET_PRIVATE (self); simple = g_simple_async_result_new (G_OBJECT (self), callback, user_data, nm_connectivity_check_async); #if WITH_CONCHECK if (priv->uri && priv->interval) { SoupMessage *msg; ConCheckCbData *cb_data = g_slice_new (ConCheckCbData); msg = soup_message_new ("GET", priv->uri); soup_message_set_flags (msg, SOUP_MESSAGE_NO_REDIRECT); /* Disable HTTP/1.1 keepalive; the connection should not persist */ soup_message_headers_append (msg->request_headers, "Connection", "close"); cb_data->simple = simple; cb_data->uri = g_strdup (priv->uri); cb_data->response = g_strdup (priv->response); /* For internal calls (periodic), remember the check-id at time of scheduling. */ cb_data->check_id_when_scheduled = IS_PERIODIC_CHECK (callback) ? priv->check_id : 0; soup_session_queue_message (priv->soup_session, msg, nm_connectivity_check_cb, cb_data); priv->initial_check_obsoleted = TRUE; _LOGD ("check: send %srequest to '%s'", IS_PERIODIC_CHECK (callback) ? "periodic " : "", priv->uri); return; } else { g_warn_if_fail (!IS_PERIODIC_CHECK (callback)); _LOGD ("check: faking request. Connectivity check disabled"); } #else _LOGD ("check: faking request. Compiled without connectivity-check support"); #endif g_simple_async_result_set_op_res_gssize (simple, priv->state); g_simple_async_result_complete_in_idle (simple); g_object_unref (simple); }
static void nm_connectivity_check_cb (SoupSession *session, SoupMessage *msg, gpointer user_data) { GSimpleAsyncResult *simple = user_data; NMConnectivity *self; NMConnectivityPrivate *priv; NMConnectivityState new_state; const char *nm_header; self = NM_CONNECTIVITY (g_async_result_get_source_object (G_ASYNC_RESULT (simple))); g_object_unref (self); priv = NM_CONNECTIVITY_GET_PRIVATE (self); if (SOUP_STATUS_IS_TRANSPORT_ERROR (msg->status_code)) { nm_log_info (LOGD_CONCHECK, "Connectivity check for uri '%s' failed with '%s'.", priv->uri, msg->reason_phrase); new_state = NM_CONNECTIVITY_LIMITED; goto done; } /* Check headers; if we find the NM-specific one we're done */ nm_header = soup_message_headers_get_one (msg->response_headers, "X-NetworkManager-Status"); if (g_strcmp0 (nm_header, "online") == 0) { nm_log_dbg (LOGD_CONCHECK, "Connectivity check for uri '%s' with Status header successful.", priv->uri); new_state = NM_CONNECTIVITY_FULL; } else if (msg->status_code == SOUP_STATUS_OK) { /* check response */ if (msg->response_body->data && (g_str_has_prefix (msg->response_body->data, priv->response))) { nm_log_dbg (LOGD_CONCHECK, "Connectivity check for uri '%s' successful.", priv->uri); new_state = NM_CONNECTIVITY_FULL; } else { nm_log_info (LOGD_CONCHECK, "Connectivity check for uri '%s' did not match expected response '%s'; assuming captive portal.", priv->uri, priv->response); new_state = NM_CONNECTIVITY_PORTAL; } } else { nm_log_info (LOGD_CONCHECK, "Connectivity check for uri '%s' returned status '%d %s'; assuming captive portal.", priv->uri, msg->status_code, msg->reason_phrase); new_state = NM_CONNECTIVITY_PORTAL; } done: g_simple_async_result_set_op_res_gssize (simple, new_state); g_simple_async_result_complete (simple); update_state (self, new_state); }
static void g_simple_async_write_thread_handler(GSimpleAsyncResult *simple, GObject *object, GCancellable *cancellable) { HevSerialPort *self = HEV_SERIAL_PORT(object); HevSerialPortPrivate *priv = HEV_SERIAL_PORT_GET_PRIVATE(self); HevSerialPortReadWriteData *data = NULL; gssize i = 0, w = 0; GPollFD fds[] = { {priv->fd, G_IO_OUT, G_IO_NVAL} }; g_debug("%s:%d[%s]", __FILE__, __LINE__, __FUNCTION__); data = g_simple_async_result_get_op_res_gpointer(simple); while(1) { gint rfds = g_poll(fds, 1, 200); if(0 < rfds) { i = write(priv->fd, data->buffer+w, data->count-w); if(-1 == i) { g_simple_async_result_set_error(simple, G_IO_ERROR, g_io_error_from_errno(errno), "%s", strerror(errno)); break; } else w += i; } if(data->count == w) break; if(g_cancellable_is_cancelled(cancellable)) goto ret; } g_simple_async_result_set_op_res_gssize(simple, w); ret: g_free(data); }
static gboolean g_tls_output_stream_gnutls_write_ready (GIOStreamAdapter *adapter, gpointer user_data) { GTlsOutputStreamGnutls *tls_stream; GSimpleAsyncResult *simple = user_data; gssize nwrote; GError *error = NULL; tls_stream = G_TLS_OUTPUT_STREAM_GNUTLS (g_async_result_get_source_object (G_ASYNC_RESULT (simple))); g_object_unref (tls_stream); nwrote = g_tls_connection_gnutls_write (tls_stream->priv->conn, tls_stream->priv->buffer, tls_stream->priv->count, FALSE, tls_stream->priv->cancellable, &error); if (nwrote == -1 && g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK)) { g_error_free (error); return TRUE; } if (error) { g_simple_async_result_set_from_error (simple, error); g_error_free (error); } else g_simple_async_result_set_op_res_gssize (simple, nwrote); if (tls_stream->priv->cancellable) g_object_unref (tls_stream->priv->cancellable); g_simple_async_result_complete (simple); g_object_unref (simple); return FALSE; }
static gboolean _g_ssl_input_stream_read_ready(gpointer data) { GSSLInputStream *stream = (GSSLInputStream *)data; GSimpleAsyncResult *simple; GError *error = NULL; gssize result; g_assert(stream != NULL); g_assert(stream->priv->result != NULL); if(!g_source_is_destroyed(g_main_current_source())) { simple = stream->priv->result; stream->priv->result = NULL; if((result = openssl_read(stream->priv->ssl, stream->priv->buffer, stream->priv->count, stream->priv->cancellable, &error)) > 0) { g_simple_async_result_set_op_res_gssize(simple, result); } if(error) { g_simple_async_result_set_from_error(simple, error); g_error_free(error); } if(stream->priv->cancellable) { g_object_unref(stream->priv->cancellable); } g_simple_async_result_complete(simple); g_object_unref(simple); } return FALSE; }
void tpaw_user_info_apply_async (TpawUserInfo *self, GAsyncReadyCallback callback, gpointer user_data) { GSimpleAsyncResult *result; const gchar *new_nickname; guint count = 0; GList *l, *next; g_return_if_fail (TPAW_IS_USER_INFO (self)); result = g_simple_async_result_new ((GObject *) self, callback, user_data, tpaw_user_info_apply_async); /* Apply avatar */ tpaw_avatar_chooser_apply_async ( (TpawAvatarChooser *) self->priv->avatar_chooser, avatar_chooser_apply_cb, g_object_ref (result)); count++; /* Apply nickname */ new_nickname = gtk_entry_get_text (GTK_ENTRY (self->priv->nickname_entry)); if (tp_strdiff (new_nickname, tp_account_get_nickname (self->priv->account))) { tp_account_set_nickname_async (self->priv->account, new_nickname, set_nickname_cb, g_object_ref (result)); count++; } /* Remove empty fields */ for (l = self->priv->details_to_set; l != NULL; l = next) { TpContactInfoField *field = l->data; next = l->next; if (field_value_is_empty (field)) { DEBUG ("Drop empty field: %s", field->field_name); tp_contact_info_field_free (field); self->priv->details_to_set = g_list_delete_link (self->priv->details_to_set, l); } } if (self->priv->details_to_set != NULL) { if (self->priv->details_changed) { tp_connection_set_contact_info_async ( tp_account_get_connection (self->priv->account), self->priv->details_to_set, set_contact_info_cb, g_object_ref (result)); count++; } tp_contact_info_list_free (self->priv->details_to_set); self->priv->details_to_set = NULL; } self->priv->details_changed = FALSE; g_simple_async_result_set_op_res_gssize (result, count); g_object_unref (result); }
static void nm_connectivity_check_cb (SoupSession *session, SoupMessage *msg, gpointer user_data) { NMConnectivity *self; NMConnectivityPrivate *priv; ConCheckCbData *cb_data = user_data; GSimpleAsyncResult *simple = cb_data->simple; NMConnectivityState new_state; const char *nm_header; const char *uri = cb_data->uri; const char *response = cb_data->response ? cb_data->response : NM_CONFIG_DEFAULT_CONNECTIVITY_RESPONSE; self = NM_CONNECTIVITY (g_async_result_get_source_object (G_ASYNC_RESULT (simple))); /* it is safe to unref @self here, @simple holds yet another reference. */ g_object_unref (self); priv = NM_CONNECTIVITY_GET_PRIVATE (self); if (SOUP_STATUS_IS_TRANSPORT_ERROR (msg->status_code)) { _LOGI ("check for uri '%s' failed with '%s'", uri, msg->reason_phrase); new_state = NM_CONNECTIVITY_LIMITED; goto done; } if (msg->status_code == 511) { _LOGD ("check for uri '%s' returned status '%d %s'; captive portal present.", uri, msg->status_code, msg->reason_phrase); new_state = NM_CONNECTIVITY_PORTAL; } else { /* Check headers; if we find the NM-specific one we're done */ nm_header = soup_message_headers_get_one (msg->response_headers, "X-NetworkManager-Status"); if (g_strcmp0 (nm_header, "online") == 0) { _LOGD ("check for uri '%s' with Status header successful.", uri); new_state = NM_CONNECTIVITY_FULL; } else if (msg->status_code == SOUP_STATUS_OK) { /* check response */ if (msg->response_body->data && g_str_has_prefix (msg->response_body->data, response)) { _LOGD ("check for uri '%s' successful.", uri); new_state = NM_CONNECTIVITY_FULL; } else { _LOGI ("check for uri '%s' did not match expected response '%s'; assuming captive portal.", uri, response); new_state = NM_CONNECTIVITY_PORTAL; } } else { _LOGI ("check for uri '%s' returned status '%d %s'; assuming captive portal.", uri, msg->status_code, msg->reason_phrase); new_state = NM_CONNECTIVITY_PORTAL; } } done: /* Only update the state, if the call was done from external, or if the periodic check * is still the one that called this async check. */ if (!cb_data->check_id_when_scheduled || cb_data->check_id_when_scheduled == priv->check_id) { /* Only update the state, if the URI and response parameters did not change * since invocation. * The interval does not matter for exernal calls, and for internal calls * we don't reach this line if the interval changed. */ if ( !g_strcmp0 (cb_data->uri, priv->uri) && !g_strcmp0 (cb_data->response, priv->response)) update_state (self, new_state); } g_simple_async_result_set_op_res_gssize (simple, new_state); g_simple_async_result_complete (simple); g_object_unref (simple); g_free (cb_data->uri); g_free (cb_data->response); g_slice_free (ConCheckCbData, cb_data); }