Skip to content
This repository has been archived by the owner on Oct 10, 2018. It is now read-only.

Commit

Permalink
Merge pull request #2 from jmfranz/SingleMessage
Browse files Browse the repository at this point in the history
Single message protocol now in effect
  • Loading branch information
jmfranz authored Apr 29, 2017
2 parents f020ab5 + 3db49c8 commit 8664e73
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 248 deletions.
6 changes: 0 additions & 6 deletions AnnelidaDispatcher.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AnnelidaDispatcher", "AnnelidaDispatcher\AnnelidaDispatcher.csproj", "{CD57BD03-19D7-4D6B-9597-E51525BC214C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestClient", "TestClient\TestClient.csproj", "{746FC4E9-C688-4753-8E8D-C75DE3C61F7A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReceiveBsonFromDispatcher", "ReceiveBsonFromDispatcher\ReceiveBsonFromDispatcher.csproj", "{A17AC5C0-945D-4C21-B68B-5FC67BFFBB25}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SendBsonToDispatcher", "SendBsonToDispatcher\SendBsonToDispatcher.csproj", "{E62775A2-0ED9-4BBA-9F25-E93E212BF308}"
Expand All @@ -21,10 +19,6 @@ Global
{CD57BD03-19D7-4D6B-9597-E51525BC214C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CD57BD03-19D7-4D6B-9597-E51525BC214C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CD57BD03-19D7-4D6B-9597-E51525BC214C}.Release|Any CPU.Build.0 = Release|Any CPU
{746FC4E9-C688-4753-8E8D-C75DE3C61F7A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{746FC4E9-C688-4753-8E8D-C75DE3C61F7A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{746FC4E9-C688-4753-8E8D-C75DE3C61F7A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{746FC4E9-C688-4753-8E8D-C75DE3C61F7A}.Release|Any CPU.Build.0 = Release|Any CPU
{A17AC5C0-945D-4C21-B68B-5FC67BFFBB25}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A17AC5C0-945D-4C21-B68B-5FC67BFFBB25}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A17AC5C0-945D-4C21-B68B-5FC67BFFBB25}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
16 changes: 9 additions & 7 deletions AnnelidaDispatcher/Model/DispatcherServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,22 +163,26 @@ public void ReadHandler(IAsyncResult ar)
}
else if (bytesRead > 0 && state.isInitialized)
{
//We are receiving the size of the package which is an int32
//We are receiving the package but don't know the size yet
//Serialized bson contains the size in the first 4 bytes.
if(state.bufferSize == 0)
{
int size = BitConverter.ToInt32(state.buffer, 0);
state.bufferSize = size;
state.buffer = new byte[size];
//We take 4 out because we already red those bytes
state.bufferSize = size - 4;
//Resize the array because we need the full set of
//bytes in order to deserialize the Bson
Array.Resize(ref state.buffer, size);
state.recvBytesCount = 0;
handler.BeginReceive(state.buffer, 0, state.bufferSize, 0,
handler.BeginReceive(state.buffer, 4, state.bufferSize , 0,
new AsyncCallback(ReadHandler), state);
}
//we already know the package size
else
{
state.recvBytesCount += bytesRead;
if (state.recvBytesCount < state.bufferSize)
handler.BeginReceive(state.buffer, state.recvBytesCount - 1, state.bufferSize, 0,
handler.BeginReceive(state.buffer, 4 + state.recvBytesCount - 1, state.bufferSize, 0,
new AsyncCallback(ReadHandler), state);
else
{
Expand Down Expand Up @@ -236,15 +240,13 @@ private void NotifyNetworkViewListeners(ClientTypes.Types sender, byte[] documen
case ClientTypes.Types.Robot:
foreach (var c in connectedClients[ClientTypes.Types.View])
{
c.Send(BitConverter.GetBytes(document.Length), 4, 0);
c.BeginSend(document, 0, document.Length, 0, null, c);
}
break;
//Notify the robot
case ClientTypes.Types.Controller:
foreach (var c in connectedClients[ClientTypes.Types.Robot])
{
c.Send(BitConverter.GetBytes(document.Length), 4, 0);
c.BeginSend(document, 0, document.Length, 0, null, c);
}
break;
Expand Down
23 changes: 10 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,25 @@ Rule based many-to-many BsonDocument dispatcher. Messages received are saved in

## Usage
TCP conections on (for now) fixed port 9999.
Messages should be send in two stages. First one is an Int32 containing the size of the BsonDocument byte array. Second message is the serialized BsonDocument.

Client Test folder contains a very simple, and poorly documented, code that connects, identifies itself and sends a serialized `BsonDocument`.
It also shows commented in the end how to receive a serialized `BsonDocument`
Robot and Controllers should send a single message containing the serialized `BsonDocument`. Clients should read the first 4 bytes to know the total size of the message to alloc the necessary space in memory.

Send and Receive BSon folders contain the appropriate examples.

### Step-by-step
1. Connect to dispatcher using TCP on port 9999
2. Self-identify to the dispatcher. While the client is not identified all messages to it are dropped
1. Send `byte[]` containing 4 (`Int32`)
2. Send `byte[]` containing the clientType (`Int32`). Options are:
1. Send `byte[]` containing the clientType (`Int32`). Options are:
1. 1 - View
2. 2 - Controller
3. 3 - Robot
3. Dispatcher now is ready to include the client in the message cycle.
4. Message cycle begins:
1. If view, client should start receiving the messages
2. If controller or robot, client can send messages when necessary

### Messages to view and robot clients
Views and robots shall receive serialized `BsonDocuments` in two steps. First a serialized `Int32` is sent containing the `BsonDocument` size then the document itself is sent

### Messages from controller and robot clients
Controllers and robots should send serialized must send serialized `BsonDocuments` in two steps. First a serialized `Int32` containing the `BsonDocument` size then the document itself.

Messages sent my theese clients are saved in their respective database in parallel to the dispatch cycle.

## Remarks
Client disconnection is not yet implemented. Clients should not close connection but break the TCP pipe. Exception handling is done inside the code to treat this as a client disconnection.
Client disconnection is not yet implemented. Clients should not close connection but break the TCP pipe. Exception handling is done inside the code to treat this as a client disconnection.

Late connections are allowed however late clients will not receive any historical or digested data.
11 changes: 5 additions & 6 deletions ReceiveBsonFromDispatcher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@ static void Main(string[] args)
{
while (!Console.KeyAvailable)
{
byte[] size = new byte[4];
byte[] buff;
c.Client.Receive(size, 4, 0);
int s = BitConverter.ToInt32(size, 0);
buff = new byte[s];
c.Client.Receive(buff, s, 0);
byte[] buff = new byte[4];
c.Client.Receive(buff, 4, 0);
int s = BitConverter.ToInt32(buff, 0);
Array.Resize(ref buff, s);
c.Client.Receive(buff,4, s - 4, 0);

var d = BsonSerializer.Deserialize<BsonDocument>(buff);
Console.WriteLine(d.ToString());
Expand Down
3 changes: 1 addition & 2 deletions SendBsonToDispatcher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ static void Main(string[] args)
{
while (!Console.KeyAvailable)
{
byte[] b = document.ToBson();
stream.Write(BitConverter.GetBytes(b.Length), 0, 4);
byte[] b = document.ToBson();
stream.Write(b, 0, b.Length);
stream.Flush();
Thread.Sleep(1000);
Expand Down
6 changes: 0 additions & 6 deletions TestClient/App.config

This file was deleted.

14 changes: 0 additions & 14 deletions TestClient/ClientTypes.cs

This file was deleted.

73 changes: 0 additions & 73 deletions TestClient/Program.cs

This file was deleted.

36 changes: 0 additions & 36 deletions TestClient/Properties/AssemblyInfo.cs

This file was deleted.

78 changes: 0 additions & 78 deletions TestClient/TestClient.csproj

This file was deleted.

7 changes: 0 additions & 7 deletions TestClient/packages.config

This file was deleted.

0 comments on commit 8664e73

Please sign in to comment.