tanghai 13 лет назад
Родитель
Сommit
60a41ce62f

+ 9 - 7
CSharp/App/Modules/Robot/RobotViewModel.cs

@@ -38,7 +38,7 @@ namespace Modules.Robot
 		public RobotViewModel()
 		{
 			Library.Initialize();
-			host = new Host(8888, Native.ENET_PROTOCOL_MAXIMUM_PEER_ID);
+			host = new Host();
 
 			timer.Tick += delegate { this.host.Run(); };
 			timer.Start();
@@ -48,7 +48,10 @@ namespace Modules.Robot
 		{
 			try
 			{
-				Peer peer = await host.ConnectAsync(new Address { Host = "192.168.10.246", Port = 8901 });
+				Peer peer = await host.ConnectAsync(
+					new Address { Host = "192.168.10.246", Port = 8901 });
+				peer.Send(1, "aaaaaaaaaaa");
+				Packet packet = await peer.ReceiveAsync();
 			}
 			catch (ENetException e)
 			{
@@ -58,11 +61,10 @@ namespace Modules.Robot
 
 		public void Start()
 		{
-			Logger.Trace("11111111111111111111111");
-			//for (int i = 0; i < 4095; ++i)
-			//{
-			//	StartClient();
-			//}
+			for (int i = 0; i < 4095; ++i)
+			{
+				StartClient();
+			}
 		}
 	}
 }

+ 9 - 21
CSharp/ThirdParty/ENetCS/Event.cs

@@ -3,32 +3,20 @@ using System.Runtime.InteropServices;
 
 namespace ENet
 {
-	public struct Event
+	public class Event
 	{
-		private readonly IntPtr e;
+		private readonly ENetEvent ev;
 
-		public Event(IntPtr e)
+		public Event(ENetEvent ev)
 		{
-			this.e = e;
+			this.ev = ev;
 		}
 
-		public ENetEvent Struct
+		public ENetEvent Ev
 		{
 			get
 			{
-				return (ENetEvent)Marshal.PtrToStructure(this.e, typeof(ENetEvent));
-			}
-			set
-			{
-				Marshal.StructureToPtr(value, this.e, false);
-			}
-		}
-
-		public IntPtr NativePtr
-		{
-			get
-			{
-				return e;
+				return this.ev;
 			}
 		}
 
@@ -36,7 +24,7 @@ namespace ENet
 		{
 			get
 			{
-				return new Packet(this.Struct.packet);
+				return new Packet(this.Ev.packet);
 			}
 		}
 
@@ -44,7 +32,7 @@ namespace ENet
 		{
 			get
 			{
-				return new Peer(this.Struct.peer);
+				return new Peer(this.Ev.peer);
 			}
 		}
 
@@ -52,7 +40,7 @@ namespace ENet
 		{
 			get
 			{
-				return this.Struct.type;
+				return this.Ev.type;
 			}
 		}
 	}

+ 7 - 13
CSharp/ThirdParty/ENetCS/Host.cs

@@ -9,11 +9,6 @@ namespace ENet
 		private readonly object eventsLock = new object();
 		private Action events;
 
-		public Host(ushort port, uint peerLimit):
-			this(new Address { Port = port }, peerLimit)
-		{
-		}
-
 		public Host(Address address, uint peerLimit = Native.ENET_PROTOCOL_MAXIMUM_PEER_ID, 
 			uint channelLimit = 0, uint incomingBandwidth = 0,
 			uint outgoingBandwidth = 0, bool enableCrc = true)
@@ -95,9 +90,9 @@ namespace ENet
 
 		private int CheckEvents(out Event e)
 		{
-			IntPtr nativeEvent;
-			int ret = Native.enet_host_check_events(this.host, out nativeEvent);
-			e = new Event(nativeEvent);
+			var enetEv = new ENetEvent();
+			int ret = Native.enet_host_check_events(this.host, enetEv);
+			e = new Event(enetEv);
 			return ret;
 		}
 
@@ -107,8 +102,7 @@ namespace ENet
 			{
 				throw new ArgumentOutOfRangeException("timeout");
 			}
-			IntPtr e = IntPtr.Zero;
-			return Native.enet_host_service(this.host, ref e, (uint)timeout);
+			return Native.enet_host_service(this.host, null, (uint)timeout);
 		}
 
 		public void Broadcast(byte channelID, ref Packet packet)
@@ -210,17 +204,17 @@ namespace ENet
 				{
 					case EventType.Connect:
 					{
-						Peer.PeerEventsManager.OnConnected(e.Struct.peer, e);
+						Peer.PeerEventsManager.OnConnected(e.Ev.peer, e);
 						break;
 					}
 					case EventType.Receive:
 					{
-						Peer.PeerEventsManager.OnReceived(e.Struct.peer, e);
+						Peer.PeerEventsManager.OnReceived(e.Ev.peer, e);
 						break;
 					}
 					case EventType.Disconnect:
 					{
-						Peer.PeerEventsManager.OnDisconnect(e.Struct.peer, e);
+						Peer.PeerEventsManager.OnDisconnect(e.Ev.peer, e);
 						break;
 					}
 				}

+ 2 - 2
CSharp/ThirdParty/ENetCS/Native.cs

@@ -76,10 +76,10 @@ namespace ENet
 		public static extern void enet_host_flush(IntPtr host);
 
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		public static extern int enet_host_check_events(IntPtr host, out IntPtr e);
+		public static extern int enet_host_check_events(IntPtr host, ENetEvent ev);
 
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
-		public static extern int enet_host_service(IntPtr host, ref IntPtr e, uint timeout);
+		public static extern int enet_host_service(IntPtr host, ENetEvent ev, uint timeout);
 
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
 		public static extern uint enet_time_get();

+ 0 - 4
CSharp/ThirdParty/ENetCS/Packet.cs

@@ -9,10 +9,6 @@ namespace ENet
 
 		public Packet(IntPtr packet)
 		{
-			if (packet == IntPtr.Zero)
-			{
-				throw new InvalidOperationException("No native packet.");
-			}
 			this.packet = packet;
 		}
 

+ 13 - 9
CSharp/ThirdParty/ENetCS/Peer.cs

@@ -20,10 +20,6 @@ namespace ENet
 
 		public Peer(IntPtr peer)
 		{
-			if (peer == IntPtr.Zero)
-			{
-				throw new InvalidOperationException("No native peer.");
-			}
 			this.peer = peer;
 			PeerEventsManager.Add(peer);
 		}
@@ -55,6 +51,10 @@ namespace ENet
 		{
 			get
 			{
+				if (this.peer == IntPtr.Zero)
+				{
+					return new ENetPeer();
+				}
 				return (ENetPeer)Marshal.PtrToStructure(this.peer, typeof(ENetPeer));
 			}
 			set
@@ -75,10 +75,19 @@ namespace ENet
 		{
 			get
 			{
+				if (this.peer == IntPtr.Zero)
+				{
+					return PeerState.Uninitialized;
+				}
 				return Struct.state;
 			}
 		}
 
+		public void Ping()
+		{
+			Native.enet_peer_ping(this.peer);
+		}
+
 		public void ConfigureThrottle(uint interval, uint acceleration, uint deceleration)
 		{
 			Native.enet_peer_throttle_configure(this.peer, interval, acceleration, deceleration);
@@ -124,10 +133,5 @@ namespace ENet
 		{
 			Native.enet_peer_disconnect_now(this.peer, data);
 		}
-
-		public void Ping()
-		{
-			Native.enet_peer_ping(this.peer);
-		}
 	}
 }

+ 6 - 11
CSharp/ThirdParty/ENetCS/Structs.cs

@@ -27,11 +27,6 @@ namespace ENet
 		Zombie = 9
 	}
 
-	public enum AddressType
-	{
-		IPv4 = 0
-	}
-
 	[Flags]
 	public enum PacketFlags
 	{
@@ -81,13 +76,13 @@ namespace ENet
 	}
 
 	[StructLayout(LayoutKind.Sequential)]
-	public struct ENetEvent
+	public class ENetEvent
 	{
-		public readonly EventType type;
-		public readonly IntPtr peer;
-		public readonly byte channelID;
-		public readonly uint data;
-		public readonly IntPtr packet;
+		public EventType type;
+		public IntPtr peer;
+		public byte channelID;
+		public uint data;
+		public IntPtr packet;
 	}
 
 	[StructLayout(LayoutKind.Sequential)]