Selaa lähdekoodia

去掉IMessagePacker,根据opcode来选择序列化方式

tanghai 5 vuotta sitten
vanhempi
commit
22c71a76ed

+ 0 - 3
Server/Hotfix/Module/Message/NetInnerComponentSystem.cs

@@ -8,7 +8,6 @@ namespace ET
 		{
 			NetInnerComponent.Instance = self;
 			self.Awake(NetworkProtocol.TCP);
-			self.MessagePacker = new MongoPacker();
 			self.MessageDispatcher = new InnerMessageDispatcher();
 		}
 	}
@@ -19,7 +18,6 @@ namespace ET
 		{
 			NetInnerComponent.Instance = self;
 			self.Awake(NetworkProtocol.TCP, a);
-			self.MessagePacker = new MongoPacker();
 			self.MessageDispatcher = new InnerMessageDispatcher();
 		}
 	}
@@ -28,7 +26,6 @@ namespace ET
 	{
 		public override void Load(NetInnerComponent self)
 		{
-			self.MessagePacker = new MongoPacker();
 			self.MessageDispatcher = new InnerMessageDispatcher();
 		}
 	}

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

@@ -1,38 +0,0 @@
-using System;
-using System.IO;
-
-namespace ET
-{
-	public class MongoPacker: IMessagePacker
-	{
-		public byte[] SerializeTo(object obj)
-		{
-			return MongoHelper.ToBson(obj);
-		}
-
-		public void SerializeTo(object obj, MemoryStream stream)
-		{
-			MongoHelper.ToBson(obj, stream);
-		}
-
-		public object DeserializeFrom(Type type, byte[] bytes, int index, int count)
-		{
-			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);
-		}
-
-		public object DeserializeFrom(object instance, MemoryStream stream)
-		{
-			return MongoHelper.FromBson(instance, stream);
-		}
-	}
-}

+ 2 - 5
Server/Model/Server.Model.csproj

@@ -173,8 +173,8 @@
     <Compile Include="..\..\Unity\Assets\Model\Module\CoroutineLock\CoroutineLockType.cs">
       <Link>Module\CoroutineLock\CoroutineLockType.cs</Link>
     </Compile>
-    <Compile Include="..\..\Unity\Assets\Model\Module\Message\MessagePool.cs">
-      <Link>Module\Message\MessagePool\ETModel\MessagePool.cs</Link>
+    <Compile Include="..\..\Unity\Assets\Model\Module\Message\MessagePackHelper.cs">
+      <Link>Module\Message\MessagePackHelper.cs</Link>
     </Compile>
     <Compile Include="..\..\Unity\Assets\Model\Module\Message\NetOuterComponent.cs">
       <Link>Module\Message\NetOuterComponent.cs</Link>
@@ -225,7 +225,6 @@
     <Compile Include="..\..\Unity\Assets\Model\Module\Message\ErrorCode.cs" Link="Module\Message\ErrorCode.cs" />
     <Compile Include="..\..\Unity\Assets\Model\Module\Message\IMessage.cs" Link="Module\Message\IMessage.cs" />
     <Compile Include="..\..\Unity\Assets\Model\Module\Message\IMessageDispatcher.cs" Link="Module\Message\IMessageDispatcher.cs" />
-    <Compile Include="..\..\Unity\Assets\Model\Module\Message\IMessagePacker.cs" Link="Module\Message\IMessagePacker.cs" />
     <Compile Include="..\..\Unity\Assets\Model\Module\Message\IMHandler.cs" Link="Module\Message\IMHandler.cs" />
     <Compile Include="..\..\Unity\Assets\Model\Module\Message\MessageAttribute.cs" Link="Module\Message\MessageAttribute.cs" />
     <Compile Include="..\..\Unity\Assets\Model\Module\Message\MessageHandlerAttribute.cs" Link="Module\Message\MessageHandlerAttribute.cs" />
