示例#1
0
static void
connect_cb(GObject *source,
           GAsyncResult *res,
           gpointer user_data)
{
    GSocketConnection *socket_conn;
    PnNode *conn;
    GError *error = NULL;

    conn = PN_NODE(user_data);
    socket_conn = g_socket_client_connect_to_host_finish(G_SOCKET_CLIENT(source), res, &error);

    g_object_unref(source);

    if (error) {
        g_error_free(error);
        return;
    }

    g_object_ref(conn);

    if (socket_conn) {
        GSocket *socket;
        GInputStream *input;

        conn->socket_conn = socket_conn;
        socket = g_socket_connection_get_socket(socket_conn);

        conn->status = PN_NODE_STATUS_OPEN;

        input = g_io_stream_get_input_stream (G_IO_STREAM (conn->socket_conn));
        g_object_ref (conn);
        g_input_stream_read_async (input, conn->input_buffer, PN_BUF_LEN,
                                   G_PRIORITY_DEFAULT, NULL,
                                   read_cb, conn);
    }
    else {
        conn->error = g_error_new_literal(PN_NODE_ERROR, PN_NODE_ERROR_OPEN,
                                          "Unable to connect");

        pn_node_error(conn);
    }

    {
        PnNodeClass *class;
        class = g_type_class_peek(PN_NODE_TYPE);
        g_signal_emit(G_OBJECT(conn), class->open_sig, 0, conn);
    }

    g_object_unref(conn);
}
示例#2
0
static void
connect_cb (gpointer data,
            gint source,
            const gchar *error_message)
{
    PnNode *conn;

    pn_log ("begin");

    conn = PN_NODE (data);
    conn->connect_data = NULL;

    g_object_ref (conn);

    if (source >= 0)
    {
        GIOChannel *channel;

        conn->stream = pn_stream_new (source);
        channel = conn->stream->channel;

        PN_NODE_GET_CLASS (conn)->channel_setup (conn, channel);

        conn->status = PN_NODE_STATUS_OPEN;

        pn_info ("connected: conn=%p,channel=%p", conn, channel);
        conn->read_watch = g_io_add_watch (channel, G_IO_IN, read_cb, conn);
#if 0
        g_io_add_watch (channel, G_IO_ERR | G_IO_HUP | G_IO_NVAL, close_cb, conn);
#endif
    }
    else
    {
        /* pn_error ("connection error: conn=%p,msg=[%s]", conn, error_message); */
        conn->error = g_error_new_literal (PN_NODE_ERROR, PN_NODE_ERROR_OPEN,
                                           error_message ? error_message : "Unable to connect");

        pn_node_error (conn);
    }

    {
        PnNodeClass *class;
        class = g_type_class_peek (PN_NODE_TYPE);
        g_signal_emit (G_OBJECT (conn), class->open_sig, 0, conn);
    }

    g_object_unref (conn);

    pn_log ("end");
}
示例#3
0
static gboolean
read_cb (GIOChannel *source,
         GIOCondition condition,
         gpointer data)
{
    PnNode *conn;
    gchar buf[PN_BUF_LEN + 1];
    gsize bytes_read;

    pn_log ("begin");

    conn = PN_NODE (data);

    pn_debug ("conn=%p,name=%s", conn, conn->name);

    g_object_ref (conn);

    {
        GIOStatus status = G_IO_STATUS_NORMAL;

        status = pn_node_read (conn, buf, PN_BUF_LEN, &bytes_read, NULL);

        if (status == G_IO_STATUS_AGAIN)
        {
            g_object_unref (conn);
            return TRUE;
        }

        if (status == G_IO_STATUS_EOF)
        {
            conn->error = g_error_new (PN_NODE_ERROR, PN_NODE_ERROR_OPEN, "End of stream");
        }

        if (conn->error)
        {
            pn_node_error (conn);
            g_object_unref (conn);
            return FALSE;
        }
    }

    pn_node_parse (conn, buf, bytes_read);

    g_object_unref (conn);

    pn_log ("end");

    return TRUE;
}
示例#4
0
static void
connect_cb(GObject *source,
           GAsyncResult *res,
           gpointer user_data)
{
    GSocketConnection *socket_conn;
    PnNode *conn;

    conn = PN_NODE(user_data);
    socket_conn = g_socket_client_connect_to_host_finish(G_SOCKET_CLIENT(source), res, NULL);

    g_object_unref(source);

    g_object_ref(conn);

    if (socket_conn) {
        GIOChannel *channel;
        GSocket *socket;

        conn->socket_conn = socket_conn;
        socket = g_socket_connection_get_socket(socket_conn);
        conn->stream = pn_stream_new(g_socket_get_fd(socket));
        channel = conn->stream->channel;

        PN_NODE_GET_CLASS (conn)->channel_setup (conn, channel);

        conn->status = PN_NODE_STATUS_OPEN;

        pn_info("connected: conn=%p,channel=%p", conn, channel);
        conn->read_watch = g_io_add_watch(channel, G_IO_IN, read_cb, conn);
#if 0
        g_io_add_watch (channel, G_IO_ERR | G_IO_HUP | G_IO_NVAL, close_cb, conn);
#endif
    }
    else {
        conn->error = g_error_new_literal(PN_NODE_ERROR, PN_NODE_ERROR_OPEN,
                                          "Unable to connect");

        pn_node_error(conn);
    }

    {
        PnNodeClass *class;
        class = g_type_class_peek(PN_NODE_TYPE);
        g_signal_emit(G_OBJECT(conn), class->open_sig, 0, conn);
    }

    g_object_unref(conn);
}
示例#5
0
static void
read_cb (GObject *source,
         GAsyncResult *result,
         gpointer user_data)
{
    PnNode *conn;
    gssize size;
    GError *error = NULL;

    conn = PN_NODE(user_data);
    size = g_input_stream_read_finish (G_INPUT_STREAM (source),
                                       result, &error);

    conn = PN_NODE(user_data);

    if (G_UNLIKELY (size == 0))
        error = g_error_new_literal(PN_NODE_ERROR, PN_NODE_ERROR_OPEN,
                                    "End of stream");

    if (error)
        goto nok;

    pn_node_parse (conn, (char *) conn->input_buffer, size);

    if (conn->status == PN_NODE_STATUS_OPEN)
        g_input_stream_read_async (G_INPUT_STREAM (source), conn->input_buffer, PN_BUF_LEN,
                                   G_PRIORITY_DEFAULT, NULL, read_cb, conn);
    else
        g_object_unref (conn);

    return;

nok:
    conn->error = error;
    pn_node_error (conn);
    g_object_unref (conn);
}
示例#6
0
static gboolean
read_cb (GIOChannel *source,
         GIOCondition condition,
         gpointer data)
{
    PnNode *conn;
    gchar buf[PN_BUF_LEN + 1];
    gsize bytes_read;

    pn_log ("begin");

    conn = PN_NODE (data);

    pn_debug ("conn=%p,source=%p", conn, source);

    g_object_ref (conn);

    {
        GIOStatus status = G_IO_STATUS_NORMAL;

        status = pn_node_read (conn, buf, PN_BUF_LEN, &bytes_read, &conn->error);

        if (status == G_IO_STATUS_AGAIN)
        {
            g_object_unref (conn);
            return TRUE;
        }

        if (conn->error)
        {
            pn_node_error (conn);
            g_object_unref (conn);
            return FALSE;
        }

        if (status != G_IO_STATUS_NORMAL)
        {
            pn_warning ("not normal, status=%d", status);
            g_object_unref (conn);
            return TRUE;
        }
    }

    if (!conn->error)
    {
        PnHttpServer *http_conn;

        http_conn = PN_HTTP_SERVER (conn);

        if (http_conn->cur)
        {
            /* make sure the server is not sending the same buffer again */
            /** @todo find out why this happens */
            if (!(http_conn->old_buffer &&
                  strncmp (buf, http_conn->old_buffer, bytes_read) == 0))
            {
                pn_node_parse (http_conn->cur, buf, bytes_read);

                g_free (http_conn->old_buffer);
                http_conn->old_buffer = g_strndup (buf, bytes_read);
            }
        }

        if (conn->error)
        {
            pn_node_error (conn);
            g_object_unref (conn);
            return FALSE;
        }
    }

    g_object_unref (conn);

    pn_log ("end");

    return TRUE;
}