Bläddra i källkod

修复了enet csharp库一个bug。c# enet调用readasync和acceptasync需要处理两种情况:
1.readasync或者acceptasync调用之前就已经发包过来,这个时候需要将包缓存起来
2.readasync或者acceptasync调用的时候,封包还未发过来,这时候需要设置回调等待

tanghai 12 år sedan
förälder
incheckning
169a975560
38 ändrade filer med 387 tillägg och 333 borttagningar
  1. 1 1
      CSharp/App/BossBase/BossBase.csproj
  2. 1 1
      CSharp/App/BossClient/BossClient.cs
  3. 1 1
      CSharp/App/BossClient/BossClient.csproj
  4. 1 1
      CSharp/App/BossCommand/BossCommand.csproj
  5. 1 1
      CSharp/App/Editor/Editor.csproj
  6. 1 1
      CSharp/App/Infrastructure/Infrastructure.csproj
  7. 1 1
      CSharp/App/Modules/Login/Login.csproj
  8. 1 1
      CSharp/App/Modules/Robot/Robot.csproj
  9. 1 1
      CSharp/App/Modules/Tree/Tree.csproj
  10. 1 1
      CSharp/App/Modules/WCFClient/WCFClient.csproj
  11. 1 1
      CSharp/Game/BehaviorTree/BehaviorTree.csproj
  12. 1 1
      CSharp/Game/BehaviorTreeTest/BehaviorTreeTest.csproj
  13. 1 1
      CSharp/Game/Component/Component.csproj
  14. 3 2
      CSharp/Game/Component/ConfigManager.cs
  15. 1 1
      CSharp/Game/Logic/Logic.csproj
  16. 1 1
      CSharp/Game/World/World.csproj
  17. 1 1
      CSharp/Game/WorldTest/WorldTest.csproj
  18. 2 2
      CSharp/Platform/ENet/EEvent.cs
  19. 4 5
      CSharp/Platform/ENet/ENet.csproj
  20. 6 6
      CSharp/Platform/ENet/EPacket.cs
  21. 87 66
      CSharp/Platform/ENet/EService.cs
  22. 126 53
      CSharp/Platform/ENet/ESocket.cs
  23. 0 74
      CSharp/Platform/ENet/ESocketEvent.cs
  24. 4 4
      CSharp/Platform/ENet/Library.cs
  25. 67 67
      CSharp/Platform/ENet/NativeMethods.cs
  26. 1 1
      CSharp/Platform/ENet/PeersManager.cs
  27. 3 2
      CSharp/Platform/ENetCpp/ENet/enet.h
  28. 1 1
      CSharp/Platform/ENetCpp/ENet/protocol.h
  29. 1 0
      CSharp/Platform/ENetCpp/ENet/win32.h
  30. 2 2
      CSharp/Platform/ENetCpp/ENetCpp.vcxproj
  31. 18 3
      CSharp/Platform/ENetCpp/unix.cc
  32. 4 0
      CSharp/Platform/ENetCpp/win32.cc
  33. 37 24
      CSharp/Platform/ENetTest/ENetClientServerTest.cs
  34. 1 1
      CSharp/Platform/ENetTest/ENetTest.csproj
  35. 1 1
      CSharp/Platform/Helper/Helper.csproj
  36. 1 1
      CSharp/Platform/Hooks/Hooks.csproj
  37. 1 1
      CSharp/Platform/Log/Log.csproj
  38. 1 1
      CSharp/Tools/ProtobufTool/ProtobufTool.csproj

+ 1 - 1
CSharp/App/BossBase/BossBase.csproj

@@ -18,7 +18,7 @@
     <DebugSymbols>true</DebugSymbols>
     <DebugType>full</DebugType>
     <Optimize>false</Optimize>
-    <OutputPath>..\..\Bin\</OutputPath>
+    <OutputPath>..\..\Bin\Debug\</OutputPath>
     <DefineConstants>DEBUG;TRACE</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>

+ 1 - 1
CSharp/App/BossClient/BossClient.cs