@@ -246,7 +245,6 @@
     <Compile Include="..\..\Unity\Assets\Model\Module\Message\OuterMessage.cs" Link="Module\Message\OuterMessage.cs" />
     <Compile Include="..\..\Unity\Assets\Model\Module\Message\OuterOpcode.cs" Link="Module\Message\OuterOpcode.cs" />
     <Compile Include="..\..\Unity\Assets\Model\Module\Message\ProtobufHelper.cs" Link="Module\Message\ProtobufHelper.cs" />
-    <Compile Include="..\..\Unity\Assets\Model\Module\Message\ProtobufPacker.cs" Link="Module\Message\ProtobufPacker.cs" />
     <Compile Include="..\..\Unity\Assets\Model\Module\Message\RpcException.cs" Link="Module\Message\RpcException.cs" />
     <Compile Include="..\..\Unity\Assets\Model\Module\Message\Session.cs" Link="Module\Message\Session.cs" />
     <Compile Include="..\..\Unity\Assets\Model\Module\Message\SessionIdleCheckComponent.cs">
@@ -281,7 +279,6 @@
     <Folder Include="Base\RecyclableMemoryStream\" />
     <Folder Include="Module\Message\Network\KCP\" />
     <Folder Include="Module\Message\Network\TCP\" />
-    <Folder Include="Module\Message\MessagePool\" />
   </ItemGroup>
   <ItemGroup>
     <Reference Include="Pathfinding.Ionic.Zip.Reduced, Version=1.9.1.9000, Culture=neutral, PublicKeyToken=null">

+ 0 - 3
Unity/Assets/Hotfix/Module/Message/NetOuterComponentSystem.cs

@@ -7,7 +7,6 @@ namespace ET
 		public override void Awake(NetOuterComponent self)
 		{
 			self.Awake(self.Protocol);
-			self.MessagePacker = new ProtobufPacker();
 			self.MessageDispatcher = new OuterMessageDispatcher();
 		}
 	}
@@ -17,7 +16,6 @@ namespace ET
 		public override void Awake(NetOuterComponent self, string address)
 		{
 			self.Awake(self.Protocol, address);
-			self.MessagePacker = new ProtobufPacker();
 			self.MessageDispatcher = new OuterMessageDispatcher();
 		}
 	}
@@ -26,7 +24,6 @@ namespace ET
 	{
 		public override void Load(NetOuterComponent self)
 		{
-			self.MessagePacker = new ProtobufPacker();
 			self.MessageDispatcher = new OuterMessageDispatcher();
 		}
 	}

+ 0 - 11
Unity/Assets/Model/Base/Helper/Dumper.cs.meta

@@ -1,11 +0,0 @@
-fileFormatVersion: 2
-guid: 6a2cb7433d50e2a4b9c8d8f1d6c871fd
-MonoImporter:
-  externalObjects: {}
-  serializedVersion: 2
-  defaultReferences: []
-  executionOrder: 0
-  icon: {instanceID: 0}
-  userData: 
-  assetBundleName: 
-  assetBundleVariant: 

+ 0 - 13
Unity/Assets/Model/Base/Helper/MongoHelper.cs

@@ -107,19 +107,6 @@ namespace ET
 			}
 		}
 		
