Ver código fonte

整理代码

tanghai 11 anos atrás
pai
commit
6aa4d7b86b

+ 2 - 2
CSharp/Platform/Network/IService.cs

@@ -11,9 +11,9 @@ namespace Network
 		/// <param name="action"></param>
 		void Add(Action action);
 
-		Task<IChannel> GetChannel(string host, int port);
+		Task<IChannel> GetChannel(string host, int port, uint channel = 255);
 
-		Task<IChannel> GetChannel(string address);
+		Task<IChannel> GetChannel(string address, uint channel = 255);
 
 		Task<IChannel> GetChannel();
 

+ 3 - 3
CSharp/Platform/TNet/TService.cs

@@ -100,7 +100,7 @@ namespace TNet
 			this.timerManager.Remove(tChannel.SendTimer);
 		}
 
-		public async Task<IChannel> GetChannel(string host, int port)
+		public async Task<IChannel> GetChannel(string host, int port, uint channelCount)
 		{
 			TChannel channel = null;
 			if (this.channels.TryGetValue(host + ":" + port, out channel))
@@ -110,11 +110,11 @@ namespace TNet
 			return await ConnectAsync(host, port);
 		}
 
-		public async Task<IChannel> GetChannel(string address)
+		public async Task<IChannel> GetChannel(string address, uint channelCount)
 		{
 			string[] ss = address.Split(':');
 			int port = Convert.ToInt32(ss[1]);
-			return await GetChannel(ss[0], port);
+			return await GetChannel(ss[0], port, channelCount);
 		}
 
 		public void RunOnce(int timeout)

+ 0 - 25
CSharp/Platform/UNet/EException.cs

@@ -1,25 +0,0 @@
-using System;
-using System.Runtime.Serialization;
-
-namespace UNet
-{
-	[Serializable]
-	public class EException: Exception
-	{
-		public EException()
-		{
-		}
-
-		public EException(string message): base(message)
-		{
-		}
-
-		public EException(string message, Exception inner): base(message, inner)
-		{
-		}
-
-		protected EException(SerializationInfo info, StreamingContext context)
-		{
-		}
-	}
-}

+ 2 - 2
CSharp/Platform/UNet/Library.cs