@@ -10,7 +10,7 @@ namespace BossClient
 	{
 		private int sessionId;
 
-		private readonly IOService ioService = new IOService();
+		private readonly EService ioService = new EService();
 
 		public BossClient()
 		{

+ 1 - 1
CSharp/App/BossClient/BossClient.csproj

@@ -18,7 +18,7 @@
     <DebugSymbols>true</DebugSymbols>
     <DebugType>full</DebugType>
     <Optimize>false</Optimize>
-    <OutputPath>..\..\Bin\</OutputPath>
+    <OutputPath>..\..\Bin\Debug\</OutputPath>
     <DefineConstants>DEBUG;TRACE</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>

+ 1 - 1
CSharp/App/BossCommand/BossCommand.csproj

@@ -18,7 +18,7 @@
     <DebugSymbols>true</DebugSymbols>
     <DebugType>full</DebugType>
     <Optimize>false</Optimize>
-    <OutputPath>..\..\Bin\</OutputPath>
+    <OutputPath>..\..\Bin\Debug\</OutputPath>
     <DefineConstants>DEBUG;TRACE</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>

+ 1 - 1
CSharp/App/Editor/Editor.csproj

@@ -41,7 +41,7 @@
     <DebugSymbols>true</DebugSymbols>
     <DebugType>full</DebugType>
     <Optimize>false</Optimize>
-    <OutputPath>..\..\Bin\</OutputPath>
+    <OutputPath>..\..\Bin\Debug\</OutputPath>
     <DefineConstants>DEBUG;TRACE</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>

+ 1 - 1
CSharp/App/Infrastructure/Infrastructure.csproj

@@ -20,7 +20,7 @@
     <DebugSymbols>true</DebugSymbols>
     <DebugType>full</DebugType>
     <Optimize>false</Optimize>
-    <OutputPath>..\..\Bin\</OutputPath>
+    <OutputPath>..\..\Bin\Debug\</OutputPath>
     <DefineConstants>DEBUG;TRACE</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>

+ 1 - 1
CSharp/App/Modules/Login/Login.csproj

@@ -20,7 +20,7 @@
     <DebugSymbols>true</DebugSymbols>
     <DebugType>full</DebugType>
     <Optimize>false</Optimize>
-    <OutputPath>..\..\..\Bin\</OutputPath>
+    <OutputPath>..\..\..\Bin\Debug\</OutputPath>
     <DefineConstants>DEBUG;TRACE</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>

+ 1 - 1
CSharp/App/Modules/Robot/Robot.csproj

@@ -18,7 +18,7 @@
     <DebugSymbols>true</DebugSymbols>
     <DebugType>full</DebugType>
     <Optimize>false</Optimize>
-    <OutputPath>..\..\..\Bin\</OutputPath>
+    <OutputPath>..\..\..\Bin\Debug\</OutputPath>
     <DefineConstants>DEBUG;TRACE</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>

+ 1 - 1
CSharp/App/Modules/Tree/Tree.csproj

@@ -22,7 +22,7 @@
     <DebugSymbols>true</DebugSymbols>
     <DebugType>full</DebugType>
     <Optimize>false</Optimize>
-    <OutputPath>..\..\..\Bin\</OutputPath>
+    <OutputPath>..\..\..\Bin\Debug\</OutputPath>
     <DefineConstants>DEBUG;TRACE</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>

+ 1 - 1
CSharp/App/Modules/WCFClient/WCFClient.csproj

@@ -20,7 +20,7 @@
     <DebugSymbols>true</DebugSymbols>
     <DebugType>full</DebugType>
     <Optimize>false</Optimize>
-    <OutputPath>..\..\..\Bin\</OutputPath>
+    <OutputPath>..\..\..\Bin\Debug\</OutputPath>
     <DefineConstants>DEBUG;TRACE</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>

+ 1 - 1
CSharp/Game/BehaviorTree/BehaviorTree.csproj

@@ -18,7 +18,7 @@
     <DebugSymbols>true</DebugSymbols>
     <DebugType>full</DebugType>
     <Optimize>false</Optimize>
-    <OutputPath>..\..\Bin\</OutputPath>
+    <OutputPath>..\..\Bin\Debug\</OutputPath>
     <DefineConstants>DEBUG;TRACE</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>

+ 1 - 1
CSharp/Game/BehaviorTreeTest/BehaviorTreeTest.csproj

@@ -21,7 +21,7 @@
     <DebugSymbols>true</DebugSymbols>
     <DebugType>full</DebugType>
     <Optimize>false</Optimize>
-    <OutputPath>..\..\Bin\</OutputPath>
+    <OutputPath>..\..\Bin\Debug\</OutputPath>
     <DefineConstants>DEBUG;TRACE</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>

+ 1 - 1
CSharp/Game/Component/Component.csproj

@@ -18,7 +18,7 @@
     <DebugSymbols>true</DebugSymbols>
     <DebugType>full</DebugType>
     <Optimize>false</Optimize>
-    <OutputPath>..\..\Bin\</OutputPath>
+    <OutputPath>..\..\Bin\Debug\</OutputPath>
     <DefineConstants>DEBUG;TRACE</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>

+ 3 - 2
CSharp/Game/Component/ConfigManager.cs

@@ -26,7 +26,7 @@ namespace Component
 
 		private void Load()
 		{
-			this.allConfig = new Dictionary<string, object>();
+			var localAllConfig = new Dictionary<string, object>();
 			string currentDir = AppDomain.CurrentDomain.BaseDirectory;
 			Type[] types = typeof(ConfigManager).Assembly.GetTypes();
 			foreach (var type in types)
@@ -66,8 +66,9 @@ namespace Component
 					iSupportInitialize.EndInit();
 				}
 
-				allConfig[iInit.ConfigName] = obj;
+				localAllConfig[iInit.ConfigName] = obj;
 			}
+			this.allConfig = localAllConfig;
 		}
 
 		public void Reload()

+ 1 - 1
CSharp/Game/Logic/Logic.csproj

@@ -16,7 +16,7 @@
     <DebugSymbols>true</DebugSymbols>
     <DebugType>full</DebugType>
     <Optimize>false</Optimize>
-    <OutputPath>..\..\Bin\</OutputPath>
+    <OutputPath>..\..\Bin\Debug\</OutputPath>
     <DefineConstants>DEBUG;TRACE</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>

+ 1 - 1
CSharp/Game/World/World.csproj

@@ -18,7 +18,7 @@
     <DebugSymbols>true</DebugSymbols>
     <DebugType>full</DebugType>
     <Optimize>false</Optimize>
-    <OutputPath>..\..\Bin\</OutputPath>
+    <OutputPath>..\..\Bin\Debug\</OutputPath>
     <DefineConstants>DEBUG;TRACE</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>

+ 1 - 1
CSharp/Game/WorldTest/WorldTest.csproj

@@ -23,7 +23,7 @@
     <DebugSymbols>true</DebugSymbols>
     <DebugType>full</DebugType>
     <Optimize>false</Optimize>
-    <OutputPath>..\..\Bin\</OutputPath>
+    <OutputPath>..\..\Bin\Debug\</OutputPath>
     <DefineConstants>DEBUG;TRACE</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>

+ 2 - 2
CSharp/Platform/ENet/Event.cs → CSharp/Platform/ENet/EEvent.cs

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

+ 4 - 5
CSharp/Platform/ENet/ENet.csproj

@@ -18,7 +18,7 @@
     <DebugSymbols>true</DebugSymbols>
     <DebugType>full</DebugType>
     <Optimize>false</Optimize>
-    <OutputPath>..\..\Bin\</OutputPath>
+    <OutputPath>..\..\Bin\Debug\</OutputPath>
     <DefineConstants>DEBUG;TRACE</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>
@@ -39,14 +39,13 @@
   <ItemGroup>
     <Compile Include="Address.cs" />
     <Compile Include="EException.cs" />
-    <Compile Include="Event.cs" />
-    <Compile Include="IOService.cs" />
+    <Compile Include="EEvent.cs" />
+    <Compile Include="EService.cs" />
     <Compile Include="Library.cs" />
     <Compile Include="NativeMethods.cs" />
-    <Compile Include="ESocketEvent.cs" />
     <Compile Include="PeersManager.cs" />
     <Compile Include="NativeStructs.cs" />
-    <Compile Include="Packet.cs" />
+    <Compile Include="EPacket.cs" />
     <Compile Include="ESocket.cs" />
   </ItemGroup>
   <ItemGroup>

+ 6 - 6
CSharp/Platform/ENet/Packet.cs → CSharp/Platform/ENet/EPacket.cs

