Byte swap when the numbers are encoded as Big Endian on BE platforms, such that they are in little endian (the protocol's spec)

This commit is contained in:
Tristan B. Kildaire 2020-12-04 17:28:29 +02:00
parent 3c84a5b0ca
commit beb635c403
1 changed files with 19 additions and 4 deletions

View File

@ -138,10 +138,25 @@ public bool sendMessage(Socket recipient, byte[] message)
/* Encode the 4 byte message length header (little endian) */
int payloadLength = cast(int)message.length;
byte* lengthBytes = cast(byte*)&payloadLength;
messageBuffer ~= *(lengthBytes+0);
messageBuffer ~= *(lengthBytes+1);
messageBuffer ~= *(lengthBytes+2);
messageBuffer ~= *(lengthBytes+3);
/* On little endian simply get the bytes as is (it would be encoded as little endian) */
version(LittleEndian)
{
messageBuffer ~= *(lengthBytes+0);
messageBuffer ~= *(lengthBytes+1);
messageBuffer ~= *(lengthBytes+2);
messageBuffer ~= *(lengthBytes+3);
}
/* On Big Endian you must swap the big-endian-encoded number to be in little endian ordering */
version(BigEndian)
{
messageBuffer ~= *(lengthBytes+3);
messageBuffer ~= *(lengthBytes+2);
messageBuffer ~= *(lengthBytes+1);
messageBuffer ~= *(lengthBytes+0);
}
/* Add the message to the buffer */
messageBuffer ~= cast(byte[])message;