From 1c70bda35c1ac02188141d8da5a7e840b4aedae9 Mon Sep 17 00:00:00 2001 From: oneRain Date: Wed, 13 May 2020 14:07:35 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E5=B0=86=20json=20=E5=BA=8F=E5=88=97?= =?UTF-8?q?=E5=8C=96=E5=B7=A5=E5=85=B7=E6=8F=90=E5=87=BA=E6=9D=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Common/Common/Json/LCJsonConverter.cs | 37 ++++++++++++++++ .../Internal/LCLiveQueryHeartBeat.cs | 7 +++ LiveQuery/LiveQuery/LCQueryExtension.cs | 7 +++ Sample/LiveQueryApp/LiveQueryApp.csproj | 8 ++++ Sample/LiveQueryApp/Program.cs | 9 ++++ .../SingleThreadSynchronizationContext.cs | 43 +++++++++++++++++++ 6 files changed, 111 insertions(+) create mode 100644 Common/Common/Json/LCJsonConverter.cs create mode 100644 LiveQuery/LiveQuery/Internal/LCLiveQueryHeartBeat.cs create mode 100644 LiveQuery/LiveQuery/LCQueryExtension.cs create mode 100644 Sample/LiveQueryApp/LiveQueryApp.csproj create mode 100644 Sample/LiveQueryApp/Program.cs create mode 100644 Sample/LiveQueryApp/SingleThreadSynchronizationContext.cs diff --git a/Common/Common/Json/LCJsonConverter.cs b/Common/Common/Json/LCJsonConverter.cs new file mode 100644 index 0000000..3e78108 --- /dev/null +++ b/Common/Common/Json/LCJsonConverter.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using Newtonsoft.Json; + +namespace LeanCloud.Common { + public class LCJsonConverter : JsonConverter { + public override bool CanConvert(Type objectType) { + return objectType == typeof(object); + } + + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { + serializer.Serialize(writer, value); + } + + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { + if (reader.TokenType == JsonToken.StartObject) { + var obj = new Dictionary(); + serializer.Populate(reader, obj); + return obj; + } + if (reader.TokenType == JsonToken.StartArray) { + var arr = new List(); + serializer.Populate(reader, arr); + return arr; + } + if (reader.TokenType == JsonToken.Integer) { + if ((long)reader.Value < int.MaxValue) { + return Convert.ToInt32(reader.Value); + } + } + + return serializer.Deserialize(reader); + } + + public readonly static LCJsonConverter Default = new LCJsonConverter(); + } +} diff --git a/LiveQuery/LiveQuery/Internal/LCLiveQueryHeartBeat.cs b/LiveQuery/LiveQuery/Internal/LCLiveQueryHeartBeat.cs new file mode 100644 index 0000000..a856d91 --- /dev/null +++ b/LiveQuery/LiveQuery/Internal/LCLiveQueryHeartBeat.cs @@ -0,0 +1,7 @@ +using System; +namespace LiveQuery.Internal { + public class LCLiveQueryHeartBeat { + public LCLiveQueryHeartBeat() { + } + } +} diff --git a/LiveQuery/LiveQuery/LCQueryExtension.cs b/LiveQuery/LiveQuery/LCQueryExtension.cs new file mode 100644 index 0000000..5877435 --- /dev/null +++ b/LiveQuery/LiveQuery/LCQueryExtension.cs @@ -0,0 +1,7 @@ +using System; +namespace LiveQuery { + public class LCQueryExtension { + public LCQueryExtension() { + } + } +} diff --git a/Sample/LiveQueryApp/LiveQueryApp.csproj b/Sample/LiveQueryApp/LiveQueryApp.csproj new file mode 100644 index 0000000..c73e0d1 --- /dev/null +++ b/Sample/LiveQueryApp/LiveQueryApp.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.1 + + + diff --git a/Sample/LiveQueryApp/Program.cs b/Sample/LiveQueryApp/Program.cs new file mode 100644 index 0000000..2888d80 --- /dev/null +++ b/Sample/LiveQueryApp/Program.cs @@ -0,0 +1,9 @@ +using System; + +namespace LiveQueryApp { + class Program { + static void Main(string[] args) { + Console.WriteLine("Hello World!"); + } + } +} diff --git a/Sample/LiveQueryApp/SingleThreadSynchronizationContext.cs b/Sample/LiveQueryApp/SingleThreadSynchronizationContext.cs new file mode 100644 index 0000000..fdfb2c9 --- /dev/null +++ b/Sample/LiveQueryApp/SingleThreadSynchronizationContext.cs @@ -0,0 +1,43 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using System.Collections.Concurrent; +using System.Collections.Generic; + +namespace RealtimeApp { + /// + /// 单线程环境,用于控制台应用 await 返回 + /// + public class SingleThreadSynchronizationContext : SynchronizationContext { + private readonly BlockingCollection> queue = new BlockingCollection>(); + + public override void Post(SendOrPostCallback d, object state) { + queue.Add(new KeyValuePair(d, state)); + } + + public void RunOnCurrentThread() { + while (queue.TryTake(out KeyValuePair workItem, Timeout.Infinite)) { + workItem.Key(workItem.Value); + } + } + + public void Complete() { + queue.CompleteAdding(); + } + + public static void Run(Func func) { + SynchronizationContext prevContext = Current; + try { + SingleThreadSynchronizationContext syncContext = new SingleThreadSynchronizationContext(); + SetSynchronizationContext(syncContext); + + Task t = func(); + syncContext.RunOnCurrentThread(); + + t.GetAwaiter().GetResult(); + } finally { + SetSynchronizationContext(prevContext); + } + } + } +}