NewLife/X

新增二进制编码器作为默认编码器
大石头 编写于 2018-05-01 22:43:10
共计: 修改6个文件,增加173行、删除20行。
修改 +1 -0
修改 +2 -1
修改 +2 -1
增加 +165 -0
修改 +1 -10
修改 +2 -8
修改 +1 -0
diff --git a/NewLife.Core/NewLife.Core.csproj b/NewLife.Core/NewLife.Core.csproj
index 6195d02..81cc55c 100644
--- a/NewLife.Core/NewLife.Core.csproj
+++ b/NewLife.Core/NewLife.Core.csproj
@@ -175,6 +175,7 @@
     <Compile Include="Remoting\IApiServer.cs" />
     <Compile Include="Remoting\IApiSession.cs" />
     <Compile Include="Remoting\IEncoder.cs" />
+    <Compile Include="Remoting\BinaryEncoder.cs" />
     <Compile Include="Remoting\JsonEncoder.cs" />
     <Compile Include="Serialization\BinaryCodec2.cs" />
     <Compile Include="Serialization\JsonCodec2.cs" />
修改 +2 -1
diff --git a/NewLife.Core/Remoting/ApiClient.cs b/NewLife.Core/Remoting/ApiClient.cs
index 982c041..7d8d90a 100644
--- a/NewLife.Core/Remoting/ApiClient.cs
+++ b/NewLife.Core/Remoting/ApiClient.cs
@@ -60,7 +60,8 @@ namespace NewLife.Remoting
             var ct = Client;
             if (ct == null) throw new ArgumentNullException(nameof(Client), "未指定通信客户端");
 
-            if (Encoder == null) Encoder = new JsonEncoder();
+            //if (Encoder == null) Encoder = new JsonEncoder();
+            if (Encoder == null) Encoder = new BinaryEncoder();
             if (Handler == null) Handler = new ApiHandler { Host = this };
 
             Encoder.Log = EncoderLog;
