Skip to content
This repository has been archived by the owner on Jun 16, 2022. It is now read-only.

Commit

Permalink
Added documentation to the MessageHandler class.
Browse files Browse the repository at this point in the history
When any MessageHandler throws on a call, the session is now terminated since it was unhanded due to invalid data sent.
Added Guid to MqFrame as a type to read and write.
Added Guid to MqMessageReader and MqMessageWriter.
Added tests for new Guid methods.
  • Loading branch information
DJGosnell committed Sep 24, 2017
1 parent ffafbc9 commit 603b245
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 21 deletions.
54 changes: 54 additions & 0 deletions src/DtronixMessageQueue.Tests/Mq/MqFrameTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -847,5 +847,59 @@ public void Frame_throws_on_ascii_text_write_when_larger_than_frame()

Assert.Throws<InvalidOperationException>(() => _actualFrame.WriteAscii(0, value, false));
}

[Fact]
public void Frame_writes_guid()
{
var value = Guid.NewGuid();

_expectedBytes = new byte[] { 3, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

Buffer.BlockCopy(value.ToByteArray(), 0, _expectedBytes, 3, 16);

_actualFrame = new MqFrame(new byte[17], MqFrameType.Last, _config);
_actualFrame.Write(0, value);

_actualBytes = _actualFrame.RawFrame();

Assert.Equal(_expectedBytes, _actualBytes);
}

[Fact]
public void Frame_writes_guid_position()
{
var value = Guid.NewGuid();

_expectedBytes = new byte[] {3, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

Buffer.BlockCopy(value.ToByteArray(), 0, _expectedBytes, 4, 16);

_actualFrame = new MqFrame(new byte[18], MqFrameType.Last, _config);
_actualFrame.Write(1, value);

_actualBytes = _actualFrame.RawFrame();

Assert.Equal(_expectedBytes, _actualBytes);
}

[Fact]
public void Frame_reads_guid()
{
var value = Guid.NewGuid();
_actualFrame = new MqFrame(new byte[16], MqFrameType.Last, _config);
_actualFrame.Write(0, value);

Assert.Equal(value, _actualFrame.ReadGuid(0));
}

[Fact]
public void Frame_reads_guid_position()
{
var value = Guid.NewGuid();
_actualFrame = new MqFrame(new byte[17], MqFrameType.Last, _config);
_actualFrame.Write(1, value);

Assert.Equal(value, _actualFrame.ReadGuid(1));
}
}
}
19 changes: 19 additions & 0 deletions src/DtronixMessageQueue.Tests/Mq/MqMessageWriterReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ public void MessageReader_reads_multi_frame_string()
[Fact]
public void MessageWriter_multiple_reads_writes()
{

_messageBuilder.Write(true);
_messageBuilder.Write(false);

Expand Down Expand Up @@ -387,6 +388,9 @@ public void MessageWriter_multiple_reads_writes()

_messageBuilder.Write(FillerText);

var expectedGuid = Guid.NewGuid();
_messageBuilder.Write((Guid)expectedGuid);

var message = _messageBuilder.ToMessage();

_messageReader.Message = message;
Expand Down Expand Up @@ -423,6 +427,9 @@ public void MessageWriter_multiple_reads_writes()
Assert.Equal(expectedByteArray, readByteArray);

Assert.Equal(FillerText, _messageReader.ReadString());

Assert.Equal(expectedGuid, _messageReader.ReadGuid());

Assert.True(_messageReader.IsAtEnd);
}

Expand Down Expand Up @@ -664,5 +671,17 @@ public void MessageReader_sets_position_and_updates_isatend()
_messageReader.Position = 8;
Assert.False(_messageReader.IsAtEnd);
}

[Fact]
public void MessageReader_writes_guid()
{
var expectedValue = (Guid)Guid.NewGuid();
_messageBuilder.Write(expectedValue);
var message = _messageBuilder.ToMessage();
_messageReader.Message = message;

Assert.Equal(expectedValue, _messageReader.ReadGuid());
Assert.True(_messageReader.IsAtEnd);
}
}
}
29 changes: 28 additions & 1 deletion src/DtronixMessageQueue/MqFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ public string ReadAscii(int index, int stringLength = -1)
return Encoding.ASCII.GetString(strBuffer);
}


/// <summary>
/// Writes a ASCII encoded string.
/// (uint16?)(string)
Expand All @@ -565,7 +566,7 @@ public void WriteAscii(int index, string value, bool prependSize = true)

if (prependSize)
{
Write(index, (ushort) stringBytes.Length);
Write(index, (ushort)stringBytes.Length);
}

if (index + stringBytes.Length + (prependSize ? 2 : 0) > _config.FrameBufferSize)
Expand All @@ -576,6 +577,32 @@ public void WriteAscii(int index, string value, bool prependSize = true)
Write(index + (prependSize ? 2 : 0), stringBytes, 0, stringBytes.Length);
}

/// <summary>
/// Writes a Guid byte array value.
/// 16 Bytes.
/// </summary>
/// <param name="index">The zero-based index to write the value to.</param>
/// <param name="value">Value to write to the specified index.</param>
public void Write(int index, Guid value)
{
var guidBytes = value.ToByteArray();

Write(index, guidBytes, 0, guidBytes.Length);
}

