mirror of
https://github.com/besterprotocol/bformat
synced 2023-12-13 21:30:24 +01:00
- Restructured whole package
This commit is contained in:
parent
80d96c20a5
commit
7245572a97
3 changed files with 103 additions and 73 deletions
85
source/bformat/marshall.d
Normal file
85
source/bformat/marshall.d
Normal file
|
@ -0,0 +1,85 @@
|
||||||
|
module bformat.marshall;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* bformatBytes =
|
||||||
|
* Returns:
|
||||||
|
*/
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* message =
|
||||||
|
* Returns:
|
||||||
|
*/
|
||||||
|
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;
|
||||||
|
}
|
3
source/bformat/package.d
Normal file
3
source/bformat/package.d
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
module bformat;
|
||||||
|
|
||||||
|
public import bformat.sockets : sendMessage, receiveMessage;
|
|
@ -1,45 +1,14 @@
|
||||||
module bmessage;
|
module bformat.sockets;
|
||||||
|
|
||||||
import std.socket : Socket, SocketFlags, MSG_WAITALL;
|
import std.socket : Socket, SocketFlags, MSG_WAITALL;
|
||||||
|
|
||||||
public byte[] decodeMessage(byte[] bformatBytes)
|
/**
|
||||||
{
|
*
|
||||||
/* Construct a buffer to receive into */
|
* Params:
|
||||||
byte[] receiveBuffer;
|
* originator =
|
||||||
|
* receiveMessage =
|
||||||
/* Get the length of the message */
|
* Returns:
|
||||||
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)
|
public bool receiveMessage(Socket originator, ref byte[] receiveMessage)
|
||||||
{
|
{
|
||||||
/* Construct a buffer to receive into */
|
/* Construct a buffer to receive into */
|
||||||
|
@ -106,40 +75,13 @@ public bool receiveMessage(Socket originator, ref byte[] receiveMessage)
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] encodeBformat(byte[] message)
|
/**
|
||||||
{
|
*
|
||||||
/* The message buffer */
|
* Params:
|
||||||
byte[] messageBuffer;
|
* recipient =
|
||||||
|
* message =
|
||||||
/* Encode the 4 byte message length header (little endian) */
|
* Returns:
|
||||||
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)
|
public bool sendMessage(Socket recipient, byte[] message)
|
||||||
{
|
{
|
||||||
/* The message buffer */
|
/* The message buffer */
|
Loading…
Reference in a new issue