Compare commits
8 Commits
8e0c161e02
...
575db2d998
Author | SHA1 | Date |
---|---|---|
Tristan B. Velloza Kildaire | 575db2d998 | |
Tristan B. Velloza Kildaire | 40ce197bb7 | |
Tristan B. Velloza Kildaire | ed71224233 | |
Tristan B. Velloza Kildaire | 1094d3d61f | |
Tristan B. Velloza Kildaire | d93e96f0f8 | |
Tristan B. Velloza Kildaire | 84e9b401a0 | |
Tristan B. Velloza Kildaire | e1a167fdbf | |
Tristan B. Velloza Kildaire | 00664bd844 |
|
@ -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?
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue