فهرست منبع

整理了网络层代码,删除一些废代码

tanghai 9 سال پیش
والد
کامیت
fe94aadc57

+ 2 - 1
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);
 		}

+ 0 - 8
Unity/Assets/Plugins/Base/Network/AService.cs

@@ -30,14 +30,6 @@ namespace Base
 
 		public abstract void Update();
 
-		public Action<AChannel, SocketError> OnError;
-
-		protected void OnChannelError(AChannel channel, SocketError error)
-		{
-			this.OnError?.Invoke(channel, error);
-			this.Remove(channel.Id);
-		}
-
 		public abstract void Dispose();
 	}
 }

+ 0 - 2
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;

+ 5 - 1
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); };
 		}
 
 		/// <summary>
@@ -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());
 		}
 	}
 }

+ 0 - 3
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;
 		}

+ 2 - 2
Unity/Assets/Plugins/Base/Network/UNet/USocket.cs

@@ -18,8 +18,8 @@ namespace Base
 		private readonly Queue<byte[]> recvQueue = new Queue<byte[]>();
 		private readonly Queue<BufferInfo> sendQueue = new Queue<BufferInfo>();
 		private bool isConnected;
-		public Action disconnect;
-		public Action received;
+		private Action disconnect;
+		private Action received;
 
 		public event Action Received
 		{

+ 5 - 6
Unity/Assets/Scripts/Component/NetworkComponent.cs

@@ -26,10 +26,10 @@ namespace Model
 
 	public class NetworkComponent: Component
 	{
-		public AService Service;
+		private AService Service;
 
-		public Dictionary<long, Entity> sessions = new Dictionary<long, Entity>();
-		public Dictionary<string, Entity> adressSessions = new Dictionary<string, Entity>();
+		private readonly Dictionary<long, Entity> sessions = new Dictionary<long, Entity>();
+		private readonly Dictionary<string, Entity> adressSessions = new Dictionary<string, Entity>();
 
 		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<MessageComponent>().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<MessageComponent, AChannel>(channel);
 			this.Add(session);
 
-			
 			return session;
 		}