comp.lang.ada
 help / color / mirror / Atom feed
From: Matthew Heaney <matthewjheaney@earthlink.net>
Subject: Re: copy constructor for sockets
Date: Sun, 23 May 2004 21:04:48 GMT
Date: 2004-05-23T21:04:48+00:00	[thread overview]
Message-ID: <uwu322155.fsf@earthlink.net> (raw)
In-Reply-To: x7v3c5svfqc.fsf@smaug.pushface.org

Simon Wright <simon@pushface.org> writes:

> And it'll be a matter of application policy, far too specific to
> delegate to a low-level socket abstraction; you'd need to make your
> own higher-level abstraction.

That's pretty much what I do: write your own abstraction on top of the
low-level primitive.  On Windows, a socket is just a HANDLE, and on Unix
a socket is just a file descriptor (small positive integer).  I always
write some kind of Socket type, with the handle as its representation.

I have such an abstraction, that uses reference counting to keep track
of how many references there are to the descriptor:

package Sockets is

   type Socket is private;
...
private

   type Rep_Type is record
      fd        : Interfaces.C.int;
      Ref_Count : Natural;
   end record;

   type Rep_Access is access Rep_Type;

   type Socket is new Controlled with record
      Rep : Rep_Access := new Rep_Type'(fd => -1, Ref_Count => 1);
   end record;
...
end Sockets;

Now you have assignment of sockets:

   S1 : Socket := S;

and during Adjust you increment the refcount of the socket.  You
decrement the refcount during Finalize.

You have to make choices specific to your application re the behavior of
Close -- can you close the file descriptor if there's more than one
reference to the socket?

I also include an operation like Close, but instead of closing the
socket, it dis-associates this socket object from the underlying
primitive:

procedure Detach (S : in out Socket) is
begin

   if S.Rep.Refcount = 1 then
      Close (S);
      return;
   end if;

   S.Rep.Refcount := S.Rep.Refcount - 1;
   --detach from old socket

   S.Rep := new Rep_Type'(fd => -1, Ref_Count => 1);
   --attach to fresh socket

end Detach;

This allows me to pass a socket around.  For example, I can create the
socket in one place, then hand it off to another abstraction somewhere
else:

procedure Initialize
  (Session : in out Session_Type;
   RTSP_Connection : in out Socket) is
begin
   Session.RTSP_Socket := RTSP_Connection;  --inc refcount
   Detach (RTSP_Connection);                --dec refcount
end;

Here I don't want to close the socket.  I merely give it to the Session
object, who assumes ownership.  The session object then closes the
socket later, during the Session's Finalize operation.

(From the point of view of the RTSP_Connection socket object, the socket
is closed, but of course the original socket hasn't been closed, because
it's now owned by the Session object, which continues to use it.)

I also provide a selector operation to get the file descriptor:

  function fd (S : Socket) return C.int;

This allows me to put the socket (really, its descriptor) in a map, say,
so that I can look up a session object, given a file descriptor.  This
is useful when handling delivery of a real-time signal.  The payload
provides the file descriptor, but I need a way to associate that with
the Session object containing that socket, for which the I/O completed.

See the latest release of the AI-302 draft for similar ideas:

<http://www.ada-auth.org/cgi-bin/cvsweb.cgi/AIs/AI-20302.TXT?rev=1.5>

-Matt




  parent reply	other threads:[~2004-05-23 21:04 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-05-22 10:19 copy constructor for sockets Andrew Carroll
2004-05-22 11:55 ` Simon Wright
2004-05-22 18:39   ` tmoran
2004-05-23 21:04   ` Matthew Heaney [this message]
2004-05-24  7:13     ` Marius Amado Alves
2004-05-24  3:23       ` Matthew Heaney
2004-05-24  4:53         ` Simon Wright
2004-05-24  5:20           ` tmoran
2004-05-25  4:53             ` Simon Wright
2004-05-24 12:36           ` Matthew Heaney
2004-05-25 21:50           ` Robert I. Eachus
  -- strict thread matches above, loose matches on Subject: below --
2004-05-22 21:18 Andrew Carroll
2004-05-22 21:46 ` tmoran
2004-05-23 11:21   ` Simon Wright
2004-05-24 18:26     ` tmoran
2004-05-25  5:10       ` Simon Wright
2004-05-25  6:37         ` tmoran
2004-05-23  9:43 ` Mark Lorenzen
2004-05-23 11:27 ` Simon Wright
2004-05-24 11:28 Andrew Carroll
2004-05-25  5:29 ` Simon Wright
replies disabled

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