comp.lang.ada
 help / color / mirror / Atom feed
From: shaunpatterson@gmail.com
Subject: Byte streams
Date: Thu, 02 Aug 2007 12:21:15 -0700
Date: 2007-08-02T12:21:15-07:00	[thread overview]
Message-ID: <1186082475.711489.104420@e9g2000prf.googlegroups.com> (raw)

You guys have been a great help lately.  Perhaps a few more of my
questions could be answered...

I've written a socket functions to send and receive data in C and then
created a spec in Ada so I could use those same functions:

-- C functions --

unsigned char *readBytes (const unsigned int numBytes);
void sendBytes (unsigned char *data, const unsigned int numBytes);


--- Ada side
type Byte is mod 256;
for Byte'Size use 8;

type ByteStream is array (Integer range <>) of Byte;


function readBytes (numBytes : Integer) return System.Address;
pragma import (C, readBytes, "readBytes");

procedure sendBytes (data : ByteStream; numBytes : Integer);
pragma import (C, sendBytes, "sendBytes");


-------------

Now I'm not sure if I'm doing the function prototypes correctly
in Ada.  I've actually gotten receiving to work correctly.

I'm having some trouble converting the byte stream to a structure
(I actually have this working... although I'm SURE there is a
better way) and then converting a structure to a byte stream


BYTESTREAM to STRUCTURE:

        type TestStructure is record
                value : Integer;
        end record;

-- data is read off the socket successfully
        function create (data : System.Address) is
                return TestStructure

                type IntegerPtr is access all Integer;
                function to_IntegerPtr is new Unchecked_Conversion
                      (source => System.Address, target =>
IntegerPtr);

                testData : IntegerPtr := to_IntegerPtr (data)

                intValue : Integer := msgData.all;
        begin
                return TestStructure'(value => intValue);
        end;


--- this works...and I can do all the conversions I need using an
unchecked_conversion

However, I cannot seem to figure out how to send data correctly.

consider:

type AnotherStructure is record
    a : Integer;
    b : Integer;
    c : Integer;
end Record;

How would I go about converting to a byte stream to send it
along to the socket sendBytes function?

Again, I'm not sure if I rewrote the spec correctly in Ada.


I have tried something like:

toByteStream is new Unchecked_Conversion
    (source => AnotherStructure, Target => ByteStream);

however ByteStream is unconstrained...

Any suggestions?

Thanks so much
--
Shaun




             reply	other threads:[~2007-08-02 19:21 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-08-02 19:21 shaunpatterson [this message]
2007-08-02 20:18 ` Byte streams Gautier
2007-08-02 20:27 ` tmoran
2007-08-02 21:44   ` Robert A Duff
2007-08-02 22:45     ` tmoran
2007-08-02 22:54       ` shaunpatterson
2007-08-03  5:34 ` Jeffrey R. Carter
2007-08-03 13:15   ` shaunpatterson
2007-08-10 20:22     ` 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