Просмотр исходного кода

修复一个bug,ENet ENetPacket.DataLength类型为size_t, linux64位平台上是8字节,C#可以使用Intptr表示size_t

tanghai 11 лет назад
Родитель
Сommit
c331f605b7

+ 1 - 18
CSharp-Mono/CSharp-Mono.userprefs

@@ -1,23 +1,6 @@
 <Properties StartupItem="Platform\Common\Common.csproj">
   <MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" />
-  <MonoDevelop.Ide.Workbench>
-    <Pads>
-      <Pad Id="MonoDevelop.NUnit.TestPad">
-        <State expanded="True">
-          <Node name="Platform" expanded="True">
-            <Node name="TNetTest" expanded="True">
-              <Node name="TNetTest" expanded="True">
-                <Node name="TServiceTest" selected="True" />
-              </Node>
-            </Node>
-            <Node name="UNetTest" expanded="True">
-              <Node name="UNetTest" expanded="True" />
-            </Node>
-          </Node>
-        </State>
-      </Pad>
-    </Pads>
-  </MonoDevelop.Ide.Workbench>
+  <MonoDevelop.Ide.Workbench />
   <MonoDevelop.Ide.DebuggingService.Breakpoints>
     <BreakpointStore />
   </MonoDevelop.Ide.DebuggingService.Breakpoints>

+ 1 - 1
CSharp/App/Editor/NLog.config

@@ -8,6 +8,6 @@
 			layout="${longdate} ${threadid:padCharacter=0:padding=4:fixedLength=true} ${message}" />
 	</targets>
 	<rules>
-		<logger name="*" minlevel="Trace" writeTo="file" />
+		<logger name="*" minlevel="Debug" writeTo="file" />
 	</rules>
 </nlog>

+ 30 - 39
CSharp/Platform/UNet/NativeMethods.cs

@@ -16,115 +16,106 @@ namespace UNet
 		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) | (10);
 		public const uint ENET_HOST_ANY = 0;
 		public const uint ENET_HOST_BROADCAST = 0xffffffff;
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_address_set_host")]
+		[DllImport(LIB, EntryPoint = "enet_address_set_host")]
 		internal static extern int EnetAddressSetHost(ref ENetAddress address, string hostName);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_address_get_host")]
+		[DllImport(LIB, EntryPoint = "enet_address_get_host")]
 		internal static extern int EnetAddressGetHost(
 				ref ENetAddress address, StringBuilder hostName, uint nameLength);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl,
-				EntryPoint = "enet_address_get_host_ip")]
+		[DllImport(LIB, EntryPoint = "enet_address_get_host_ip")]
 		internal static extern int EnetAddressGetHostIp(
 				ref ENetAddress address, StringBuilder hostIp, uint ipLength);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_deinitialize")]
+		[DllImport(LIB, EntryPoint = "enet_deinitialize")]
 		internal static extern void EnetDeinitialize();
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_initialize")]
+		[DllImport(LIB, EntryPoint = "enet_initialize")]
 		internal static extern int EnetInitialize();
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_host_create")]
+		[DllImport(LIB, 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, EntryPoint = "enet_host_create")]
+		[DllImport(LIB, EntryPoint = "enet_host_create")]
 		internal static extern IntPtr EnetHostCreate(
 				IntPtr address, uint peerLimit, uint channelLimit, uint incomingBandwidth,
 				uint outgoingBandwidth);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_host_destroy")]
+		[DllImport(LIB, EntryPoint = "enet_host_destroy")]
 		internal static extern void EnetHostDestroy(IntPtr host);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_host_connect")]
+		[DllImport(LIB, EntryPoint = "enet_host_connect")]
 		internal static extern IntPtr EnetHostConnect(
 				IntPtr host, ref ENetAddress address, uint channelCount, uint data);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_host_broadcast")]
+		[DllImport(LIB, EntryPoint = "enet_host_broadcast")]
 		internal static extern void EnetHostBroadcast(IntPtr host, byte channelID, IntPtr packet);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_host_compress")]
+		[DllImport(LIB, EntryPoint = "enet_host_compress")]
 		internal static extern void EnetHostCompress(IntPtr host, IntPtr compressor);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl,
-				EntryPoint = "enet_host_compress_with_range_coder")]
+		[DllImport(LIB, EntryPoint = "enet_host_compress_with_range_coder")]
 		internal static extern int EnetHostCompressWithRangeCoder(IntPtr host);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl,
-				EntryPoint = "enet_host_channel_limit")]
+		[DllImport(LIB, EntryPoint = "enet_host_channel_limit")]
 		internal static extern void EnetHostChannelLimit(IntPtr host, uint channelLimit);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl,
-				EntryPoint = "enet_host_bandwidth_limit")]
+		[DllImport(LIB, EntryPoint = "enet_host_bandwidth_limit")]
 		internal static extern void EnetHostBandwidthLimit(
 				IntPtr host, uint incomingBandwidth, uint outgoingBandwidth);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_host_flush")]
+		[DllImport(LIB, EntryPoint = "enet_host_flush")]
 		internal static extern void EnetHostFlush(IntPtr host);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_host_check_events"
-				)]
+		[DllImport(LIB, EntryPoint = "enet_host_check_events")]
 		internal static extern int EnetHostCheckEvents(IntPtr host, ENetEvent ev);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_host_service")]
