Mercurial > hg > Database > Christie-sharp
view datagear/dg/MessagePackDataGear.cs @ 23:46cfeb0609c5
Add TcpConnections
author | riono <e165729@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 15 Dec 2020 22:11:40 +0900 |
parents | e6f5b7d14dd1 |
children | 1236da135f79 |
line wrap: on
line source
using System; using MessagePack; namespace Christie_net.datagear.dg { public class MessagePackDataGear<T> : DataGear<T> { private int dataSize; private byte[] messagePack; private readonly object syncObject = new object(); public MessagePackDataGear(T data) : base(data) { GetMessagePack(); } public MessagePackDataGear(Type clazz) : base(clazz) { } public MessagePackDataGear(byte[] messagePack, Type clazz) : base(clazz) { this.messagePack = messagePack; } public byte[] GetMessagePack() { if (messagePack != null) return messagePack; try { messagePack = MessagePackSerializer.Serialize(data); SetDataSize(messagePack.Length); } catch (Exception e) { Console.WriteLine(e.StackTrace); } return messagePack; } public T GetData() { lock (syncObject) { if (data == null) try { SetData(MessagePackSerializer.Deserialize<T>(messagePack)); } catch (Exception e) { Console.WriteLine(e.StackTrace); } return base.GetData(); } } public void SetDataSize(int dataSize) { this.dataSize = dataSize; } } }