Mercurial > hg > Database > Christie-sharp
changeset 3:f27db91a852e
update MessagePackDagaGear.cs
author | riono <e165729@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 23 Jun 2020 16:03:32 +0900 |
parents | 839e355f92ed |
children | a6f7466473f5 |
files | Christie_net.csproj datagear/dg/DataGear.cs datagear/dg/MessagePackDataGear.cs |
diffstat | 3 files changed, 52 insertions(+), 17 deletions(-) [+] |
line wrap: on
line diff
--- a/Christie_net.csproj Sun Jun 21 15:43:48 2020 +0900 +++ b/Christie_net.csproj Tue Jun 23 16:03:32 2020 +0900 @@ -5,4 +5,8 @@ <TargetFramework>netcoreapp3.1</TargetFramework> </PropertyGroup> + <ItemGroup> + <PackageReference Include="MessagePack" Version="2.1.143" /> + </ItemGroup> + </Project>
--- a/datagear/dg/DataGear.cs Sun Jun 21 15:43:48 2020 +0900 +++ b/datagear/dg/DataGear.cs Tue Jun 23 16:03:32 2020 +0900 @@ -2,7 +2,7 @@ namespace Christie_net.datagear.dg { public class DataGear<T> { - protected T data = default(T); + protected T data; protected Type clazz = null; public DataGear(T data) { @@ -45,5 +45,17 @@ public void SetClazz(Type clazz) { this.clazz = clazz; } + + public Type GetClazz() { + return clazz; + } + + public T GetData() { + return data; + } + + public override string ToString() { + return "DataGear : classname " + clazz; + } } } \ No newline at end of file
--- a/datagear/dg/MessagePackDataGear.cs Sun Jun 21 15:43:48 2020 +0900 +++ b/datagear/dg/MessagePackDataGear.cs Tue Jun 23 16:03:32 2020 +0900 @@ -1,11 +1,11 @@ using System; +using MessagePack; namespace Christie_net.datagear.dg { public class MessagePackDataGear<T> : DataGear<T> { private byte[] messagePack = null; private int dataSize; - //private byte[] messagepack = - private + private object syncObject = new object(); public MessagePackDataGear(T data) : base(data){ GetMessagePack(); @@ -14,19 +14,38 @@ public MessagePackDataGear(Type clazz) : base(clazz) { } - // public byte[] GetMessagePack() { - // if (messagePack != null) { - // return messagePack; - // } - // else { - // try { - // messagePack = - // } - // catch (Exception e) { - // Console.WriteLine(e); - // throw; - // } - // } - // } + public byte[] GetMessagePack() { + if (messagePack != null) { + return messagePack; + } + else { + 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; + } } } \ No newline at end of file