From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,d0728b52f51f685e X-Google-Attributes: gid103376,public Path: controlnews3.google.com!news1.google.com!news.glorb.com!prodigy.com!atl-c02.usenetserver.com!c03.atl99!c01.usenetserver.com!news.usenetserver.com!elnk-atl-nf1!newsfeed.earthlink.net!stamper.news.atl.earthlink.net!newsread2.news.atl.earthlink.net.POSTED!14bb18d8!not-for-mail Sender: mheaney@MHEANEYX200 Newsgroups: comp.lang.ada Subject: Re: copy constructor for sockets References: From: Matthew Heaney Message-ID: User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Date: Sun, 23 May 2004 21:04:48 GMT NNTP-Posting-Host: 64.185.133.124 X-Complaints-To: abuse@earthlink.net X-Trace: newsread2.news.atl.earthlink.net 1085346288 64.185.133.124 (Sun, 23 May 2004 14:04:48 PDT) NNTP-Posting-Date: Sun, 23 May 2004 14:04:48 PDT Organization: EarthLink Inc. -- http://www.EarthLink.net Xref: controlnews3.google.com comp.lang.ada:791 Date: 2004-05-23T21:04:48+00:00 List-Id: Simon Wright 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: -Matt