Added `encodeBformat(byte[])` wich allows one to simply encode a message into the bformat format
This commit is contained in:
parent
b3b3ee9d49
commit
82f9509e78
|
@ -68,6 +68,40 @@ public bool receiveMessage(Socket originator, ref byte[] receiveMessage)
|
|||
return status;
|
||||
}
|
||||
|
||||
public byte[] encodeBformat(byte[] message)
|
||||
{
|
||||
/* The message buffer */
|
||||
byte[] messageBuffer;
|
||||
|
||||
/* Encode the 4 byte message length header (little endian) */
|
||||
int payloadLength = cast(int)message.length;
|
||||
byte* lengthBytes = cast(byte*)&payloadLength;
|
||||
|
||||
/* 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;
|
||||
|
||||
return messageBuffer;
|
||||
}
|
||||
|
||||
public bool sendMessage(Socket recipient, byte[] message)
|
||||
{
|
||||
/* The message buffer */
|
||||
|
|
Loading…
Reference in New Issue