comp.lang.ada
 help / color / mirror / Atom feed
From: Samuel Tardieu <sam@rfc1149.net>
Subject: Re: Question about Streams and UDP sockets using GNAT.Sockets
Date: 18 Jul 2006 22:52:14 +0200
Date: 2006-07-18T22:54:01+02:00	[thread overview]
Message-ID: <87ejwid4ap.fsf@willow.rfc1149.net> (raw)
In-Reply-To: 1153220522.188057.200810@p79g2000cwp.googlegroups.com

>>>>> "Juanma" == lekktu@gmail com <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;



  parent reply	other threads:[~2006-07-18 20:52 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-07-18  9:41 Question about Streams and UDP sockets using GNAT.Sockets lekktu
2006-07-18  9:55 ` Dmitry A. Kazakov
2006-07-18 10:25   ` Alex R. Mosteo
2006-07-18 11:02     ` lekktu
2006-07-18 14:25       ` Alex R. Mosteo
2006-07-18 14:58         ` lekktu
2006-07-18 20:52       ` Samuel Tardieu [this message]
2006-07-18 20:58     ` Randy Brukardt
2006-07-19 10:36       ` Alex R. Mosteo
2006-07-19 18:50       ` Simon Wright
2006-07-18 19:07   ` Jeffrey R. Carter
2006-07-18 20:13     ` Dmitry A. Kazakov
2006-07-19  0:32       ` Jeffrey R. Carter
2006-07-19  8:12         ` Dmitry A. Kazakov
2006-07-19 18:59 ` Simon Wright
2006-07-19 20:54   ` lekktu
replies disabled

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