From fe94aadc57c2d038efaea180e29241409f6f581c Mon Sep 17 00:00:00 2001 From: tanghai Date: Wed, 19 Oct 2016 15:08:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86=E4=BA=86=E7=BD=91=E7=BB=9C?= =?UTF-8?q?=E5=B1=82=E4=BB=A3=E7=A0=81=EF=BC=8C=E5=88=A0=E9=99=A4=E4=B8=80?= =?UTF-8?q?=E4=BA=9B=E5=BA=9F=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Unity/Assets/Plugins/Base/Network/AChannel.cs | 3 ++- Unity/Assets/Plugins/Base/Network/AService.cs | 8 -------- Unity/Assets/Plugins/Base/Network/TNet/TService.cs | 2 -- Unity/Assets/Plugins/Base/Network/UNet/UChannel.cs | 6 +++++- Unity/Assets/Plugins/Base/Network/UNet/UService.cs | 3 --- Unity/Assets/Plugins/Base/Network/UNet/USocket.cs | 4 ++-- Unity/Assets/Scripts/Component/NetworkComponent.cs | 11 +++++------ 7 files changed, 14 insertions(+), 23 deletions(-) diff --git a/Unity/Assets/Plugins/Base/Network/AChannel.cs b/Unity/Assets/Plugins/Base/Network/AChannel.cs index 20f5eac5..f5d2e662 100644 --- a/Unity/Assets/Plugins/Base/Network/AChannel.cs +++ b/Unity/Assets/Plugins/Base/Network/AChannel.cs @@ -17,6 +17,7 @@ namespace Base public abstract class AChannel: IDisposable { public long Id { get; private set; } + protected AService service; public string RemoteAddress { get; protected set; } @@ -35,7 +36,7 @@ namespace Base } } - public void OnError(AChannel channel, SocketError e) + protected void OnError(AChannel channel, SocketError e) { this.errorCallback(channel, e); } diff --git a/Unity/Assets/Plugins/Base/Network/AService.cs b/Unity/Assets/Plugins/Base/Network/AService.cs index 159e69a0..0738d561 100644 --- a/Unity/Assets/Plugins/Base/Network/AService.cs +++ b/Unity/Assets/Plugins/Base/Network/AService.cs @@ -30,14 +30,6 @@ namespace Base public abstract void Update(); - public Action OnError; - - protected void OnChannelError(AChannel channel, SocketError error) - { - this.OnError?.Invoke(channel, error); - this.Remove(channel.Id); - } - public abstract void Dispose(); } } \ No newline at end of file diff --git a/Unity/Assets/Plugins/Base/Network/TNet/TService.cs b/Unity/Assets/Plugins/Base/Network/TNet/TService.cs index 999c1829..f942804f 100644 --- a/Unity/Assets/Plugins/Base/Network/TNet/TService.cs +++ b/Unity/Assets/Plugins/Base/Network/TNet/TService.cs @@ -63,7 +63,6 @@ namespace Base TSocket socket = new TSocket(this.poller); await this.acceptor.AcceptAsync(socket); TChannel channel = new TChannel(socket, this); - channel.ErrorCallback += this.OnChannelError; this.idChannels[channel.Id] = channel; return channel; } @@ -72,7 +71,6 @@ namespace Base { TSocket newSocket = new TSocket(this.poller); TChannel channel = new TChannel(newSocket, host, port, this); - channel.ErrorCallback += this.OnChannelError; this.idChannels[channel.Id] = channel; return channel; diff --git a/Unity/Assets/Plugins/Base/Network/UNet/UChannel.cs b/Unity/Assets/Plugins/Base/Network/UNet/UChannel.cs index 62bd55a6..3feb980e 100644 --- a/Unity/Assets/Plugins/Base/Network/UNet/UChannel.cs +++ b/Unity/Assets/Plugins/Base/Network/UNet/UChannel.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net.Sockets; using System.Threading.Tasks; namespace Base @@ -21,6 +22,7 @@ namespace Base this.RemoteAddress = host + ":" + port; this.socket.ConnectAsync(host, (ushort)port); this.socket.Received += this.OnRecv; + this.socket.Disconnect += () => { this.OnError(this, SocketError.SocketError); }; } /// @@ -32,6 +34,7 @@ namespace Base this.service = service; this.RemoteAddress = socket.RemoteAddress; this.socket.Received += this.OnRecv; + this.socket.Disconnect += () => { this.OnError(this, SocketError.SocketError); }; } public override void Dispose() @@ -82,8 +85,9 @@ namespace Base private void OnRecv() { - this.recvTcs?.SetResult(this.socket.RecvQueue.Dequeue()); + var tcs = this.recvTcs; this.recvTcs = null; + tcs?.SetResult(this.socket.RecvQueue.Dequeue()); } } } \ No newline at end of file diff --git a/Unity/Assets/Plugins/Base/Network/UNet/UService.cs b/Unity/Assets/Plugins/Base/Network/UNet/UService.cs index 48e54497..6219cca5 100644 --- a/Unity/Assets/Plugins/Base/Network/UNet/UService.cs +++ b/Unity/Assets/Plugins/Base/Network/UNet/UService.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Net.Sockets; using System.Threading.Tasks; namespace Base @@ -53,7 +52,6 @@ namespace Base { USocket socket = await this.poller.AcceptAsync(); UChannel channel = new UChannel(socket, this); - socket.Disconnect += () => this.OnChannelError(channel, SocketError.SocketError); this.idChannels[channel.Id] = channel; return channel; } @@ -62,7 +60,6 @@ namespace Base { USocket newSocket = new USocket(this.poller); UChannel channel = new UChannel(newSocket, host, port, this); - newSocket.Disconnect += () => this.OnChannelError(channel, SocketError.SocketError); this.idChannels[channel.Id] = channel; return channel; } diff --git a/Unity/Assets/Plugins/Base/Network/UNet/USocket.cs b/Unity/Assets/Plugins/Base/Network/UNet/USocket.cs index f628d70a..f311cc69 100644 --- a/Unity/Assets/Plugins/Base/Network/UNet/USocket.cs +++ b/Unity/Assets/Plugins/Base/Network/UNet/USocket.cs @@ -18,8 +18,8 @@ namespace Base private readonly Queue recvQueue = new Queue(); private readonly Queue sendQueue = new Queue(); private bool isConnected; - public Action disconnect; - public Action received; + private Action disconnect; + private Action received; public event Action Received { diff --git a/Unity/Assets/Scripts/Component/NetworkComponent.cs b/Unity/Assets/Scripts/Component/NetworkComponent.cs index 1dae6f2a..454841d8 100644 --- a/Unity/Assets/Scripts/Component/NetworkComponent.cs +++ b/Unity/Assets/Scripts/Component/NetworkComponent.cs @@ -26,10 +26,10 @@ namespace Model public class NetworkComponent: Component { - public AService Service; + private AService Service; - public Dictionary sessions = new Dictionary(); - public Dictionary adressSessions = new Dictionary(); + private readonly Dictionary sessions = new Dictionary(); + private readonly Dictionary adressSessions = new Dictionary(); public void Awake(NetworkProtocol protocol) { @@ -81,13 +81,13 @@ namespace Model } } - public void Add(Entity session) + private void Add(Entity session) { this.sessions.Add(session.Id, session); this.adressSessions.Add(session.GetComponent().RemoteAddress, session); } - public void Remove(long id) + private void Remove(long id) { Entity session; if (!this.sessions.TryGetValue(id, out session)) @@ -122,7 +122,6 @@ namespace Model session.AddComponent(channel); this.Add(session); - return session; } -- GitLab