I am trying to write some network code but to do that I need to be able to construct my packets.
I wrote the code below to push into a message...
The type of the message as a byte
The length of the message content as a long
The message as a string
and read it back out again to make sure I didn't suffer from garbage in garbage out.
Code: Select all
Debug("===> Setting up message data...")
messageType.b = 1
message.s = "Test Message :)"
messageLength.l = Len(message)
Debug("===> Printing source message data...")
Debug("")
Debug("--- Source Message Begins ---")
Debug("Message type : " + Str(messageType))
Debug("Message length : " + Str(messageLength))
Debug("Message : " + message)
Debug("--- Source Message Ends---")
Debug("")
Debug("===> Allocating memory block (size = " + Str((5 + messageLength)) + ")...")
;Allocate 1 byte for the type (byte), 4 for the length (long) and the length of the message string
*packet = AllocateMemory(5 + messageLength)
Debug("===> Writing message to memory block...")
PokeB(*packet, messageType)
PokeL(*packet, messageLength)
PokeS(*packet, message)
Debug("===> Extracting message from memory block...")
msgType.b = PeekB(*packet)
msgLen.l = PeekL(*packet)
msg.s = PeekS(*packet)
Debug("===> Printing extracted message data...")
Debug("")
Debug("--- Extracted Message Begins ---")
Debug("Message type : " + Str(msgType))
Debug("Message length : " + Str(msgLen))
Debug("Message : " + msg)
Debug("--- Extracted Message Ends ---")
Debug("")
Message type : 1
Message length : 15
Message : Test Message :)
What I actually get is
Message type : 84
Message length : 1953719636
Message : Test Message :)
Here is the full output...
Code: Select all
===> Setting up message data...
===> Printing source message data...
--- Source Message Begins ---
Message type : 1
Message length : 15
Message : Test Message :)
--- Source Message Ends---
===> Allocating memory block (size = 20)...
===> Writing message to memory block...
===> Extracting message from memory block...
===> Printing extracted message data...
--- Extracted Message Begins ---
Message type : 84
Message length : 1953719636
Message : Test Message :)
--- Extracted Message Ends ---
Noodle