From: mheaney@ni.net (Matthew Heaney)
Subject: How to implement I/O using Streams
Date: 1997/04/01
Date: 1997-04-01T00:00:00+00:00 [thread overview]
Message-ID: <mheaney-ya023680000104972123200001@news.ni.net> (raw)
The package Ada.Streams.Stream_IO is example of using a stream to do
(heterogeneous) file I/O. I want to do network I/O - using BSD Sockets,
say - and use a streams interface to do it.
How do I do this? The example in RM95 shows how a file object gives the
client a pointer to a stream (through which the I/O is performed), but is
silent about how the stream and file connect to each other. How does the
stream know about the file?
If I have a socket descriptor, how can I use a stream to do the I/O over
the socket?
How do I implement the Read and Write operations of my type that derives
from Root_Stream?
declare
sd : int; -- or perhaps I'll use a higher-level abstraction
The_Stream : Stream_Access;
The_Data : My_Record;
begin
<create socket and connect to server>
The_Stream := <get a stream connected to the socket descriptor sd>; -- ???
My_Record'Write (The_Stream, The_Data); -- more or less done for me
end;
Does anyone know how the implement that middle line? Has anyone
implemented a streams interface, ie used something other than
Ada.Streams.Stream_IO or Ada.Text_IO.Text_Streams?
Here's a trial attempt:
type My_Stream (sd : access int) is
new Root_Stream_Type with null record;
procedure Read (Stream ...);
-- reads from the socket descriptor discriminent
procedure Write (Stream ...);
-- writes to the socket descriptor discriminent
Suppose I built a higher-level abstraction. Would this work:
type Socket_File is limited private;
type Stream_Access is access all Root_Stream_Type'Class;
function Stream (File : Socket_File) return Stream_Access;
...
private
type My_Stream (File : access Socket_File) is
new Root_Stream_Type with null record;
type Socket_File is limited
record
sd : int;
Stream : aliased My_Stream (Socket_File'Access);
end record;
procedure Read (Stream : in out My_Stream...);
procedure Write (Stream : in out My_Stream ...);
end;
Well, is that the way? Is that how the language designers intended a
stream to be implemented?
Thank you much!
Matt
--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
<mailto:matthew_heaney@acm.org>
(818) 985-1271
next reply other threads:[~1997-04-01 0:00 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
1997-04-01 0:00 Matthew Heaney [this message]
1997-04-02 0:00 ` How to implement I/O using Streams Robert Dewar
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox