瀏覽代碼

给Host增加一个event,方便其它线程扔事件到Host.Run所在的线程

tanghai 13 年之前
父節點
當前提交
8d4b5f4d28
共有 1 個文件被更改,包括 37 次插入0 次删除
  1. 37 0
      CSharp/ThirdParty/ENetCS/Host.cs

+ 37 - 0
CSharp/ThirdParty/ENetCS/Host.cs

@@ -30,6 +30,7 @@ namespace ENet
 	{
 	{
 		private Native.ENetHost* host;
 		private Native.ENetHost* host;
 		private readonly PeerManager peerManager = new PeerManager();
 		private readonly PeerManager peerManager = new PeerManager();
+		private Action events;
 
 
 		public Host(ushort port, uint peerLimit):
 		public Host(ushort port, uint peerLimit):
 			this(new Address { Port = port }, peerLimit)
 			this(new Address { Port = port }, peerLimit)
@@ -192,8 +193,44 @@ namespace ENet
 			Native.enet_host_channel_limit(this.host, channelLimit);
 			Native.enet_host_channel_limit(this.host, channelLimit);
 		}
 		}
 
 
+		public event Action Events
+		{
+			add
+			{
+				lock (events)
+				{
+					events += value;
+				}
+			}
+			remove
+			{
+				lock (events)
+				{
+					events -= value;
+				}
+			}
+		}
+
+		private void OnExecuteEvents()
+		{
+			Action local = null;
+			lock (events)
+			{
+				if (events == null)
+				{
+					return;
+				}
+				local = events;
+				events = null;
+			}
+			local();
+		}
+
 		public void Run()
 		public void Run()
 		{
 		{
+			// 处理其它线程扔过来的事件
+			OnExecuteEvents();
+
 			if (this.Service(0) < 0)
 			if (this.Service(0) < 0)
 			{
 			{
 				return;
 				return;