@@ -3,29 +3,29 @@ using System.Runtime.InteropServices;
 
 namespace ENet
 {
-	internal sealed class Packet: IDisposable
+	internal sealed class EPacket: IDisposable
 	{
 		private IntPtr packet;
 
-		public Packet(IntPtr packet)
+		public EPacket(IntPtr packet)
 		{
 			this.packet = packet;
 		}
 
-		public Packet(byte[] data, PacketFlags flags = PacketFlags.None)
+		public EPacket(byte[] data, PacketFlags flags = PacketFlags.None)
 		{
 			if (data == null)
 			{
 				throw new ArgumentNullException("data");
 			}
-			this.packet = NativeMethods.enet_packet_create(data, (uint) data.Length, flags);
+			this.packet = NativeMethods.EnetPacketCreate(data, (uint) data.Length, flags);
 			if (this.packet == IntPtr.Zero)
 			{
 				throw new EException("Packet creation call failed");
 			}
 		}
 
-		~Packet()
+		~EPacket()
 		{
 			this.Dispose(false);
 		}
@@ -43,7 +43,7 @@ namespace ENet
 				return;
 			}
 
-			NativeMethods.enet_packet_destroy(this.packet);
+			NativeMethods.EnetPacketDestroy(this.packet);
 			this.packet = IntPtr.Zero;
 		}
 

+ 87 - 66
CSharp/Platform/ENet/IOService.cs → CSharp/Platform/ENet/EService.cs

@@ -1,30 +1,41 @@
-using System;
-
+using System;
+using System.Collections.Generic;
+using Log;
+
 namespace ENet
 {
-	public sealed class IOService: IDisposable
+	public sealed class EService: IDisposable
 	{
-		static IOService()
+		static EService()
 		{
 			Library.Initialize();
 		}
 
-		private readonly PeersManager peersManager = new PeersManager();
-
-		public PeersManager PeersManager
+		private readonly PeersManager peersManager = new PeersManager();
+		private readonly LinkedList<EEvent> connEEvents = new LinkedList<EEvent>();
+
+		internal PeersManager PeersManager
 		{
 			get
 			{
 				return this.peersManager;
 			}
-		}
-
-		private IntPtr host;
+		}
+
+		internal LinkedList<EEvent> ConnEEvents
+		{
+			get
+			{
+				return this.connEEvents;
+			}
+		}
+
+		private IntPtr host;
 		private bool isRunning = true;
 		private readonly object eventsLock = new object();
 		private Action events;
 
-		public IOService(string hostName, ushort port, 
+		public EService(string hostName, ushort port, 
 			uint peerLimit = NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID,
 			uint channelLimit = 0, 
 			uint incomingBandwidth = 0, 
@@ -42,7 +53,7 @@ namespace ENet
 
 			var address = new Address { HostName = hostName, Port = port };
 			ENetAddress nativeAddress = address.Struct;
-			this.host = NativeMethods.enet_host_create(
+			this.host = NativeMethods.EnetHostCreate(
 				ref nativeAddress, peerLimit, channelLimit, incomingBandwidth, 
 				outgoingBandwidth);
 
@@ -52,7 +63,7 @@ namespace ENet
 			}
 		}
 
-		public IOService(
+		public EService(
 			uint peerLimit = NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID, 
 			uint channelLimit = 0,
 			uint incomingBandwidth = 0, 
@@ -68,7 +79,7 @@ namespace ENet
 				throw new ArgumentOutOfRangeException(string.Format("channelLimit: {0}", channelLimit));
 			}
 
-			this.host = NativeMethods.enet_host_create(
+			this.host = NativeMethods.EnetHostCreate(
 				IntPtr.Zero, peerLimit, channelLimit, incomingBandwidth, outgoingBandwidth);
 
 			if (this.host == IntPtr.Zero)
@@ -77,7 +88,7 @@ namespace ENet
 			}
 		}
 
-		~IOService()
+		~EService()
 		{
 			this.Dispose(false);
 		}
@@ -86,8 +97,8 @@ namespace ENet
 		{
 			this.Dispose(true);
 			GC.SuppressFinalize(this);
-		}
-
+		}
+
 		private void Dispose(bool disposing)
 		{
 			if (this.host == IntPtr.Zero)
@@ -95,7 +106,7 @@ namespace ENet
 				return;
 			}
 
-			NativeMethods.enet_host_destroy(this.host);
+			NativeMethods.EnetHostDestroy(this.host);
 
 			this.host = IntPtr.Zero;
 		}
@@ -110,39 +121,39 @@ namespace ENet
 
 		public void EnableCrc()
 		{
-			NativeMethods.enet_enable_crc(this.host);
+			NativeMethods.EnetEnableCrc(this.host);
 		}
 
-		private Event GetEvent()
+		private EEvent GetEvent()
 		{
 			var enetEv = new ENetEvent();
-			int ret = NativeMethods.enet_host_check_events(this.host, enetEv);
+			int ret = NativeMethods.EnetHostCheckEvents(this.host, enetEv);
 			if (ret <= 0)
 			{
 				return null;
 			}
-			var e = new Event(enetEv);
+			var e = new EEvent(enetEv);
 			return e;
 		}
 
 		public void CompressWithRangeCoder()
 		{
-			NativeMethods.enet_host_compress_with_range_coder(this.host);
+			NativeMethods.EnetHostCompressWithRangeCoder(this.host);
 		}
 
 		public void DoNotCompress()
 		{
-			NativeMethods.enet_host_compress(this.host, IntPtr.Zero);
+			NativeMethods.EnetHostCompress(this.host, IntPtr.Zero);
 		}
 
 		public void Flush()
 		{
-			NativeMethods.enet_host_flush(this.host);
+			NativeMethods.EnetHostFlush(this.host);
 		}
 
 		public void SetBandwidthLimit(uint incomingBandwidth, uint outgoingBandwidth)
 		{
-			NativeMethods.enet_host_bandwidth_limit(this.host, incomingBandwidth, outgoingBandwidth);
+			NativeMethods.EnetHostBandwidthLimit(this.host, incomingBandwidth, outgoingBandwidth);
 		}
 
 		public void SetChannelLimit(uint channelLimit)
@@ -151,7 +162,7 @@ namespace ENet
 			{
 				throw new ArgumentOutOfRangeException(string.Format("channelLimit: {0}", channelLimit));
 			}
-			NativeMethods.enet_host_channel_limit(this.host, channelLimit);
+			NativeMethods.EnetHostChannelLimit(this.host, channelLimit);
 		}
 
 		public event Action Events
@@ -198,7 +209,7 @@ namespace ENet
 			{
 				throw new ArgumentOutOfRangeException(string.Format("timeout: {0}", timeout));
 			}
-			return NativeMethods.enet_host_service(this.host, null, (uint)timeout);
+			return NativeMethods.EnetHostService(this.host, null, (uint)timeout);
 		}
 
 		public void RunOnce(int timeout = 0)
