Re: Data available for reading in a GIOChannel



Hi,

> you never know how much readable data is available until you read it, you are only ever guaranteed to have one byte of data available for reading anyway.

In my opinion, the channel should _always_ know how much data is available, how can it tell that there is nothing there?... Plus, it can't not know the amount of data it _has_ actually recieved and is buffered ready for me...

Java InputStreams for example have the available() method: http://java.sun.com/javase/6/docs/api/java/io/InputStream.html#available()

> [...] I read the data in 1024 byte chunks [...]
This sounds like a good workaround, which will work well in a watch callback; I'll use that

Thanks,
Jonathan

On 5/28/07, Armin Burgmeier <armin arbur net> wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Tristan Van Berkom wrote:
> On Sun, 2007-05-27 at 16:57 +0200, Jonathan Winterflood wrote:
>> Hi,
>>
>> I need to read a large amount of data from a GIOChannel (200K, over the internet).
>>
>> So far, I use the gnet library to create the
>> socket, and then I use the GIOChannel to integrate the read/writing into the program's loop (a GTK application)
>>
>> I use g_io_add_watch(_channel, G_IO_IN, &(_imageDataReadyForReading),
>> this); to register the callback for data reading.
>>
>> How can I determine the number of bytes available for reading, so as
>> not to block on reading the data?
>
> I dont know if gnet provides anything for this, what I typically
> do is:
>
>    while (select (fds, readable condition)) {
>       read (a single byte of data);
>    }

I don't think this is very efficient because it calls select and read
for a every single byte you want to receive. I normally put the socket
in non-blocking mode and as soon as glib indicates readability I read
the data in 1024 byte chunks until I get EGAIN from recv() (or,
G_IO_STATUS_AGAIN from g_io_channel_read_chars(), respectively).

Greetings,
Armin
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGWtx9hOtxKlDYm6cRAg/mAKCFWyGdpMern/YeJ8TrDP1VtJ3j3QCfVEYY
eyd41u6r79MbkumRMGo/WOc=
=mZjC
-----END PGP SIGNATURE-----



--
<Morpheus> linux, c'est une question de VI ou de MORE

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