123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- namespace WatsonTcp
- {
- using MessagePack;
- using System;
- using System.Collections.Generic;
- using System.IO;
- /// <summary>
- /// WatsonTcp message.
- /// </summary>
- [MessagePackObject(AllowPrivate =true)]
- public class WatsonMessage
- {
- #region Public-Members
- /// <summary>
- /// Length of the data.
- /// </summary>
- [MessagePack.Key(0)]
- public long ContentLength { get; set; }
- /// <summary>
- /// Preshared key for connection authentication.
- /// </summary>
- [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);
- }
- }
- }
- /// <summary>
- /// Status of the message.
- /// </summary>
- [MessagePack.Key(2)]
- public MessageStatus Status { get; set; } = MessageStatus.Normal;
- /// <summary>
- /// Metadata dictionary; contains user-supplied metadata.
- /// </summary>
- [MessagePack.Key(3)]
- public Dictionary<string, object> Metadata { get; set; } = null;
- /// <summary>
- /// Indicates if the message is a synchronous request.
- /// </summary>
- [MessagePack.Key(4)]
- public bool SyncRequest { get; set; } = false;
- /// <summary>
- /// Indicates if the message is a synchronous response.
- /// </summary>
- [MessagePack.Key(5)]
- public bool SyncResponse { get; set; } = false;
- /// <summary>
- /// Indicates the current time as perceived by the sender; useful for determining expiration windows.
- /// </summary>
- [MessagePack.Key(6)]
- public DateTime TimestampUtc { get; set; } = DateTime.UtcNow;
- /// <summary>
- /// Indicates an expiration time in UTC; only applicable to synchronous requests.
- /// </summary>
- [MessagePack.Key(7)]
- public DateTime? ExpirationUtc { get; set; } = null;
- /// <summary>
- /// Indicates the conversation GUID of the message.
- /// </summary>
- [MessagePack.Key(8)]
- public Guid ConversationGuid { get; set; } = Guid.NewGuid();
- /// <summary>
- /// Sender GUID.
- /// </summary>
- [MessagePack.Key(9)]
- public Guid SenderGuid { get; set; } = default(Guid);
- /// <summary>
- /// Stream containing the message data.
- /// </summary>
- [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
- /// <summary>
- /// Instantiate.
- /// </summary>
- public WatsonMessage()
- {
- }
-
- #endregion
-
- #region Public-Methods
-
- /// <summary>
- /// Human-readable string version of the object.
- /// </summary>
- /// <returns>String.</returns>
- 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
- }
- }
|