Compare commits

...

8 Commits

Author SHA1 Message Date
Tristan B. Velloza Kildaire 575db2d998 - Updated `README.md` to reflect support for river-based `Stream`s 2023-04-30 13:02:17 +02:00
Tristan B. Velloza Kildaire 40ce197bb7 Merge branch 'feature/oop' into feature/river_stream 2023-04-30 13:01:49 +02:00
Tristan B. Velloza Kildaire ed71224233 Merge branch 'master' into feature/oop 2023-04-30 13:01:03 +02:00
Tristan B. Velloza Kildaire 1094d3d61f - Updated description in `dub.json` and README 2023-04-30 12:59:58 +02:00
Tristan B. Velloza Kildaire d93e96f0f8 BClient
- Documented unit test
2023-04-30 12:54:11 +02:00
Tristan B. Velloza Kildaire 84e9b401a0 BClient
- Documented both constructors
2023-04-30 12:53:00 +02:00
Tristan B. Velloza Kildaire e1a167fdbf Package ( `bformat`)
- Updated comment for `bformat.client`

- Renamed `sockets` module to `client` module
2023-04-30 12:49:13 +02:00
Tristan B. Velloza Kildaire 00664bd844 Sockets
- Added a unittest which tests `BClient`
2023-04-30 12:42:59 +02:00
3 changed files with 85 additions and 15 deletions

View File

@ -3,7 +3,7 @@ bformat
[![D](https://github.com/besterprotocol/bformat/actions/workflows/d.yml/badge.svg)](https://github.com/besterprotocol/bformat/actions/workflows/d.yml)
A simple message format for length prefixed messages
A simple message format for automatically length-prefixing messages over any [`Socket`](https://dlang.org/phobos/std_socket.html#.Socket) or [River-based](https://github.com/deavmi/river) `Stream`.
## What is bformat?

View File

@ -1,12 +1,16 @@
/**
* Socket encoding/decoding functions
*/
module bformat.sockets;
module bformat.client;
import std.socket : Socket;
import river.core;
import river.impls.sock : SockStream;
/**
* Bformat client to encode and decode via a
* `Socket` or river-based `Stream`
*/
public class BClient
{
/**
@ -14,13 +18,25 @@ public class BClient
*/
private Stream stream;
// TODO: comment
/**
* Constructs a new `BClient` for encoding and decoding
* to and from the provided `Socket`
*
* Params:
* socket = the `Socket` to use for writing and reading
*/
this(Socket socket)
{
this(new SockStream(socket));
}
// TODO: Comment
/**
* Constructs a new `BClient` for encoding and decoding
* to and from the provided river-based `Stream`
*
* Params:
* stream = the `Stream` to use for writing and reading
*/
this(Stream stream)
{
this.stream = stream;
@ -129,4 +145,65 @@ public class BClient
}
}
}
version(unittest)
{
import std.socket;
import core.thread;
import std.stdio;
}
/**
* 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);
}

View File

@ -4,18 +4,11 @@
module bformat;
/**
* Encodes the provided message into the bformat format
* and sends it over the provided socket
* Provides a client which consumes a stream
* which can encode and decode messages to
* and from it
*/
public import bformat.sockets : BClient;
/**
* Receives a message from the provided socket
* by decoding the streamed bytes into bformat
* and finally placing the resulting payload in
* the provided array
*/
// public import bformat.sockets : receiveMessage;
public import bformat.client : BClient;
/**
* Encodes the provided message into the bformat format