static gboolean do_write(VDAgentConnection *self, gboolean block)
{
    VDAgentConnectionPrivate *priv = vdagent_connection_get_instance_private(self);
    GOutputStream *out;
    GBytes *msg;
    gssize res;
    GError *err = NULL;

    msg = g_queue_peek_head(priv->write_queue);
    out = g_io_stream_get_output_stream(priv->io_stream);

    if (!msg) {
        return FALSE;
    }

    res = g_pollable_stream_write(out,
        g_bytes_get_data(msg, NULL) + priv->bytes_written,
        g_bytes_get_size(msg) - priv->bytes_written,
        block, priv->cancellable, &err);

    if (err) {
        if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK)) {
            g_error_free(err);
            return TRUE;
        } else if (g_error_matches(err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
            g_error_free(err);
            return FALSE;
        } else {
            priv->error_cb(self, err);
            return FALSE;
        }
    }

    priv->bytes_written += res;

    if (priv->bytes_written == g_bytes_get_size(msg)) {
        g_bytes_unref(g_queue_pop_head(priv->write_queue));
        priv->bytes_written = 0;
    }

    return !g_queue_is_empty(priv->write_queue);
}
static gboolean do_write(VDAgentConnection *self, gboolean block)
{
    VDAgentConnectionPrivate *priv = vdagent_connection_get_instance_private(self);
    GOutputStream *out;
    GBytes *msg;
    gssize res;
    GError *err = NULL;

    msg = g_queue_peek_head(priv->write_queue);
    out = g_io_stream_get_output_stream(priv->io_stream);

    if (!msg) {
        return FALSE;
    }

    res = g_pollable_stream_write(out,
        g_bytes_get_data(msg, NULL) + priv->bytes_written,
        g_bytes_get_size(msg) - priv->bytes_written,
        block, priv->cancellable, &err);

    if (err) {
        if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK)) {
            g_error_free(err);
            return TRUE;
        } else if (g_error_matches(err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
            g_error_free(err);
            return FALSE;
        } else {
            priv->error_cb(self, err);
            return FALSE;
        }
    }

    priv->bytes_written += res;

    if (priv->bytes_written == g_bytes_get_size(msg)) {
        g_bytes_unref(g_queue_pop_head(priv->write_queue));
        priv->bytes_written = 0;
    }

    return !g_queue_is_empty(priv->write_queue);
}