Re: reading from GInputStream


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

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('')->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;


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.


[ ] ebassi [ gmail com]

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