NetTalk Central

Author Topic: Getting length from a non Clarion NetSimple packet  (Read 15026 times)

Paul Blais

  • Newbie
  • *
  • Posts: 8
    • View Profile
Getting length from a non Clarion NetSimple packet
« on: August 23, 2011, 06:53:58 PM »
The WholePacket NetSimple Methodis great for NetTalk to NetTalk objects since it sends the length as a 4 byte header and can assemble the packets for you saving some code. (See the manual boundary example).

There is a standard way that many non NetTalk TCP applications use. They send the length as a 4 bytes header but it is no Clarion LONG you can distinguish.

The Bytes are sent as a Big Endian ULONG Integer. This is great until you find out Clarion is not Big Endian So we pretend it's a STRING instead and it's all better. You just need a little funny math.

Should Capesoft add an option  to WholePacket to use Big Endian longs they could make WholePacket work with other players as well as Clarion and it wouldn't break anyones code since we compute length as a Clarion LONG, but NetTalk could use Big Endian as an option and we wouldn't care since it is doing all the work.

The following functions will convert properly (we tested it today with a an Online service)

ToBigEndian            PROCEDURE  (LONG RawVal),STRING   

ByteArray               BYTE,DIM(4)
OutputVal               STRING(4),OVER(ByteArray)

  CODE

  ByteArray[1] = INT(RawVal/256^3)
  ByteArray[2] = INT((RawVal % 256^3) / 256^2)
  ByteArray[3] = INT((RawVal % 256^2) / 256)
  ByteArray[4] = INT(RawVal  % 256)
 
  RETURN OutputVal


And to go the other way:

FromBEndian          PROCEDURE  (STRING RawVal), LONG   

ByteArray               BYTE,DIM(4)
InputVal                STRING(4),OVER(ByteArray)
Result                  LONG

  CODE
  InputVal = RawVal

  Result = (INT(ByteArray[1]) * 256^3 + |
            INT(ByteArray[2]) * 256^2 + |
            INT(ByteArray[3]) * 256   + |
            INT(ByteArray[4]))   ! The packet includes the 4 byte header

  RETURN Result


Special thanks to Marc Walgren of MItten Software for being the tester that would have asked me to fix it if it didn't work..


Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11250
    • View Profile
Re: Getting length from a non Clarion NetSimple packet
« Reply #1 on: August 23, 2011, 10:39:38 PM »
Hi Paul,

Thanks for the suggestion. The following applies to 5.33 and later.

I've added a property,
WholePacketUseBigEndian
to the NetSimple Class.

This only has affect if the WholePacketUseLengthField property is set. If WholePacketUseBigEndian is set then the incoming length field, and the outgoing length field is added ot the header in "Big Endian" format - ie with the most significant byte first.

If this property is not set then the length field is assumed to be in Little Endian format (ie least significant byte first).

Cheers
Bruce

Paul Blais

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Getting length from a non Clarion NetSimple packet
« Reply #2 on: August 30, 2011, 04:07:08 AM »
Sounds exactly as required Bruce. Thanks for the feature!