-		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());
-		}
-		
 		public static object FromStream(Type type, Stream stream)
 		{
 			return BsonSerializer.Deserialize(stream, type);

+ 0 - 12
Unity/Assets/Model/Module/Message/IMessagePacker.cs

@@ -1,12 +0,0 @@
-using System;
-using System.IO;
-
-namespace ET
-{
-	public interface IMessagePacker
-	{
-		void SerializeTo(object obj, MemoryStream stream);
-		object DeserializeFrom(Type type, byte[] bytes, int index, int count);
-		object DeserializeFrom(Type type, MemoryStream stream);
-	}
-}

+ 0 - 12
Unity/Assets/Model/Module/Message/IMessagePacker.cs.meta

@@ -1,12 +0,0 @@
-fileFormatVersion: 2
-guid: 09fe3a4882b1b50498860b3f672d1d47
-timeCreated: 1501467234
-licenseType: Free
-MonoImporter:
-  serializedVersion: 2
-  defaultReferences: []
-  executionOrder: 0
-  icon: {instanceID: 0}
-  userData: 
-  assetBundleName: 
-  assetBundleVariant: 

+ 38 - 0
Unity/Assets/Model/Module/Message/MessagePackHelper.cs

@@ -0,0 +1,38 @@
+using System;
+using System.IO;
+
+namespace ET
+{
+    public static class MessagePackHelper
+    {
+        public static void SerializeTo(ushort opcode, object obj, MemoryStream stream)
+        {
+            if (opcode >= 20000)
+            {
+                ProtobufHelper.ToStream(obj, stream);
+                return;
+            }
+
+            MongoHelper.ToBson(obj, stream);
+        }
+        public static object DeserializeFrom(ushort opcode, Type type, byte[] bytes, int index, int count)
+        {
+            if (opcode >= 20000)
+            {
+                return ProtobufHelper.FromBytes(type, bytes, index, count);
+            }
+
+            return MongoHelper.FromBson(type, bytes, index, count);
+        }
+
+        public static object DeserializeFrom(ushort opcode, Type type, MemoryStream stream)
+        {
+            if (opcode >= 20000)
+            {
+                return ProtobufHelper.FromStream(type, stream);
+            }
+
+            return MongoHelper.FromStream(type, stream);
+        }
+    }
+}

+ 1 - 1
Unity/Assets/Model/Module/Message/MessagePool.cs.meta → Unity/Assets/Model/Module/Message/MessagePackHelper.cs.meta

@@ -1,5 +1,5 @@
 fileFormatVersion: 2
-guid: 1d413cb22ac106a439af32933382ce66
+guid: f6c5c5505ab984795a67f5da89e16761
 MonoImporter:
   externalObjects: {}
   serializedVersion: 2

+ 0 - 64
Unity/Assets/Model/Module/Message/MessagePool.cs

@@ -1,64 +0,0 @@
-using System;
-#if !SERVER
-using System.Collections.Generic;		
-#endif
-
-namespace ET
-{
-	// 客户端为了0GC需要消息池,服务端消息需要跨协程不需要消息池
-	public class MessagePool
-	{
-		public static MessagePool Instance { get; } = new MessagePool();
-
-#if !SERVER
-		private readonly Dictionary<Type, Queue<object>> dictionary = new Dictionary<Type, Queue<object>>();
-#endif
-
-		public object Fetch(Type type)
-		{
-#if !SERVER
-			Queue<object> queue;
-			if (!this.dictionary.TryGetValue(type, out queue))
-			{
-				queue = new Queue<object>();
-				this.dictionary.Add(type, queue);
-			}
-
-			object obj;
-			if (queue.Count > 0)
-			{
-				obj = queue.Dequeue();
-			}
-			else
-			{
-				obj = Activator.CreateInstance(type);
-			}
-
-			return obj;
-#else
-			return Activator.CreateInstance(type);
-#endif
-		}
-
-		public T Fetch<T>() where T : class
-		{
-			T t = (T) this.Fetch(typeof (T));
-			return t;
-		}
-
-		public void Recycle(object obj)
-		{
-#if !SERVER
-			Type type = obj.GetType();
-			Queue<object> queue;
-			if (!this.dictionary.TryGetValue(type, out queue))
-			{
-				queue = new Queue<object>();
-				this.dictionary.Add(type, queue);
-			}
-
-			queue.Enqueue(obj);
-#endif
-		}
-	}
-}

+ 0 - 2
Unity/Assets/Model/Module/Message/NetworkComponent.cs

@@ -10,8 +10,6 @@ namespace ET
 
 		public readonly Dictionary<long, Session> Sessions = new Dictionary<long, Session>();
 
-		public IMessagePacker MessagePacker { get; set; }
-
 		public IMessageDispatcher MessageDispatcher { get; set; }
 
 		public void Awake(NetworkProtocol protocol)

+ 0 - 23
Unity/Assets/Model/Module/Message/ProtobufPacker.cs

@@ -1,23 +0,0 @@
-using System;
-using System.IO;
-
-namespace ET
-{
-	public class ProtobufPacker : IMessagePacker
-	{
-		public void SerializeTo(object obj, MemoryStream stream)
-		{
-			ProtobufHelper.ToStream(obj, stream);
-		}
-
-		public object DeserializeFrom(Type type, byte[] bytes, int index, int count)
-		{
-			return ProtobufHelper.FromBytes(type, bytes, index, count);
-		}
-
-		public object DeserializeFrom(Type type, MemoryStream stream)
-		{
-			return ProtobufHelper.FromStream(type, stream);
-		}
-	}
-}

+ 0 - 12
Unity/Assets/Model/Module/Message/ProtobufPacker.cs.meta

@@ -1,12 +0,0 @@
-fileFormatVersion: 2
-guid: ea1ea7cb44e1e18448edde8ab653d905
-timeCreated: 1519350935
-licenseType: Free
-MonoImporter:
-  serializedVersion: 2
-  defaultReferences: []
-  executionOrder: 0
-  icon: {instanceID: 0}
-  userData: 
-  assetBundleName: 
-  assetBundleVariant: 

+ 2 - 2
Unity/Assets/Model/Module/Message/Session.cs

@@ -133,7 +133,7 @@ namespace ET
 			try
 			{
 				Type type = OpcodeTypeComponent.Instance.GetType(opcode);
-				message = this.Network.MessagePacker.DeserializeFrom(type, memoryStream);
+				message = MessagePackHelper.DeserializeFrom(opcode, type, memoryStream);
 
 				if (OpcodeHelper.IsNeedDebugLogMessage(opcode))
 				{
@@ -260,7 +260,7 @@ namespace ET
 			
 			stream.Seek(Packet.MessageIndex, SeekOrigin.Begin);
 			stream.SetLength(Packet.MessageIndex);
-			this.Network.MessagePacker.SerializeTo(message, stream);
+			MessagePackHelper.SerializeTo(opcode, message, stream);
 			stream.Seek(0, SeekOrigin.Begin);
 			
 			opcodeBytes.WriteTo(0, opcode);

+ 0 - 8
Unity/Assets/ThirdParty/Google.Protobuf.meta

@@ -1,8 +0,0 @@
-fileFormatVersion: 2
-guid: 0c65cefeb5a4691489086c452ed06a7d
-folderAsset: yes
-DefaultImporter:
-  externalObjects: {}
-  userData: 
-  assetBundleName: 
-  assetBundleVariant: 

+ 1 - 3
Unity/Unity.Model.csproj

@@ -156,12 +156,11 @@
      <Compile Include="Assets\Model\Module\Message\IMHandler.cs" />
      <Compile Include="Assets\Model\Module\Message\IMessage.cs" />
      <Compile Include="Assets\Model\Module\Message\IMessageDispatcher.cs" />
-     <Compile Include="Assets\Model\Module\Message\IMessagePacker.cs" />
      <Compile Include="Assets\Model\Module\Message\MessageAttribute.cs" />
      <Compile Include="Assets\Model\Module\Message\MessageDispatcherComponent.cs" />
      <Compile Include="Assets\Model\Module\Message\MessageHandlerAttribute.cs" />
      <Compile Include="Assets\Model\Module\Message\MessageInfo.cs" />
-     <Compile Include="Assets\Model\Module\Message\MessagePool.cs" />
+     <Compile Include="Assets\Model\Module\Message\MessagePackHelper.cs" />
      <Compile Include="Assets\Model\Module\Message\MessageProxy.cs" />
      <Compile Include="Assets\Model\Module\Message\NetOuterComponent.cs" />
      <Compile Include="Assets\Model\Module\Message\Network\AChannel.cs" />
@@ -184,7 +183,6 @@
      <Compile Include="Assets\Model\Module\Message\OuterMessageDispatcher.cs" />
      <Compile Include="Assets\Model\Module\Message\OuterOpcode.cs" />
      <Compile Include="Assets\Model\Module\Message\ProtobufHelper.cs" />
-     <Compile Include="Assets\Model\Module\Message\ProtobufPacker.cs" />
      <Compile Include="Assets\Model\Module\Message\RpcException.cs" />
      <Compile Include="Assets\Model\Module\Message\Session.cs" />
      <Compile Include="Assets\Model\Module\Message\SessionCallbackComponent.cs" />