* Hints for networking @ 2003-01-31 16:37 Jano 2003-01-31 18:40 ` tmoran ` (2 more replies) 0 siblings, 3 replies; 6+ messages in thread From: Jano @ 2003-01-31 16:37 UTC (permalink / raw) Hello everybody. I'm about to program a networking package for the first time in Ada and am somewhat undecided the way to go. I'd like to hear some pointers for examples or hints. The protocol has some fixed-size packets and others of unknown size (but with known headers which contains the full size). I'm wondering what representation clauses could help me, if any. Also, I've my doubts about streams, because in the past I've tried them to write files and in Gnat I couldn't manage to output exact sized types except uncheckedconverting them into byte arrays. For example an integer subtype with representation clauses for 16 bits was always dumped as its base type, i.e. 32 bits. Frankly, the RM is too dense for me so some beginner documentation would be very useful. I've read some tutorials and books from adapower lately but can't find something more on the subject. Clues welcomed. TIA, -- ------------------------- Jano 402450[at]cepsz.unizar.es ------------------------- ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Hints for networking 2003-01-31 16:37 Hints for networking Jano @ 2003-01-31 18:40 ` tmoran 2003-01-31 21:21 ` David C. Hoos 2003-02-01 18:32 ` Alfred Hilscher 2 siblings, 0 replies; 6+ messages in thread From: tmoran @ 2003-01-31 18:40 UTC (permalink / raw) > Also, I've my doubts about streams, because in the past I've tried them > to write files and in Gnat I couldn't manage to output exact sized types > except uncheckedconverting Streams can be quite convenient, but they are like programming without rep clauses. If the representation doesn't matter, fine. If it does, you have to write your own 'read/'write routines to and from Stream_Element_Array. But you probably want your IO to be in terms of objects at a higher level than Integer or Boolean, etc, so you would want to write your own Some_Record 'Write routines anyway. At the low level, your code will contain things like type Some_Record is record ... subtype External_Form_Some_Record is Ada.Streams.Stream_Element_Array(1 .. 6); function External_Some_Record is new Ada.Unchecked_Conversion (Source=>Some_Record, Target=>External_Form_Some_Record); but at higher levels writing a complex object just becomes a series of calls to 'Write on lower level subrecords, and, if there's nothing fancy going on, you can let the compiler create the series of subrecord 'Writes. ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Hints for networking 2003-01-31 16:37 Hints for networking Jano 2003-01-31 18:40 ` tmoran @ 2003-01-31 21:21 ` David C. Hoos 2003-02-01 19:29 ` Jano 2003-02-01 18:32 ` Alfred Hilscher 2 siblings, 1 reply; 6+ messages in thread From: David C. Hoos @ 2003-01-31 21:21 UTC (permalink / raw) To: comp.lang.ada mail to news gateway Hi Jano, I have written many many network protocol representations using Ada95 streams. The elegance of Ada Streams, and the simplicity of their use once you have made the effort to properly define your types and their stream attributes really makes the effort worthwhile. Here are some hints and rules needed to do this successfully: These rules are based on the presumption that there is a specific network representation required by your protocol. 1, This rule number 1 is very important. NEVER base any value written to a stream on the standard Ada type Integer. You should always define project-specific types that do not depend on implementations that are subject to variation between compilers or platform types. For example, you can base integer types on those available in the package Interfaces -- Integer_16, Unsigned_16, etc. That's what the Interface package is for -- i.e. to permit writing portable code for interfaces. 2. You need to write stream attributes for multi-octet numbers that respect the bendiness of your platform. Even when you've been told that "this only has to run on Sparc," sooner or later your code will also need to run on Intel, so I've always taken the trouble to deal with bendiness. 3. All objects written to a stream must have a 'Size that is an integer multiple of 8. So, if you have an octet with two four-bit fields, I make them into a record type, and write the stream attributes for that record. I've done things like a record that has a 31-bit field, and a 1-bit field. Happy streaming! David Hoos ----- Original Message ----- From: "Jano" <402450@cepsz.unizar.es> Newsgroups: comp.lang.ada To: <comp.lang.ada@ada.eu.org> Sent: Friday, January 31, 2003 10:37 AM Subject: Hints for networking > Hello everybody. > > I'm about to program a networking package for the first time in Ada and > am somewhat undecided the way to go. I'd like to hear some pointers for > examples or hints. > > The protocol has some fixed-size packets and others of unknown size (but > with known headers which contains the full size). I'm wondering what > representation clauses could help me, if any. Also, I've my doubts about > streams, because in the past I've tried them to write files and in Gnat > I couldn't manage to output exact sized types except uncheckedconverting > them into byte arrays. For example an integer subtype with > representation clauses for 16 bits was always dumped as its base type, > i.e. 32 bits. Frankly, the RM is too dense for me so some beginner > documentation would be very useful. > > I've read some tutorials and books from adapower lately but can't find > something more on the subject. Clues welcomed. > > TIA, > > -- > ------------------------- > Jano > 402450[at]cepsz.unizar.es > ------------------------- > _______________________________________________ > comp.lang.ada mailing list > comp.lang.ada@ada.eu.org > http://ada.eu.org/mailman/listinfo/comp.lang.ada > ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Hints for networking 2003-01-31 21:21 ` David C. Hoos @ 2003-02-01 19:29 ` Jano 2003-02-02 7:43 ` Simon Wright 0 siblings, 1 reply; 6+ messages in thread From: Jano @ 2003-02-01 19:29 UTC (permalink / raw) En el mensaje <mailman.4.1044048101.3911.comp.lang.ada@ada.eu.org>, david.c.hoos.sr@ada95.com dice... Hello David, > 2. You need to write stream attributes for multi-octet numbers > that respect the bendiness of your platform. Even when > you've been told that "this only has to run on Sparc," sooner > or later your code will also need to run on Intel, so I've > always taken the trouble to deal with bendiness. I'm going to work on Intel but the protocol is using big endian fields, so I was already concerned about that. The only related thing I've found is the 'bit_order and system.default_bit_order, but I suspect that's related to storage representation and not to real byte ordering. Must I detect machine endingness manually? Are there helping packages for networking in the wild? > 3. All objects written to a stream must have a 'Size that is an > integer multiple of 8. So, if you have an octet with two four-bit > fields, I make them into a record type, and write the stream > attributes for that record. I've done things like a record that > has a 31-bit field, and a 1-bit field. I think that will not be a problem. I don't foresee streams of bits, all I'll need is 8-bit multiple types. So to conclude: I must define my "atomic" types and implement their own stream input/output attributes. Then construct the more complex packets using them. > Happy streaming! Thanks to all who answered! -- ------------------------- Jano 402450[at]cepsz.unizar.es ------------------------- ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Hints for networking 2003-02-01 19:29 ` Jano @ 2003-02-02 7:43 ` Simon Wright 0 siblings, 0 replies; 6+ messages in thread From: Simon Wright @ 2003-02-02 7:43 UTC (permalink / raw) Jano <402450@cepsz.unizar.es> writes: > Must I detect machine endingness manually? Are there helping > packages for networking in the wild? I think that there are theoretical circumstances in which it is not OK to check whether system.default_bit_order is low_order_first (little-endian) or high_order_first (big-endian), but that has worked for me on Sparc, Alpha, PowerPC, Intel (GNAT in all cases). ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Hints for networking 2003-01-31 16:37 Hints for networking Jano 2003-01-31 18:40 ` tmoran 2003-01-31 21:21 ` David C. Hoos @ 2003-02-01 18:32 ` Alfred Hilscher 2 siblings, 0 replies; 6+ messages in thread From: Alfred Hilscher @ 2003-02-01 18:32 UTC (permalink / raw) Jano schrieb: > > Hello everybody. > > I'm about to program a networking package for the first time in Ada and > am somewhat undecided the way to go. I'd like to hear some pointers for > examples or hints. Have a look at http://alfred.hilscher.bei.t-online.de/sophi.zip ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2003-02-02 7:43 UTC | newest] Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2003-01-31 16:37 Hints for networking Jano 2003-01-31 18:40 ` tmoran 2003-01-31 21:21 ` David C. Hoos 2003-02-01 19:29 ` Jano 2003-02-02 7:43 ` Simon Wright 2003-02-01 18:32 ` Alfred Hilscher
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox