namespace WatsonTcp
{
using MessagePack;
using System;
using System.Collections.Generic;
using System.IO;
///
/// WatsonTcp message.
///
[MessagePackObject(AllowPrivate =true)]
public class WatsonMessage
{
#region Public-Members
///
/// Length of the data.
///
[MessagePack.Key(0)]
public long ContentLength { get; set; }
///
/// Preshared key for connection authentication.
///
[MessagePack.Key(1)]
public byte[] PresharedKey
{
get
{
return _PresharedKey;
}
set
{
if (value == null)
{
_PresharedKey = null;
}
else
{
if (value.Length != 16) throw new ArgumentException("PresharedKey must be 16 bytes.");
_PresharedKey = new byte[16];
Buffer.BlockCopy(value, 0, _PresharedKey, 0, 16);
}
}
}
///
/// Status of the message.
///
[MessagePack.Key(2)]
public MessageStatus Status { get; set; } = MessageStatus.Normal;
///
/// Metadata dictionary; contains user-supplied metadata.
///
[MessagePack.Key(3)]
public Dictionary Metadata { get; set; } = null;
///
/// Indicates if the message is a synchronous request.
///
[MessagePack.Key(4)]
public bool SyncRequest { get; set; } = false;
///
/// Indicates if the message is a synchronous response.
///
[MessagePack.Key(5)]
public bool SyncResponse { get; set; } = false;
///
/// Indicates the current time as perceived by the sender; useful for determining expiration windows.
///
[MessagePack.Key(6)]
public DateTime TimestampUtc { get; set; } = DateTime.UtcNow;
///
/// Indicates an expiration time in UTC; only applicable to synchronous requests.
///
[MessagePack.Key(7)]
public DateTime? ExpirationUtc { get; set; } = null;
///
/// Indicates the conversation GUID of the message.
///
[MessagePack.Key(8)]
public Guid ConversationGuid { get; set; } = Guid.NewGuid();
///
/// Sender GUID.
///
[MessagePack.Key(9)]
public Guid SenderGuid { get; set; } = default(Guid);
///
/// Stream containing the message data.
///
[MessagePack.IgnoreMember]
public Stream DataStream { get; set; } = null;
#endregion
#region Internal-Members
#endregion
#region Private-Members
// 1 2 3
// 12345678901234567890123456789012
[MessagePack.IgnoreMember]
private string _DateTimeFormat = "yyyy-MM-dd HH:mm:ss.fffzzz"; // 32 bytes
[MessagePack.IgnoreMember]
private byte[] _PresharedKey = null;
#endregion
#region Constructors-and-Factories
///
/// Instantiate.
///
public WatsonMessage()
{
}
#endregion
#region Public-Methods
///
/// Human-readable string version of the object.
///
/// String.
public override string ToString()
{
string ret = "---" + Environment.NewLine;
ret += " Preshared key : " + (PresharedKey != null ? WatsonCommon.ByteArrayToHex(PresharedKey) : "null") + Environment.NewLine;
ret += " Status : " + Status.ToString() + Environment.NewLine;
ret += " SyncRequest : " + SyncRequest.ToString() + Environment.NewLine;
ret += " SyncResponse : " + SyncResponse.ToString() + Environment.NewLine;
ret += " ExpirationUtc : " + (ExpirationUtc != null ? ExpirationUtc.Value.ToString(_DateTimeFormat) : "null") + Environment.NewLine;
ret += " ConversationGuid : " + ConversationGuid.ToString() + Environment.NewLine;
if (Metadata != null && Metadata.Count > 0)
{
ret += " Metadata : " + Metadata.Count + " entries" + Environment.NewLine;
}
if (DataStream != null)
ret += " DataStream : present, " + ContentLength + " bytes" + Environment.NewLine;
return ret;
}
#endregion Public-Methods
#region Private-Methods
#endregion
}
}