From 2f58cdfe50376a8e5988189fe95af9a3b80562ba Mon Sep 17 00:00:00 2001 From: oneRain Date: Tue, 10 Mar 2020 16:54:50 +0800 Subject: [PATCH] rename --- .../{AppRouter.cs => LCAppRouter.cs} | 33 +++++++++++++------ .../{AppServer.cs => LCAppServer.cs} | 12 +++---- Common/Http/{HttpUtils.cs => LCHttpUtils.cs} | 6 ++-- .../{JsonExtensions.cs => LCJsonUtils.cs} | 2 +- Common/Log/{LogLevel.cs => LCLogLevel.cs} | 2 +- Common/Log/{Logger.cs => LCLogger.cs} | 16 ++++----- ...{TaskExtensions.cs => LCTaskExtensions.cs} | 2 +- 7 files changed, 43 insertions(+), 30 deletions(-) rename Common/AppRouter/{AppRouter.cs => LCAppRouter.cs} (69%) rename Common/AppRouter/{AppServer.cs => LCAppServer.cs} (80%) rename Common/Http/{HttpUtils.cs => LCHttpUtils.cs} (93%) rename Common/Json/{JsonExtensions.cs => LCJsonUtils.cs} (96%) rename Common/Log/{LogLevel.cs => LCLogLevel.cs} (92%) rename Common/Log/{Logger.cs => LCLogger.cs} (58%) rename Common/Task/{TaskExtensions.cs => LCTaskExtensions.cs} (99%) diff --git a/Common/AppRouter/AppRouter.cs b/Common/AppRouter/LCAppRouter.cs similarity index 69% rename from Common/AppRouter/AppRouter.cs rename to Common/AppRouter/LCAppRouter.cs index a46ed81..523b465 100644 --- a/Common/AppRouter/AppRouter.cs +++ b/Common/AppRouter/LCAppRouter.cs @@ -5,14 +5,14 @@ using System.Collections.Generic; using Newtonsoft.Json; namespace LeanCloud.Common { - public class AppRouter { + public class LCAppRouter { private readonly string appId; private readonly string server; - private AppServer appServer; + private LCAppServer appServer; - public AppRouter(string appId, string server) { + public LCAppRouter(string appId, string server) { if (!IsInternalApp(appId) && string.IsNullOrEmpty(server)) { // 国内节点必须配置自定义域名 throw new Exception("Please init with your server url."); @@ -26,36 +26,49 @@ namespace LeanCloud.Common { if (!string.IsNullOrEmpty(server)) { return server; } + LCAppServer appServ = await FetchAppServer(); + return appServ.ApiServer; + } + + public async Task GetRealtimeServer() { + if (!string.IsNullOrEmpty(server)) { + return server; + } + LCAppServer appServ = await FetchAppServer(); + return appServ.PushServer; + } + + async Task FetchAppServer() { // 判断节点地区 if (!IsInternalApp(appId)) { // 国内节点必须配置自定义域名 throw new Exception("Please init with your server url."); } // 向 App Router 请求地址 - if (appServer == null || appServer.IsExpired) { + if (appServer == null || !appServer.IsValid) { try { HttpRequestMessage request = new HttpRequestMessage { RequestUri = new Uri($"https://app-router.com/2/route?appId={appId}"), Method = HttpMethod.Get }; HttpClient client = new HttpClient(); - HttpUtils.PrintRequest(client, request); + LCHttpUtils.PrintRequest(client, request); HttpResponseMessage response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead); request.Dispose(); string resultString = await response.Content.ReadAsStringAsync(); response.Dispose(); - HttpUtils.PrintResponse(response, resultString); + LCHttpUtils.PrintResponse(response, resultString); Dictionary data = JsonConvert.DeserializeObject>(resultString); - appServer = new AppServer(data); + appServer = new LCAppServer(data); } catch (Exception e) { - Logger.Error(e.Message); + LCLogger.Error(e.Message); // 拉取服务地址失败后,使用国际节点的默认服务地址 - appServer = AppServer.GetInternalFallbackAppServer(appId); + appServer = LCAppServer.GetInternalFallbackAppServer(appId); } } - return appServer.ApiServer; + return appServer; } private static bool IsInternalApp(string appId) { diff --git a/Common/AppRouter/AppServer.cs b/Common/AppRouter/LCAppServer.cs similarity index 80% rename from Common/AppRouter/AppServer.cs rename to Common/AppRouter/LCAppServer.cs index 0ae29ae..7f4e3ec 100644 --- a/Common/AppRouter/AppServer.cs +++ b/Common/AppRouter/LCAppServer.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; namespace LeanCloud.Common { - public class AppServer { + public class LCAppServer { public string ApiServer { get; private set; } @@ -19,9 +19,9 @@ namespace LeanCloud.Common { get; private set; } - public bool IsExpired { + public bool IsValid { get { - return ttl != -1 && DateTime.Now > expiredAt; + return ttl != -1 || DateTime.Now < expiredAt; } } @@ -29,7 +29,7 @@ namespace LeanCloud.Common { private readonly int ttl; - public AppServer(Dictionary data) { + public LCAppServer(Dictionary data) { ApiServer = GetUrlWithScheme(data["api_server"] as string); PushServer = GetUrlWithScheme(data["push_server"] as string); EngineServer = GetUrlWithScheme(data["engine_server"] as string); @@ -41,9 +41,9 @@ namespace LeanCloud.Common { return url.StartsWith("https://") ? url : $"https://{url}"; } - internal static AppServer GetInternalFallbackAppServer(string appId) { + internal static LCAppServer GetInternalFallbackAppServer(string appId) { string prefix = appId.Substring(0, 8).ToLower(); - return new AppServer(new Dictionary { + return new LCAppServer(new Dictionary { { "api_server", $"https://{prefix}.api.lncldglobal.com" }, { "push_server", $"https://{prefix}.engine.lncldglobal.com" }, { "engine_server", $"https://{prefix}.push.lncldglobal.com" }, diff --git a/Common/Http/HttpUtils.cs b/Common/Http/LCHttpUtils.cs similarity index 93% rename from Common/Http/HttpUtils.cs rename to Common/Http/LCHttpUtils.cs index 7edaa6e..40fd13f 100644 --- a/Common/Http/HttpUtils.cs +++ b/Common/Http/LCHttpUtils.cs @@ -3,7 +3,7 @@ using System.Text; using System.Net.Http; namespace LeanCloud.Common { - public static class HttpUtils { + public static class LCHttpUtils { public static void PrintRequest(HttpClient client, HttpRequestMessage request, string content = null) { if (client == null) { return; @@ -31,7 +31,7 @@ namespace LeanCloud.Common { sb.AppendLine($"Content: {content}"); } sb.AppendLine("=== HTTP Request End ==="); - Logger.Debug(sb.ToString()); + LCLogger.Debug(sb.ToString()); } public static void PrintResponse(HttpResponseMessage response, string content = null) { @@ -43,7 +43,7 @@ namespace LeanCloud.Common { sb.AppendLine($"Content: {content}"); } sb.AppendLine("=== HTTP Response End ==="); - Logger.Debug(sb.ToString()); + LCLogger.Debug(sb.ToString()); } } } diff --git a/Common/Json/JsonExtensions.cs b/Common/Json/LCJsonUtils.cs similarity index 96% rename from Common/Json/JsonExtensions.cs rename to Common/Json/LCJsonUtils.cs index d0a131b..25ec9fd 100644 --- a/Common/Json/JsonExtensions.cs +++ b/Common/Json/LCJsonUtils.cs @@ -5,7 +5,7 @@ namespace LeanCloud.Common { /// /// 为 Json 解析提供异步接口 /// - public static class JsonUtils { + public static class LCJsonUtils { public static async Task SerializeObjectAsync(object obj) { string str = null; await Task.Run(() => { diff --git a/Common/Log/LogLevel.cs b/Common/Log/LCLogLevel.cs similarity index 92% rename from Common/Log/LogLevel.cs rename to Common/Log/LCLogLevel.cs index 5d9344c..fefcd9d 100644 --- a/Common/Log/LogLevel.cs +++ b/Common/Log/LCLogLevel.cs @@ -2,7 +2,7 @@ /// /// 日志级别 /// - public enum LogLevel { + public enum LCLogLevel { /// /// 调试级别 /// diff --git a/Common/Log/Logger.cs b/Common/Log/LCLogger.cs similarity index 58% rename from Common/Log/Logger.cs rename to Common/Log/LCLogger.cs index 4e499d5..63b917e 100644 --- a/Common/Log/Logger.cs +++ b/Common/Log/LCLogger.cs @@ -4,37 +4,37 @@ namespace LeanCloud.Common { /// /// 日志类 /// - public static class Logger { + public static class LCLogger { /// /// 日志回调接口,方便开发者调试 /// /// The log delegate. - public static Action LogDelegate { + public static Action LogDelegate { get; set; } public static void Debug(string log) { - LogDelegate?.Invoke(LogLevel.Debug, log); + LogDelegate?.Invoke(LCLogLevel.Debug, log); } public static void Debug(string format, params object[] args) { - LogDelegate?.Invoke(LogLevel.Debug, string.Format(format, args)); + LogDelegate?.Invoke(LCLogLevel.Debug, string.Format(format, args)); } public static void Warn(string log) { - LogDelegate?.Invoke(LogLevel.Warn, log); + LogDelegate?.Invoke(LCLogLevel.Warn, log); } public static void Warn(string format, params object[] args) { - LogDelegate?.Invoke(LogLevel.Warn, string.Format(format, args)); + LogDelegate?.Invoke(LCLogLevel.Warn, string.Format(format, args)); } public static void Error(string log) { - LogDelegate?.Invoke(LogLevel.Error, log); + LogDelegate?.Invoke(LCLogLevel.Error, log); } public static void Error(string format, params object[] args) { - LogDelegate?.Invoke(LogLevel.Error, string.Format(format, args)); + LogDelegate?.Invoke(LCLogLevel.Error, string.Format(format, args)); } } } diff --git a/Common/Task/TaskExtensions.cs b/Common/Task/LCTaskExtensions.cs similarity index 99% rename from Common/Task/TaskExtensions.cs rename to Common/Task/LCTaskExtensions.cs index 2137ec5..bd0b0cb 100644 --- a/Common/Task/TaskExtensions.cs +++ b/Common/Task/LCTaskExtensions.cs @@ -10,7 +10,7 @@ namespace LeanCloud.Common { /// /// Provides helper methods that allow us to use terser code elsewhere. /// - public static class TaskExtensions { + public static class LCTaskExtensions { /// /// Ensures a task (even null) is awaitable. ///