Эх сурвалжийг харах

flag字段整合到opcode里面,opcode最高位表示是否压缩

tanghai 8 жил өмнө
parent
commit
978053f590

+ 0 - 3
Server/Base/Server.Base.csproj

@@ -80,9 +80,6 @@
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Helper\NetHelper.cs">
       <Link>Helper\NetHelper.cs</Link>
     </Compile>
-    <Compile Include="..\..\Unity\Assets\Scripts\Base\Helper\NetworkHelper.cs">
-      <Link>Helper\NetworkHelper.cs</Link>
-    </Compile>
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Helper\ProtobufHelper.cs">
       <Link>Helper\ProtobufHelper.cs</Link>
     </Compile>

+ 0 - 37
Unity/Assets/Scripts/Base/Helper/NetworkHelper.cs

@@ -1,37 +0,0 @@
-using System.Net;
-
-namespace Model
-{
-	public static class NetworkHelper
-	{
-		public static uint NetworkToHostOrder(uint a)
-		{
-			return (uint) IPAddress.NetworkToHostOrder((int) a);
-		}
-
-		public static ushort NetworkToHostOrder(ushort a)
-		{
-			return (ushort)IPAddress.NetworkToHostOrder((short)a);
-		}
-
-		public static ulong NetworkToHostOrder(ulong a)
-		{
-			return (ushort)IPAddress.NetworkToHostOrder((long)a);
-		}
-
-		public static uint HostToNetworkOrder(uint a)
-		{
-			return (uint)IPAddress.HostToNetworkOrder((int)a);
-		}
-
-		public static ushort HostToNetworkOrder(ushort a)
-		{
-			return (ushort)IPAddress.HostToNetworkOrder((short)a);
-		}
-
-		public static ulong HostToNetworkOrder(ulong a)
-		{
-			return (ushort)IPAddress.HostToNetworkOrder((long)a);
-		}
-	}
-}

+ 0 - 12
Unity/Assets/Scripts/Base/Helper/NetworkHelper.cs.meta

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

+ 0 - 1
Unity/Assets/Scripts/Base/Network/TNet/PacketParser.cs

@@ -44,7 +44,6 @@ namespace Model
 						{
 							this.buffer.RecvFrom(this.packetSizeBuffer);
 							this.packetSize = BitConverter.ToUInt16(this.packetSizeBuffer, 0);
-							this.packetSize = NetworkHelper.NetworkToHostOrder(this.packetSize);
 							if (packetSize > 60000)
 							{
 								throw new Exception($"packet too large, size: {this.packetSize}");

+ 0 - 1
Unity/Assets/Scripts/Base/Network/TNet/TChannel.cs

@@ -107,7 +107,6 @@ namespace Model
 				throw new Exception("TChannel已经被Dispose, 不能发送消息");
 			}
 			ushort size = (ushort)buffers.Select(b => b.Length).Sum();
-			size = NetworkHelper.HostToNetworkOrder(size);
 			byte[] sizeBuffer = BitConverter.GetBytes(size);
 			this.sendBuffer.SendTo(sizeBuffer);
 			foreach (byte[] buffer in buffers)

+ 12 - 15
Unity/Assets/Scripts/Entity/Session.cs

@@ -11,6 +11,7 @@ namespace Model
 		private readonly NetworkComponent network;
 		private readonly Dictionary<uint, Action<object>> requestCallback = new Dictionary<uint, Action<object>>();
 		private readonly AChannel channel;
+		private readonly List<byte[]> byteses = new List<byte[]>() {new byte[0], new byte[0]};
 
 		public Session(NetworkComponent network, AChannel channel)
 		{
@@ -65,7 +66,6 @@ namespace Model
 				}
 
 				ushort opcode = BitConverter.ToUInt16(messageBytes, 0);
-				opcode = NetworkHelper.NetworkToHostOrder(opcode);
 				try
 				{
 					this.Run(opcode, messageBytes);
@@ -80,20 +80,18 @@ namespace Model
 		private void Run(ushort opcode, byte[] messageBytes)
 		{
 			int offset = 0;
-			byte flag = messageBytes[2];
-
-			bool isCompressed = (flag & 0x80) > 0;
-			const int opcodeAndFlagLength = 3;
+			// opcode最高位表示是否压缩
+			bool isCompressed = (opcode & 0x8000) > 0;
 			if (isCompressed) // 最高位为1,表示有压缩,需要解压缩
 			{
-				messageBytes = ZipHelper.Decompress(messageBytes, opcodeAndFlagLength, messageBytes.Length - opcodeAndFlagLength);
+				messageBytes = ZipHelper.Decompress(messageBytes, 2, messageBytes.Length - 2);
 				offset = 0;
 			}
 			else
 			{
-				offset = opcodeAndFlagLength;
+				offset = 2;
 			}
-
+			opcode &= 0x7fff;
 			this.RunDecompressedBytes(opcode, messageBytes, offset);
 		}
 
@@ -210,24 +208,23 @@ namespace Model
 		{
 			Log.Debug($"send: {MongoHelper.ToJson(message)}");
 			ushort opcode = this.network.Entity.GetComponent<OpcodeTypeComponent>().GetOpcode(message.GetType());
-			opcode = NetworkHelper.HostToNetworkOrder(opcode);
-			byte[] opcodeBytes = BitConverter.GetBytes(opcode);
 
 			byte[] messageBytes = this.network.MessagePacker.SerializeToByteArray(message);
-			byte flag = 0;
 			if (messageBytes.Length > 100)
 			{
 				byte[] newMessageBytes = ZipHelper.Compress(messageBytes);
 				if (newMessageBytes.Length < messageBytes.Length)
 				{
 					messageBytes = newMessageBytes;
-					flag |= 0x80;
+					opcode |= 0x8000;
 				}
 			}
 
-			byte[] flagBytes = { flag };
-
-			channel.Send(new List<byte[]> { opcodeBytes, flagBytes, messageBytes });
+			byte[] opcodeBytes = BitConverter.GetBytes(opcode);
+			
+			this.byteses[0] = opcodeBytes;
+			this.byteses[1] = messageBytes;
+			channel.Send(this.byteses);
 		}
 
 		public override void Dispose()

+ 0 - 1
Unity/Unity.csproj

@@ -426,7 +426,6 @@
     <Compile Include="Assets\Scripts\Base\Helper\MD5Helper.cs" />
     <Compile Include="Assets\Scripts\Base\Helper\MethodInfoHelper.cs" />
     <Compile Include="Assets\Scripts\Base\Helper\NetHelper.cs" />
-    <Compile Include="Assets\Scripts\Base\Helper\NetworkHelper.cs" />
     <Compile Include="Assets\Scripts\Base\Helper\ProtobufHelper.cs" />
     <Compile Include="Assets\Scripts\Base\Helper\RandomHelper.cs" />
     <Compile Include="Assets\Scripts\Base\Helper\StringHelper.cs" />