1
0
Fork 0
mirror of https://github.com/besterprotocol/besterd synced 2023-12-13 21:00:32 +01:00

WIP: Message receive function

This commit is contained in:
Tristan B. Kildaire 2020-04-26 17:47:54 +02:00
parent a2100a4ac9
commit c810ef3a77
2 changed files with 125 additions and 1 deletions

View file

@ -194,7 +194,6 @@ public final class BesterConnection : Thread
* message header in little-endian containing the message's
* length.
*/
public static void sendMessage(Socket recipient, JSONValue jsonMessage)
{
/* The message buffer */
@ -218,6 +217,129 @@ public final class BesterConnection : Thread
recipient.send(messageBuffer);
}
/* TODO: Implement me */
/**
* Generalized socket receive function which will read into the
* variable pointed to by `receiveMessage` by reading from the
* socket `originator`.
*/
public static void receiveMessage(Socket originator, ref JSONValue receiveMessage)
{
/* TODO: Implement me */
/* Construct a buffer to receive into */
byte[] receiveBuffer;
/* The current byte */
uint currentByte = 0;
/* The amount of bytes received */
long bytesReceived;
/* Loop consume the next 4 bytes */
while(currentByte < 4)
{
/* Temporary buffer */
byte[4] tempBuffer;
/* Read at-most 4 bytes */
bytesReceived = handlerSocket.receive(tempBuffer);
/* If there was an error reading from the socket */
if(!(bytesReceived > 0))
{
/* TODO: Error handling */
debugPrint("Error receiving from UNIX domain socket");
}
/* If there is no error reading from the socket */
else
{
/* Add the read bytes to the *real* buffer */
receiveBuffer ~= tempBuffer[0..bytesReceived];
/* Increment the byte counter */
currentByte += bytesReceived;
}
}
/* Response message length */
int messageLength = *cast(int*)receiveBuffer.ptr;
writeln("Message length is: ", cast(uint)messageLength);
/* Response message buffer */
byte[] fullMessage;
/* Reset the byte counter */
currentByte = 0;
while(currentByte < messageLength)
{
debugPrint("dhjkh");
/**
* Receive 20 bytes (at most) at a time and don't dequeue from
* the kernel's TCP stack's buffer.
*/
byte[20] tempBuffer;
bytesReceived = handlerSocket.receive(tempBuffer, SocketFlags.PEEK);
/* Check for an error whilst receiving */
if(!(bytesReceived > 0))
{
/* TODO: Error handling */
debugPrint("Error whilst receiving from unix domain socket");
}
else
{
/* TODO: Make sure we only take [0, messageLength) bytes */
if(cast(uint)bytesReceived+currentByte > messageLength)
{
byte[] remainingBytes;
remainingBytes.length = messageLength-currentByte;
handlerSocket.receive(remainingBytes);
/* Increment counter of received bytes */
currentByte += remainingBytes.length;
/* Append the received bytes to the FULL message buffer */
fullMessage ~= remainingBytes;
writeln("Received ", currentByte, "/", cast(uint)messageLength, " bytes");
}
else
{
/* Increment counter of received bytes */
currentByte += bytesReceived;
/* Append the received bytes to the FULL message buffer */
fullMessage ~= tempBuffer[0..bytesReceived];
/* TODO: Bug when over send, we must not allow this */
writeln("Received ", currentByte, "/", cast(uint)messageLength, " bytes");
handlerSocket.receive(tempBuffer);
}
}
}
writeln("MEssage ", fullMessage);
/* Set the message in `receiveMessage */
receiveMessage = parseJSON(cast(string)messageBuffer);
}
/* TODO: Pass in type and just payload or what */
private bool dispatch(string payloadType, JSONValue payload)

2
source/utils/coding.d Normal file
View file

@ -0,0 +1,2 @@
module utils.coding;