Switch to niknaks (#4)
* Dub - Added `niknaks` dependency * Marshall - `encodeBformat(byte[])` now uses niknaks for binary operations * Dub - Upgraded `niknaks` dependency to version `0.3.0` * Client - Removed bit-twiddling, use `niiknaks.bits` * Marshall - `encodeBformat(byte[])` now uses niknaks
This commit is contained in:
parent
6067155fca
commit
6ed46d051c
3
dub.json
3
dub.json
|
@ -4,6 +4,7 @@
|
||||||
],
|
],
|
||||||
"copyright": "Copyright © 2023, Tristan B. Kildaire",
|
"copyright": "Copyright © 2023, Tristan B. Kildaire",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"niknaks": ">=0.3.0",
|
||||||
"river": ">=0.3.6"
|
"river": ">=0.3.6"
|
||||||
},
|
},
|
||||||
"description": "A simple message format for automatically length-prefixing messages over any socket or stream",
|
"description": "A simple message format for automatically length-prefixing messages over any socket or stream",
|
||||||
|
@ -11,4 +12,4 @@
|
||||||
"license": "LGPL v3",
|
"license": "LGPL v3",
|
||||||
"name": "bformat",
|
"name": "bformat",
|
||||||
"targetType": "library"
|
"targetType": "library"
|
||||||
}
|
}
|
|
@ -6,6 +6,7 @@ module bformat.client;
|
||||||
import std.socket : Socket;
|
import std.socket : Socket;
|
||||||
import river.core;
|
import river.core;
|
||||||
import river.impls.sock : SockStream;
|
import river.impls.sock : SockStream;
|
||||||
|
import niknaks.bits : bytesToIntegral, order, Order;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Bformat client to encode and decode via a
|
* Bformat client to encode and decode via a
|
||||||
|
@ -76,27 +77,9 @@ public class BClient
|
||||||
/* Response message length */
|
/* Response message length */
|
||||||
uint messageLength;
|
uint messageLength;
|
||||||
|
|
||||||
/* Little endian version you simply read if off the bone (it's already in the correct order) */
|
/* Order the bytes into Little endian (only flips if host order doesn't match LE) */
|
||||||
version(LittleEndian)
|
messageLength = order(bytesToIntegral!(uint)(cast(ubyte[])messageLengthBytes), Order.LE);
|
||||||
{
|
|
||||||
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 */
|
/* Read the full message */
|
||||||
receiveBuffer.length = messageLength;
|
receiveBuffer.length = messageLength;
|
||||||
try
|
try
|
||||||
|
|
|
@ -3,6 +3,8 @@
|
||||||
*/
|
*/
|
||||||
module bformat.marshall;
|
module bformat.marshall;
|
||||||
|
|
||||||
|
import niknaks.bits : toBytes, bytesToIntegral, order, Order;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decodes the provided bformat message into the
|
* Decodes the provided bformat message into the
|
||||||
* message itself
|
* message itself
|
||||||
|
@ -22,25 +24,8 @@ public byte[] decodeMessage(byte[] bformatBytes)
|
||||||
/* Response message length */
|
/* Response message length */
|
||||||
uint messageLength;
|
uint messageLength;
|
||||||
|
|
||||||
/* Little endian version you simply read if off the bone (it's already in the correct order) */
|
/* Order the bytes into Little endian (only flips if host order doesn't match LE) */
|
||||||
version(LittleEndian)
|
messageLength = order(bytesToIntegral!(uint)(cast(ubyte[])messageLengthBytes), Order.LE);
|
||||||
{
|
|
||||||
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 */
|
/* Read the full message */
|
||||||
|
@ -62,27 +47,7 @@ public byte[] encodeBformat(byte[] message)
|
||||||
byte[] messageBuffer;
|
byte[] messageBuffer;
|
||||||
|
|
||||||
/* Encode the 4 byte message length header (little endian) */
|
/* Encode the 4 byte message length header (little endian) */
|
||||||
int payloadLength = cast(int)message.length;
|
messageBuffer ~= cast(byte[])toBytes(order(cast(int)message.length, Order.LE));
|
||||||
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 */
|
/* Add the message to the buffer */
|
||||||
messageBuffer ~= cast(byte[])message;
|
messageBuffer ~= cast(byte[])message;
|
||||||
|
|
Loading…
Reference in New Issue