123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Vinno.IUS.Common.IO;
- using Vinno.IUS.Common.Utilities;
- namespace Vinno.IUS.Common.Network.Transfer
- {
- public interface IElement
- {
- TransferType TransferType { get; }
- IBuffer GetBuffer();
- }
- public abstract class Element<T>:IElement
- {
- public T Value { get; set; }
- protected Element(TransferType transferType, T value)
- {
- TransferType = transferType;
- Value = value;
- }
- /// <summary>
- /// ValueBuffer = ValueData
- /// </summary>
- /// <returns></returns>
- protected abstract IBuffer GetValueBuffer();
- public TransferType TransferType { get; }
- /// <summary>
- /// Buffer = Type|ValueBuffer
- /// </summary>
- /// <returns></returns>
- public IBuffer GetBuffer()
- {
- var buffer = new CompositBuffer();
- buffer.Add(new ByteBuffer(BitConverter.GetBytes((int)TransferType)));
- buffer.Add(GetValueBuffer());
- return buffer;
- }
- public override string ToString()
- {
- return $"<Element type=\"{TransferType}\" value=\"{Value}\"/>";
- }
- }
- public class EmptyElement:Element<object>
- {
- public EmptyElement() : base(TransferType.Empty, null)
- {
- }
- protected override IBuffer GetValueBuffer()
- {
- return new ByteBuffer(new byte[0]);
- }
- }
- public class IntegerElement : Element<int>
- {
- public IntegerElement(int value)
- : base(TransferType.Integer, value)
- {
- }
- protected override IBuffer GetValueBuffer()
- {
- return new ByteBuffer(BitConverter.GetBytes(Value));
- }
- }
- public class LongElement : Element<long>
- {
- public LongElement(long value)
- : base(TransferType.Long, value)
- {
- }
- protected override IBuffer GetValueBuffer()
- {
- return new ByteBuffer(BitConverter.GetBytes(Value));
- }
- }
- public class FloatElement:Element<float>
- {
- public FloatElement(float value) : base(TransferType.Float, value)
- {
- }
- protected override IBuffer GetValueBuffer()
- {
- return new ByteBuffer(BitConverter.GetBytes(Value));
- }
- }
- public class DoubleElement : Element<double>
- {
- public DoubleElement(double value) : base(TransferType.Double, value)
- {
- }
- protected override IBuffer GetValueBuffer()
- {
- return new ByteBuffer(BitConverter.GetBytes(Value));
- }
- }
- public class BoolElement:Element<bool>
- {
- public BoolElement(bool value)
- : base(TransferType.Bool, value)
- {
- }
- protected override IBuffer GetValueBuffer()
- {
- return new ByteBuffer(BitConverter.GetBytes(Value));
- }
- }
- public class StringElement:Element<string>
- {
- public StringElement(string value) : base(TransferType.String, value)
- {
- }
- protected override IBuffer GetValueBuffer()
- {
- if (Value == null)
- {
- Value = string.Empty;
- }
- var data = Encoding.UTF8.GetBytes(Value);
- return new ByteBuffer(data);
- }
- }
- public class DateTimeElement : Element<DateTime>
- {
- public DateTimeElement(DateTime value) :
- base(TransferType.DateTime, value)
- {
- }
- protected override IBuffer GetValueBuffer()
- {
- return new ByteBuffer(BitConverter.GetBytes(Value.Ticks));
- }
- }
- public class BufferElement:Element<IBuffer>
- {
- public BufferElement(IBuffer value)
- : base(TransferType.Binary, value)
- {
- }
- protected override IBuffer GetValueBuffer()
- {
- return Value;
- }
- }
- public class ListElement : Element<List<Message>>
- {
- public ListElement()
- : base(TransferType.List, new List<Message>())
- {
-
- }
- public void AddMessage(Message message)
- {
- Value.Add(message);
- }
- public void AddMessage(MessageTag tag, IElement element)
- {
- var message = new Message(tag, element);
- AddMessage(message);
- }
- public void RemoveMessage(Message message)
- {
- var exist = Value.FirstOrDefault(x=> x== message);
- if (exist != null)
- {
- Value.Remove(message);
- }
- }
- protected override IBuffer GetValueBuffer()
- {
- var buffer = new CompositBuffer();
- foreach (var message in Value)
- {
- buffer.Add(message.GetBuffer());
- }
- return buffer;
- }
- public override string ToString()
- {
- var sb = new StringBuilder();
- sb.Append($"<Element type=\"{TransferType}\" count=\"{Value.Count}\">");
- foreach (var message in Value)
- {
- sb.Append($"{message}");
- }
- sb.Append("</Element>");
- return sb.ToString();
- }
- }
- public class IdElement : Element<FasterUId>
- {
- public IdElement(FasterUId value) : base(TransferType.Id, value)
- {
- }
- protected override IBuffer GetValueBuffer()
- {
- return new ByteBuffer(Value);
- }
- }
- public class DictionaryElement : Element<Dictionary<MessageTag,Message>>
- {
- public DictionaryElement()
- : base(TransferType.Dictionary, new Dictionary<MessageTag,Message>())
- {
- }
- public void AddMessage(Message message)
- {
- if (Value.ContainsKey(message.Tag))
- {
- throw new ArgumentException($"Tag Id:{message.Tag},Key already exists.");
- }
- Value.Add(message.Tag,message);
- }
- public void AddMessage(MessageTag tag, IElement element)
- {
- var message = new Message(tag, element);
- AddMessage(message);
- }
- public void RemoveMessage(MessageTag tag)
- {
- if (Value.ContainsKey(tag))
- {
- Value.Remove(tag);
- }
- }
- protected override IBuffer GetValueBuffer()
- {
- var buffer = new CompositBuffer();
- foreach (var message in Value.Values)
- {
- buffer.Add(message.GetBuffer());
- }
- return buffer;
- }
- public override string ToString()
- {
- var sb = new StringBuilder();
- sb.Append($"<Element type=\"{TransferType}\" count=\"{Value.Count}\">");
- foreach (var message in Value)
- {
- sb.Append($"{message.Value}");
- }
- sb.Append("</Element>");
- return sb.ToString();
- }
- }
- }
|