WatsonMessage.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. namespace WatsonTcp
  2. {
  3. using MessagePack;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. /// <summary>
  8. /// WatsonTcp message.
  9. /// </summary>
  10. [MessagePackObject(AllowPrivate =true)]
  11. public class WatsonMessage
  12. {
  13. #region Public-Members
  14. /// <summary>
  15. /// Length of the data.
  16. /// </summary>
  17. [MessagePack.Key(0)]
  18. public long ContentLength { get; set; }
  19. /// <summary>
  20. /// Preshared key for connection authentication.
  21. /// </summary>
  22. [MessagePack.Key(1)]
  23. public byte[] PresharedKey
  24. {
  25. get
  26. {
  27. return _PresharedKey;
  28. }
  29. set
  30. {
  31. if (value == null)
  32. {
  33. _PresharedKey = null;
  34. }
  35. else
  36. {
  37. if (value.Length != 16) throw new ArgumentException("PresharedKey must be 16 bytes.");
  38. _PresharedKey = new byte[16];
  39. Buffer.BlockCopy(value, 0, _PresharedKey, 0, 16);
  40. }
  41. }
  42. }
  43. /// <summary>
  44. /// Status of the message.
  45. /// </summary>
  46. [MessagePack.Key(2)]
  47. public MessageStatus Status { get; set; } = MessageStatus.Normal;
  48. /// <summary>
  49. /// Metadata dictionary; contains user-supplied metadata.
  50. /// </summary>
  51. [MessagePack.Key(3)]
  52. public Dictionary<string, object> Metadata { get; set; } = null;
  53. /// <summary>
  54. /// Indicates if the message is a synchronous request.
  55. /// </summary>
  56. [MessagePack.Key(4)]
  57. public bool SyncRequest { get; set; } = false;
  58. /// <summary>
  59. /// Indicates if the message is a synchronous response.
  60. /// </summary>
  61. [MessagePack.Key(5)]
  62. public bool SyncResponse { get; set; } = false;
  63. /// <summary>
  64. /// Indicates the current time as perceived by the sender; useful for determining expiration windows.
  65. /// </summary>
  66. [MessagePack.Key(6)]
  67. public DateTime TimestampUtc { get; set; } = DateTime.UtcNow;
  68. /// <summary>
  69. /// Indicates an expiration time in UTC; only applicable to synchronous requests.
  70. /// </summary>
  71. [MessagePack.Key(7)]
  72. public DateTime? ExpirationUtc { get; set; } = null;
  73. /// <summary>
  74. /// Indicates the conversation GUID of the message.
  75. /// </summary>
  76. [MessagePack.Key(8)]
  77. public Guid ConversationGuid { get; set; } = Guid.NewGuid();
  78. /// <summary>
  79. /// Sender GUID.
  80. /// </summary>
  81. [MessagePack.Key(9)]
  82. public Guid SenderGuid { get; set; } = default(Guid);
  83. /// <summary>
  84. /// Stream containing the message data.
  85. /// </summary>
  86. [MessagePack.IgnoreMember]
  87. public Stream DataStream { get; set; } = null;
  88. #endregion
  89. #region Internal-Members
  90. #endregion
  91. #region Private-Members
  92. // 1 2 3
  93. // 12345678901234567890123456789012
  94. [MessagePack.IgnoreMember]
  95. private string _DateTimeFormat = "yyyy-MM-dd HH:mm:ss.fffzzz"; // 32 bytes
  96. [MessagePack.IgnoreMember]
  97. private byte[] _PresharedKey = null;
  98. #endregion
  99. #region Constructors-and-Factories
  100. /// <summary>
  101. /// Instantiate.
  102. /// </summary>
  103. public WatsonMessage()
  104. {
  105. }
  106. #endregion
  107. #region Public-Methods
  108. /// <summary>
  109. /// Human-readable string version of the object.
  110. /// </summary>
  111. /// <returns>String.</returns>
  112. public override string ToString()
  113. {
  114. string ret = "---" + Environment.NewLine;
  115. ret += " Preshared key : " + (PresharedKey != null ? WatsonCommon.ByteArrayToHex(PresharedKey) : "null") + Environment.NewLine;
  116. ret += " Status : " + Status.ToString() + Environment.NewLine;
  117. ret += " SyncRequest : " + SyncRequest.ToString() + Environment.NewLine;
  118. ret += " SyncResponse : " + SyncResponse.ToString() + Environment.NewLine;
  119. ret += " ExpirationUtc : " + (ExpirationUtc != null ? ExpirationUtc.Value.ToString(_DateTimeFormat) : "null") + Environment.NewLine;
  120. ret += " ConversationGuid : " + ConversationGuid.ToString() + Environment.NewLine;
  121. if (Metadata != null && Metadata.Count > 0)
  122. {
  123. ret += " Metadata : " + Metadata.Count + " entries" + Environment.NewLine;
  124. }
  125. if (DataStream != null)
  126. ret += " DataStream : present, " + ContentLength + " bytes" + Environment.NewLine;
  127. return ret;
  128. }
  129. #endregion Public-Methods
  130. #region Private-Methods
  131. #endregion
  132. }
  133. }