@@ -217,62 +228,72 @@ namespace ENet
 
 			while (true)
 			{
-				Event ev = this.GetEvent();
-				if (ev == null)
+				EEvent eEvent = this.GetEvent();
+				if (eEvent == null)
 				{
 					return;
 				}
 
-				switch (ev.Type)
+				switch (eEvent.Type)
 				{
 					case EventType.Connect:
 					{
-						// 这是一个connect peer,否则是一个accept peer
-						if (this.PeersManager.ContainsKey(ev.PeerPtr))
-						{
-							var peer = this.PeersManager[ev.PeerPtr];
-							peer.ESocketEvent.OnConnected(ev);
-							peer.ESocketEvent.Connected = null;
-						}
+						// 这是一个connect peer
+						if (this.PeersManager.ContainsKey(eEvent.PeerPtr))
+						{
+							ESocket eSocket = this.PeersManager[eEvent.PeerPtr];
+							eSocket.OnConnected(eEvent);
+						}
+						// accept peer
 						else
-						{
-							var peer = this.PeersManager[IntPtr.Zero];
-
-							this.PeersManager.Remove(IntPtr.Zero);
-
-							peer.PeerPtr = ev.PeerPtr;
-							this.PeersManager.Add(peer.PeerPtr, peer);
-
-							peer.ESocketEvent.OnConnected(ev);
-							peer.ESocketEvent.Connected = null;
+						{
+							// 如果server端没有acceptasync,则请求放入队列
+							if (!this.PeersManager.ContainsKey(IntPtr.Zero))
+							{
+								this.connEEvents.AddLast(eEvent);
+							}
+							else
+							{
+								ESocket eSocket = this.PeersManager[IntPtr.Zero];
+								eSocket.OnConnected(eEvent);
+							}
 						}
 						break;
 					}
 					case EventType.Receive:
-					{
-						var peer = this.PeersManager[ev.PeerPtr];
-						peer.ESocketEvent.OnReceived(ev);
-						peer.ESocketEvent.Received = null;
+					{
+						ESocket eSocket = this.PeersManager[eEvent.PeerPtr];
+						eSocket.OnReceived(eEvent);
 						break;
 					}
 					case EventType.Disconnect:
-					{
-						ev.EventState = EventState.DISCONNECTED;
-
-						var peer = this.PeersManager[ev.PeerPtr];
-						ESocketEvent peerEvent = peer.ESocketEvent;
-
-						this.PeersManager.Remove(peer.PeerPtr);
+					{
+						// 如果链接还在缓存中,则删除
+						foreach (EEvent connEEvent in this.connEEvents)
+						{
+							if (connEEvent.PeerPtr != eEvent.PeerPtr)
+							{
+								continue;
+							}
+							this.connEEvents.Remove(connEEvent);
+							return;
+						}
+
+						// 链接已经被应用层接收
+						eEvent.EventState = EventState.DISCONNECTED;
+						ESocket eSocket = this.PeersManager[eEvent.PeerPtr];
+						this.PeersManager.Remove(eSocket.PeerPtr);
 						// enet_peer_disconnect会reset Peer,这里设置为0,防止再次Dispose
-						peer.PeerPtr = IntPtr.Zero;
-
-						if (peerEvent.Received != null)
-						{
-							peerEvent.OnReceived(ev);
+						eSocket.PeerPtr = IntPtr.Zero;
+
+						// 等待接收数据中的task抛出异常
+						if (eSocket.Received != null)
+						{
+							eSocket.OnReceived(eEvent);
 						}
 						else
-						{
-							peerEvent.OnDisconnect(ev);
+						{
+							eSocket.OnDisconnect(eEvent);
 						}
 						break;
 					}

+ 126 - 53
CSharp/Platform/ENet/ESocket.cs

@@ -1,16 +1,23 @@
-using System;
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
 using System.Runtime.InteropServices;
-using System.Threading.Tasks;
-
+using System.Threading.Tasks;
+using Log;
+
 namespace ENet
 {
 	public sealed class ESocket: IDisposable
 	{
-		private readonly ESocketEvent eSocketEvent = new ESocketEvent();
 		private IntPtr peerPtr = IntPtr.Zero;
-		private readonly IOService service;
-
-		public ESocket(IOService service)
+		private readonly EService service;
+		private readonly LinkedList<EEvent> recvEEvents = new LinkedList<EEvent>();
+
+		public Action<EEvent> Connected { get; set; }
+		public Action<EEvent> Received { get; set; }
+		public Action<EEvent> Disconnect { get; set; }
+
+		public ESocket(EService service)
 		{
 			this.service = service;
 		}
@@ -21,7 +28,7 @@ namespace ENet
 			{
 				return;
 			}
-			NativeMethods.enet_peer_reset(this.peerPtr);
+			NativeMethods.EnetPeerReset(this.peerPtr);
 			this.peerPtr = IntPtr.Zero;
 		}
 
@@ -53,14 +60,6 @@ namespace ENet
 			}
 		}
 
-		public ESocketEvent ESocketEvent
-		{
-			get
-			{
-				return this.eSocketEvent;
-			}
-		}
-
 		public PeerState State
 		{
 			get
@@ -75,12 +74,12 @@ namespace ENet
 
 		public void Ping()
 		{
-			NativeMethods.enet_peer_ping(this.peerPtr);
+			NativeMethods.EnetPeerPing(this.peerPtr);
 		}
 
 		public void ConfigureThrottle(uint interval, uint acceleration, uint deceleration)
 		{
-			NativeMethods.enet_peer_throttle_configure(this.peerPtr, interval, acceleration, deceleration);
+			NativeMethods.EnetPeerThrottleConfigure(this.peerPtr, interval, acceleration, deceleration);
 		}
 
 		public Task<bool> ConnectAsync(
@@ -96,76 +95,150 @@ namespace ENet
 			var tcs = new TaskCompletionSource<bool>();
 			var address = new Address { HostName = hostName, Port = port };
 			ENetAddress nativeAddress = address.Struct;
-			this.peerPtr = NativeMethods.enet_host_connect(
+			this.peerPtr = NativeMethods.EnetHostConnect(
 				this.service.HostPtr, ref nativeAddress, channelLimit, data);
 			if (this.peerPtr == IntPtr.Zero)
 			{
-				throw new EException("Host connect call failed.");
+				throw new EException("host connect call failed.");
 			}
 			this.service.PeersManager.Add(this.peerPtr, this);
-			this.ESocketEvent.Connected += e => tcs.TrySetResult(true);
+			this.Connected = e => tcs.TrySetResult(true);
 			return tcs.Task;
 		}
 
 		public Task<bool> AcceptAsync()
-		{
-			if (this.service.PeersManager.ContainsKey(IntPtr.Zero))
-			{
-				throw new EException("Do Not Accept Twice!");
+		{
+			if (this.service.PeersManager.ContainsKey(IntPtr.Zero))
+			{
+				throw new EException("do not accept twice!");
+			}
+
+			var tcs = new TaskCompletionSource<bool>();
+
+			// 如果有请求连接缓存的包,从缓存中取
+			if (this.service.ConnEEvents.Count > 0)
+			{
+				EEvent eEvent = this.service.ConnEEvents.First.Value;
+				this.service.ConnEEvents.RemoveFirst();
+
+				this.PeerPtr = eEvent.PeerPtr;
+				this.service.PeersManager.Add(this.PeerPtr, this);
+
+				tcs.TrySetResult(true);
+			}
+			else
+			{
+				this.service.PeersManager.Add(this.PeerPtr, this);
+				this.Connected = eEvent =>
+				{
+					this.service.PeersManager.Remove(IntPtr.Zero);
+
+					this.PeerPtr = eEvent.PeerPtr;
+					this.service.PeersManager.Add(this.PeerPtr, this);
+					tcs.TrySetResult(true); 
+				};
 			}
-			var tcs = new TaskCompletionSource<bool>();
-			this.service.PeersManager.Add(this.PeerPtr, this);
-			this.ESocketEvent.Connected += e => tcs.TrySetResult(true);
 			return tcs.Task;
-		}
-
-		public void WriteAsync(byte[] data, byte channelID = 0, PacketFlags flags = PacketFlags.None)
+		}
+
+		public void WriteAsync(byte[] data, byte channelID = 0, PacketFlags flags = PacketFlags.Reliable)
 		{
-			var packet = new Packet(data, flags);
-			NativeMethods.enet_peer_send(this.peerPtr, channelID, packet.PacketPtr);
+			var packet = new EPacket(data, flags);
+			NativeMethods.EnetPeerSend(this.peerPtr, channelID, packet.PacketPtr);
 			// enet_peer_send函数会自动删除packet,设置为0,防止Dispose或者析构函数再次删除
 			packet.PacketPtr = IntPtr.Zero;
 		}
 
 		public Task<byte[]> ReadAsync()
 		{
-			var tcs = new TaskCompletionSource<byte[]>();
-			this.ESocketEvent.Received += e =>
-			{
-				if (e.EventState == EventState.DISCONNECTED)
-				{
-					tcs.TrySetException(new EException("Peer Disconnected In Received"));
-				}
-
-				using (var packet = new Packet(e.PacketPtr))
-				{
-					var bytes = packet.Bytes;
-					packet.Dispose();
-					tcs.TrySetResult(bytes);
-				}
-			};
+			var tcs = new TaskCompletionSource<byte[]>();
+
+			// 如果有缓存的包,从缓存中取
+			if (this.recvEEvents.Count > 0)
+			{
+				EEvent eEvent = this.recvEEvents.First.Value;
+				this.recvEEvents.RemoveFirst();
+				using (var packet = new EPacket(eEvent.PacketPtr))
+				{
+					var bytes = packet.Bytes;
+					tcs.TrySetResult(bytes);
+				}
+			}
+			// 没有缓存封包,设置回调等待
+			else
+			{
+				this.Received = eEvent => 
+				{
+					if (eEvent.EventState == EventState.DISCONNECTED)
+					{
+						tcs.TrySetException(new EException("Peer Disconnected In Received"));
+					}
+
+					using (var packet = new EPacket(eEvent.PacketPtr))
+					{
+						var bytes = packet.Bytes;
+						tcs.TrySetResult(bytes);
+					}
+				};
+			}
 			return tcs.Task;
 		}
 
 		public Task<bool> DisconnectAsync(uint data = 0)
 		{
-			NativeMethods.enet_peer_disconnect(this.peerPtr, data);
+			NativeMethods.EnetPeerDisconnect(this.peerPtr, data);
 			var tcs = new TaskCompletionSource<bool>();
-			this.ESocketEvent.Disconnect += e => tcs.TrySetResult(true);
+			this.Disconnect = e => tcs.TrySetResult(true);
 			return tcs.Task;
 		}
 
 		public Task<bool> DisconnectLaterAsync(uint data = 0)
 		{
-			NativeMethods.enet_peer_disconnect_later(this.peerPtr, data);
+			NativeMethods.EnetPeerDisconnectLater(this.peerPtr, data);
 			var tcs = new TaskCompletionSource<bool>();
-			this.ESocketEvent.Disconnect += e => tcs.TrySetResult(true);
+			this.Disconnect = e => tcs.TrySetResult(true);
 			return tcs.Task;
 		}
 
 		public void DisconnectNow(uint data)
 		{
-			NativeMethods.enet_peer_disconnect_now(this.peerPtr, data);
+			NativeMethods.EnetPeerDisconnectNow(this.peerPtr, data);
+		}
+
+		internal void OnConnected(EEvent eEvent)
+		{
+			if (this.Connected == null)
+			{
+				return;
+			}
+			Action<EEvent> localConnected = this.Connected;
+			this.Connected = null;
+			// 此调用将让await ConnectAsync返回,所以null必须在此之前设置
+			localConnected(eEvent);
+		}
+
+		internal void OnReceived(EEvent eEvent)
+		{
+			if (this.Received == null)
+			{
+				this.recvEEvents.AddLast(eEvent);
+			}
+			else
+			{
+				Action<EEvent> localReceived = this.Received;
+				this.Received = null;
+				// 此调用将让await ReadAsync返回,所以null必须在此之前设置
+				localReceived(eEvent);	
+			}
+		}
+
+		internal void OnDisconnect(EEvent e)
+		{
+			if (this.Disconnect == null)
+			{
+				return;
+			}
+			this.Disconnect(e);
 		}
 	}
 }

+ 0 - 74
CSharp/Platform/ENet/ESocketEvent.cs

@@ -1,74 +0,0 @@
-using System;
-
-namespace ENet
-{
-	public class ESocketEvent
-	{
-		private Action<Event> connected;
-		private Action<Event> received;
-		private Action<Event> disconnect;
-
-		public Action<Event> Connected
-		{
-			get
-			{
-				return this.connected;
-			}
-			set
-			{
-				this.connected = value;
-			}
-		}
-
-		public Action<Event> Received
-		{
-			get
-			{
-				return this.received;
-			}
-			set
-			{
-				this.received = value;
-			}
-		}
-
-		public Action<Event> Disconnect
-		{
-			get
-			{
-				return this.disconnect;
-			}
-			set
-			{
-				this.disconnect = value;
-			}
-		}
-
-		internal void OnConnected(Event e)
-		{
-			if (this.connected == null)
-			{
-				return;
-			}
-			this.connected(e);
-		}
-
-		internal void OnReceived(Event e)
-		{
-			if (this.received == null)
-			{
-				return;
-			}
-			this.received(e);
-		}
-
-		internal void OnDisconnect(Event e)
-		{
-			if (this.disconnect == null)
-			{
-				return;
-			}
-			this.disconnect(e);
-		}
-	}
-}

+ 4 - 4
CSharp/Platform/ENet/Library.cs

@@ -5,7 +5,7 @@
 		public static void Initialize()
 		{
 			var inits = new ENetCallbacks();
-			int ret = NativeMethods.enet_initialize_with_callbacks(
+			int ret = NativeMethods.EnetInitializeWithCallbacks(
 				NativeMethods.ENET_VERSION, ref inits);
 			if (ret < 0)
 			{
@@ -15,18 +15,18 @@
 
 		public static void Deinitialize()
 		{
-			NativeMethods.enet_deinitialize();
+			NativeMethods.EnetDeinitialize();
 		}
 
 		public static uint Time
 		{
 			get
 			{
-				return NativeMethods.enet_time_get();
+				return NativeMethods.EnetTimeGet();
 			}
 			set
 			{
-				NativeMethods.enet_time_set(value);
+				NativeMethods.EnetTimeSet(value);
 			}
 		}
 	}

+ 67 - 67
CSharp/Platform/ENet/NativeMethods.cs

@@ -12,117 +12,117 @@ namespace ENet
 		public const int ENET_PEER_PACKET_THROTTLE_ACCELERATION = 2;
 		public const int ENET_PEER_PACKET_THROTTLE_DECELERATION = 2;
 		public const int ENET_PEER_PACKET_THROTTLE_INTERVAL = 5000;
-		public const int ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT = 0x01;
-		public const int ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT = 0xff;
+		public const int ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT = 1;
+		public const int ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT = 255;
 		public const int ENET_PROTOCOL_MAXIMUM_PEER_ID = 0xfff;
-		public const uint ENET_VERSION = (1 << 16) | (3 << 8) | (1);
+		public const uint ENET_VERSION = (1 << 16) | (3 << 8) | (10);
 		public const uint ENET_HOST_ANY = 0;
 		public const uint ENET_HOST_BROADCAST = 0xffffffff;
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern int enet_address_set_host(ref ENetAddress address, string hostName);
+		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_address_set_host")]
+		internal static extern int EnetAddressSetHost(ref ENetAddress address, string hostName);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern int enet_address_get_host(
+		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_address_get_host")]
+		internal static extern int EnetAddressGetHost(
 			ref ENetAddress address, StringBuilder hostName, uint nameLength);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern int enet_address_get_host_ip(
+		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_address_get_host_ip")]
+		internal static extern int EnetAddressGetHostIp(
 			ref ENetAddress address, StringBuilder hostIp, uint ipLength);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern void enet_deinitialize();
+		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_deinitialize")]
+		internal static extern void EnetDeinitialize();
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern int enet_initialize();
+		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_initialize")]
+		internal static extern int EnetInitialize();
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern int enet_initialize_with_callbacks(uint version, ref ENetCallbacks inits);
+		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_initialize_with_callbacks")]
+		internal static extern int EnetInitializeWithCallbacks(uint version, ref ENetCallbacks inits);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern void enet_enable_crc(IntPtr host);
+		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_enable_crc")]
+		internal static extern void EnetEnableCrc(IntPtr host);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern IntPtr enet_host_create(
+		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_host_create")]
+		internal static extern IntPtr EnetHostCreate(
 			ref ENetAddress address, uint peerLimit, uint channelLimit, uint incomingBandwidth,
 			uint outgoingBandwidth);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern IntPtr enet_host_create(
+		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_host_create")]
+		internal static extern IntPtr EnetHostCreate(
 			IntPtr address, uint peerLimit, uint channelLimit, uint incomingBandwidth,
 			uint outgoingBandwidth);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern void enet_host_destroy(IntPtr host);
+		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_host_destroy")]
+		internal static extern void EnetHostDestroy(IntPtr host);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern IntPtr enet_host_connect(
+		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_host_connect")]
+		internal static extern IntPtr EnetHostConnect(
 			IntPtr host, ref ENetAddress address, uint channelCount, uint data);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern void enet_host_broadcast(IntPtr host, byte channelID, IntPtr packet);
+		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_host_broadcast")]
+		internal static extern void EnetHostBroadcast(IntPtr host, byte channelID, IntPtr packet);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern void enet_host_compress(IntPtr host, IntPtr compressor);
+		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_host_compress")]
+		internal static extern void EnetHostCompress(IntPtr host, IntPtr compressor);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern int enet_host_compress_with_range_coder(IntPtr host);
+		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_host_compress_with_range_coder")]
+		internal static extern int EnetHostCompressWithRangeCoder(IntPtr host);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern void enet_host_channel_limit(IntPtr host, uint channelLimit);
+		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_host_channel_limit")]
+		internal static extern void EnetHostChannelLimit(IntPtr host, uint channelLimit);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern void enet_host_bandwidth_limit(
+		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_host_bandwidth_limit")]
+		internal static extern void EnetHostBandwidthLimit(
 			IntPtr host, uint incomingBandwidth, uint outgoingBandwidth);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern void enet_host_flush(IntPtr host);
+		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_host_flush")]
+		internal static extern void EnetHostFlush(IntPtr host);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern int enet_host_check_events(IntPtr host, ENetEvent ev);
+		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_host_check_events")]
+		internal static extern int EnetHostCheckEvents(IntPtr host, ENetEvent ev);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern int enet_host_service(IntPtr host, ENetEvent ev, uint timeout);
+		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_host_service")]
+		internal static extern int EnetHostService(IntPtr host, ENetEvent ev, uint timeout);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern uint enet_time_get();
+		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_time_get")]
+		internal static extern uint EnetTimeGet();
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern void enet_time_set(uint newTimeBase);
+		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_time_set")]
+		internal static extern void EnetTimeSet(uint newTimeBase);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern IntPtr enet_packet_create(
+		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_packet_create")]
+		internal static extern IntPtr EnetPacketCreate(
 			byte[] data, uint dataLength, PacketFlags flags);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern void enet_packet_destroy(IntPtr packet);
+		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_packet_destroy")]
+		internal static extern void EnetPacketDestroy(IntPtr packet);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern int enet_packet_resize(IntPtr packet, uint dataLength);
+		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_packet_resize")]
+		internal static extern int EnetPacketResize(IntPtr packet, uint dataLength);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern void enet_peer_throttle_configure(
+		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_peer_throttle_configure")]
+		internal static extern void EnetPeerThrottleConfigure(
 				IntPtr peer, uint interval, uint acceleration, uint deceleration);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern int enet_peer_send(IntPtr peer, byte channelID, IntPtr packet);
+		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_peer_send")]
+		internal static extern int EnetPeerSend(IntPtr peer, byte channelID, IntPtr packet);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern IntPtr enet_peer_receive(IntPtr peer, out byte channelID);
+		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_peer_receive")]
+		internal static extern IntPtr EnetPeerReceive(IntPtr peer, out byte channelID);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern void enet_peer_reset(IntPtr peer);
+		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_peer_reset")]
+		internal static extern void EnetPeerReset(IntPtr peer);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern void enet_peer_ping(IntPtr peer);
+		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_peer_ping")]
+		internal static extern void EnetPeerPing(IntPtr peer);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern void enet_peer_disconnect_now(IntPtr peer, uint data);
+		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_peer_disconnect_now")]
+		internal static extern void EnetPeerDisconnectNow(IntPtr peer, uint data);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern void enet_peer_disconnect(IntPtr peer, uint data);
+		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_peer_disconnect")]
+		internal static extern void EnetPeerDisconnect(IntPtr peer, uint data);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern void enet_peer_disconnect_later(IntPtr peer, uint data);
+		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_peer_disconnect_later")]
+		internal static extern void EnetPeerDisconnectLater(IntPtr peer, uint data);
 	}
 }

+ 1 - 1
CSharp/Platform/ENet/PeersManager.cs

@@ -3,7 +3,7 @@ using System.Collections.Generic;
 
 namespace ENet
 {
-	public class PeersManager
+	internal class PeersManager
 	{
 		private readonly Dictionary<IntPtr, ESocket> peersManager = new Dictionary<IntPtr, ESocket>();
 

+ 3 - 2
CSharp/Platform/ENetCpp/ENet/enet.h

@@ -25,7 +25,7 @@ extern "C"
 
 #define ENET_VERSION_MAJOR 1
 #define ENET_VERSION_MINOR 3
-#define ENET_VERSION_PATCH 9
+#define ENET_VERSION_PATCH 10
 #define ENET_VERSION_CREATE(major, minor, patch) (((major)<<16) | ((minor)<<8) | (patch))
 #define ENET_VERSION_GET_MAJOR(version) (((version)>>16)&0xFF)
 #define ENET_VERSION_GET_MINOR(version) (((version)>>8)&0xFF)
@@ -61,7 +61,8 @@ typedef enum _ENetSocketOption
    ENET_SOCKOPT_REUSEADDR = 5,
    ENET_SOCKOPT_RCVTIMEO  = 6,
    ENET_SOCKOPT_SNDTIMEO  = 7,
-   ENET_SOCKOPT_ERROR     = 8
+   ENET_SOCKOPT_ERROR     = 8,
+   ENET_SOCKOPT_NODELAY   = 9
 } ENetSocketOption;
 
 typedef enum _ENetSocketShutdown

+ 1 - 1
CSharp/Platform/ENetCpp/ENet/protocol.h

@@ -13,7 +13,7 @@ enum
    ENET_PROTOCOL_MAXIMUM_MTU             = 4096,
    ENET_PROTOCOL_MAXIMUM_PACKET_COMMANDS = 32,
    ENET_PROTOCOL_MINIMUM_WINDOW_SIZE     = 4096,
-   ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE     = 32768,
+   ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE     = 65536,
    ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT   = 1,
    ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT   = 255,
    ENET_PROTOCOL_MAXIMUM_PEER_ID         = 0xFFF,

+ 1 - 0
CSharp/Platform/ENetCpp/ENet/win32.h

@@ -11,6 +11,7 @@
 #pragma warning (disable: 4267) // size_t to int conversion
 #pragma warning (disable: 4244) // 64bit to 32bit int
 #pragma warning (disable: 4018) // signed/unsigned mismatch
+#pragma warning (disable: 4146) // unary minus operator applied to unsigned type
 #endif
 #endif
 

+ 2 - 2
CSharp/Platform/ENetCpp/ENetCpp.vcxproj

@@ -45,8 +45,8 @@
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
     <LinkIncremental>true</LinkIncremental>
     <IncludePath>$(SolutionDir)\Platform\;$(IncludePath)</IncludePath>
-    <OutDir>$(SolutionDir)\Bin\</OutDir>
-    <IntDir>$(SolutionDir)\Temp\</IntDir>
+    <OutDir>$(SolutionDir)\Bin\Debug\</OutDir>
+    <IntDir>$(SolutionDir)\Temp\Debug\</IntDir>
     <TargetExt>.dll</TargetExt>
     <TargetName>$(ProjectName)</TargetName>
   </PropertyGroup>

+ 18 - 3
CSharp/Platform/ENetCpp/unix.cc

@@ -2,13 +2,14 @@
  @file  unix.c
  @brief ENet Unix system specific functions
 */
-#ifndef WIN32
+#ifndef _WIN32
 
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <sys/ioctl.h>
 #include <sys/time.h>
 #include <arpa/inet.h>
+#include <netinet/tcp.h>
 #include <netdb.h>
 #include <unistd.h>
 #include <string.h>
@@ -258,11 +259,25 @@ enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value)
             break;
 
         case ENET_SOCKOPT_RCVTIMEO:
-            result = setsockopt (socket, SOL_SOCKET, SO_RCVTIMEO, (char *) & value, sizeof (int));
+        {
+            struct timeval timeVal;
+            timeVal.tv_sec = value / 1000;
+            timeVal.tv_usec = (value % 1000) * 1000;
+            result = setsockopt (socket, SOL_SOCKET, SO_RCVTIMEO, (char *) & timeVal, sizeof (struct timeval));
             break;
+        }
 
         case ENET_SOCKOPT_SNDTIMEO:
-            result = setsockopt (socket, SOL_SOCKET, SO_SNDTIMEO, (char *) & value, sizeof (int));
+        {
+            struct timeval timeVal;
+            timeVal.tv_sec = value / 1000;
+            timeVal.tv_usec = (value % 1000) * 1000;
+            result = setsockopt (socket, SOL_SOCKET, SO_SNDTIMEO, (char *) & timeVal, sizeof (struct timeval));
+            break;
+        }
+
+        case ENET_SOCKOPT_NODELAY:
+            result = setsockopt (socket, IPPROTO_TCP, TCP_NODELAY, (char *) & value, sizeof (int));
             break;
 
         default:

+ 4 - 0
CSharp/Platform/ENetCpp/win32.cc

@@ -195,6 +195,10 @@ enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value)
             result = setsockopt (socket, SOL_SOCKET, SO_SNDTIMEO, (char *) & value, sizeof (int));
             break;
 
+        case ENET_SOCKOPT_NODELAY:
+            result = setsockopt (socket, IPPROTO_TCP, TCP_NODELAY, (char *) & value, sizeof (int));
+            break;
+
         default:
             break;
     }

+ 37 - 24
CSharp/Platform/ENetTest/ENetClientServerTest.cs

@@ -1,6 +1,8 @@
-using System.Threading;
+using System.Diagnostics;
+using System.Threading;
 using ENet;
 using Helper;
+using Log;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 
 namespace ENetCSTest
@@ -8,32 +10,43 @@ namespace ENetCSTest
 	[TestClass]
 	public class ENetClientServerTest
 	{
-		private static async void ClientEvent(IOService service, string hostName, ushort port)
-		{
-			var eSocket = new ESocket(service);
-			await eSocket.ConnectAsync(hostName, port);
-			eSocket.WriteAsync("0123456789".ToByteArray());
-
-			var bytes = await eSocket.ReadAsync();
-			CollectionAssert.AreEqual("9876543210".ToByteArray(), bytes);
-
+		private const int pingPangCount = 10000;
+		private static async void ClientEvent(EService service, string hostName, ushort port)
+		{
+			var eSocket = new ESocket(service);
+			await eSocket.ConnectAsync(hostName, port);
+			var stopWatch = new Stopwatch();
+			stopWatch.Start();
+			for (int i = 0; i < pingPangCount; ++i)
+			{
+				eSocket.WriteAsync("0123456789".ToByteArray());
+
+				var bytes = await eSocket.ReadAsync();
+
+				CollectionAssert.AreEqual("9876543210".ToByteArray(), bytes);
+			}
+			stopWatch.Stop();
+			Logger.Debug("time: {0}", stopWatch.ElapsedMilliseconds);
 			await eSocket.DisconnectAsync();
-
 			service.Stop();
 		}
 
-		private static async void ServerEvent(IOService service, Barrier barrier)
+		private static async void ServerEvent(EService service, Barrier barrier)
 		{
-			barrier.SignalAndWait();
-			var eSocket = new ESocket(service);
-			await eSocket.AcceptAsync();
-			// Client断开,Server端收到Disconnect事件,结束Server线程
-			eSocket.ESocketEvent.Disconnect += ev => service.Stop();
-
-			var bytes = await eSocket.ReadAsync();
-			CollectionAssert.AreEqual("0123456789".ToByteArray(), bytes);
-
-			eSocket.WriteAsync("9876543210".ToByteArray(), 0, PacketFlags.Reliable);
+			barrier.SignalAndWait();
+			var eSocket = new ESocket(service);
+			await eSocket.AcceptAsync();
+			// Client断开,Server端收到Disconnect事件,结束Server线程
+			eSocket.Disconnect += ev => service.Stop();
+
+			for (int i = 0; i < pingPangCount; ++i)
+			{
+				var bytes = await eSocket.ReadAsync();
+
+				CollectionAssert.AreEqual("0123456789".ToByteArray(), bytes);
+
+				eSocket.WriteAsync("9876543210".ToByteArray());
+			}
 		}
 
 		[TestMethod]
@@ -41,8 +54,8 @@ namespace ENetCSTest
 		{
 			const string hostName = "127.0.0.1";
 			const ushort port = 8888;
-			var clientHost = new IOService();
-			var serverHost = new IOService(hostName, port);
+			var clientHost = new EService();
+			var serverHost = new EService(hostName, port);
 
 			var serverThread = new Thread(() => serverHost.Start(10));
 			var clientThread = new Thread(() => clientHost.Start(10));

+ 1 - 1
CSharp/Platform/ENetTest/ENetTest.csproj

@@ -21,7 +21,7 @@
     <DebugSymbols>true</DebugSymbols>
     <DebugType>full</DebugType>
     <Optimize>false</Optimize>
-    <OutputPath>..\..\Bin\</OutputPath>
+    <OutputPath>..\..\Bin\Debug\</OutputPath>
     <DefineConstants>DEBUG;TRACE</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>

+ 1 - 1
CSharp/Platform/Helper/Helper.csproj

@@ -18,7 +18,7 @@
     <DebugSymbols>true</DebugSymbols>
     <DebugType>full</DebugType>
     <Optimize>false</Optimize>
-    <OutputPath>..\..\Bin\</OutputPath>
+    <OutputPath>..\..\Bin\Debug\</OutputPath>
     <DefineConstants>DEBUG;TRACE</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>

+ 1 - 1
CSharp/Platform/Hooks/Hooks.csproj

@@ -18,7 +18,7 @@
     <DebugSymbols>true</DebugSymbols>
     <DebugType>full</DebugType>
     <Optimize>false</Optimize>
-    <OutputPath>..\..\Bin\</OutputPath>
+    <OutputPath>..\..\Bin\Debug\</OutputPath>
     <DefineConstants>DEBUG;TRACE</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>

+ 1 - 1
CSharp/Platform/Log/Log.csproj

@@ -18,7 +18,7 @@
     <DebugSymbols>true</DebugSymbols>
     <DebugType>full</DebugType>
     <Optimize>false</Optimize>
-    <OutputPath>..\..\Bin\</OutputPath>
+    <OutputPath>..\..\Bin\Debug\</OutputPath>
     <DefineConstants>DEBUG;TRACE</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>

+ 1 - 1
CSharp/Tools/ProtobufTool/ProtobufTool.csproj

@@ -16,7 +16,7 @@
     <DebugSymbols>true</DebugSymbols>
     <DebugType>full</DebugType>
     <Optimize>false</Optimize>
-    <OutputPath>..\..\Bin\</OutputPath>
+    <OutputPath>..\..\Bin\Debug\</OutputPath>
     <DefineConstants>DEBUG;TRACE</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>