/// <summary>
/// Reads a Guid value at the specified index.
/// 16 Bytes.
/// </summary>
/// <param name="index">The zero-based index to read the value from.</param>
public Guid ReadGuid(int index)
{
var guidBuffer = new byte[16];
Read(index, guidBuffer, 0, 16);
return new Guid(guidBuffer);
}


/// <summary>
/// Reads the bytes from this frame.
/// </summary>
Expand Down
14 changes: 14 additions & 0 deletions src/DtronixMessageQueue/MqMessageReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,20 @@ public override string ReadString()
return _encoding.GetString(strBuffer);
}

/// <summary>
/// Reads a Guid.
/// 16 Bytes.
/// </summary>
public Guid ReadGuid()
{
EnsureBuffer(16);
var value = _currentFrame.ReadGuid(_framePosition);
_framePosition += 16;
_absolutePosition += 16;

return value;
}

/// <summary>
/// Reads the rest of the message bytes from the current position to the end.
/// >1 Byte.
Expand Down
52 changes: 33 additions & 19 deletions src/DtronixMessageQueue/MqMessageWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,39 @@ public override void Write(decimal value)
_position += 16;
}


/// <summary>
/// Writes a string.
/// >4 Bytes.
/// 1 or more frames.
/// </summary>
/// <param name="value">Value to write to the message.</param>
public override void Write(string value)
{
var stringBytes = Encoding.UTF8.GetBytes(value);

// Write the length prefix
EnsureSpace(4);
_builderFrame.Write(_position, stringBytes.Length);
_position += 4;

// Write the buffer to the message.
Write(stringBytes, 0, stringBytes.Length);
}


/// <summary>
/// Writes a Guid.
/// 16 Bytes.
/// </summary>
/// <param name="value">Value to write to the message.</param>
public void Write(Guid value)
{
EnsureSpace(16);
_builderFrame.Write(_position, value);
_position += 16;
}

/// <summary>
/// Appends an existing message to this message.
/// </summary>
Expand Down Expand Up @@ -325,25 +358,6 @@ public void Write()
_frames.Add(new MqFrame(null, MqFrameType.Empty, _config));
}

/// <summary>
/// Writes a string.
/// >4 Bytes.
/// 1 or more frames.
/// </summary>
/// <param name="value">Value to write to the message.</param>
public override void Write(string value)
{
var stringBytes = Encoding.UTF8.GetBytes(value);

// Write the length prefix
EnsureSpace(4);
_builderFrame.Write(_position, stringBytes.Length);
_position += 4;

// Write the buffer to the message.
Write(stringBytes, 0, stringBytes.Length);
}

/// <summary>
/// Writes a byte array to this one or more frames.
/// >1 Byte.
Expand Down
35 changes: 34 additions & 1 deletion src/DtronixMessageQueue/Rpc/MessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,48 @@

namespace DtronixMessageQueue.Rpc
{
/// <summary>
/// Base class to be used for all Rpc message handlers.
/// </summary>
/// <typeparam name="TSession">Session type for this connection.</typeparam>
/// <typeparam name="TConfig">Configuration for this connection.</typeparam>
public abstract class MessageHandler<TSession, TConfig>
where TSession : RpcSession<TSession, TConfig>, new()
where TConfig : RpcConfig
{
public delegate void ActionHandler(byte actionHandler, MqMessage message);
/// <summary>
/// Delegate used to be called on each successfully parsed action call in this MessageHandler.
/// </summary>
/// <param name="actionId">Value of the enum for the action called.</param>
/// <param name="message">Full message excluding the action id.</param>
public delegate void ActionHandler(byte actionId, MqMessage message);

/// <summary>
/// Id byte which precedes all messages all messages of this type.
/// </summary>
public abstract byte Id { get; }

/// <summary>
/// Session of the connection.
/// </summary>
public TSession Session;

/// <summary>
/// All registered handlers for the MessageHandler.
/// </summary>
protected Dictionary<byte, ActionHandler> Handlers = new Dictionary<byte, ActionHandler>();

protected MessageHandler(TSession session)
{
Session = session;
}

/// <summary>
/// Parses and passes the message to the correct action in the handler.
/// Automatically removes the first frame of the message used to determine which action id to call.
/// </summary>
/// <param name="message">Message to parse</param>
/// <returns>True on successful parsing.</returns>
public bool HandleMessage(MqMessage message)
{
if (message[0][0] != Id)
Expand All @@ -45,15 +67,26 @@ public bool HandleMessage(MqMessage message)
return false;
}

/// <summary>
/// Send a null message to the
/// </summary>
/// <param name="actionId"></param>
public void SendHandlerMessage(byte actionId)
{
SendHandlerMessage(actionId, null);
}

/// <summary>
/// Sends the passed message to the specified action handler.
/// </summary>
/// <param name="actionId">Action enum value to pass the message to.</param>
/// <param name="message">Message to send to the connection.</param>
public void SendHandlerMessage(byte actionId, MqMessage message)
{
var headerFrame = Session.CreateFrame(new byte[2], MqFrameType.More);
// Id of the message handler.
headerFrame.Write(0, Id);
// Id of the action to call.
headerFrame.Write(1, actionId);

var sendMessage = new MqMessage
Expand Down

0 comments on commit 603b245

Please sign in to comment.