@@ -4,11 +4,11 @@
 	{
 		public static void Initialize()
 		{
-			var inits = new ENetCallbacks();
+			ENetCallbacks inits = new ENetCallbacks();
 			int ret = NativeMethods.EnetInitializeWithCallbacks(NativeMethods.ENET_VERSION, ref inits);
 			if (ret < 0)
 			{
-				throw new EException(string.Format("Initialization failed, ret: {0}", ret));
+				throw new UException(string.Format("Initialization failed, ret: {0}", ret));
 			}
 		}
 

+ 4 - 4
CSharp/Platform/UNet/Address.cs → CSharp/Platform/UNet/UAddress.cs

@@ -3,12 +3,12 @@ using System.Net;
 
 namespace UNet
 {
-	public struct Address
+	public struct UAddress
 	{
 		private uint ip;
 		private ushort port;
 
-		public uint Ip
+		public uint IP
 		{
 			get
 			{
@@ -36,7 +36,7 @@ namespace UNet
 		{
 			get
 			{
-				var hostInfo = Dns.GetHostEntry(new IPAddress(this.ip));
+				IPHostEntry hostInfo = Dns.GetHostEntry(new IPAddress(this.ip));
 				return hostInfo.HostName;
 			}
 			set
@@ -54,7 +54,7 @@ namespace UNet
 		{
 			get
 			{
-				var address = new ENetAddress { Host = this.ip, Port = this.port };
+				ENetAddress address = new ENetAddress { Host = this.ip, Port = this.port };
 				return address;
 			}
 		}

+ 2 - 2
CSharp/Platform/UNet/UChannel.cs

@@ -45,13 +45,13 @@ namespace UNet
 
 		public void SendAsync(byte[] buffer, byte channelID = 0, PacketFlags flags = PacketFlags.Reliable)
 		{
-			this.socket.WriteAsync(buffer, channelID, flags);
+			this.socket.SendAsync(buffer, channelID, flags);
 		}
 
 
 		public async Task<byte[]> RecvAsync()
 		{
-			return await this.socket.ReadAsync();
+			return await this.socket.RecvAsync();
 		}
 
 		public string RemoteAddress

+ 2 - 2
CSharp/Platform/UNet/EEvent.cs → CSharp/Platform/UNet/UEvent.cs

@@ -8,12 +8,12 @@ namespace UNet
 		DISCONNECTED = 1,
 	}
 
-	public class EEvent
+	public class UEvent
 	{
 		private readonly ENetEvent ev;
 		private EventState peerState = EventState.CONNECTED;
 
-		public EEvent(ENetEvent ev)
+		public UEvent(ENetEvent ev)
 		{
 			this.ev = ev;
 		}

+ 25 - 0
CSharp/Platform/UNet/UException.cs

@@ -0,0 +1,25 @@
+using System;
+using System.Runtime.Serialization;
+
+namespace UNet
+{
+	[Serializable]
+	public class UException: Exception
+	{
+		public UException()
+		{
+		}
+
+		public UException(string message): base(message)
+		{
+		}
+
+		public UException(string message, Exception inner): base(message, inner)
+		{
+		}
+
+		protected UException(SerializationInfo info, StreamingContext context)
+		{
+		}
+	}
+}

+ 5 - 5
CSharp/Platform/UNet/UNet.csproj

@@ -38,16 +38,16 @@
     <Reference Include="System.Core" />
   </ItemGroup>
   <ItemGroup>
-    <Compile Include="Address.cs" />
-    <Compile Include="EException.cs" />
-    <Compile Include="EEvent.cs" />
+    <Compile Include="UAddress.cs" />
+    <Compile Include="UException.cs" />
+    <Compile Include="UEvent.cs" />
     <Compile Include="ErrorCode.cs" />
-    <Compile Include="EService.cs" />
+    <Compile Include="UPoller.cs" />
     <Compile Include="Library.cs" />
     <Compile Include="NativeMethods.cs" />
     <Compile Include="PeersManager.cs" />
     <Compile Include="NativeStructs.cs" />
-    <Compile Include="EPacket.cs" />
+    <Compile Include="UPacket.cs" />
     <Compile Include="USocket.cs" />
     <Compile Include="UChannel.cs" />
     <Compile Include="UService.cs" />

+ 5 - 5
CSharp/Platform/UNet/EPacket.cs → CSharp/Platform/UNet/UPacket.cs

@@ -4,16 +4,16 @@ using Network;
 
 namespace UNet
 {
-	internal sealed class EPacket: IDisposable
+	internal sealed class UPacket: IDisposable
 	{
 		private IntPtr packet;
 
-		public EPacket(IntPtr packet)
+		public UPacket(IntPtr packet)
 		{
 			this.packet = packet;
 		}
 
-		public EPacket(byte[] data, PacketFlags flags = PacketFlags.None)
+		public UPacket(byte[] data, PacketFlags flags = PacketFlags.None)
 		{
 			if (data == null)
 			{
@@ -22,11 +22,11 @@ namespace UNet
 			this.packet = NativeMethods.EnetPacketCreate(data, (uint) data.Length, flags);
 			if (this.packet == IntPtr.Zero)
 			{
-				throw new EException("Packet creation call failed");
+				throw new UException("Packet creation call failed");
 			}
 		}
 
-		~EPacket()
+		~UPacket()
 		{
 			this.Dispose(false);
 		}

+ 33 - 33
CSharp/Platform/UNet/EService.cs → CSharp/Platform/UNet/UPoller.cs

@@ -3,15 +3,15 @@ using System.Collections.Generic;
 
 namespace UNet
 {
-	public sealed class EService: IDisposable
+	public sealed class UPoller: IDisposable
 	{
-		static EService()
+		static UPoller()
 		{
 			Library.Initialize();
 		}
 
 		private readonly PeersManager peersManager = new PeersManager();
-		private readonly LinkedList<EEvent> connEEvents = new LinkedList<EEvent>();
+		private readonly LinkedList<UEvent> connEEvents = new LinkedList<UEvent>();
 
 		internal PeersManager PeersManager
 		{
@@ -21,7 +21,7 @@ namespace UNet
 			}
 		}
 
-		internal LinkedList<EEvent> ConnEEvents
+		internal LinkedList<UEvent> ConnEEvents
 		{
 			get
 			{
@@ -33,31 +33,31 @@ namespace UNet
 		private readonly object eventsLock = new object();
 		private Action events;
 
-		public EService(string hostName, ushort port)
+		public UPoller(string hostName, ushort port)
 		{
-			Address address = new Address { HostName = hostName, Port = port };
+			UAddress address = new UAddress { HostName = hostName, Port = port };
 			ENetAddress nativeAddress = address.Struct;
 			this.host = NativeMethods.EnetHostCreate(
 				ref nativeAddress, NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID, 0, 0, 0);
 
 			if (this.host == IntPtr.Zero)
 			{
-				throw new EException("Host creation call failed.");
+				throw new UException("Host creation call failed.");
 			}
 		}
 
-		public EService()
+		public UPoller()
 		{
 			this.host = NativeMethods.EnetHostCreate(
 				IntPtr.Zero, NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID, 0, 0, 0);
 
 			if (this.host == IntPtr.Zero)
 			{
-				throw new EException("Host creation call failed.");
+				throw new UException("Host creation call failed.");
 			}
 		}
 
-		~EService()
+		~UPoller()
 		{
 			this.Dispose(false);
 		}
@@ -88,16 +88,16 @@ namespace UNet
 			}
 		}
 
-		private EEvent GetEvent()
+		private UEvent GetEvent()
 		{
-			ENetEvent enetEv = new ENetEvent();
-			int ret = NativeMethods.EnetHostCheckEvents(this.host, enetEv);
+			ENetEvent eEvent = new ENetEvent();
+			int ret = NativeMethods.EnetHostCheckEvents(this.host, eEvent);
 			if (ret <= 0)
 			{
 				return null;
 			}
-			EEvent e = new EEvent(enetEv);
-			return e;
+			UEvent u = new UEvent(eEvent);
+			return u;
 		}
 
 		public void CompressWithRangeCoder()
@@ -187,21 +187,21 @@ namespace UNet
 
 			while (true)
 			{
-				EEvent eEvent = this.GetEvent();
-				if (eEvent == null)
+				UEvent uEvent = this.GetEvent();
+				if (uEvent == null)
 				{
 					return;
 				}
 
-				switch (eEvent.Type)
+				switch (uEvent.Type)
 				{
 					case EventType.Connect:
 					{
 						// 这是一个connect peer
-						if (this.PeersManager.ContainsKey(eEvent.PeerPtr))
+						if (this.PeersManager.ContainsKey(uEvent.PeerPtr))
 						{
-							USocket uSocket = this.PeersManager[eEvent.PeerPtr];
-							uSocket.OnConnected(eEvent);
+							USocket uSocket = this.PeersManager[uEvent.PeerPtr];
+							uSocket.OnConnected(uEvent);
 						}
 								// accept peer
 						else
@@ -209,28 +209,28 @@ namespace UNet
 							// 如果server端没有acceptasync,则请求放入队列
 							if (!this.PeersManager.ContainsKey(IntPtr.Zero))
 							{
-								this.connEEvents.AddLast(eEvent);
+								this.connEEvents.AddLast(uEvent);
 							}
 							else
 							{
 								USocket uSocket = this.PeersManager[IntPtr.Zero];
-								uSocket.OnConnected(eEvent);
+								uSocket.OnConnected(uEvent);
 							}
 						}
 						break;
 					}
 					case EventType.Receive:
 					{
-						USocket uSocket = this.PeersManager[eEvent.PeerPtr];
-						uSocket.OnReceived(eEvent);
+						USocket uSocket = this.PeersManager[uEvent.PeerPtr];
+						uSocket.OnReceived(uEvent);
 						break;
 					}
 					case EventType.Disconnect:
 					{
 						// 如果链接还在缓存中,则删除
-						foreach (EEvent connEEvent in this.connEEvents)
+						foreach (UEvent connEEvent in this.connEEvents)
 						{
-							if (connEEvent.PeerPtr != eEvent.PeerPtr)
+							if (connEEvent.PeerPtr != uEvent.PeerPtr)
 							{
 								continue;
 							}
@@ -239,22 +239,22 @@ namespace UNet
 						}
 
 						// 链接已经被应用层接收
-						eEvent.EventState = EventState.DISCONNECTED;
-						USocket uSocket = this.PeersManager[eEvent.PeerPtr];
-						this.PeersManager.Remove(eEvent.PeerPtr);
+						uEvent.EventState = EventState.DISCONNECTED;
+						USocket uSocket = this.PeersManager[uEvent.PeerPtr];
+						this.PeersManager.Remove(uEvent.PeerPtr);
 
 						// 等待的task将抛出异常
 						if (uSocket.Connected != null)
 						{
-							uSocket.OnConnected(eEvent);
+							uSocket.OnConnected(uEvent);
 						}
 						else if (uSocket.Received != null)
 						{
-							uSocket.OnReceived(eEvent);
+							uSocket.OnReceived(uEvent);
 						}
 						else if (uSocket.Disconnect != null)
 						{
-							uSocket.OnDisconnect(eEvent);
+							uSocket.OnDisconnect(uEvent);
 						}
 
 						uSocket.OnError(ErrorCode.ClientDisconnect);

+ 27 - 27
CSharp/Platform/UNet/UService.cs

@@ -7,7 +7,7 @@ namespace UNet
 {
 	public sealed class UService: IService
 	{
-		private EService service;
+		private UPoller poller;
 
 		private readonly Dictionary<string, UChannel> channels = new Dictionary<string, UChannel>();
 
@@ -18,7 +18,7 @@ namespace UNet
 		/// <param name="port"></param>
 		public UService(string host, int port)
 		{
-			this.service = new EService(host, (ushort)port);
+			this.poller = new UPoller(host, (ushort)port);
 		}
 
 		/// <summary>
@@ -26,21 +26,21 @@ namespace UNet
 		/// </summary>
 		public UService()
 		{
-			this.service = new EService();
+			this.poller = new UPoller();
 		}
 
 		private void Dispose(bool disposing)
 		{
-			if (service == null)
+			if (this.poller == null)
 			{
 				return;
 			}
 
 			if (disposing)
 			{
-				service.Dispose();	
+				this.poller.Dispose();	
 			}
-			service = null;
+			this.poller = null;
 		}
 
 		~UService()
@@ -56,36 +56,46 @@ namespace UNet
 
 		public void Add(Action action)
 		{
-			this.service.Events += action;
+			this.poller.Events += action;
 		}
 
-		public EService Service
+		public UPoller Poller
 		{
 			get
 			{
-				return service;
+				return this.poller;
 			}
 		}
 
-		private async Task<IChannel> ConnectAsync(string host, int port)
+		private async Task<IChannel> ConnectAsync(string host, int port, uint channelCount)
 		{
-			USocket newSocket = new USocket(service);
-			await newSocket.ConnectAsync(host, (ushort) port);
+			USocket newSocket = new USocket(this.poller);
+			await newSocket.ConnectAsync(host, (ushort)port, channelCount);
 			UChannel channel = new UChannel(newSocket, this);
 			channels[channel.RemoteAddress] = channel;
 			return channel;
 		}
 
-		public async Task<IChannel> GetChannel(string address)
+		public async Task<IChannel> GetChannel(string address, uint channelCount)
 		{
 			string[] ss = address.Split(':');
 			int port = Convert.ToInt32(ss[1]);
-			return await GetChannel(ss[0], port);
+			return await GetChannel(ss[0], port, channelCount);
+		}
+
+		public async Task<IChannel> GetChannel(string host, int port, uint channelCount)
+		{
+			UChannel channel = null;
+			if (this.channels.TryGetValue(host + ":" + port, out channel))
+			{
+				return channel;
+			}
+			return await ConnectAsync(host, port, channelCount);
 		}
 
 		public async Task<IChannel> GetChannel()
 		{
-			USocket socket = new USocket(this.service);
+			USocket socket = new USocket(this.poller);
 			await socket.AcceptAsync();
 			UChannel channel = new UChannel(socket, this);
 			channels[channel.RemoteAddress] = channel;
@@ -102,24 +112,14 @@ namespace UNet
 			this.channels.Remove(channel.RemoteAddress);
 		}
 
-		public async Task<IChannel> GetChannel(string host, int port)
-		{
-			UChannel channel = null;
-			if (this.channels.TryGetValue(host + ":" + port, out channel))
-			{
-				return channel;
-			}
-			return await ConnectAsync(host, port);
-		}
-
 		public void RunOnce(int timeout)
 		{
-			this.service.RunOnce(timeout);
+			this.poller.RunOnce(timeout);
 		}
 
 		public void Run()
 		{
-			this.service.Run();
+			this.poller.Run();
 		}
 	}
 }

+ 31 - 32
CSharp/Platform/UNet/USocket.cs

@@ -9,15 +9,15 @@ namespace UNet
 	public sealed class USocket: IDisposable
 	{
 		private IntPtr peerPtr = IntPtr.Zero;
-		private readonly EService service;
+		private readonly UPoller service;
 		private readonly LinkedList<byte[]> recvBuffer = new LinkedList<byte[]>();
 
-		public Action<EEvent> Connected { get; set; }
-		public Action<EEvent> Received { get; set; }
-		public Action<EEvent> Disconnect { get; set; }
+		public Action<UEvent> Connected { get; set; }
+		public Action<UEvent> Received { get; set; }
+		public Action<UEvent> Disconnect { get; set; }
 		public Action<int> Error { get; set; }
 
-		public USocket(EService service)
+		public USocket(UPoller service)
 		{
 			this.service = service;
 		}
@@ -94,28 +94,27 @@ namespace UNet
 
 		public Task<bool> ConnectAsync(
 				string hostName, ushort port,
-				uint channelLimit = NativeMethods.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT, uint data = 0)
+				uint channel = NativeMethods.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
 		{
-			if (channelLimit > NativeMethods.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
+			if (channel > NativeMethods.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT || channel < NativeMethods.ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT)
 			{
-				throw new ArgumentOutOfRangeException("channelLimit");
+				throw new ArgumentOutOfRangeException("channel", channel.ToString());
 			}
 
 			var tcs = new TaskCompletionSource<bool>();
-			Address address = new Address { HostName = hostName, Port = port };
+			UAddress address = new UAddress { HostName = hostName, Port = port };
 			ENetAddress nativeAddress = address.Struct;
-			this.peerPtr = NativeMethods.EnetHostConnect(this.service.HostPtr, ref nativeAddress,
-					channelLimit, data);
+			this.peerPtr = NativeMethods.EnetHostConnect(this.service.HostPtr, ref nativeAddress, channel, 0);
 			if (this.peerPtr == IntPtr.Zero)
 			{
-				throw new EException("host connect call failed.");
+				throw new UException("host connect call failed.");
 			}
 			this.service.PeersManager.Add(this.peerPtr, this);
 			this.Connected = eEvent =>
 			{
 				if (eEvent.EventState == EventState.DISCONNECTED)
 				{
-					tcs.TrySetException(new EException("socket disconnected in connect"));
+					tcs.TrySetException(new UException("socket disconnected in connect"));
 				}
 				tcs.TrySetResult(true);
 			};
@@ -126,7 +125,7 @@ namespace UNet
 		{
 			if (this.service.PeersManager.ContainsKey(IntPtr.Zero))
 			{
-				throw new EException("do not accept twice!");
+				throw new UException("do not accept twice!");
 			}
 
 			var tcs = new TaskCompletionSource<bool>();
@@ -134,10 +133,10 @@ namespace UNet
 			// 如果有请求连接缓存的包,从缓存中取
 			if (this.service.ConnEEvents.Count > 0)
 			{
-				EEvent eEvent = this.service.ConnEEvents.First.Value;
+				UEvent uEvent = this.service.ConnEEvents.First.Value;
 				this.service.ConnEEvents.RemoveFirst();
 
-				this.PeerPtr = eEvent.PeerPtr;
+				this.PeerPtr = uEvent.PeerPtr;
 				this.service.PeersManager.Add(this.PeerPtr, this);
 				tcs.TrySetResult(true);
 			}
@@ -148,7 +147,7 @@ namespace UNet
 				{
 					if (eEvent.EventState == EventState.DISCONNECTED)
 					{
-						tcs.TrySetException(new EException("socket disconnected in accpet"));
+						tcs.TrySetException(new UException("socket disconnected in accpet"));
 					}
 
 					this.service.PeersManager.Remove(IntPtr.Zero);
@@ -161,15 +160,15 @@ namespace UNet
 			return tcs.Task;
 		}
 
-		public void WriteAsync(byte[] data, byte channelID = 0, PacketFlags flags = PacketFlags.Reliable)
+		public void SendAsync(byte[] data, byte channelID = 0, PacketFlags flags = PacketFlags.Reliable)
 		{
-			var packet = new EPacket(data, flags);
+			UPacket packet = new UPacket(data, flags);
 			NativeMethods.EnetPeerSend(this.peerPtr, channelID, packet.PacketPtr);
 			// enet_peer_send函数会自动删除packet,设置为0,防止Dispose或者析构函数再次删除
 			packet.PacketPtr = IntPtr.Zero;
 		}
 
-		public Task<byte[]> ReadAsync()
+		public Task<byte[]> RecvAsync()
 		{
 			var tcs = new TaskCompletionSource<byte[]>();
 
@@ -187,10 +186,10 @@ namespace UNet
 				{
 					if (eEvent.EventState == EventState.DISCONNECTED)
 					{
-						tcs.TrySetException(new EException("socket disconnected in receive"));
+						tcs.TrySetException(new UException("socket disconnected in receive"));
 					}
 
-					using (EPacket packet = new EPacket(eEvent.PacketPtr))
+					using (UPacket packet = new UPacket(eEvent.PacketPtr))
 					{
 						byte[] bytes = packet.Bytes;
 						tcs.TrySetResult(bytes);
@@ -227,45 +226,45 @@ namespace UNet
 			this.PeerPtr = IntPtr.Zero;
 		}
 
-		internal void OnConnected(EEvent eEvent)
+		internal void OnConnected(UEvent uEvent)
 		{
 			if (this.Connected == null)
 			{
 				return;
 			}
-			Action<EEvent> localConnected = this.Connected;
+			Action<UEvent> localConnected = this.Connected;
 			this.Connected = null;
 			// 此调用将让await ConnectAsync返回,所以null必须在此之前设置
-			localConnected(eEvent);
+			localConnected(uEvent);
 		}
 
-		internal void OnReceived(EEvent eEvent)
+		internal void OnReceived(UEvent uEvent)
 		{
 			// 如果应用层还未调用readasync则将包放到缓存队列
 			if (this.Received == null)
 			{
-				using (var packet = new EPacket(eEvent.PacketPtr))
+				using (UPacket packet = new UPacket(uEvent.PacketPtr))
 				{
-					var bytes = packet.Bytes;
+					byte[] bytes = packet.Bytes;
 					this.recvBuffer.AddLast(bytes);
 				}
 			}
 			else
 			{
-				Action<EEvent> localReceived = this.Received;
+				Action<UEvent> localReceived = this.Received;
 				this.Received = null;
 				// 此调用将让await ReadAsync返回,所以null必须在此之前设置
-				localReceived(eEvent);
+				localReceived(uEvent);
 			}
 		}
 
-		internal void OnDisconnect(EEvent eEvent)
+		internal void OnDisconnect(UEvent uEvent)
 		{
 			if (this.Disconnect == null)
 			{
 				return;
 			}
-			this.Disconnect(eEvent);
+			this.Disconnect(uEvent);
 		}
 
 		internal void OnError(int errorCode)