Re: reading from GInputStream



..small correction:

qw'get http://perl.org' => qw'GET http://perl.org'

On 08/31/2015 07:29 PM, vividsnow wrote:
Hi

Thanks for explanation.
Turns out, that there is straightforward solution:

use strict; use warnings;
use Gtk3; use HTTP::Soup; use Gtk3::WebKit;

my $session = Gtk3::WebKit::get_default_session;

$session->queue_message(HTTP::Soup::Message->new(qw'get http://perl.org'), sub {
    my (undef, $msg) = @_;
    print $msg->response_body->data;
    Gtk3->main_quit });

Gtk3->main;


On 08/31/2015 05:17 PM, Emmanuele Bassi wrote:
Hi;

On 31 August 2015 at 14:01, vividsnow <vividsnow gmail com> wrote:
Hello

I'm trying to read data from GInputStream:


use strict; use warnings;
use Glib::Object::Introspection;
Glib::Object::Introspection->setup(qw'basename Gio version 2.0 package Gio');

You could use Glib::IO, but it needs to be released on CPAN at some point.

use Gtk3; use HTTP::Soup; use Gtk3::WebKit;

my $session = Gtk3::WebKit::get_default_session;

$session->request('http://perl.org')->send_async(undef, sub {
    my ($req, $id) = @_;
    my $stream = $req->send_finish($id);
    # my $bytes = $stream->read_bytes(4096);
    # line above fails with: "GType GBytes (48024976) is not registered with gperl at.."
    my @buf = (undef)x(2**22); #4Mb max
    my ($status, $bytes) = $stream->read_all(\ buf);
    if ($status) {
        splice @buf, $bytes; # truncate
        printf "read %d bytes but defined %d\n",
               $bytes, scalar grep defined, @buf;
    }
    Gtk3->main_quit
});

Gtk3->main;

read_all() returns an opaque pointer, thus the introspection machinery
has no idea how to pack it safely into a Perl value.

The proper solution is to use the read_bytes() variant, but GBytes is
not part of the perl-Glib type machinery, as you have noticed.

.. but got empty buffer(@buf). Is it a bug or I do it wrong?

It's a missing feature — i.e. an enhancement.

Ciao,
 Emmanuele.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]