ntminer 5 anni fa
parent
commit
4cb3b9868b

+ 1 - 6
src/NTMinerClient/Core/Profiles/Impl/GpuProfileSet.cs

@@ -195,12 +195,7 @@ namespace NTMiner.Core.Profiles.Impl {
                 if (!_isInited) {
                     string json = SpecialPath.ReadGpuProfilesJsonFile();
                     if (!string.IsNullOrEmpty(json)) {
-                        GpuProfilesJsonDb data = null;
-                        try {
-                            data = VirtualRoot.JsonSerializer.Deserialize<GpuProfilesJsonDb>(json);
-                        }
-                        catch {
-                        }
+                        GpuProfilesJsonDb data = VirtualRoot.JsonSerializer.Deserialize<GpuProfilesJsonDb>(json);
                         if (data != null) {
                             _data = data;
                         }

+ 3 - 8
src/NTMinerClient/NTMinerRoot.partials.static.cs

@@ -205,13 +205,8 @@ namespace NTMiner {
                     if (!_localJsonInited) {
                         string localJson = SpecialPath.ReadLocalJsonFile();
                         if (!string.IsNullOrEmpty(localJson)) {
-                            try {
-                                LocalJsonDb data = VirtualRoot.JsonSerializer.Deserialize<LocalJsonDb>(localJson);
-                                _localJson = data ?? new LocalJsonDb();
-                            }
-                            catch (Exception e) {
-                                Logger.ErrorDebugLine(e);
-                            }
+                            LocalJsonDb data = VirtualRoot.JsonSerializer.Deserialize<LocalJsonDb>(localJson);
+                            _localJson = data ?? new LocalJsonDb();
                         }
                         else {
                             _localJson = new LocalJsonDb();
@@ -267,8 +262,8 @@ namespace NTMiner {
                     if (!_serverJsonInited) {
                         string serverJson = SpecialPath.ReadServerJsonFile();
                         if (!string.IsNullOrEmpty(serverJson)) {
+                            ServerJsonDb data = VirtualRoot.JsonSerializer.Deserialize<ServerJsonDb>(serverJson) ?? new ServerJsonDb();
                             try {
-                                ServerJsonDb data = VirtualRoot.JsonSerializer.Deserialize<ServerJsonDb>(serverJson);
                                 _serverJson = data;
                                 if (KernelBrandId != Guid.Empty) {
                                     var kernelToRemoves = data.Kernels.Where(a => a.BrandId != KernelBrandId).ToArray();

+ 1 - 12
src/NTMinerDataSchemas/WsMessage.cs

@@ -5,17 +5,6 @@ namespace NTMiner {
     public class WsMessage : Dictionary<string, Object> {
         public WsMessage() { }
 
-        public WsMessage SetMessageId(string value) {
-            base["messageId"] = value;
-            return this;
-        }
-        public string GetMessageId() {
-            if (base.TryGetValue("messageId", out object obj) && obj != null) {
-                return obj.ToString();
-            }
-            return string.Empty;
-        }
-
         public WsMessage SetAction(string value) {
             base["action"] = value;
             return this;
@@ -65,7 +54,7 @@ namespace NTMiner {
             return this;
         }
         public object GetData() {
-            if (base.TryGetValue("des", out object obj)) {
+            if (base.TryGetValue("data", out object obj)) {
                 return obj;
             }
             return null;

+ 1 - 6
src/NTMinerRpcClient/KernelOutputKeyword/KernelOutputKeywordSet.cs

@@ -158,12 +158,7 @@ namespace NTMiner.KernelOutputKeyword {
                         using (MemoryStream ms = new MemoryStream()) {
                             db.FileStorage.Download(GetFileId(), ms);
                             var json = Encoding.UTF8.GetString(ms.ToArray());
-                            try {
-                                return VirtualRoot.JsonSerializer.Deserialize<List<KernelOutputKeywordData>>(json);
-                            }
-                            catch {
-                                return new List<KernelOutputKeywordData>();
-                            }
+                            return VirtualRoot.JsonSerializer.Deserialize<List<KernelOutputKeywordData>>(json) ?? new List<KernelOutputKeywordData>();
                         }
                     }
                     else {

+ 1 - 7
src/NTMinerRpcClient/Services/Client/NTMinerDaemonService.cs

@@ -50,13 +50,7 @@ namespace NTMiner.Services.Client {
 
         public void GetGpuProfilesJsonAsync(string clientIp, Action<GpuProfilesJsonDb, Exception> callback) {
             RpcRoot.PostAsync(clientIp, NTKeyword.NTMinerDaemonPort, _controllerName, nameof(INTMinerDaemonController.GetGpuProfilesJson), null, (string json, Exception e) => {
-                GpuProfilesJsonDb data;
-                try {
-                    data = VirtualRoot.JsonSerializer.Deserialize<GpuProfilesJsonDb>(json);
-                }
-                catch {
-                    data = new GpuProfilesJsonDb();
-                }
+                GpuProfilesJsonDb data = VirtualRoot.JsonSerializer.Deserialize<GpuProfilesJsonDb>(json) ?? new GpuProfilesJsonDb();
                 callback?.Invoke(data, null);
             }, timeountMilliseconds: 3000);
         }

+ 6 - 0
src/NTMinerlib/Serialization/INTSerializer.cs

@@ -2,6 +2,12 @@
     public interface INTSerializer {
         string Serialize<TObject>(TObject obj);
 
+        /// <summary>
+        /// 不会抛出异常,如果格式不正确返回default
+        /// </summary>
+        /// <typeparam name="TObject"></typeparam>
+        /// <param name="json"></param>
+        /// <returns></returns>
         TObject Deserialize<TObject>(string json);
     }
 }

+ 6 - 1
src/NTMinerlib/Serialization/NTJsonSerializer.cs

@@ -15,7 +15,12 @@ namespace NTMiner.Serialization {
         }
 
         public virtual TObject Deserialize<TObject>(string json) {
-            return JsonConvert.DeserializeObject<TObject>(json, jsonSerializerSettings);
+            try {
+                return JsonConvert.DeserializeObject<TObject>(json, jsonSerializerSettings);
+            }
+            catch {
+                return default;
+            }
         }
     }
 }

+ 1 - 1
src/UnitTests/JsonSerializerTests.cs

@@ -19,7 +19,7 @@ namespace UnitTests {
                 raiseException = true;
             }
             finally {
-                Assert.IsTrue(raiseException);
+                Assert.IsFalse(raiseException);
             }
         }
     }

+ 0 - 22
src/UnitTests/JsonTests.cs

@@ -1,22 +0,0 @@
-using Microsoft.VisualStudio.TestTools.UnitTesting;
-using Newtonsoft.Json;
-using NTMiner;
-using NTMiner.JsonDb;
-
-namespace UnitTests {
-    [TestClass]
-    public class JsonTests {
-        [TestMethod]
-        public void TestMethod1() {
-            GpuProfilesJsonDb data = VirtualRoot.JsonSerializer.Deserialize<GpuProfilesJsonDb>("");
-            Assert.IsNull(data); 
-        }
-
-        [TestMethod]
-        [ExpectedException(typeof(JsonReaderException))]
-        public void TestMethod2() {
-            var data = VirtualRoot.JsonSerializer.Deserialize<GpuProfilesJsonDb>("sss");
-            Assert.IsNull(data);
-        }
-    }
-}

+ 0 - 1
src/UnitTests/UnitTests.csproj

@@ -80,7 +80,6 @@
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="IPTests.cs" />
     <Compile Include="MarshalTests.cs" />
-    <Compile Include="JsonTests.cs" />
     <Compile Include="RegexTests.cs" />
     <Compile Include="TimingTests.cs" />
     <Compile Include="RemoteDesktopTests.cs" />

+ 2 - 7
src/WebSocketClientTests/Program.cs

@@ -15,19 +15,14 @@ namespace NTMiner {
                     if (string.IsNullOrEmpty(e.Data) || e.Data[0] != '{' || e.Data[e.Data.Length - 1] != '}') {
                         return;
                     }
-                    WsMessage message = null;
-                    try {
-                        message = VirtualRoot.JsonSerializer.Deserialize<WsMessage>(e.Data);
-                    }
-                    catch {
-                    }
+                    WsMessage message = VirtualRoot.JsonSerializer.Deserialize<WsMessage>(e.Data);
                     if (message == null) {
                         return;
                     }
                     switch (message.GetAction()) {
                         case GetSpeedWsCommand.Action:
                             ws.SendAsync(new WsMessage().SetAction(GetSpeedWsCommand.Result)
-                                .SetMessageId(message.GetMessageId()).SetCode(200).SetPhrase("Ok").SetDes("成功")
+                                .SetCode(200).SetPhrase("Ok").SetDes("成功")
                                 .SetData(new Dictionary<string, object> {
                                         {"str", "hello" },
                                         {"num", 111 },

+ 1 - 6
src/WebSocketServerTests/AllInOne.cs

@@ -44,12 +44,7 @@ namespace NTMiner {
             if (string.IsNullOrEmpty(e.Data) || e.Data[0] != '{' || e.Data[e.Data.Length - 1] != '}') {
                 return;
             }
-            WsMessage message = null;
-            try {
-                message = VirtualRoot.JsonSerializer.Deserialize<WsMessage>(e.Data);
-            }
-            catch {
-            }
+            WsMessage message = VirtualRoot.JsonSerializer.Deserialize<WsMessage>(e.Data);
             if (message == null) {
                 return;
             }

+ 0 - 6
src/WebSocketServerTests/client.html

@@ -61,12 +61,6 @@
                 return ws;
             }
 
-            function guid() {
-                return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
-                    var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
-                    return v.toString(16);
-                });
-            }
             function send(e) {
                 e.preventDefault();
                 var val = input.value;