+		[DllImport(LIB, EntryPoint = "enet_host_service")]
 		internal static extern int EnetHostService(IntPtr host, ENetEvent ev, uint timeout);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_time_get")]
+		[DllImport(LIB, EntryPoint = "enet_time_get")]
 		internal static extern uint EnetTimeGet();
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_time_set")]
+		[DllImport(LIB, EntryPoint = "enet_time_set")]
 		internal static extern void EnetTimeSet(uint newTimeBase);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_packet_create")]
+		[DllImport(LIB, EntryPoint = "enet_packet_create")]
 		internal static extern IntPtr EnetPacketCreate(byte[] data, uint dataLength, PacketFlags flags);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_packet_destroy")]
+		[DllImport(LIB, EntryPoint = "enet_packet_destroy")]
 		internal static extern void EnetPacketDestroy(IntPtr packet);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_packet_resize")]
+		[DllImport(LIB, EntryPoint = "enet_packet_resize")]
 		internal static extern int EnetPacketResize(IntPtr packet, uint dataLength);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl,
-				EntryPoint = "enet_peer_throttle_configure")]
+		[DllImport(LIB, EntryPoint = "enet_peer_throttle_configure")]
 		internal static extern void EnetPeerThrottleConfigure(
 				IntPtr peer, uint interval, uint acceleration, uint deceleration);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_peer_send")]
+		[DllImport(LIB, EntryPoint = "enet_peer_send")]
 		internal static extern int EnetPeerSend(IntPtr peer, byte channelID, IntPtr packet);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_peer_receive")]
+		[DllImport(LIB, EntryPoint = "enet_peer_receive")]
 		internal static extern IntPtr EnetPeerReceive(IntPtr peer, out byte channelID);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_peer_reset")]
+		[DllImport(LIB, EntryPoint = "enet_peer_reset")]
 		internal static extern void EnetPeerReset(IntPtr peer);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_peer_ping")]
+		[DllImport(LIB, EntryPoint = "enet_peer_ping")]
 		internal static extern void EnetPeerPing(IntPtr peer);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl,
-				EntryPoint = "enet_peer_disconnect_now")]
+		[DllImport(LIB, EntryPoint = "enet_peer_disconnect_now")]
 		internal static extern void EnetPeerDisconnectNow(IntPtr peer, uint data);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_peer_disconnect")]
+		[DllImport(LIB, EntryPoint = "enet_peer_disconnect")]
 		internal static extern void EnetPeerDisconnect(IntPtr peer, uint data);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl,
-				EntryPoint = "enet_peer_disconnect_later")]
+		[DllImport(LIB, EntryPoint = "enet_peer_disconnect_later")]
 		internal static extern void EnetPeerDisconnectLater(IntPtr peer, uint data);
 	}
 }

+ 2 - 4
CSharp/Platform/UNet/NativeStructs.cs

@@ -54,12 +54,10 @@ namespace UNet
 	[StructLayout(LayoutKind.Sequential)]
 	internal struct ENetPacket
 	{
-		public uint ReferenceCount;
+		public IntPtr ReferenceCount; // size_t
 		public uint Flags;
 		public IntPtr Data;
-		public uint DataLength;
-		public IntPtr FreeCallback;
-		public IntPtr UserData;
+		public IntPtr DataLength; // size_t
 	}
 
 	[StructLayout(LayoutKind.Sequential)]

+ 3 - 1
CSharp/Platform/UNet/UPacket.cs

@@ -1,5 +1,6 @@
 using System;
 using System.Runtime.InteropServices;
+using Common.Logger;
 using Common.Network;
 
 namespace UNet
@@ -77,7 +78,8 @@ namespace UNet
 			get
 			{
 				ENetPacket enetPacket = this.Struct;
-				var bytes = new byte[enetPacket.DataLength];
+				Log.Debug("111111111111111111111: " + enetPacket.DataLength);
+				var bytes = new byte[(long)enetPacket.DataLength];
 				Marshal.Copy(enetPacket.Data, bytes, 0, (int) enetPacket.DataLength);
 				return bytes;
 			}

+ 1 - 3
CSharp/Platform/UNetTest/UServiceTest.cs

@@ -2,6 +2,7 @@
 using System.Threading;
 using System.Threading.Tasks;
 using Common.Helper;
+using Common.Logger;
 using Common.Network;
 using NUnit.Framework;
 using UNet;
@@ -20,7 +21,6 @@ namespace UNetTest
 
 			byte[] bytes = await channel.RecvAsync();
 			CollectionAssert.AreEqual("9876543210".ToByteArray(), bytes);
-
 			this.barrier.RemoveParticipant();
 		}
 
@@ -31,7 +31,6 @@ namespace UNetTest
 			CollectionAssert.AreEqual("0123456789".ToByteArray(), bytes);
 			Array.Reverse(bytes);
 			channel.SendAsync(bytes);
-
 			this.barrier.RemoveParticipant();
 		}
 
@@ -53,7 +52,6 @@ namespace UNetTest
 
 			// 往client host线程增加事件,client线程连接server
 			clientService.Add(() => this.ClientEvent(clientService, hostName, port));
-
 			this.barrier.SignalAndWait();
 		}
 	}