The message format used for Bester.
Go to file
Tristan B. Velloza Kildaire 6ed46d051c
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
2023-10-01 19:02:42 +02:00
.github/workflows Pipelines 2023-10-01 17:53:19 +02:00
source/bformat Switch to niknaks (#4) 2023-10-01 19:02:42 +02:00
.gitignore - Added unit tests 2023-03-25 22:32:40 +02:00
LICENSE - Updated LICENSE to LGPL 3.0 2023-03-25 21:37:18 +02:00
README.md REAMDE 2023-10-01 17:56:18 +02:00
dub.json Switch to niknaks (#4) 2023-10-01 19:02:42 +02:00

README.md

bformat

D DUB DUB DUB Coverage Status

A simple message format for automatically length-prefixing messages over any Socket or River-based Stream.

What is bformat?

bformat makes it easy to build applications whereby you want to send data over a streaming interface (either a Socket opened in SocketType.STREAM mode or a River-based Stream) and want to be able to read the data as length-prefixed messages, without the hassle of implementing this yourself. This is where bformat shines by providing support for this in a cross-platform manner so you do not have to worry about implementing it yourself countless times again every time you require such functionality in a project.

Usage

You can see the API for information on how to use it but it boils down to spawning a new BClient which takes in either a Socket or Stream (see River) and then you can either send data using sendMessage(byte[]) and receive using receiveMessage(ref byte[]).

Below we have an example application which does just this:

/**
 * Create a server that encodes a message to the client
 * and then let the client decode it from us; both making
 * use of `BClient` to accomplish this
 */
unittest
{
	UnixAddress unixAddr = new UnixAddress("/tmp/bformatServer.sock");

	scope(exit)
	{
		import std.stdio;
		remove(cast(char*)unixAddr.path());
	}

	Socket serverSocket = new Socket(AddressFamily.UNIX, SocketType.STREAM);
	serverSocket.bind(unixAddr);
	serverSocket.listen(0);

	class ServerThread : Thread
	{
		private Socket servSock;

		this(Socket servSock)
		{
			this.servSock = servSock;
			super(&worker);
		}

		private void worker()
		{
			Socket clientSock = servSock.accept();

			BClient bClient = new BClient(clientSock);

			byte[] message = cast(byte[])"ABBA";
			bClient.sendMessage(message);
		}
	}

	Thread serverThread = new ServerThread(serverSocket);
	serverThread.start();

	Socket client = new Socket(AddressFamily.UNIX, SocketType.STREAM);
	client.connect(unixAddr);
	BClient bClient = new BClient(client);

	byte[] receivedMessage;
	bClient.receiveMessage(receivedMessage);
	assert(receivedMessage == "ABBA");
	writeln(receivedMessage);
	writeln(cast(string)receivedMessage);
}

Adding to your peoject

It's rather easy to add it to your D project, just run the command dub add bformat.

License

The license used is LGPL v3.