mirror of
http://deavmi.assigned.network/git/deavmi/tristanable
synced 2025-02-24 00:36:50 +00:00
Added protocol encoder decoder library
This commit is contained in:
parent
04cc444d32
commit
83b863a9d8
@ -2,6 +2,6 @@
|
|||||||
"fileVersion": 1,
|
"fileVersion": 1,
|
||||||
"versions": {
|
"versions": {
|
||||||
"bformat": "1.0.8",
|
"bformat": "1.0.8",
|
||||||
"tristanable": "0.0.10"
|
"tristanable": "0.0.11"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
44
source/encoding.d
Normal file
44
source/encoding.d
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
public final class DataMessage
|
||||||
|
{
|
||||||
|
|
||||||
|
public ulong tag;
|
||||||
|
public byte[] data;
|
||||||
|
|
||||||
|
public static DataMessage decode(byte[] bytes)
|
||||||
|
{
|
||||||
|
/* Fetch the `tag` */
|
||||||
|
ulong receivedTag = *(cast(ulong*)bytes.ptr);
|
||||||
|
|
||||||
|
/* Fetch the `data` */
|
||||||
|
byte[] receivedData = receivedMessage = bytes[8..bytes.length];
|
||||||
|
|
||||||
|
return new DataMessage(receivedTag, receivedData);
|
||||||
|
}
|
||||||
|
|
||||||
|
this(ulong tag, byte[] data)
|
||||||
|
{
|
||||||
|
this.tag = tag;
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] encode()
|
||||||
|
{
|
||||||
|
/* Construct the message array */
|
||||||
|
byte[] messageData;
|
||||||
|
|
||||||
|
/* Add the `tag` bytes */
|
||||||
|
messageData ~= *(cast(byte*)&tag);
|
||||||
|
messageData ~= *(cast(byte*)&tag+1);
|
||||||
|
messageData ~= *(cast(byte*)&tag+2);
|
||||||
|
messageData ~= *(cast(byte*)&tag+3);
|
||||||
|
messageData ~= *(cast(byte*)&tag+4);
|
||||||
|
messageData ~= *(cast(byte*)&tag+5);
|
||||||
|
messageData ~= *(cast(byte*)&tag+6);
|
||||||
|
messageData ~= *(cast(byte*)&tag+7);
|
||||||
|
|
||||||
|
/* Add the `data` bytes (the actual message) */
|
||||||
|
messageData ~= data;
|
||||||
|
|
||||||
|
return messageData;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user