Просмотр исходного кода

以后一个App可能存在多个MessageDispather,所以不能直接给Game.Scene的MessageDispather组件。改成,转给自己的Scene上挂在的MessageDispather处理,

tanghai 9 лет назад
Родитель
Сommit
052ff131ca

+ 2 - 3
Unity/Assets/Scripts/Component/NetworkComponent.cs

@@ -1,7 +1,6 @@
 using System;
 using System;
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.Linq;
 using System.Linq;
-using System.Net.Sockets;
 using Base;
 using Base;
 
 
 namespace Model
 namespace Model
@@ -70,7 +69,7 @@ namespace Model
 
 
 				AChannel channel = await this.Service.AcceptChannel();
 				AChannel channel = await this.Service.AcceptChannel();
 
 
-				Session session = new Session(channel);
+				Session session = new Session(this.GetOwner<Scene>(), channel);
 				channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
 				channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
 				this.Add(session);
 				this.Add(session);
 			}
 			}
@@ -138,7 +137,7 @@ namespace Model
 			int port = int.Parse(ss[1]);
 			int port = int.Parse(ss[1]);
 			string host = ss[0];
 			string host = ss[0];
 			AChannel channel = this.Service.ConnectChannel(host, port);
 			AChannel channel = this.Service.ConnectChannel(host, port);
-			Session session = new Session(channel);
+			Session session = new Session(this.GetOwner<Scene>(), channel);
 			channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
 			channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
 			return session;
 			return session;
 		}
 		}

+ 6 - 4
Unity/Assets/Scripts/Entity/Session.cs

@@ -9,11 +9,13 @@ namespace Model
 	public sealed class Session: Entity
 	public sealed class Session: Entity
 	{
 	{
 		private static uint RpcId { get; set; }
 		private static uint RpcId { get; set; }
+		private readonly Scene scene;
 		private readonly Dictionary<uint, Action<byte[], int, int>> requestCallback = new Dictionary<uint, Action<byte[], int, int>>();
 		private readonly Dictionary<uint, Action<byte[], int, int>> requestCallback = new Dictionary<uint, Action<byte[], int, int>>();
 		private readonly AChannel channel;
 		private readonly AChannel channel;
 
 
-		public Session(AChannel channel) : base(EntityType.Session)
+		public Session(Scene scene, AChannel channel) : base(EntityType.Session)
 		{
 		{
+			this.scene = scene;
 			this.channel = channel;
 			this.channel = channel;
 			this.StartRecv();
 			this.StartRecv();
 		}
 		}
@@ -88,7 +90,7 @@ namespace Model
 			// 普通消息
 			// 普通消息
 			if (rpcId == 0)
 			if (rpcId == 0)
 			{
 			{
-				Game.Scene.GetComponent<MessageDispatherComponent>().Handle(this, opcode, messageBytes, offset, 0);
+				this.scene.GetComponent<MessageDispatherComponent>().Handle(this, opcode, messageBytes, offset, 0);
 				return;
 				return;
 			}
 			}
 
 
@@ -106,7 +108,7 @@ namespace Model
 			}
 			}
 			else // 这是一个rpc请求消息
 			else // 这是一个rpc请求消息
 			{
 			{
-				Game.Scene.GetComponent<MessageDispatherComponent>().Handle(this, opcode, messageBytes, offset, rpcId);
+				this.scene.GetComponent<MessageDispatherComponent>().Handle(this, opcode, messageBytes, offset, rpcId);
 			}
 			}
 		}
 		}
 
 
@@ -195,7 +197,7 @@ namespace Model
 
 
 		private void SendMessage(uint rpcId, object message, bool isCall = true)
 		private void SendMessage(uint rpcId, object message, bool isCall = true)
 		{
 		{
-			ushort opcode = Game.Scene.GetComponent<MessageDispatherComponent>().GetOpcode(message.GetType());
+			ushort opcode = this.scene.GetComponent<MessageDispatherComponent>().GetOpcode(message.GetType());
 			byte[] opcodeBytes = BitConverter.GetBytes(opcode);
 			byte[] opcodeBytes = BitConverter.GetBytes(opcode);
 			if (!isCall)
 			if (!isCall)
 			{
 			{