Re: Receiving multiple lines with g_io_channel_read_line



Andrew Gatt wrote:
I've had some sucess using g_io_channel_read_line to
receive terminated lines from a fifo formed with a
server app i wrote. However i need to receive unknown
amounts of bytes with line terminators until a final
escape character is received, then act on this packet.
Is this possible to do in one callback, or should i be
using multiple callbacks and a global gchar to buffer
until the escape character - or would it be better to
somehow use g_io_channel_read? I've seen a previous
post about receiving the characters one at a time,
which i could use - but i couldn't get it to work
properly and didn't think this would be too efficient.

You should hook up a callback with g_io_add_watch() or something
similar and in this callback read every single byte of data that
has come to you.  Then parse the data looking for the terminating
character and, whenever it is seen, call your catch_package()
function.  You will need to keep some buffer (better dynamically
(re)allocated) to keep any unfinished parts of packages and take
care to properly concatenate incoming chunks.

Note that reading everything available is important, otherwise
your callback will be called constantly because the channel will
be in more-data-available mode.

Paul




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