[Gambas-user] gb3 RC1: using structures to replace the loss of Mk$ functions

Kevin Fishburne kevinfishburne at ...1887...
Mon Apr 4 06:26:24 CEST 2011


On 04/03/2011 08:46 PM, Benoît Minisini wrote:
> Finally I come with a solution. Well I hope.
>
> I added three new methods to the Stream class (and so to its child classes:
> File, Socket, UdpSocket...) in revision #3730.
>
> Stream.Begin(), Stream.Send() and Stream.Drop().
>
> All WRITE instructions between a Stream.Begin() and a Stream.Send() will be
> merged, and send in one shot when Stream.Send() is called.
>
> Stream.Drop() just cancels that process.
>
> That way, to forge and send a packet to your UdpSocket, instead of doing
> something like:
>
> 	Write #MyUdpSocket, Chr$(42)&  MkShort$(A)&  MkInt$(B)&  MkFloat$(C)
> 	&  SomeString ...
>
> You will do:
>
> 	MyUdpSocket.Begin
> 	Write #MyUdpSocket, 42 As Byte
> 	Write #MyUdpSocket, A As Short
> 	Write #MyUdpSocket, B As Integer
> 	Write #MyUdpSocket, C As Float
> 	Write #MyUdpSocket, SomeString, Len(SomeString)
> 	MyUdpSocket.Send
>
> To read the packet, just use the Read instruction. The UdpSocket already
> buffers the received packet internally.
>
> All that should allow you to do the same thing as before with Mk*() functions,
> with more lines of code, but with endianness support, without adding too much
> code to the interpreter.
>
> Please tell me that you can do something with that, I have a release candidate
> to prepare. :-)

Excellent, that's an elegant solution. Thanks! Is it possible to read 
one or more values from the beginning of the packet normally, then read 
the remaining data into a single string? Something like this:

Public Sub UDP_Read()

   Dim id As Byte
   Dim type As Byte
   Dim data As String

   Read #udp, id As Byte
   Read #udp, type As Byte
   Read #udp, data, Lof(udp) - 2

End

If this works (and I don't see why it wouldn't), then everything is 
fixed. I can have a transaction array that can store any transaction 
type by saving the "payload" as a string and decoding it later with the 
*@() functions.

-- 
Kevin Fishburne
Eight Virtues
www: http://sales.eightvirtues.com
e-mail: sales at ...1887...
phone: (770) 853-6271





More information about the User mailing list