comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Problems with Ada.Streams.Read (blocking)
Date: Fri, 8 Aug 2008 15:56:27 +0200
Date: 2008-08-08T15:56:27+02:00	[thread overview]
Message-ID: <1c7gwe0hi3ew5$.1c9zms1qkzjel.dlg@40tude.net> (raw)
In-Reply-To: g7hher$mtt$1@aioe.org

On Fri, 08 Aug 2008 15:24:43 +0200, Dennis Hoppe wrote:

> This approach works very well, but some ftp commands send a messages
> over several lines. I do not know in advance, how many lines I should
> read in. Subsequently, Ada.Streams.Read has to be called in a loop, 
> which will eventually block, again.
> 
> How can I query the stream, if new elements are ready to read?

You have to implement the application layer protocol above the stream.
Stream is a flow of stream elements, nothing more. If you have a
record-oriented layer above it, that segments the stream into records, then
you define a procedure to receive one record according to the definition
of. For instance, a sequence of stream elements until CR/LF:

procedure Read_Record
   (  Channel : in out Socket_Type;
       Buffer : in out Stream_Element_Array;
      Last : out Stream_Element_Offset
   )  is
   -- Reads the stream until CR/LF filling Buffer. Last is the index of
   -- of the last element of the packet. It is Buffer'First - 1 when
   -- the record body is empty.
   Index : Stream_Element_Offset;
   Has_CR : Boolean := False;
begin
   while Index in Buffer'Range loop
      Read (Channel, Buffer (Index));
      if Has_CR and then
         Storage_Element'Pos (Buffer (Index) = Character'Pos (LF)
      then
         Last := Index - 2;
         return;
      else
         Has_CR :=
            Storage_Element'Pos (Buffer (Index) = Character'Pos (CR);
      end if;
   end loop;
   raise Data_Overrun_Error; -- Too large packet
end Read_Record;

> How can I query the stream, if new elements are ready to read?

You need not, because record-oriented protocols let you know either the
record size in advance or else provide a way to determine the record end.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



  reply	other threads:[~2008-08-08 13:56 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-08-08 13:24 Problems with Ada.Streams.Read (blocking) Dennis Hoppe
2008-08-08 13:56 ` Dmitry A. Kazakov [this message]
2008-08-08 18:00   ` Dennis Hoppe
2008-08-08 18:51     ` Dmitry A. Kazakov
2008-08-08 19:37       ` Dennis Hoppe
2008-08-08 21:25       ` Maciej Sobczak
2008-08-09  7:30         ` Dmitry A. Kazakov
2008-08-08 14:48 ` Adam Beneschan
2008-08-08 18:08   ` Dennis Hoppe
2008-08-09  3:04 ` anon
2008-08-22 22:13 ` Paweł 'Nivertius' Płazieński
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox