diff --git a/Unity/Assets/Scripts/Component/NetworkComponent.cs b/Unity/Assets/Scripts/Component/NetworkComponent.cs index 464c95c4e8a571537991b71255a90858af33a181..345f4bca5b87cf560f4164ea9f5882b2deaef8fd 100644 --- a/Unity/Assets/Scripts/Component/NetworkComponent.cs +++ b/Unity/Assets/Scripts/Component/NetworkComponent.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Net.Sockets; using Base; namespace Model @@ -70,7 +69,7 @@ namespace Model AChannel channel = await this.Service.AcceptChannel(); - Session session = new Session(channel); + Session session = new Session(this.GetOwner(), channel); channel.ErrorCallback += (c, e) => { this.Remove(session.Id); }; this.Add(session); } @@ -138,7 +137,7 @@ namespace Model int port = int.Parse(ss[1]); string host = ss[0]; AChannel channel = this.Service.ConnectChannel(host, port); - Session session = new Session(channel); + Session session = new Session(this.GetOwner(), channel); channel.ErrorCallback += (c, e) => { this.Remove(session.Id); }; return session; } diff --git a/Unity/Assets/Scripts/Entity/Session.cs b/Unity/Assets/Scripts/Entity/Session.cs index 99bbbabb2835a5fb30ab08f65095ae092aa46d67..1b560bb45f31a52b24fd179ffb12125a54885801 100644 --- a/Unity/Assets/Scripts/Entity/Session.cs +++ b/Unity/Assets/Scripts/Entity/Session.cs @@ -9,11 +9,13 @@ namespace Model public sealed class Session: Entity { private static uint RpcId { get; set; } + private readonly Scene scene; private readonly Dictionary> requestCallback = new Dictionary>(); 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.StartRecv(); } @@ -88,7 +90,7 @@ namespace Model // 普通消息 if (rpcId == 0) { - Game.Scene.GetComponent().Handle(this, opcode, messageBytes, offset, 0); + this.scene.GetComponent().Handle(this, opcode, messageBytes, offset, 0); return; } @@ -106,7 +108,7 @@ namespace Model } else // 这是一个rpc请求消息 { - Game.Scene.GetComponent().Handle(this, opcode, messageBytes, offset, rpcId); + this.scene.GetComponent().Handle(this, opcode, messageBytes, offset, rpcId); } } @@ -195,7 +197,7 @@ namespace Model private void SendMessage(uint rpcId, object message, bool isCall = true) { - ushort opcode = Game.Scene.GetComponent().GetOpcode(message.GetType()); + ushort opcode = this.scene.GetComponent().GetOpcode(message.GetType()); byte[] opcodeBytes = BitConverter.GetBytes(opcode); if (!isCall) {