mirror of
https://github.com/besterprotocol/bformat
synced 2023-12-13 21:30:24 +01:00
Added decodeMessage(byte[])
which can decoode a bformat-encoded message and return the message payload
This commit is contained in:
parent
82f9509e78
commit
80d96c20a5
1 changed files with 38 additions and 0 deletions
|
@ -2,6 +2,44 @@ module bmessage;
|
|||
|
||||
import std.socket : Socket, SocketFlags, MSG_WAITALL;
|
||||
|
||||
public byte[] decodeMessage(byte[] bformatBytes)
|
||||
{
|
||||
/* Construct a buffer to receive into */
|
||||
byte[] receiveBuffer;
|
||||
|
||||
/* Get the length of the message */
|
||||
byte[4] messageLengthBytes = bformatBytes[0..4];
|
||||
|
||||
/* Response message length */
|
||||
uint messageLength;
|
||||
|
||||
/* Little endian version you simply read if off the bone (it's already in the correct order) */
|
||||
version(LittleEndian)
|
||||
{
|
||||
messageLength = *cast(int*)messageLengthBytes.ptr;
|
||||
}
|
||||
|
||||
/* Big endian requires we byte-sapped the little-endian encoded number */
|
||||
version(BigEndian)
|
||||
{
|
||||
byte[] swappedLength;
|
||||
swappedLength.length = 4;
|
||||
|
||||
swappedLength[0] = messageLengthBytes[3];
|
||||
swappedLength[1] = messageLengthBytes[2];
|
||||
swappedLength[2] = messageLengthBytes[1];
|
||||
swappedLength[3] = messageLengthBytes[0];
|
||||
|
||||
messageLength = *cast(int*)swappedLength.ptr;
|
||||
}
|
||||
|
||||
|
||||
/* Read the full message */
|
||||
receiveBuffer = bformatBytes[4..4+messageLength];
|
||||
|
||||
return receiveBuffer;
|
||||
}
|
||||
|
||||
public bool receiveMessage(Socket originator, ref byte[] receiveMessage)
|
||||
{
|
||||
/* Construct a buffer to receive into */
|
||||
|
|
Loading…
Reference in a new issue