瀏覽代碼

1.修改Google.Protobuf ByteString代码,可以直接获取里面的byte[],减少GC
2.帧同步demo消息去掉GC

tanghai 7 年之前
父節點
當前提交
5096c27571

+ 8 - 0
Server/Model/Base/Helper/MongoHelper.cs

@@ -66,6 +66,14 @@ namespace ETModel
 			}
 		}
 		
+		public static object FromBson(object instance, byte[] bytes, int index, int count)
+		{
+			using (MemoryStream memoryStream = new MemoryStream(bytes, index, count))
+			{
+				return BsonSerializer.Deserialize(memoryStream, instance.GetType());
+			}
+		}
+		
 		public static object FromBson(object instance, Stream stream)
 		{
 			return BsonSerializer.Deserialize(stream, instance.GetType());

+ 5 - 0
Server/Model/Module/Message/MongoPacker.cs

@@ -15,6 +15,11 @@ namespace ETModel
 			return MongoHelper.FromBson(type, bytes, index, count);
 		}
 
+		public object DeserializeFrom(object instance, byte[] bytes, int index, int count)
+		{
+			return MongoHelper.FromBson(instance, bytes, index, count);
+		}
+
 		public object DeserializeFrom(Type type, MemoryStream stream)
 		{
 			return MongoHelper.FromStream(type, stream);

+ 5 - 3
Unity/Assets/Scripts/Module/FrameSync/ClientFrameComponent.cs

@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using Google.Protobuf;
 
 namespace ETModel
 {
@@ -50,10 +51,11 @@ namespace ETModel
 
 				Session session = sessionFrameMessage.Session;
 				OpcodeTypeComponent opcodeTypeComponent = session.Network.Entity.GetComponent<OpcodeTypeComponent>();
-	            Type type = opcodeTypeComponent.GetType((ushort)oneFrameMessage.Op);
+	            ushort opcode = (ushort) oneFrameMessage.Op;
+	            object instance = opcodeTypeComponent.GetInstance(opcode);
 
-	            byte[] bytes = oneFrameMessage.AMessage.ToByteArray();
-	            IMessage message = (IMessage)session.Network.MessagePacker.DeserializeFrom(type, bytes, 0, bytes.Length);
+	            byte[] bytes = ByteString.Unsafe.GetBuffer(oneFrameMessage.AMessage);
+	            IMessage message = (IMessage)session.Network.MessagePacker.DeserializeFrom(instance, bytes, 0, bytes.Length);
                 Game.Scene.GetComponent<MessageDispatherComponent>().Handle(sessionFrameMessage.Session, new MessageInfo((ushort)oneFrameMessage.Op, message));
             }
         }

+ 1 - 0
Unity/Assets/Scripts/Module/Message/IMessagePacker.cs

@@ -7,6 +7,7 @@ namespace ETModel
 	{
 		byte[] SerializeToByteArray(object obj);
 		object DeserializeFrom(Type type, byte[] bytes, int index, int count);
+		object DeserializeFrom(object instance, byte[] bytes, int index, int count);
 		object DeserializeFrom(Type type, MemoryStream stream);
 		object DeserializeFrom(object instance, MemoryStream stream);
 	}

+ 13 - 0
Unity/Assets/Scripts/Module/Message/ProtobufHelper.cs

@@ -25,6 +25,19 @@ namespace ETModel
 			return message;
 		}
 		
+		public static object FromBytes(object instance, byte[] bytes, int index, int count)
+		{
+			object message = instance;
+			((Google.Protobuf.IMessage)message).MergeFrom(bytes, index, count);
+			ISupportInitialize iSupportInitialize = message as ISupportInitialize;
+			if (iSupportInitialize == null)
+			{
+				return message;
+			}
+			iSupportInitialize.EndInit();
+			return message;
+		}
+		
 		public static object FromStream(Type type, MemoryStream stream)
 		{
 			object message = Activator.CreateInstance(type);

+ 5 - 0
Unity/Assets/Scripts/Module/Message/ProtobufPacker.cs

@@ -15,6 +15,11 @@ namespace ETModel
 			return ProtobufHelper.FromBytes(type, bytes, index, count);
 		}
 
+		public object DeserializeFrom(object instance, byte[] bytes, int index, int count)
+		{
+			return ProtobufHelper.FromBytes(instance, bytes, index, count);
+		}
+
 		public object DeserializeFrom(Type type, MemoryStream stream)
 		{
 			return ProtobufHelper.FromStream(type, stream);

+ 3 - 3
Unity/Assets/ThirdParty/Google.Protobuf/ByteString.cs

@@ -53,13 +53,13 @@ namespace Google.Protobuf
         /// <summary>
         /// Unsafe operations that can cause IO Failure and/or other catestrophic side-effects.
         /// </summary>
-        internal static class Unsafe
+        public static class Unsafe
         {
             /// <summary>
             /// Constructs a new ByteString from the given byte array. The array is
             /// *not* copied, and must not be modified after this constructor is called.
             /// </summary>
-            internal static ByteString FromBytes(byte[] bytes)
+            public static ByteString FromBytes(byte[] bytes)
             {
                 return new ByteString(bytes);
             }
@@ -68,7 +68,7 @@ namespace Google.Protobuf
             /// Provides direct, unrestricted access to the bytes contained in this instance.
             /// You must not modify or resize the byte array returned by this method.
             /// </summary>
-            internal static byte[] GetBuffer(ByteString bytes)
+            public static byte[] GetBuffer(ByteString bytes)
             {
                 return bytes.bytes;
             }

+ 1 - 1
Unity/Hotfix/Module/FrameSync/OperaComponent.cs

@@ -56,7 +56,7 @@ namespace ETHotfix
 		    {
 			    M2C_TestActorResponse response = (M2C_TestActorResponse)await SessionComponent.Instance.Session.Call(
 						new C2M_TestActorRequest() { Info = "actor rpc request" });
-			    Log.Info(response.Info);
+			    //Log.Info(response.Info);
 			}
 		    catch (Exception e)
 		    {