فهرست منبع

网络层消息长度不需要放到MemoryStream中,MemoryStream.Length就表示消息的长度

tanghai 7 سال پیش
والد
کامیت
2334cce569

+ 2 - 0
Unity/Assets/Scripts/Module/Message/Network/Circularbuffer.cs

@@ -198,6 +198,7 @@ namespace ETModel
 		    return n;
 		    return n;
 	    }
 	    }
 
 
+	    // 把CircularBuffer中数据写入buffer
         public override int Read(byte[] buffer, int offset, int count)
         public override int Read(byte[] buffer, int offset, int count)
         {
         {
 	        if (buffer.Length < offset + count)
 	        if (buffer.Length < offset + count)
@@ -233,6 +234,7 @@ namespace ETModel
 	        return count;
 	        return count;
         }
         }
 
 
+	    // 把buffer写入CircularBuffer中
         public override void Write(byte[] buffer, int offset, int count)
         public override void Write(byte[] buffer, int offset, int count)
         {
         {
 	        int alreadyCopyCount = 0;
 	        int alreadyCopyCount = 0;

+ 5 - 6
Unity/Assets/Scripts/Module/Message/Network/TCP/PacketParser.cs

@@ -14,9 +14,9 @@ namespace ETModel
 		public const int SizeLength = 2;
 		public const int SizeLength = 2;
 		public const int MinSize = 3;
 		public const int MinSize = 3;
 		public const int MaxSize = 60000;
 		public const int MaxSize = 60000;
-		public const int FlagIndex = 2;
-		public const int OpcodeIndex = 3;
-		public const int MessageIndex = 5;
+		public const int FlagIndex = 0;
+		public const int OpcodeIndex = 1;
+		public const int MessageIndex = 3;
 
 
 		/// <summary>
 		/// <summary>
 		/// 只读,不允许修改
 		/// 只读,不允许修改
@@ -93,10 +93,9 @@ namespace ETModel
 						else
 						else
 						{
 						{
 							this.packet.Stream.Seek(0, SeekOrigin.Begin);
 							this.packet.Stream.Seek(0, SeekOrigin.Begin);
-							this.packet.Stream.SetLength(this.packetSize + Packet.SizeLength);
+							this.packet.Stream.SetLength(this.packetSize);
 							byte[] bytes = this.packet.Stream.GetBuffer();
 							byte[] bytes = this.packet.Stream.GetBuffer();
-							bytes.WriteTo(0, this.packetSize);
-							this.buffer.Read(bytes, Packet.SizeLength, this.packetSize);
+							this.buffer.Read(bytes, 0, this.packetSize);
 							this.isOK = true;
 							this.isOK = true;
 							this.state = ParserState.PacketSize;
 							this.state = ParserState.PacketSize;
 							finish = true;
 							finish = true;

+ 7 - 0
Unity/Assets/Scripts/Module/Message/Network/TCP/TChannel.cs

@@ -25,6 +25,8 @@ namespace ETModel
 
 
 		public readonly PacketParser parser;
 		public readonly PacketParser parser;
 
 
+		public readonly byte[] cache = new byte[2];
+
 		public TChannel(IPEndPoint ipEndPoint, TService service): base(service, ChannelType.Connect)
 		public TChannel(IPEndPoint ipEndPoint, TService service): base(service, ChannelType.Connect)
 		{
 		{
 			this.InstanceId = IdGenerater.GenerateId();
 			this.InstanceId = IdGenerater.GenerateId();
@@ -100,6 +102,9 @@ namespace ETModel
 			{
 			{
 				throw new Exception("TChannel已经被Dispose, 不能发送消息");
 				throw new Exception("TChannel已经被Dispose, 不能发送消息");
 			}
 			}
+			
+			cache.WriteTo(0, (ushort)length);
+			this.sendBuffer.Write(this.cache, 0, this.cache.Length);
 			this.sendBuffer.Write(buffer, index, length);
 			this.sendBuffer.Write(buffer, index, length);
 
 
 			if(!this.isSending)
 			if(!this.isSending)
@@ -115,6 +120,8 @@ namespace ETModel
 				throw new Exception("TChannel已经被Dispose, 不能发送消息");
 				throw new Exception("TChannel已经被Dispose, 不能发送消息");
 			}
 			}
 
 
+			cache.WriteTo(0, (ushort)stream.Length);
+			this.sendBuffer.Write(this.cache, 0, this.cache.Length);
 			this.sendBuffer.ReadFrom(stream);
 			this.sendBuffer.ReadFrom(stream);
 
 
 			if(!this.isSending)
 			if(!this.isSending)

+ 3 - 5
Unity/Assets/Scripts/Module/Message/Session.cs

@@ -24,7 +24,7 @@ namespace ETModel
 		private AChannel channel;
 		private AChannel channel;
 
 
 		private readonly Dictionary<int, Action<IResponse>> requestCallback = new Dictionary<int, Action<IResponse>>();
 		private readonly Dictionary<int, Action<IResponse>> requestCallback = new Dictionary<int, Action<IResponse>>();
-		private readonly List<byte[]> byteses = new List<byte[]>() { new byte[2], new byte[1], new byte[2] };
+		private readonly List<byte[]> byteses = new List<byte[]>() { new byte[1], new byte[2] };
 
 
 		public NetworkComponent Network
 		public NetworkComponent Network
 		{
 		{
@@ -271,10 +271,8 @@ namespace ETModel
 			this.Network.MessagePacker.SerializeTo(message, stream);
 			this.Network.MessagePacker.SerializeTo(message, stream);
 			stream.Seek(0, SeekOrigin.Begin);
 			stream.Seek(0, SeekOrigin.Begin);
 			
 			
-			ushort size = (ushort)(stream.Length - Packet.SizeLength);
-			this.byteses[0].WriteTo(0, size);
-			this.byteses[1][0] = flag;
-			this.byteses[2].WriteTo(0, opcode);
+			this.byteses[0][0] = flag;
+			this.byteses[1].WriteTo(0, opcode);
 			int index = 0;
 			int index = 0;
 			foreach (var bytes in this.byteses)
 			foreach (var bytes in this.byteses)
 			{
 			{