MessageElements.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Vinno.IUS.Common.IO;
  6. using Vinno.IUS.Common.Utilities;
  7. namespace Vinno.IUS.Common.Network.Transfer
  8. {
  9. public interface IElement
  10. {
  11. TransferType TransferType { get; }
  12. IBuffer GetBuffer();
  13. }
  14. public abstract class Element<T>:IElement
  15. {
  16. public T Value { get; set; }
  17. protected Element(TransferType transferType, T value)
  18. {
  19. TransferType = transferType;
  20. Value = value;
  21. }
  22. /// <summary>
  23. /// ValueBuffer = ValueData
  24. /// </summary>
  25. /// <returns></returns>
  26. protected abstract IBuffer GetValueBuffer();
  27. public TransferType TransferType { get; }
  28. /// <summary>
  29. /// Buffer = Type|ValueBuffer
  30. /// </summary>
  31. /// <returns></returns>
  32. public IBuffer GetBuffer()
  33. {
  34. var buffer = new CompositBuffer();
  35. buffer.Add(new ByteBuffer(BitConverter.GetBytes((int)TransferType)));
  36. buffer.Add(GetValueBuffer());
  37. return buffer;
  38. }
  39. public override string ToString()
  40. {
  41. return $"<Element type=\"{TransferType}\" value=\"{Value}\"/>";
  42. }
  43. }
  44. public class EmptyElement:Element<object>
  45. {
  46. public EmptyElement() : base(TransferType.Empty, null)
  47. {
  48. }
  49. protected override IBuffer GetValueBuffer()
  50. {
  51. return new ByteBuffer(new byte[0]);
  52. }
  53. }
  54. public class IntegerElement : Element<int>
  55. {
  56. public IntegerElement(int value)
  57. : base(TransferType.Integer, value)
  58. {
  59. }
  60. protected override IBuffer GetValueBuffer()
  61. {
  62. return new ByteBuffer(BitConverter.GetBytes(Value));
  63. }
  64. }
  65. public class LongElement : Element<long>
  66. {
  67. public LongElement(long value)
  68. : base(TransferType.Long, value)
  69. {
  70. }
  71. protected override IBuffer GetValueBuffer()
  72. {
  73. return new ByteBuffer(BitConverter.GetBytes(Value));
  74. }
  75. }
  76. public class FloatElement:Element<float>
  77. {
  78. public FloatElement(float value) : base(TransferType.Float, value)
  79. {
  80. }
  81. protected override IBuffer GetValueBuffer()
  82. {
  83. return new ByteBuffer(BitConverter.GetBytes(Value));
  84. }
  85. }
  86. public class DoubleElement : Element<double>
  87. {
  88. public DoubleElement(double value) : base(TransferType.Double, value)
  89. {
  90. }
  91. protected override IBuffer GetValueBuffer()
  92. {
  93. return new ByteBuffer(BitConverter.GetBytes(Value));
  94. }
  95. }
  96. public class BoolElement:Element<bool>
  97. {
  98. public BoolElement(bool value)
  99. : base(TransferType.Bool, value)
  100. {
  101. }
  102. protected override IBuffer GetValueBuffer()
  103. {
  104. return new ByteBuffer(BitConverter.GetBytes(Value));
  105. }
  106. }
  107. public class StringElement:Element<string>
  108. {
  109. public StringElement(string value) : base(TransferType.String, value)
  110. {
  111. }
  112. protected override IBuffer GetValueBuffer()
  113. {
  114. if (Value == null)
  115. {
  116. Value = string.Empty;
  117. }
  118. var data = Encoding.UTF8.GetBytes(Value);
  119. return new ByteBuffer(data);
  120. }
  121. }
  122. public class DateTimeElement : Element<DateTime>
  123. {
  124. public DateTimeElement(DateTime value) :
  125. base(TransferType.DateTime, value)
  126. {
  127. }
  128. protected override IBuffer GetValueBuffer()
  129. {
  130. return new ByteBuffer(BitConverter.GetBytes(Value.Ticks));
  131. }
  132. }
  133. public class BufferElement:Element<IBuffer>
  134. {
  135. public BufferElement(IBuffer value)
  136. : base(TransferType.Binary, value)
  137. {
  138. }
  139. protected override IBuffer GetValueBuffer()
  140. {
  141. return Value;
  142. }
  143. }
  144. public class ListElement : Element<List<Message>>
  145. {
  146. public ListElement()
  147. : base(TransferType.List, new List<Message>())
  148. {
  149. }
  150. public void AddMessage(Message message)
  151. {
  152. Value.Add(message);
  153. }
  154. public void AddMessage(MessageTag tag, IElement element)
  155. {
  156. var message = new Message(tag, element);
  157. AddMessage(message);
  158. }
  159. public void RemoveMessage(Message message)
  160. {
  161. var exist = Value.FirstOrDefault(x=> x== message);
  162. if (exist != null)
  163. {
  164. Value.Remove(message);
  165. }
  166. }
  167. protected override IBuffer GetValueBuffer()
  168. {
  169. var buffer = new CompositBuffer();
  170. foreach (var message in Value)
  171. {
  172. buffer.Add(message.GetBuffer());
  173. }
  174. return buffer;
  175. }
  176. public override string ToString()
  177. {
  178. var sb = new StringBuilder();
  179. sb.Append($"<Element type=\"{TransferType}\" count=\"{Value.Count}\">");
  180. foreach (var message in Value)
  181. {
  182. sb.Append($"{message}");
  183. }
  184. sb.Append("</Element>");
  185. return sb.ToString();
  186. }
  187. }
  188. public class IdElement : Element<FasterUId>
  189. {
  190. public IdElement(FasterUId value) : base(TransferType.Id, value)
  191. {
  192. }
  193. protected override IBuffer GetValueBuffer()
  194. {
  195. return new ByteBuffer(Value);
  196. }
  197. }
  198. public class DictionaryElement : Element<Dictionary<MessageTag,Message>>
  199. {
  200. public DictionaryElement()
  201. : base(TransferType.Dictionary, new Dictionary<MessageTag,Message>())
  202. {
  203. }
  204. public void AddMessage(Message message)
  205. {
  206. if (Value.ContainsKey(message.Tag))
  207. {
  208. throw new ArgumentException($"Tag Id:{message.Tag},Key already exists.");
  209. }
  210. Value.Add(message.Tag,message);
  211. }
  212. public void AddMessage(MessageTag tag, IElement element)
  213. {
  214. var message = new Message(tag, element);
  215. AddMessage(message);
  216. }
  217. public void RemoveMessage(MessageTag tag)
  218. {
  219. if (Value.ContainsKey(tag))
  220. {
  221. Value.Remove(tag);
  222. }
  223. }
  224. protected override IBuffer GetValueBuffer()
  225. {
  226. var buffer = new CompositBuffer();
  227. foreach (var message in Value.Values)
  228. {
  229. buffer.Add(message.GetBuffer());
  230. }
  231. return buffer;
  232. }
  233. public override string ToString()
  234. {
  235. var sb = new StringBuilder();
  236. sb.Append($"<Element type=\"{TransferType}\" count=\"{Value.Count}\">");
  237. foreach (var message in Value)
  238. {
  239. sb.Append($"{message.Value}");
  240. }
  241. sb.Append("</Element>");
  242. return sb.ToString();
  243. }
  244. }
  245. }