修改 +2 -1
diff --git a/NewLife.Core/Remoting/ApiServer.cs b/NewLife.Core/Remoting/ApiServer.cs
index 34a907d..425e6a4 100644
--- a/NewLife.Core/Remoting/ApiServer.cs
+++ b/NewLife.Core/Remoting/ApiServer.cs
@@ -67,7 +67,8 @@ namespace NewLife.Remoting
         {
             if (Active) return;
 
-            if (Encoder == null) Encoder = new JsonEncoder();
+            //if (Encoder == null) Encoder = new JsonEncoder();
+            if (Encoder == null) Encoder = new BinaryEncoder();
             if (Handler == null) Handler = new ApiHandler { Host = this };
 
             Encoder.Log = EncoderLog;
增加 +165 -0
diff --git a/NewLife.Core/Remoting/BinaryEncoder.cs b/NewLife.Core/Remoting/BinaryEncoder.cs
new file mode 100644
index 0000000..0d0a328
--- /dev/null
+++ b/NewLife.Core/Remoting/BinaryEncoder.cs
@@ -0,0 +1,165 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Text;
+using NewLife.Data;
+using NewLife.Messaging;
+using NewLife.Serialization;
+
+namespace NewLife.Remoting
+{
+    /// <summary>二进制编码器</summary>
+    public class BinaryEncoder : EncoderBase, IEncoder
+    {
+        /// <summary>编码</summary>
+        public Encoding Encoding { get; set; } = Encoding.UTF8;
+
+        /// <summary>编码请求</summary>
+        /// <param name="action"></param>
+        /// <param name="args"></param>
+        /// <returns></returns>
+        public IMessage Encode(String action, Object args)
+        {
+            var ms = new MemoryStream();
+            ms.Seek(4, SeekOrigin.Begin);
+
+            // 写入服务名
+            var writer = new BinaryWriter(ms);
+            writer.Write(action);
+
+            // 写入参数
+            if (args != null)
+            {
+                if (args is Packet pk)
+                    pk.WriteTo(ms);
+                else
+                {
+                    var bn = new Binary { Stream = ms };
+                    bn.Write(args);
+                }
+            }
+
+            // 构造消息
+            var msg = new DefaultMessage
+            {
+                Payload = new Packet(ms.GetBuffer(), 4, (Int32)ms.Length - 4)
+            };
+
+            return msg;
+        }
+
+        /// <summary>编码响应</summary>
+        /// <param name="action"></param>
+        /// <param name="code"></param>
+        /// <param name="result"></param>
+        /// <returns></returns>
+        public Packet Encode(String action, Int32 code, Object result)
+        {
+            // 不支持序列化异常
+            if (result is Exception ex) result = ex.GetTrue()?.Message;
+
+            var ms = new MemoryStream();
+            ms.Seek(4, SeekOrigin.Begin);
+
+            // 写入服务名
+            var writer = new BinaryWriter(ms);
+            //writer.Write(action);
+            writer.Write(code);
+
+            // 写入参数
+            if (result != null)
+            {
+                if (result is Packet pk)
+                    pk.WriteTo(ms);
+                else
+                {
+                    var bn = new Binary { Stream = ms };
+                    bn.Write(result);
+                }
+            }
+
+            return new Packet(ms.GetBuffer(), 4, (Int32)ms.Length - 4);
+        }
+
+        /// <summary>解码请求</summary>
+        /// <param name="msg"></param>
+        /// <param name="action"></param>
+        /// <param name="args"></param>
+        /// <returns></returns>
+        public Boolean TryGetRequest(IMessage msg, out String action, out IDictionary<String, Object> args)
+        {
+            action = null;
+            args = null;
+
+            if (msg.Reply) return false;
+
+            // 读取服务名
+            var ms = msg.Payload.GetStream();
+            var reader = new BinaryReader(ms);
+            action = reader.ReadString();
+            if (action.IsNullOrEmpty()) return false;
+
+            // 读取参数
+            args = ms as IDictionary<String, Object>;
+
+            return true;
+        }
+
+        /// <summary>解码响应</summary>
+        /// <param name="msg"></param>
+        /// <param name="code"></param>
+        /// <param name="result"></param>
+        /// <returns></returns>
+        public Boolean TryGetResponse(IMessage msg, out Int32 code, out Object result)
+        {
+            code = 0;
+            result = null;
+
+            if (!msg.Reply) return false;
+
+            // 读取服务名
+            var ms = msg.Payload.GetStream();
+            var reader = new BinaryReader(ms);
+            code = reader.ReadInt32();
+
+            // 读取结果
+            result = ms;
+
+            return true;
+        }
+
+        /// <summary>转换为对象</summary>
+        /// <typeparam name="T"></typeparam>
+        /// <param name="obj"></param>
+        /// <returns></returns>
+        public T Convert<T>(Object obj) => (T)Convert(obj, typeof(T));
+
+        /// <summary>转换为目标类型</summary>
+        /// <param name="obj"></param>
+        /// <param name="targetType"></param>
+        /// <returns></returns>
+        public Object Convert(Object obj, Type targetType)
+        {
+            if (obj is Stream ms)
+            {
+                var bn = new Binary { Stream = ms, EncodeInt = true };
+                return bn.Read(targetType);
+            }
+
+            return JsonHelper.Default.Convert(obj, targetType);
+        }
+
+        //class Request
+        //{
+        //    public String Action;
+        //    public Object Args;
+        //}
+
+        //class Response
+        //{
+        //    public String Action;
+        //    public Int32 Code;
+        //    public Object Result;
+        //}
+    }
+}
\ No newline at end of file
修改 +1 -10
diff --git a/NewLife.Core/Serialization/Binary/BinaryComposite.cs b/NewLife.Core/Serialization/Binary/BinaryComposite.cs
index a3b3cca..9ee29e2 100644
--- a/NewLife.Core/Serialization/Binary/BinaryComposite.cs
+++ b/NewLife.Core/Serialization/Binary/BinaryComposite.cs
@@ -9,17 +9,8 @@ namespace NewLife.Serialization
     /// <summary>复合对象处理器</summary>
     public class BinaryComposite : BinaryHandlerBase
     {
-        ///// <summary>要忽略的成员</summary>
-        //public ICollection<String> IgnoreMembers { get; set; }
-
         /// <summary>实例化</summary>
-        public BinaryComposite()
-        {
-            Priority = 100;
-
-            //IgnoreMembers = new HashSet<String>(StringComparer.OrdinalIgnoreCase);
-            //IgnoreMembers = new HashSet<String>();
-        }
+        public BinaryComposite() => Priority = 100;
 
         /// <summary>写入对象</summary>
         /// <param name="value">目标对象</param>
修改 +2 -8
diff --git a/NewLife.Core/Serialization/Interface/IFormatterX.cs b/NewLife.Core/Serialization/Interface/IFormatterX.cs
index 2fc931b..4dafe06 100644
--- a/NewLife.Core/Serialization/Interface/IFormatterX.cs
+++ b/NewLife.Core/Serialization/Interface/IFormatterX.cs
@@ -136,10 +136,7 @@ namespace NewLife.Serialization
         /// <summary>输出日志</summary>
         /// <param name="format"></param>
         /// <param name="args"></param>
-        public virtual void WriteLog(String format, params Object[] args)
-        {
-            Log?.Info(format, args);
-        }
+        public virtual void WriteLog(String format, params Object[] args) => Log?.Info(format, args);
         #endregion
     }
 
@@ -175,9 +172,6 @@ namespace NewLife.Serialization
         /// <summary>输出日志</summary>
         /// <param name="format"></param>
         /// <param name="args"></param>
-        public void WriteLog(String format, params Object[] args)
-        {
-            Host.Log.Info(format, args);
-        }
+        public void WriteLog(String format, params Object[] args) => Host.Log.Info(format, args);
     }
 }
\ No newline at end of file