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-Thread: 103376,8bbf2dbc48e08e2f X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!t-online.de!feeder.news-service.com!news.astraweb.com!newsrouter-eu.astraweb.com!proxad.net!cleanfeed2-a.proxad.net!nnrp6-1.free.fr!not-for-mail Sender: sam@willow.rfc1149.net From: Samuel Tardieu Newsgroups: comp.lang.ada Subject: Re: Question about Streams and UDP sockets using GNAT.Sockets References: <1153215666.455584.201990@m73g2000cwd.googlegroups.com> <1fpj192j49rf4$.ma23qkoukku3.dlg@40tude.net> <4i3r3nF215adU1@individual.net> <1153220522.188057.200810@p79g2000cwp.googlegroups.com> Date: 18 Jul 2006 22:52:14 +0200 Message-ID: <87ejwid4ap.fsf@willow.rfc1149.net> User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.4 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Leafnode-NNTP-Posting-Host: 2001:6f8:37a:2::2 Organization: Guest of ProXad - France NNTP-Posting-Date: 18 Jul 2006 22:54:01 MEST NNTP-Posting-Host: 88.191.14.223 X-Trace: 1153256041 nnrp6-1.free.fr 18096 88.191.14.223:43359 X-Complaints-To: abuse@proxad.net Xref: g2news2.google.com comp.lang.ada:5788 Date: 2006-07-18T22:54:01+02:00 List-Id: >>>>> "Juanma" == lekktu@gmail com writes: Juanma> Is there any simpler (or more correct) way, or that's about Juanma> it? You can also make your own stream type with overloaded Write and Read procedures and a Send function that will send the data onto the socket. It will work with anything you want to write into your UDP packet, not only strings. The advantage of this is that if you use GLADE encoding library instead of GNAT one, you get a portable UDP packet format even accross different architecture. Here is an excerpt of some code I wrote to do this very thing. Then Sock'Write (Stream.Content (1 .. Stream.Last)) was used. with Ada.Streams; use Ada.Streams; private package Shix.Streams is -- Gather stream oriented data into one element to be able to -- transmit atomic packets. Max_Transfer_Size : constant := 1000; type Atomic_Stream_Type is new Root_Stream_Type with record Content : Stream_Element_Array (1 .. Max_Transfer_Size); Next : Stream_Element_Offset := 1; Last : Stream_Element_Offset := 0; end record; procedure Read (Stream : in out Atomic_Stream_Type; Item : out Stream_Element_Array; Last : out Stream_Element_Offset); procedure Write (Stream : in out Atomic_Stream_Type; Item : in Stream_Element_Array); end Shix.Streams; package body Shix.Streams is ---------- -- Read -- ---------- procedure Read (Stream : in out Atomic_Stream_Type; Item : out Stream_Element_Array; Last : out Stream_Element_Offset) is Count : constant Stream_Element_Count := Stream_Element_Count'Min (Item'Length, Stream.Last - Stream.Next + 1); begin Last := Item'First + Count - 1; Item (Item'First .. Last) := Stream.Content (Stream.Next .. Stream.Next + Count - 1); Stream.Next := Stream.Next + Count; end Read; ----------- -- Write -- ----------- procedure Write (Stream : in out Atomic_Stream_Type; Item : in Stream_Element_Array) is begin pragma Assert (Stream.Last + Item'Length <= Stream.Content'Last); Stream.Content (Stream.Last + 1 .. Stream.Last + Item'Length) := Item; Stream.Last := Stream.Last + Item'Length; end Write; end Shix.Streams;