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

重构了网络层,使用TcpListener tcpClient类实现Tcp通信

tanghai 8 лет назад
Родитель
Сommit
42bd5e86b2
100 измененных файлов с 746 добавлено и 642 удалено
  1. 12 6
      Server/App/Program.cs
  2. 6 5
      Server/Base/Network/TNet/PacketParser.cs
  3. 118 126
      Server/Base/Network/TNet/TChannel.cs
  4. 0 38
      Server/Base/Network/TNet/TPoller.cs
  5. 12 13
      Server/Base/Network/TNet/TService.cs
  6. 0 243
      Server/Base/Network/TNet/TSocket.cs
  7. 3 2
      Server/Base/Server.Base.csproj
  8. 16 0
      Server/Hotfix/Helper/MessageHelper.cs
  9. 3 3
      Server/Hotfix/Message/C2R_LoginHandler.cs
  10. 2 0
      Server/Hotfix/Server.Hotfix.csproj
  11. 60 9
      Server/Model/Component/ActorComponent.cs
  12. 0 1
      Server/Model/Component/ActorManagerComponent.cs
  13. 8 3
      Server/Model/Component/ActorMessageDispatherComponent.cs
  14. 57 0
      Server/Model/Component/GamerComponent.cs
  15. 1 1
      Server/Model/Component/LocationProxyComponent.cs
  16. 2 2
      Server/Model/Component/MessageDispatherComponent.cs
  17. 6 4
      Server/Model/Component/NetInnerComponent.cs
  18. 5 3
      Server/Model/Component/NetOuterComponent.cs
  19. 11 7
      Server/Model/Component/NetworkComponent.cs
  20. 2 2
      Server/Model/Component/OpcodeTypeComponent.cs
  21. 19 0
      Server/Model/Component/Session/SessionGamerComponent.cs
  22. 9 2
      Server/Model/Component/StartConfigComponent.cs
  23. 7 0
      Server/Model/Component/Unit/SessionInfoComponent.cs
  24. 14 0
      Server/Model/Component/UnitComponent.cs
  25. 2 0
      Server/Model/Config/AConfigComponent.cs
  26. 45 5
      Server/Model/Entity/ActorProxy.cs
  27. 0 1
      Server/Model/Entity/DBEntity.cs
  28. 0 19
      Server/Model/Entity/DBSceneEntity.cs
  29. 0 2
      Server/Model/Entity/Game.cs
  30. 44 0
      Server/Model/Entity/Gamer.cs
  31. 3 3
      Server/Model/Entity/Message/InnerOpcode.cs
  32. 15 30
      Server/Model/Entity/Session.cs
  33. 9 2
      Server/Model/Entity/Unit.cs
  34. 11 3
      Server/Model/Message/AActorMessage.cs
  35. 18 9
      Server/Model/Message/AMActorHandler.cs
  36. 6 6
      Server/Model/Message/AMHandler.cs
  37. 1 1
      Server/Model/Message/AMessage.cs
  38. 6 4
      Server/Model/Message/AppType.cs
  39. 9 6
      Server/Model/Message/ErrorCode.cs
  40. 27 0
      Server/Model/Message/IEntityActorHandler.cs
  41. 7 0
      Server/Model/Message/IMessageDispatcher.cs
  42. 26 0
      Server/Model/Message/InnerMessageDispatcher.cs
  43. 0 27
      Server/Model/Message/MessageInfo.cs
  44. 39 0
      Server/Model/Message/OuterMessageDispatcher.cs
  45. 4 1
      Server/Model/Object/Component.cs
  46. 4 3
      Server/Model/Object/Entity.cs
  47. 34 2
      Server/Model/Other/OneThreadSynchronizationContext.cs
  48. 21 11
      Server/Model/Server.Model.csproj
  49. 0 6
      Server/Model/Server.Model.csproj.user
  50. 1 1
      Server/Server.sln
  51. 2 1
      Server/Server.sln.DotSettings.user
  52. 0 9
      Unity/Assets/Bundles/Unit.meta
  53. BIN
      Unity/Assets/Res/Code/Hotfix.dll.bytes
  54. BIN
      Unity/Assets/Res/Code/Hotfix.pdb.bytes
  55. 0 9
      Unity/Assets/Res/Scenes.meta
  56. 0 9
      Unity/Assets/Res/Unit.meta
  57. 0 0
      Unity/Assets/Scripts/Base/Config.meta
  58. 0 0
      Unity/Assets/Scripts/Base/Config/ACategory.cs
  59. 0 0
      Unity/Assets/Scripts/Base/Config/ACategory.cs.meta
  60. 0 0
      Unity/Assets/Scripts/Base/Config/AConfig.cs
  61. 0 0
      Unity/Assets/Scripts/Base/Config/AConfig.cs.meta
  62. 0 0
      Unity/Assets/Scripts/Base/Config/AConfigComponent.cs
  63. 0 0
      Unity/Assets/Scripts/Base/Config/AConfigComponent.cs.meta
  64. 0 0
      Unity/Assets/Scripts/Base/Config/ConfigAttribute.cs
  65. 0 0
      Unity/Assets/Scripts/Base/Config/ConfigAttribute.cs.meta
  66. 0 0
      Unity/Assets/Scripts/Base/Config/ConfigHelper.cs
  67. 0 0
      Unity/Assets/Scripts/Base/Config/ConfigHelper.cs.meta
  68. 0 0
      Unity/Assets/Scripts/Base/Config/ICategory.cs
  69. 0 0
      Unity/Assets/Scripts/Base/Config/ICategory.cs.meta
  70. 0 0
      Unity/Assets/Scripts/Base/Event.meta
  71. 0 0
      Unity/Assets/Scripts/Base/Event/AEventAttribute.cs
  72. 0 0
      Unity/Assets/Scripts/Base/Event/AEventAttribute.cs.meta
  73. 0 0
      Unity/Assets/Scripts/Base/Event/CrossEventAttribute.cs
  74. 0 0
      Unity/Assets/Scripts/Base/Event/CrossEventAttribute.cs.meta
  75. 0 0
      Unity/Assets/Scripts/Base/Event/CrossIdType.cs
  76. 0 0
      Unity/Assets/Scripts/Base/Event/CrossIdType.cs.meta
  77. 0 0
      Unity/Assets/Scripts/Base/Event/Env.cs
  78. 0 0
      Unity/Assets/Scripts/Base/Event/Env.cs.meta
  79. 0 0
      Unity/Assets/Scripts/Base/Event/EnvKey.cs
  80. 0 0
      Unity/Assets/Scripts/Base/Event/EnvKey.cs.meta
  81. 0 0
      Unity/Assets/Scripts/Base/Event/EventAttribute.cs
  82. 0 0
      Unity/Assets/Scripts/Base/Event/EventAttribute.cs.meta
  83. 0 0
      Unity/Assets/Scripts/Base/Event/EventIdType.cs
  84. 0 0
      Unity/Assets/Scripts/Base/Event/EventIdType.cs.meta
  85. 0 0
      Unity/Assets/Scripts/Base/Event/IEvent.cs
  86. 0 0
      Unity/Assets/Scripts/Base/Event/IEvent.cs.meta
  87. 37 0
      Unity/Assets/Scripts/Base/Helper/NetworkHelper.cs
  88. 2 2
      Unity/Assets/Scripts/Base/Helper/NetworkHelper.cs.meta
  89. 0 0
      Unity/Assets/Scripts/Base/IL.meta
  90. 0 0
      Unity/Assets/Scripts/Base/IL/IAsyncStateMachineAdaptor.cs
  91. 0 0
      Unity/Assets/Scripts/Base/IL/IAsyncStateMachineAdaptor.cs.meta
  92. 0 0
      Unity/Assets/Scripts/Base/IL/IDisposableAdaptor.cs
  93. 0 0
      Unity/Assets/Scripts/Base/IL/IDisposableAdaptor.cs.meta
  94. 0 0
      Unity/Assets/Scripts/Base/IL/ILAdapterAttribute.cs
  95. 0 0
      Unity/Assets/Scripts/Base/IL/ILAdapterAttribute.cs.meta
  96. 0 0
      Unity/Assets/Scripts/Base/IL/ILBindingAttribute.cs
  97. 0 0
      Unity/Assets/Scripts/Base/IL/ILBindingAttribute.cs.meta
  98. 0 0
      Unity/Assets/Scripts/Base/IL/ILRedirection.cs
  99. 0 0
      Unity/Assets/Scripts/Base/IL/ILRedirection.cs.meta
  100. 0 0
      Unity/Assets/Scripts/Base/Message.meta

+ 12 - 6
Server/App/Program.cs

@@ -10,7 +10,8 @@ namespace App
 		private static void Main(string[] args)
 		{
 			// 异步方法全部会回掉到主线程
-			SynchronizationContext.SetSynchronizationContext(new OneThreadSynchronizationContext());
+			OneThreadSynchronizationContext contex = new OneThreadSynchronizationContext();
+			SynchronizationContext.SetSynchronizationContext(contex);
 
 			try
 			{
@@ -42,19 +43,22 @@ namespace App
 						Game.Scene.AddComponent<AppManagerComponent>();
 						break;
 					case AppType.Realm:
+						Game.Scene.AddComponent<UnitComponent>();
 						Game.Scene.AddComponent<ActorMessageDispatherComponent>();
 						Game.Scene.AddComponent<ActorManagerComponent>();
-						Game.Scene.AddComponent<ActorComponent>();
 						Game.Scene.AddComponent<NetInnerComponent, string, int>(innerConfig.Host, innerConfig.Port);
 						Game.Scene.AddComponent<NetOuterComponent, string, int>(outerConfig.Host, outerConfig.Port);
+						Game.Scene.AddComponent<LocationProxyComponent>();
+						Game.Scene.AddComponent<ActorComponent>();
 						Game.Scene.AddComponent<RealmGateAddressComponent>();
 						break;
 					case AppType.Gate:
 						Game.Scene.AddComponent<ActorMessageDispatherComponent>();
 						Game.Scene.AddComponent<ActorManagerComponent>();
-						Game.Scene.AddComponent<ActorComponent>();
 						Game.Scene.AddComponent<NetInnerComponent, string, int>(innerConfig.Host, innerConfig.Port);
 						Game.Scene.AddComponent<NetOuterComponent, string, int>(outerConfig.Host, outerConfig.Port);
+						Game.Scene.AddComponent<LocationProxyComponent>();
+						Game.Scene.AddComponent<ActorComponent>();
 						Game.Scene.AddComponent<GateSessionKeyComponent>();
 						break;
 					case AppType.Location:
@@ -62,14 +66,16 @@ namespace App
 						Game.Scene.AddComponent<LocationComponent>();
 						break;
 					case AppType.AllServer:
+						Game.Scene.AddComponent<GamerComponent>();
+						Game.Scene.AddComponent<UnitComponent>();
 						Game.Scene.AddComponent<LocationComponent>();
 						Game.Scene.AddComponent<ActorMessageDispatherComponent>();
+						Game.Scene.AddComponent<ActorManagerComponent>();
 						Game.Scene.AddComponent<NetInnerComponent, string, int>(innerConfig.Host, innerConfig.Port);
 						Game.Scene.AddComponent<NetOuterComponent, string, int>(outerConfig.Host, outerConfig.Port);
-						Game.Scene.AddComponent<AppManagerComponent>();
 						Game.Scene.AddComponent<LocationProxyComponent>();
-						Game.Scene.AddComponent<ActorManagerComponent>();
 						Game.Scene.AddComponent<ActorComponent>();
+						Game.Scene.AddComponent<AppManagerComponent>();
 						Game.Scene.AddComponent<RealmGateAddressComponent>();
 						Game.Scene.AddComponent<GateSessionKeyComponent>();
 						break;
@@ -86,7 +92,7 @@ namespace App
 					try
 					{
 						Thread.Sleep(1);
-						Game.Poller.Update();
+						contex.Update();
 						ObjectEvents.Instance.Update();
 					}
 					catch (Exception e)

+ 6 - 5
Server/Base/Network/TNet/PacketParser.cs

@@ -12,8 +12,8 @@ namespace Model
 	{
 		private readonly TBuffer buffer;
 
-		private uint packetSize;
-		private readonly byte[] packetSizeBuffer = new byte[4];
+		private ushort packetSize;
+		private readonly byte[] packetSizeBuffer = new byte[2];
 		private ParserState state;
 		private byte[] packet;
 		private bool isOK;
@@ -36,15 +36,16 @@ namespace Model
 				switch (this.state)
 				{
 					case ParserState.PacketSize:
-						if (this.buffer.Count < 4)
+						if (this.buffer.Count < 2)
 						{
 							finish = true;
 						}
 						else
 						{
 							this.buffer.RecvFrom(this.packetSizeBuffer);
-							this.packetSize = BitConverter.ToUInt32(this.packetSizeBuffer, 0);
-							if (packetSize > 1024 * 1024)
+							this.packetSize = BitConverter.ToUInt16(this.packetSizeBuffer, 0);
+							this.packetSize = NetworkHelper.NetworkToHostOrder(this.packetSize);
+							if (packetSize > 60000)
 							{
 								throw new Exception($"packet too large, size: {this.packetSize}");
 							}

+ 118 - 126
Server/Base/Network/TNet/TChannel.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Net;
 using System.Net.Sockets;
 using System.Threading.Tasks;
 
@@ -8,7 +9,7 @@ namespace Model
 {
 	public class TChannel : AChannel
 	{
-		private readonly TSocket socket;
+		private readonly TcpClient tcpClient;
 
 		private readonly TBuffer recvBuffer = new TBuffer();
 		private readonly TBuffer sendBuffer = new TBuffer();
@@ -21,32 +22,47 @@ namespace Model
 		/// <summary>
 		/// connect
 		/// </summary>
-		public TChannel(TSocket socket, string host, int port, TService service) : base(service, ChannelType.Connect)
+		public TChannel(TcpClient tcpClient, string host, int port, TService service) : base(service, ChannelType.Connect)
 		{
-			this.socket = socket;
+			this.tcpClient = tcpClient;
 			this.parser = new PacketParser(this.recvBuffer);
 			this.RemoteAddress = host + ":" + port;
-			
-			bool result = this.socket.ConnectAsync(host, port);
-			if (!result)
-			{
-				this.OnConnected(this.Id, SocketError.Success);
-				return;
-			}
-			this.socket.OnConn += e => OnConnected(this.Id, e);
+
+			this.ConnectAsync(host, port);
 		}
 
 		/// <summary>
 		/// accept
 		/// </summary>
-		public TChannel(TSocket socket, TService service) : base(service, ChannelType.Accept)
+		public TChannel(TcpClient tcpClient, TService service) : base(service, ChannelType.Accept)
 		{
-			this.socket = socket;
+			this.tcpClient = tcpClient;
 			this.parser = new PacketParser(this.recvBuffer);
-			this.RemoteAddress = socket.RemoteAddress;
+
+			IPEndPoint ipEndPoint = (IPEndPoint)this.tcpClient.Client.RemoteEndPoint;
+			this.RemoteAddress = ipEndPoint.Address + ":" + ipEndPoint.Port;
 			this.OnAccepted();
 		}
 
+		private async void ConnectAsync(string host, int port)
+		{
+			try
+			{
+				await this.tcpClient.ConnectAsync(host, port);
+				this.isConnected = true;
+				this.StartSend();
+				this.StartRecv();
+			}
+			catch (SocketException e)
+			{
+				Log.Error($"connect error: {e.SocketErrorCode}");
+			}
+			catch (Exception e)
+			{
+				Log.Error(e.ToString());
+			}
+		}
+
 		public override void Dispose()
 		{
 			if (this.Id == 0)
@@ -58,7 +74,7 @@ namespace Model
 
 			base.Dispose();
 
-			this.socket.Dispose();
+			this.tcpClient.Close();
 			this.service.Remove(id);
 		}
 
@@ -69,32 +85,16 @@ namespace Model
 			this.StartRecv();
 		}
 
-		private void OnConnected(long channelId, SocketError error)
-		{
-			if (this.service.GetChannel(channelId) == null)
-			{
-				return;
-			}
-			if (error != SocketError.Success)
-			{
-				Log.Error($"connect error: {error}");
-				return;
-			}
-			this.isConnected = true;
-			this.StartSend();
-			this.StartRecv();
-		}
-
 		public override void Send(byte[] buffer, byte channelID = 0, PacketFlags flags = PacketFlags.Reliable)
 		{
 			if (this.Id == 0)
 			{
 				throw new Exception("TChannel已经被Dispose, 不能发送消息");
 			}
-			byte[] size = BitConverter.GetBytes(buffer.Length);
+			byte[] size = BitConverter.GetBytes((ushort)buffer.Length);
 			this.sendBuffer.SendTo(size);
 			this.sendBuffer.SendTo(buffer);
-			if (!this.isSending && this.isConnected)
+			if (this.isConnected)
 			{
 				this.StartSend();
 			}
@@ -106,123 +106,115 @@ namespace Model
 			{
 				throw new Exception("TChannel已经被Dispose, 不能发送消息");
 			}
-			int size = buffers.Select(b => b.Length).Sum();
+			ushort size = (ushort)buffers.Select(b => b.Length).Sum();
+			size = NetworkHelper.HostToNetworkOrder(size);
 			byte[] sizeBuffer = BitConverter.GetBytes(size);
 			this.sendBuffer.SendTo(sizeBuffer);
 			foreach (byte[] buffer in buffers)
 			{
 				this.sendBuffer.SendTo(buffer);
 			}
-			if (!this.isSending && this.isConnected)
+			if (this.isConnected)
 			{
 				this.StartSend();
 			}
 		}
 
-		private void StartSend()
-		{
-			if (this.Id == 0)
-			{
-				return;
-			}
-			// 没有数据需要发送
-			if (this.sendBuffer.Count == 0)
-			{
-				this.isSending = false;
-				return;
-			}
-
-			this.isSending = true;
-
-			int sendSize = TBuffer.ChunkSize - this.sendBuffer.FirstIndex;
-			if (sendSize > this.sendBuffer.Count)
-			{
-				sendSize = this.sendBuffer.Count;
-			}
-
-			if (!this.socket.SendAsync(this.sendBuffer.First, this.sendBuffer.FirstIndex, sendSize))
-			{
-				this.OnSend(sendSize, SocketError.Success);
-				return;
-			}
-			this.socket.OnSend = this.OnSend;
-		}
-
-		private void OnSend(int n, SocketError error)
+		private async void StartSend()
 		{
-			if (this.Id == 0)
-			{
-				return;
-			}
-			this.socket.OnSend = null;
-			if (error != SocketError.Success)
-			{
-				this.OnError(this, error);
-				return;
-			}
-			this.sendBuffer.FirstIndex += n;
-			if (this.sendBuffer.FirstIndex == TBuffer.ChunkSize)
+			try
 			{
-				this.sendBuffer.FirstIndex = 0;
-				this.sendBuffer.RemoveFirst();
-			}
-
-			this.StartSend();
-		}
+				// 如果正在发送中,不需要再次发送
+				if (this.isSending)
+				{
+					return;
+				}
 
-		private void StartRecv()
-		{
-			if (this.Id == 0)
-			{
-				return;
+				while (true)
+				{
+					if (this.Id == 0)
+					{
+						return;
+					}
+
+					// 没有数据需要发送
+					if (this.sendBuffer.Count == 0)
+					{
+						this.isSending = false;
+						return;
+					}
+
+					this.isSending = true;
+
+					int sendSize = TBuffer.ChunkSize - this.sendBuffer.FirstIndex;
+					if (sendSize > this.sendBuffer.Count)
+					{
+						sendSize = this.sendBuffer.Count;
+					}
+					await this.tcpClient.GetStream().WriteAsync(this.sendBuffer.First, this.sendBuffer.FirstIndex, sendSize);
+					this.sendBuffer.FirstIndex += sendSize;
+					if (this.sendBuffer.FirstIndex == TBuffer.ChunkSize)
+					{
+						this.sendBuffer.FirstIndex = 0;
+						this.sendBuffer.RemoveFirst();
+					}
+				}
 			}
-			int size = TBuffer.ChunkSize - this.recvBuffer.LastIndex;
-			
-			if (!this.socket.RecvAsync(this.recvBuffer.Last, this.recvBuffer.LastIndex, size))
+			catch (Exception e)
 			{
-				this.OnRecv(size, SocketError.Success);
+				Log.Error(e.ToString());
+				this.OnError(this, SocketError.SocketError);
 			}
-			this.socket.OnRecv = this.OnRecv;
 		}
 
-		private void OnRecv(int n, SocketError error)
+		private async void StartRecv()
 		{
-			if (this.Id == 0)
-			{
-				return;
-			}
-			this.socket.OnRecv = null;
-			if (error != SocketError.Success)
-			{
-				this.OnError(this, error);
-				return;
-			}
-
-			if (n == 0)
+			try
 			{
-				this.OnError(this, error);
-				return;
+				while (true)
+				{
+					if (this.Id == 0)
+					{
+						return;
+					}
+					int size = TBuffer.ChunkSize - this.recvBuffer.LastIndex;
+
+					int n = await this.tcpClient.GetStream().ReadAsync(this.recvBuffer.Last, this.recvBuffer.LastIndex, size);
+
+					if (n == 0)
+					{
+						this.OnError(this, SocketError.NetworkReset);
+						return;
+					}
+
+					this.recvBuffer.LastIndex += n;
+
+					if (this.recvBuffer.LastIndex == TBuffer.ChunkSize)
+					{
+						this.recvBuffer.AddLast();
+						this.recvBuffer.LastIndex = 0;
+					}
+
+					if (this.recvTcs != null)
+					{
+						byte[] packet = this.parser.GetPacket();
+						if (packet != null)
+						{
+							var tcs = this.recvTcs;
+							this.recvTcs = null;
+							tcs.SetResult(packet);
+						}
+					}
+				}
 			}
-			
-			this.recvBuffer.LastIndex += n;
-			if (this.recvBuffer.LastIndex == TBuffer.ChunkSize)
+			catch (ObjectDisposedException)
 			{
-				this.recvBuffer.AddLast();
-				this.recvBuffer.LastIndex = 0;
 			}
-			
-			if (this.recvTcs != null)
+			catch (Exception e)
 			{
-				byte[] packet = this.parser.GetPacket();
-				if (packet != null)
-				{
-					var tcs = this.recvTcs;
-					this.recvTcs = null;
-					tcs.SetResult(packet);
-				}
+				Log.Error(e.ToString());
+				this.OnError(this, SocketError.SocketError);
 			}
-
-			StartRecv();
 		}
 
 		public override Task<byte[]> Recv()
@@ -231,13 +223,13 @@ namespace Model
 			{
 				throw new Exception("TChannel已经被Dispose, 不能接收消息");
 			}
-			
+
 			byte[] packet = this.parser.GetPacket();
 			if (packet != null)
 			{
 				return Task.FromResult(packet);
 			}
-			
+
 			recvTcs = new TaskCompletionSource<byte[]>();
 			return recvTcs.Task;
 		}

+ 0 - 38
Server/Base/Network/TNet/TPoller.cs

@@ -1,38 +0,0 @@
-using System;
-using System.Collections.Generic;
-
-namespace Model
-{
-	public class TPoller
-	{
-		// 线程同步队列,发送接收socket回调都放到该队列,由poll线程统一执行
-		private Queue<Action> queue = new Queue<Action>();
-
-		private Queue<Action> localQueue = new Queue<Action>();
-
-		private readonly object lockObject = new object();
-
-		public void Add(Action action)
-		{
-			lock (lockObject)
-			{
-				this.queue.Enqueue(action);
-			}
-		}
-
-		public void Update()
-		{
-			lock (lockObject)
-			{
-				localQueue = queue;
-				queue = new Queue<Action>();
-			}
-
-			while (this.localQueue.Count > 0)
-			{
-				Action a = this.localQueue.Dequeue();
-				a();
-			}
-		}
-	}
-}

+ 12 - 13
Server/Base/Network/TNet/TService.cs

@@ -1,14 +1,15 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Net;
+using System.Net.Sockets;
 using System.Threading.Tasks;
 
 namespace Model
 {
 	public sealed class TService: AService
 	{
-		private TPoller poller = new TPoller();
-		private readonly TSocket acceptor;
+		private TcpListener acceptor;
 
 		private readonly Dictionary<long, TChannel> idChannels = new Dictionary<long, TChannel>();
 
@@ -19,7 +20,8 @@ namespace Model
 		/// <param name="port"></param>
 		public TService(string host, int port)
 		{
-			this.acceptor = new TSocket(this.poller, host, port);
+			this.acceptor = new TcpListener(new IPEndPoint(IPAddress.Parse(host), port));
+			this.acceptor.Start();
 		}
 
 		public TService()
@@ -28,7 +30,7 @@ namespace Model
 
 		public override void Dispose()
 		{
-			if (this.poller == null)
+			if (this.acceptor == null)
 			{
 				return;
 			}
@@ -38,13 +40,12 @@ namespace Model
 				TChannel channel = this.idChannels[id];
 				channel.Dispose();
 			}
-			this.acceptor?.Dispose();
-			this.poller = null;
+			this.acceptor.Stop();
+			this.acceptor = null;
 		}
 
 		public override void Add(Action action)
 		{
-			this.poller.Add(action);
 		}
 
 		public override AChannel GetChannel(long id)
@@ -60,17 +61,16 @@ namespace Model
 			{
 				throw new Exception("service construct must use host and port param");
 			}
-			TSocket socket = new TSocket(this.poller);
-			await this.acceptor.AcceptAsync(socket);
-			TChannel channel = new TChannel(socket, this);
+			TcpClient tcpClient = await this.acceptor.AcceptTcpClientAsync();
+			TChannel channel = new TChannel(tcpClient, this);
 			this.idChannels[channel.Id] = channel;
 			return channel;
 		}
 
 		public override AChannel ConnectChannel(string host, int port)
 		{
-			TSocket newSocket = new TSocket(this.poller);
-			TChannel channel = new TChannel(newSocket, host, port, this);
+			TcpClient tcpClient = new TcpClient();
+			TChannel channel = new TChannel(tcpClient, host, port, this);
 			this.idChannels[channel.Id] = channel;
 
 			return channel;
@@ -94,7 +94,6 @@ namespace Model
 		
 		public override void Update()
 		{
-			this.poller.Update();
 		}
 	}
 }

+ 0 - 243
Server/Base/Network/TNet/TSocket.cs

@@ -1,243 +0,0 @@
-using System;
-using System.Net;
-using System.Net.Sockets;
-using System.Threading.Tasks;
-
-namespace Model
-{
-	/// <summary>
-	/// 封装Socket,将回调push到主线程处理
-	/// </summary>
-	public sealed class TSocket: IDisposable
-	{
-		private readonly TPoller poller;
-		private Socket socket;
-		private readonly SocketAsyncEventArgs innArgs = new SocketAsyncEventArgs();
-		private readonly SocketAsyncEventArgs outArgs = new SocketAsyncEventArgs();
-
-		public Action<SocketError> OnConn;
-		public Action<int, SocketError> OnRecv;
-		public Action<int, SocketError> OnSend;
-		public Action<SocketError> OnDisconnect;
-
-		public TSocket(TPoller poller)
-		{
-			this.poller = poller;
-			this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
-			this.innArgs.Completed += this.OnComplete;
-			this.outArgs.Completed += this.OnComplete;
-		}
-
-		public TSocket(TPoller poller, string host, int port): this(poller)
-		{
-			try
-			{
-				this.Bind(host, port);
-				this.Listen(100);
-			}
-			catch (Exception e)
-			{
-				throw new Exception($"socket bind error: {host} {port}", e);
-			}
-		}
-		
-		public Socket Socket
-		{
-			get
-			{
-				return this.socket;
-			}
-		}
-
-		public string RemoteAddress
-		{
-			get
-			{
-				IPEndPoint ipEndPoint = (IPEndPoint)this.socket.RemoteEndPoint;
-				return ipEndPoint.Address + ":" + ipEndPoint.Port;
-			}
-		}
-
-		public void Dispose()
-		{
-			if (this.socket == null)
-			{
-				return;
-			}
-			
-			this.socket.Close();
-			this.innArgs.Dispose();
-			this.outArgs.Dispose();
-			this.socket = null;
-		}
-
-		private void Bind(string host, int port)
-		{
-			this.socket.Bind(new IPEndPoint(IPAddress.Parse(host), port));
-		}
-
-		private void Listen(int backlog)
-		{
-			this.socket.Listen(backlog);
-		}
-
-		public Task<bool> AcceptAsync(TSocket accpetSocket)
-		{
-			if (this.socket == null)
-			{
-				throw new Exception($"TSocket已经被Dispose,不能接收连接!");
-			}
-			var tcs = new TaskCompletionSource<bool>();
-			this.innArgs.UserToken = tcs;
-			this.innArgs.AcceptSocket = accpetSocket.socket;
-			if (!this.socket.AcceptAsync(this.innArgs))
-			{
-				OnAcceptComplete(this.innArgs);
-			}
-			return tcs.Task;
-		}
-
-		private void OnAcceptComplete(SocketAsyncEventArgs e)
-		{
-			if (this.socket == null)
-			{
-				return;
-			}
-			var tcs = (TaskCompletionSource<bool>)e.UserToken;
-			e.UserToken = null;
-			if (e.SocketError != SocketError.Success)
-			{
-				tcs.SetException(new Exception($"socket error: {e.SocketError}"));
-				return;
-			}
-			tcs.SetResult(true);
-		}
-
-		private void OnComplete(object sender, SocketAsyncEventArgs e)
-		{
-			Action action;
-			switch (e.LastOperation)
-			{
-				case SocketAsyncOperation.Connect:
-					action = () => OnConnectComplete(e);
-					break;
-				case SocketAsyncOperation.Receive:
-					action = () => OnRecvComplete(e);
-					break;
-				case SocketAsyncOperation.Send:
-					action = () => OnSendComplete(e);
-					break;
-				case SocketAsyncOperation.Disconnect:
-					action = () => OnDisconnectComplete(e);
-					break;
-				case SocketAsyncOperation.Accept:
-					action = () => OnAcceptComplete(e);
-					break;
-				default:
-					throw new Exception($"socket error: {e.LastOperation}");
-			}
-
-			// 回调到主线程处理
-			this.poller.Add(action);
-		}
-
-		public bool ConnectAsync(string host, int port)
-		{
-			this.outArgs.RemoteEndPoint = new IPEndPoint(IPAddress.Parse(host), port);
-			if (this.socket.ConnectAsync(this.outArgs))
-			{
-				return true;
-			}
-			OnConnectComplete(this.outArgs);
-			return false;
-		}
-
-		private void OnConnectComplete(SocketAsyncEventArgs e)
-		{
-			if (this.socket == null)
-			{
-				return;
-			}
-			if (this.OnConn == null)
-			{
-				return;
-			}
-			this.OnConn(e.SocketError);
-		}
-
-		public bool RecvAsync(byte[] buffer, int offset, int count)
-		{
-			try
-			{
-				this.innArgs.SetBuffer(buffer, offset, count);
-			}
-			catch (Exception e)
-			{
-				throw new Exception($"socket set buffer error: {buffer.Length}, {offset}, {count}", e);
-			}
-			if (this.socket.ReceiveAsync(this.innArgs))
-			{
-				return true;
-			}
-			OnRecvComplete(this.innArgs);
-			return false;
-		}
-
-		private void OnRecvComplete(SocketAsyncEventArgs e)
-		{
-			if (this.socket == null)
-			{
-				return;
-			}
-			if (this.OnRecv == null)
-			{
-				return;
-			}
-			this.OnRecv(e.BytesTransferred, e.SocketError);
-		}
-
-		public bool SendAsync(byte[] buffer, int offset, int count)
-		{
-			try
-			{
-				this.outArgs.SetBuffer(buffer, offset, count);
-			}
-			catch (Exception e)
-			{
-				throw new Exception($"socket set buffer error: {buffer.Length}, {offset}, {count}", e);
-			}
-			if (this.socket.SendAsync(this.outArgs))
-			{
-				return true;
-			}
-			OnSendComplete(this.outArgs);
-			return false;
-		}
-
-		private void OnSendComplete(SocketAsyncEventArgs e)
-		{
-			if (this.socket == null)
-			{
-				return;
-			}
-			if (this.OnSend == null)
-			{
-				return;
-			}
-			this.OnSend(e.BytesTransferred, e.SocketError);
-		}
-
-		private void OnDisconnectComplete(SocketAsyncEventArgs e)
-		{
-			if (this.socket == null)
-			{
-				return;
-			}
-			if (this.OnDisconnect == null)
-			{
-				return;
-			}
-			this.OnDisconnect(e.SocketError);
-		}
-	}
-}

+ 3 - 2
Server/Base/Server.Base.csproj

@@ -47,6 +47,9 @@
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="..\..\Unity\Assets\Scripts\Base\Helper\NetworkHelper.cs">
+      <Link>Helper\NetworkHelper.cs</Link>
+    </Compile>
     <Compile Include="DoubleMap.cs" />
     <Compile Include="Helper\ArrayHelper.cs" />
     <Compile Include="Helper\ByteHelper.cs" />
@@ -74,9 +77,7 @@
     <Compile Include="Network\TNet\PacketParser.cs" />
     <Compile Include="Network\TNet\TBuffer.cs" />
     <Compile Include="Network\TNet\TChannel.cs" />
-    <Compile Include="Network\TNet\TPoller.cs" />
     <Compile Include="Network\TNet\TService.cs" />
-    <Compile Include="Network\TNet\TSocket.cs" />
     <Compile Include="Network\UNet\Library.cs" />
     <Compile Include="Network\UNet\NativeMethods.cs" />
     <Compile Include="Network\UNet\NativeStructs.cs" />

+ 16 - 0
Server/Hotfix/Helper/MessageHelper.cs

@@ -0,0 +1,16 @@
+using Model;
+
+namespace Hotfix
+{
+	public static class MessageHelper
+	{
+		public static void Broadcast<Message>(Message message) where Message: AMessage
+		{
+			Gamer[] gamers = Game.Scene.GetComponent<GamerComponent>().GetAll();
+			foreach (Gamer gamer in gamers)
+			{
+				gamer.GetComponent<SessionInfoComponent>().Session.Send(message);
+			}
+		}
+	}
+}

+ 3 - 3
Server/Hotfix/Message/C2R_LoginHandler.cs

@@ -4,7 +4,7 @@ using Model;
 namespace Hotfix
 {
 	[MessageHandler(AppType.Realm)]
-	public class C2R_LoginHandler: AMRpcHandler<C2R_Login, R2C_Login>
+	public class C2R_LoginHandler : AMRpcHandler<C2R_Login, R2C_Login>
 	{
 		protected override async void Run(Session session, C2R_Login message, Action<R2C_Login> reply)
 		{
@@ -23,10 +23,10 @@ namespace Hotfix
 				//Log.Debug($"gate address: {MongoHelper.ToJson(config)}");
 				string innerAddress = $"{config.GetComponent<InnerConfig>().Host}:{config.GetComponent<InnerConfig>().Port}";
 				Session gateSession = Game.Scene.GetComponent<NetInnerComponent>().Get(innerAddress);
-				
+
 				// 向gate请求一个key,客户端可以拿着这个key连接gate
 				G2R_GetLoginKey g2RGetLoginKey = await gateSession.Call<R2G_GetLoginKey, G2R_GetLoginKey>(new R2G_GetLoginKey());
-				
+
 				string outerAddress = $"{config.GetComponent<OuterConfig>().Host}:{config.GetComponent<OuterConfig>().Port}";
 
 				response.Address = outerAddress;

+ 2 - 0
Server/Hotfix/Server.Hotfix.csproj

@@ -32,9 +32,11 @@
   <ItemGroup>
     <Reference Include="System" />
     <Reference Include="System.Core" />
+    <Reference Include="System.Numerics" />
   </ItemGroup>
   <ItemGroup>
     <Compile Include="Component\RealmGateAddressComponentE.cs" />
+    <Compile Include="Helper\MessageHelper.cs" />
     <Compile Include="Message\C2G_LoginGateHandler.cs" />
     <Compile Include="Message\C2R_PingHandler.cs" />
     <Compile Include="Message\G2G_LockReleaseRequestHandler.cs" />

+ 60 - 9
Server/Model/Component/ActorComponent.cs

@@ -1,34 +1,85 @@
 using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
 
 namespace Model
 {
+	public struct ActorMessageInfo
+	{
+		public Session Session;
+		public IActorMessage Message;
+	}
+
 	[ObjectEvent]
-	public class ActorComponentEvent : ObjectEvent<ActorComponent>, IAwake
+	public class ActorComponentEvent : ObjectEvent<ActorComponent>, IAwake<IEntityActorHandler>
 	{
-		public void Awake()
+		public void Awake(IEntityActorHandler iEntityActorHandler)
 		{
-			this.Get().Awake();
+			this.Get().Awake(iEntityActorHandler);
 		}
 	}
 
 	/// <summary>
-	/// 挂上这个组件表示该Entity是一个Actor, 它会将Entity位置注册到Location Server
+	/// 挂上这个组件表示该Entity是一个Actor, 它会将Entity位置注册到Location Server, 接收的消息将会队列处理
 	/// </summary>
 	public class ActorComponent: Component
 	{
+		private IEntityActorHandler entityActorHandler;
+
 		private long actorId;
 
-		public async void Awake()
+		// 队列处理消息
+		private readonly Queue<ActorMessageInfo> queue = new Queue<ActorMessageInfo>();
+
+		private TaskCompletionSource<ActorMessageInfo> tcs;
+
+		public async void Awake(IEntityActorHandler iEntityActorHandler)
 		{
 			try
 			{
+				this.entityActorHandler = iEntityActorHandler;
 				this.actorId = this.Owner.Id;
 				Game.Scene.GetComponent<ActorManagerComponent>().Add(this.Owner);
 				await Game.Scene.GetComponent<LocationProxyComponent>().Add(this.actorId);
 			}
-			catch (Exception e)
+			catch (Exception)
+			{
+				Log.Error($"register actor fail: {this.actorId}");
+			}
+
+			this.HandleAsync();
+		}
+
+		public void Add(ActorMessageInfo info)
+		{
+			this.queue.Enqueue(info);
+
+			if (this.tcs == null)
+			{
+				return;
+			}
+			
+			this.tcs?.SetResult(this.queue.Dequeue());
+			this.tcs = null;
+		}
+
+		private Task<ActorMessageInfo> GetAsync()
+		{
+			if (this.queue.Count > 0)
+			{
+				return Task.FromResult(this.queue.Dequeue());
+			}
+
+			this.tcs = new TaskCompletionSource<ActorMessageInfo>();
+			return this.tcs.Task;
+		}
+
+		private async void HandleAsync()
+		{
+			while (true)
 			{
-				Log.Error(e.ToString());
+				ActorMessageInfo info = await this.GetAsync();
+				this.entityActorHandler.Handle(info.Session, this.Owner, info.Message);
 			}
 		}
 
@@ -47,9 +98,9 @@ namespace Model
 
 				await Game.Scene.GetComponent<LocationProxyComponent>().Remove(this.actorId);
 			}
-			catch (Exception e)
+			catch (Exception)
 			{
-				Log.Error(e.ToString());
+				Log.Error($"unregister actor fail: {this.actorId}");
 			}
 		}
 	}

+ 0 - 1
Server/Model/Component/ActorManagerComponent.cs

@@ -20,7 +20,6 @@ namespace Model
 
 		public void Awake()
 		{
-			
 		}
 
 		public void Add(Entity entity)

+ 8 - 3
Server/Model/Component/ActorMessageDispatherComponent.cs

@@ -64,14 +64,19 @@ namespace Model
 			}
 		}
 
-		public void Handle(Session session, object message)
+		public IMActorHandler GetActorHandler(Type type)
+		{
+			this.handlers.TryGetValue(type, out IMActorHandler actorHandler);
+			return actorHandler;
+		}
+
+		public void Handle(Session session, Entity entity, IActorMessage message)
 		{
 			if (!this.handlers.TryGetValue(message.GetType(), out IMActorHandler handler))
 			{
-				Log.Error($"not found message handler: {message.GetType()}");
+				Log.Error($"not found message handler: {message.GetType().FullName}");
 				return;
 			}
-			Entity entity = this.GetComponent<ActorManagerComponent>().Get(((AActorMessage)message).Id);
 			handler.Handle(session, entity, message);
 		}
 

+ 57 - 0
Server/Model/Component/GamerComponent.cs

@@ -0,0 +1,57 @@
+using System.Collections.Generic;
+using System.Linq;
+
+namespace Model
+{
+	public class GamerComponent : Component
+	{
+		private readonly Dictionary<long, Gamer> idGamers = new Dictionary<long, Gamer>();
+
+		public void Add(Gamer gamer)
+		{
+			if (this.idGamers.Count > 1)
+			{
+				gamer.Team = 1;
+			}
+			this.idGamers.Add(gamer.Id, gamer);
+		}
+
+		public Gamer Get(long id)
+		{
+			this.idGamers.TryGetValue(id, out Gamer gamer);
+			return gamer;
+		}
+
+		public void Remove(long id)
+		{
+			this.idGamers.Remove(id);
+		}
+
+		public int Count
+		{
+			get
+			{
+				return this.idGamers.Count;
+			}
+		}
+
+		public Gamer[] GetAll()
+		{
+			return this.idGamers.Values.ToArray();
+		}
+
+		public override void Dispose()
+		{
+			if (this.Id == 0)
+			{
+				return;
+			}
+			base.Dispose();
+
+			foreach (Gamer gamer in this.idGamers.Values)
+			{
+				gamer.Dispose();
+			}
+		}
+	}
+}

+ 1 - 1
Server/Model/Component/LocationProxyComponent.cs

@@ -21,7 +21,7 @@ namespace Model
 		}
 
 		public async Task Add(long key)
-		{			
+		{
 			Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(this.LocationAddress);
 			await session.Call<ObjectAddRequest, ObjectAddResponse>(new ObjectAddRequest() { Key = key });
 		}

+ 2 - 2
Server/Model/Component/MessageDispatherComponent.cs

@@ -34,7 +34,7 @@ namespace Model
 		public void Load()
 		{
 			this.handlers = new Dictionary<Type, List<IMHandler>>();
-
+			
 			Type[] types = DllHelper.GetMonoTypes();
 			foreach (Type type in types)
 			{
@@ -75,7 +75,7 @@ namespace Model
 				Log.Error($"消息 {message.GetType().FullName} 没有处理");
 				return;
 			}
-
+			
 			foreach (IMHandler ev in actions)
 			{
 				try

+ 6 - 4
Server/Model/Component/NetInnerComponent.cs

@@ -20,21 +20,23 @@ namespace Model
 			this.Get().Update();
 		}
 	}
-
-	public class NetInnerComponent : NetworkComponent
+	
+	public class NetInnerComponent: NetworkComponent
 	{
 		private readonly Dictionary<string, Session> adressSessions = new Dictionary<string, Session>();
 
 		public void Awake()
 		{
 			this.Awake(NetworkProtocol.TCP);
-			this.messagePacker = new MongoPacker();
+			this.MessagePacker = new MongoPacker();
+			this.MessageDispatcher = new InnerMessageDispatcher();
 		}
 
 		public void Awake(string host, int port)
 		{
 			this.Awake(NetworkProtocol.TCP, host, port);
-			this.messagePacker = new MongoPacker();
+			this.MessagePacker = new MongoPacker();
+			this.MessageDispatcher = new InnerMessageDispatcher();
 		}
 
 		public new void Update()

+ 5 - 3
Server/Model/Component/NetOuterComponent.cs

@@ -19,18 +19,20 @@
 		}
 	}
 
-	public class NetOuterComponent : NetworkComponent
+	public class NetOuterComponent: NetworkComponent
 	{
 		public void Awake()
 		{
 			this.Awake(NetworkProtocol.TCP);
-			this.messagePacker = new JsondotnetPacker();
+			this.MessagePacker = new JsondotnetPacker();
+			this.MessageDispatcher = new OuterMessageDispatcher();
 		}
 
 		public void Awake(string host, int port)
 		{
 			this.Awake(NetworkProtocol.TCP, host, port);
-			this.messagePacker = new JsondotnetPacker();
+			this.MessagePacker = new JsondotnetPacker();
+			this.MessageDispatcher = new OuterMessageDispatcher();
 		}
 
 		public new void Update()

+ 11 - 7
Server/Model/Component/NetworkComponent.cs

@@ -11,7 +11,9 @@ namespace Model
 
 		private readonly Dictionary<long, Session> sessions = new Dictionary<long, Session>();
 
-		protected IMessagePacker messagePacker;
+		public IMessagePacker MessagePacker { get; protected set; }
+
+		public IMessageDispatcher MessageDispatcher { get; protected set; }
 
 		protected void Awake(NetworkProtocol protocol)
 		{
@@ -58,10 +60,10 @@ namespace Model
 			}
 		}
 
-		private async Task<Session> Accept()
+		public virtual async Task<Session> Accept()
 		{
 			AChannel channel = await this.Service.AcceptChannel();
-			Session session = new Session(this, channel, messagePacker);
+			Session session = new Session(this, channel);
 			channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
 			this.sessions.Add(session.Id, session);
 			return session;
@@ -69,7 +71,8 @@ namespace Model
 
 		public virtual void Remove(long id)
 		{
-			if (!this.sessions.TryGetValue(id, out Session session))
+			Session session;
+			if (!this.sessions.TryGetValue(id, out session))
 			{
 				return;
 			}
@@ -79,20 +82,21 @@ namespace Model
 
 		public Session Get(long id)
 		{
-			this.sessions.TryGetValue(id, out Session session);
+			Session session;
+			this.sessions.TryGetValue(id, out session);
 			return session;
 		}
 
 		/// <summary>
 		/// 创建一个新Session
 		/// </summary>
-		public Session Create(string address)
+		public virtual Session Create(string address)
 		{
 			string[] ss = address.Split(':');
 			int port = int.Parse(ss[1]);
 			string host = ss[0];
 			AChannel channel = this.Service.ConnectChannel(host, port);
-			Session session = new Session(this, channel, this.messagePacker);
+			Session session = new Session(this, channel);
 			channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
 			this.sessions.Add(session.Id, session);
 			return session;

+ 2 - 2
Server/Model/Component/OpcodeTypeComponent.cs

@@ -19,8 +19,8 @@ namespace Model
 	
 	public class OpcodeTypeComponent : Component
 	{
-		private Dictionary<ushort, Type> opcodeType;
-		private Dictionary<Type, MessageAttribute> messageOpcode;
+		private Dictionary<ushort, Type> opcodeType { get; set; }
+		private Dictionary<Type, MessageAttribute> messageOpcode { get; set; }
 
 		public void Awake()
 		{

+ 19 - 0
Server/Model/Component/Session/SessionGamerComponent.cs

@@ -0,0 +1,19 @@
+namespace Model
+{
+	public class SessionGamerComponent : Component
+	{
+		public Gamer Gamer;
+
+		public override void Dispose()
+		{
+			if (this.Id == 0)
+			{
+				return;
+			}
+
+			base.Dispose();
+
+			Game.Scene.GetComponent<GamerComponent>().Remove(this.Gamer.Id);
+		}
+	}
+}

+ 9 - 2
Server/Model/Component/StartConfigComponent.cs

@@ -50,7 +50,7 @@ namespace Model
 
 					if (startConfig.AppType.Is(AppType.Location))
 					{
-						this.LocationConfig = startConfig;
+						LocationConfig = startConfig;
 					}
 				}
 				catch (Exception)
@@ -64,7 +64,14 @@ namespace Model
 
 		public StartConfig Get(int id)
 		{
-			return this.configDict[id];
+			try
+			{
+				return this.configDict[id];
+			}
+			catch (Exception e)
+			{
+				throw new Exception($"not found startconfig: {id}", e);
+			}
 		}
 
 		public StartConfig[] GetAll()

+ 7 - 0
Server/Model/Component/Unit/SessionInfoComponent.cs

@@ -0,0 +1,7 @@
+namespace Model
+{
+	public class SessionInfoComponent : Component
+	{
+		public Session Session;
+	}
+}

+ 14 - 0
Server/Model/Component/UnitComponent.cs

@@ -1,4 +1,5 @@
 using System.Collections.Generic;
+using System.Linq;
 
 namespace Model
 {
@@ -22,6 +23,19 @@ namespace Model
 			this.idUnits.Remove(id);
 		}
 
+		public int Count
+		{
+			get
+			{
+				return this.idUnits.Count;
+			}
+		}
+
+		public Unit[] GetAll()
+		{
+			return this.idUnits.Values.ToArray();
+		}
+
 		public override void Dispose()
 		{
 			if (this.Id == 0)

+ 2 - 0
Server/Model/Config/AConfigComponent.cs

@@ -8,6 +8,8 @@ namespace Model
 	[BsonKnownTypes(typeof (ClientConfig))]
 	[BsonKnownTypes(typeof (InnerConfig))]
 	[BsonKnownTypes(typeof (OuterConfig))]
+	[BsonKnownTypes(typeof (HttpConfig))]
+	[BsonKnownTypes(typeof (DBConfig))]
 	public abstract class AConfigComponent: ComponentDB
 	{
 	}

+ 45 - 5
Server/Model/Entity/ActorProxy.cs

@@ -1,4 +1,5 @@
-using System.Threading.Tasks;
+using System;
+using System.Threading.Tasks;
 
 namespace Model
 {
@@ -18,11 +19,50 @@ namespace Model
 
 		public async Task<Response> Call<Request, Response>(Request request) where Request : AActorRequest where Response: AActorResponse
 		{
-			this.Address = await this.Parent.GetComponent<LocationProxyComponent>().Get(this.Id);
-			
+			try
+			{
+				Response response = null;
+				if (this.Address == "")
+				{
+					this.Address = await this.Parent.GetComponent<LocationProxyComponent>().Get(this.Id);
+				}
+				response = await OnceCall<Request, Response>(0, request);
+				return response;
+			}
+			catch (RpcException e)
+			{
+				Console.WriteLine(e);
+				throw;
+			}
+		}
+
+		public async Task<Response> OnceCall<Request, Response>(int retryTime, Request request) where Request : AActorRequest where Response : AActorResponse
+		{
+			Response response = null;
+			if (retryTime > 0)
+			{
+				await this.Parent.GetComponent<TimerComponent>().WaitAsync(retryTime * 500);
+				this.Address = await this.Parent.GetComponent<LocationProxyComponent>().Get(this.Id);
+			}
 			Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(this.Address);
-			Response response = await session.Call<Request, Response>(request);
-			return response;
+			response = await session.Call<Request, Response>(request);
+
+			if (response.Error == ErrorCode.ERR_Success)
+			{
+				return response;
+			}
+
+			if (retryTime >= 3)
+			{
+				throw new RpcException(response.Error, response.Message);
+			}
+
+			if (response.Error == ErrorCode.ERR_NotFoundActor)
+			{
+				response = await OnceCall<Request, Response>(++retryTime, request);
+			}
+
+			throw new RpcException(response.Error, response.Message);
 		}
 
 		public override void Dispose()

+ 0 - 1
Server/Model/Entity/DBEntity.cs

@@ -5,7 +5,6 @@ namespace Model
 	[BsonIgnoreExtraElements]
 	[BsonKnownTypes(typeof(RechargeRecord))]
 	[BsonKnownTypes(typeof(Recharge))]
-	[BsonKnownTypes(typeof(DBSceneEntity))]
 	public class DBEntity: Entity
 	{
 		protected DBEntity()

+ 0 - 19
Server/Model/Entity/DBSceneEntity.cs

@@ -1,19 +0,0 @@
-using MongoDB.Bson.Serialization.Attributes;
-
-namespace Model
-{
-	// 需要记录自己所在Scene的继承这个类
-	public class DBSceneEntity : DBEntity
-	{
-		[BsonIgnore]
-		public Scene Scene { get; set; }
-
-		protected DBSceneEntity()
-		{
-		}
-
-		protected DBSceneEntity(long id) : base(id)
-		{
-		}
-	}
-}

+ 0 - 2
Server/Model/Entity/Game.cs

@@ -2,8 +2,6 @@
 {
 	public static class Game
 	{
-		public static TPoller Poller { get; } = new TPoller();
-
 		private static Scene scene;
 
 		public static Scene Scene

+ 44 - 0
Server/Model/Entity/Gamer.cs

@@ -0,0 +1,44 @@
+using System.Collections.Generic;
+using System.Linq;
+
+namespace Model
+{
+	public sealed class Gamer : Entity
+	{
+		public string Account { get; }
+
+		public int Team { get; set; }
+
+		private readonly Dictionary<long, Unit> dictionary = new Dictionary<long, Unit>();
+
+		public Gamer(string account)
+		{
+			this.Account = account;
+		}
+
+		public Unit[] GetAll()
+		{
+			return this.dictionary.Values.ToArray();
+		}
+
+		public void Add(Unit unit)
+		{
+			this.dictionary.Add(unit.Id, unit);
+		}
+
+		public void Remove(long id)
+		{
+			this.dictionary.Remove(id);
+		}
+
+		public override void Dispose()
+		{
+			if (this.Id == 0)
+			{
+				return;
+			}
+
+			base.Dispose();
+		}
+	}
+}

+ 3 - 3
Server/Model/Entity/Message/InnerOpcode.cs

@@ -31,8 +31,8 @@
 		public const ushort ObjectUnLockResponse = 77;
 		public const ushort ObjectGetRequest = 78;
 		public const ushort ObjectGetResponse = 79;
-		
-		public const ushort R2G_GetLoginKey = 10001;
-		public const ushort G2R_GetLoginKey = 10002;
+
+		public const ushort R2G_GetLoginKey = 101;
+		public const ushort G2R_GetLoginKey = 102;
 	}
 }

+ 15 - 30
Server/Model/Entity/Session.cs

@@ -11,15 +11,11 @@ namespace Model
 		private readonly NetworkComponent network;
 		private readonly Dictionary<uint, Action<object>> requestCallback = new Dictionary<uint, Action<object>>();
 		private readonly AChannel channel;
-		private bool isRpc;
 
-		private readonly IMessagePacker messagePacker;
-
-		public Session(NetworkComponent network, AChannel channel, IMessagePacker messagePacker)
+		public Session(NetworkComponent network, AChannel channel)
 		{
 			this.network = network;
 			this.channel = channel;
-			this.messagePacker = messagePacker;
 			this.StartRecv();
 		}
 
@@ -41,7 +37,6 @@ namespace Model
 
 		private async void StartRecv()
 		{
-			TimerComponent timerComponent = Game.Scene.GetComponent<TimerComponent>();
 			while (true)
 			{
 				if (this.Id == 0)
@@ -52,12 +47,11 @@ namespace Model
 				byte[] messageBytes;
 				try
 				{
-					if (this.isRpc)
+					messageBytes = await channel.Recv();
+					if (this.Id == 0)
 					{
-						this.isRpc = false;
-						await timerComponent.WaitAsync(0);
+						return;
 					}
-					messageBytes = await channel.Recv();
 				}
 				catch (Exception e)
 				{
@@ -71,6 +65,7 @@ namespace Model
 				}
 
 				ushort opcode = BitConverter.ToUInt16(messageBytes, 0);
+				opcode = NetworkHelper.NetworkToHostOrder(opcode);
 				try
 				{
 					this.Run(opcode, messageBytes);
@@ -105,24 +100,15 @@ namespace Model
 		private void RunDecompressedBytes(ushort opcode, byte[] messageBytes, int offset)
 		{
 			Type messageType = this.network.Owner.GetComponent<OpcodeTypeComponent>().GetType(opcode);
-			object message = messagePacker.DeserializeFrom(messageType, messageBytes, offset, messageBytes.Length - offset);
-			
-			if (message is AActorMessage)
-			{
-				this.network.Owner.GetComponent<ActorMessageDispatherComponent>().Handle(this, message);
-				return;
-			}
-			if (message is AMessage || message is ARequest)
-			{
-				this.network.Owner.GetComponent<MessageDispatherComponent>().Handle(this, message);
-				return;
-			}
+			object message = this.network.MessagePacker.DeserializeFrom(messageType, messageBytes, offset, messageBytes.Length - offset);
 
-			if (message is AResponse response)
+			AResponse response = message as AResponse;
+			if (response != null)
 			{
 				// rpcFlag>0 表示这是一个rpc响应消息
 				// Rpc回调有找不着的可能,因为client可能取消Rpc调用
-				if (!this.requestCallback.TryGetValue(response.RpcId, out Action<object> action))
+				Action<object> action;
+				if (!this.requestCallback.TryGetValue(response.RpcId, out action))
 				{
 					return;
 				}
@@ -131,7 +117,7 @@ namespace Model
 				return;
 			}
 
-			throw new Exception($"message type error: {message.GetType().FullName}");
+			this.network.MessageDispatcher.Dispatch(this, opcode, offset, messageBytes, message);
 		}
 
 		/// <summary>
@@ -150,13 +136,12 @@ namespace Model
 				try
 				{
 					Response response = (Response)message;
-					if (response.Error != 0)
+					if (response.Error > 100)
 					{
 						tcs.SetException(new RpcException(response.Error, response.Message));
 						return;
 					}
 					//Log.Debug($"recv: {response.ToJson()}");
-					this.isRpc = true;
 					tcs.SetResult(response);
 				}
 				catch (Exception e)
@@ -184,13 +169,12 @@ namespace Model
 				try
 				{
 					Response response = (Response)message;
-					if (response.Error != 0)
+					if (response.Error > 100)
 					{
 						tcs.SetException(new RpcException(response.Error, response.Message));
 						return;
 					}
 					//Log.Debug($"recv: {response.ToJson()}");
-					this.isRpc = true;
 					tcs.SetResult(response);
 				}
 				catch (Exception e)
@@ -224,9 +208,10 @@ namespace Model
 		{
 			//Log.Debug($"send: {message.ToJson()}");
 			ushort opcode = this.network.Owner.GetComponent<OpcodeTypeComponent>().GetOpcode(message.GetType());
+			opcode = NetworkHelper.HostToNetworkOrder(opcode);
 			byte[] opcodeBytes = BitConverter.GetBytes(opcode);
 
-			byte[] messageBytes = messagePacker.SerializeToByteArray(message);
+			byte[] messageBytes = this.network.MessagePacker.SerializeToByteArray(message);
 			byte flag = 0;
 			if (messageBytes.Length > 100)
 			{

+ 9 - 2
Server/Model/Entity/Unit.cs

@@ -1,4 +1,6 @@
-namespace Model
+using System.Numerics;
+
+namespace Model
 {
 	public enum UnitType
 	{
@@ -10,6 +12,10 @@
 	{
 		public UnitType UnitType { get; }
 
+		public Gamer Gamer;
+
+		public Vector3 Position;
+
 		public override void Dispose()
 		{
 			if (this.Id == 0)
@@ -20,8 +26,9 @@
 			base.Dispose();
 		}
 
-		public Unit(UnitType unitType)
+		public Unit(Gamer gamer, UnitType unitType)
 		{
+			this.Gamer = gamer;
 			this.UnitType = unitType;
 		}
 	}

+ 11 - 3
Server/Model/Message/AActorMessage.cs

@@ -1,12 +1,20 @@
-namespace Model
+using MongoDB.Bson.Serialization.Attributes;
+
+namespace Model
 {
-	public abstract class AActorMessage: AMessage
+	public interface IActorMessage
+	{
+		long Id { get; set; }
+	}
+
+	public abstract class AActorMessage: AMessage, IActorMessage
 	{
 		public long Id { get; set; }
 	}
 
-	public abstract class AActorRequest : ARequest
+	public abstract class AActorRequest : ARequest, IActorMessage
 	{
+		[BsonIgnoreIfDefault]
 		public long Id { get; set; }
 	}
 

+ 18 - 9
Server/Model/Message/AMActorHandler.cs

@@ -2,7 +2,7 @@
 
 namespace Model
 {
-	public abstract class AMActorHandler<E, Message> : IMActorHandler where E : Entity where Message : AActorMessage
+	public abstract class AMActorHandler<E, Message>: IMActorHandler where E: Entity where Message : AActorMessage
 	{
 		protected abstract void Run(E entity, Message message);
 
@@ -11,23 +11,25 @@ namespace Model
 			Message message = msg as Message;
 			if (message == null)
 			{
-				Log.Error($"消息类型转换错误: {msg.GetType().FullName} to {typeof(Message).Name}");
+				Log.Error($"消息类型转换错误: {msg.GetType().FullName} to {typeof (Message).Name}");
+				return;
 			}
 			E e = entity as E;
 			if (e == null)
 			{
 				Log.Error($"Actor类型转换错误: {entity.GetType().Name} to {typeof(E).Name}");
+				return;
 			}
 			this.Run(e, message);
 		}
 
 		public Type GetMessageType()
 		{
-			return typeof(Message);
+			return typeof (Message);
 		}
 	}
 
-	public abstract class AMActorRpcHandler<Request, Response> : IMActorHandler where Request : AActorRequest where Response : AActorResponse
+	public abstract class AMActorRpcHandler<E, Request, Response>: IMActorHandler where E: Entity where Request : AActorRequest where Response : AActorResponse
 	{
 		protected static void ReplyError(Response response, Exception e, Action<Response> reply)
 		{
@@ -37,18 +39,25 @@ namespace Model
 			reply(response);
 		}
 
-		protected abstract void Run(Entity entity, Request message, Action<Response> reply);
+		protected abstract void Run(E entity, Request message, Action<Response> reply);
 
-		public void Handle(Session session, Entity entity, object message)
+		public void Handle(Session session, Entity entity,  object message)
 		{
 			try
 			{
 				Request request = message as Request;
 				if (request == null)
 				{
-					Log.Error($"消息类型转换错误: {message.GetType().FullName} to {typeof(Request).Name}");
+					Log.Error($"消息类型转换错误: {message.GetType().FullName} to {typeof (Request).Name}");
+					return;
 				}
-				this.Run(session, request, response =>
+				E e = entity as E;
+				if (e == null)
+				{
+					Log.Error($"Actor类型转换错误: {entity.GetType().Name} to {typeof(E).Name}");
+					return;
+				}
+				this.Run(e, request, response =>
 				{
 					// 等回调回来,session可以已经断开了,所以需要判断session id是否为0
 					if (session.Id == 0)
@@ -67,7 +76,7 @@ namespace Model
 
 		public Type GetMessageType()
 		{
-			return typeof(Request);
+			return typeof (Request);
 		}
 	}
 }

+ 6 - 6
Server/Model/Message/AMHandler.cs

@@ -2,7 +2,7 @@
 
 namespace Model
 {
-	public abstract class AMHandler<Message> : IMHandler where Message : AMessage
+	public abstract class AMHandler<Message>: IMHandler where Message : AMessage
 	{
 		protected abstract void Run(Session session, Message message);
 
@@ -11,18 +11,18 @@ namespace Model
 			Message message = msg as Message;
 			if (message == null)
 			{
-				Log.Error($"消息类型转换错误: {msg.GetType().Name} to {typeof(Message).Name}");
+				Log.Error($"消息类型转换错误: {msg.GetType().Name} to {typeof (Message).Name}");
 			}
 			this.Run(session, message);
 		}
 
 		public Type GetMessageType()
 		{
-			return typeof(Message);
+			return typeof (Message);
 		}
 	}
 
-	public abstract class AMRpcHandler<Request, Response> : IMHandler where Request : ARequest where Response : AResponse
+	public abstract class AMRpcHandler<Request, Response>: IMHandler where Request : ARequest where Response : AResponse
 	{
 		protected static void ReplyError(Response response, Exception e, Action<Response> reply)
 		{
@@ -41,7 +41,7 @@ namespace Model
 				Request request = message as Request;
 				if (request == null)
 				{
-					Log.Error($"消息类型转换错误: {message.GetType().Name} to {typeof(Request).Name}");
+					Log.Error($"消息类型转换错误: {message.GetType().Name} to {typeof (Request).Name}");
 				}
 				this.Run(session, request, response =>
 				{
@@ -62,7 +62,7 @@ namespace Model
 
 		public Type GetMessageType()
 		{
-			return typeof(Request);
+			return typeof (Request);
 		}
 	}
 }

+ 1 - 1
Server/Model/Message/AMessage.cs

@@ -20,7 +20,7 @@
 		public string Message = "";
 	}
 
-	public abstract class AFrameMessage : AMessage
+	public abstract class AFrameMessage: AMessage
 	{
 		public long Id;
 	}

+ 6 - 4
Server/Model/Message/AppType.cs

@@ -10,14 +10,16 @@ namespace Model
 		Manager = 1,
 		Realm = 2,
 		Gate = 4,
-		Location = 8,
+		Http = 8,
+		DB = 16,
+		Location = 32,
 
-		Benchmark = 16,
-		Client = 32,
 		Robot = 64,
+		Benchmark = 128,
+		Client = 256,
 
 		// 7
-		AllServer = Manager | Realm | Gate | Location
+		AllServer = Manager | Realm | Gate | Http | DB | Location
 	}
 
 	public static class AppTypeHelper

+ 9 - 6
Server/Model/Message/ErrorCode.cs

@@ -3,11 +3,14 @@ namespace Model
 	public static class ErrorCode
 	{
 		public const int ERR_Success = 0;
-		public const int ERR_RpcFail = 1;
-		public const int ERR_AccountOrPasswordError = 2;
-		public const int ERR_ConnectGateKeyError = 3;
-		public const int ERR_ReloadFail = 4;
-		public const int ERR_NotFoundUnit = 5;
-		public const int ERR_ActorLocationNotFound = 6;
+
+		public const int ERR_NotFoundActor = 1;
+
+		public const int ERR_RpcFail = 101;
+		public const int ERR_AccountOrPasswordError = 102;
+		public const int ERR_ConnectGateKeyError = 103;
+		public const int ERR_ReloadFail = 104;
+		public const int ERR_NotFoundUnit = 105;
+		public const int ERR_ActorLocationNotFound = 106;
 	}
 }

+ 27 - 0
Server/Model/Message/IEntityActorHandler.cs

@@ -0,0 +1,27 @@
+namespace Model
+{
+	public interface IEntityActorHandler
+	{
+		void Handle(Session session, Entity entity, IActorMessage message);
+	}
+
+	/// <summary>
+	/// gate session 收到的消息直接转发给客户端
+	/// </summary>
+	public class GateSessionEntityActorHandler : IEntityActorHandler
+	{
+		public void Handle(Session session, Entity entity, IActorMessage message)
+		{
+			message.Id = 0;
+			((Session)entity).Send((AMessage)message);
+		}
+	}
+
+	public class CommonEntityActorHandler : IEntityActorHandler
+	{
+		public void Handle(Session session, Entity entity, IActorMessage message)
+		{
+			Game.Scene.GetComponent<ActorMessageDispatherComponent>().Handle(session, entity, message);
+		}
+	}
+}

+ 7 - 0
Server/Model/Message/IMessageDispatcher.cs

@@ -0,0 +1,7 @@
+namespace Model
+{
+	public interface IMessageDispatcher
+	{
+		void Dispatch(Session session, ushort opcode, int offset, byte[] messageBytes, object message);
+	}
+}

+ 26 - 0
Server/Model/Message/InnerMessageDispatcher.cs

@@ -0,0 +1,26 @@
+using System;
+
+namespace Model
+{
+	public class InnerMessageDispatcher: IMessageDispatcher
+	{
+		public void Dispatch(Session session, ushort opcode, int offset, byte[] messageBytes, object message)
+		{
+			// 收到actor消息分发给actor自己去处理
+			if (message is IActorMessage actorMessage)
+			{
+				Entity entity = Game.Scene.GetComponent<ActorManagerComponent>().Get(actorMessage.Id);
+				entity.GetComponent<ActorComponent>().Add(new ActorMessageInfo() { Session = session, Message = actorMessage });
+				return;
+			}
+
+			if (message is AMessage || message is ARequest)
+			{
+				Game.Scene.GetComponent<MessageDispatherComponent>().Handle(session, message);
+				return;
+			}
+
+			throw new Exception($"message type error: {message.GetType().FullName}");
+		}
+	}
+}

+ 0 - 27
Server/Model/Message/MessageInfo.cs

@@ -1,27 +0,0 @@
-namespace Model
-{
-	public class MessageInfo
-	{
-		public ushort Opcode { get; }
-		public byte[] MessageBytes { get; }
-		public int Offset { get; }
-		public uint RpcId { get; }
-		public object Message { get; set; }
-
-		public MessageInfo(ushort opcode, byte[] messageBytes, int offset, uint rpcId)
-		{
-			this.Opcode = opcode;
-			this.MessageBytes = messageBytes;
-			this.Offset = offset;
-			this.RpcId = rpcId;
-		}
-
-		public int Count
-		{
-			get
-			{
-				return this.MessageBytes.Length - this.Offset;
-			}
-		}
-	}
-}

+ 39 - 0
Server/Model/Message/OuterMessageDispatcher.cs

@@ -0,0 +1,39 @@
+using System;
+
+namespace Model
+{
+	public class OuterMessageDispatcher: IMessageDispatcher
+	{
+		public async void Dispatch(Session session, ushort opcode, int offset, byte[] messageBytes, object message)
+		{
+			// gate session收到actor消息直接转发给actor自己去处理
+			if (message is AActorMessage aActorMessage)
+			{
+				ActorProxy actorProxy = Game.Scene.GetComponent<ActorProxyComponent>().Get(aActorMessage.Id);
+				aActorMessage.Id = session.GetComponent<SessionGamerComponent>().Gamer.Id;
+				actorProxy.Send(aActorMessage);
+				return;
+			}
+
+			// gate session收到actor rpc消息,先向actor 发送rpc请求,再将请求结果返回客户端
+			if (message is AActorRequest aActorRequest)
+			{
+				ActorProxy actorProxy = Game.Scene.GetComponent<ActorProxyComponent>().Get(aActorRequest.Id);
+				aActorRequest.Id = session.GetComponent<SessionGamerComponent>().Gamer.Id;
+				uint rpcId = aActorRequest.RpcId;
+				AActorResponse aActorResponse = await actorProxy.Call<AActorRequest, AActorResponse>(aActorRequest);
+				aActorResponse.RpcId = rpcId;
+				session.Reply(aActorResponse);
+				return;
+			}
+
+			if (message is AMessage || message is ARequest)
+			{
+				Game.Scene.GetComponent<MessageDispatherComponent>().Handle(session, message);
+				return;
+			}
+
+			throw new Exception($"message type error: {message.GetType().FullName}");
+		}
+	}
+}

+ 4 - 1
Server/Model/Object/Component.cs

@@ -37,7 +37,10 @@ namespace Model
 
 			base.Dispose();
 
-			this.Owner.RemoveComponent(this.GetType());
+			if (this.Owner.Id != 0)
+			{
+				this.Owner?.RemoveComponent(this.GetType());
+			}
 		}
 
 		public override void EndInit()

+ 4 - 3
Server/Model/Object/Entity.cs

@@ -7,10 +7,11 @@ namespace Model
 {
 	public class Entity: Disposer
 	{
-		public EntityType Type { get; set; }
-
+		[BsonIgnore]
 		public Entity Parent { get; set; }
 
+		public EntityType Type { get; set; }
+
 		[BsonElement]
 		[BsonIgnoreIfNull]
 		private HashSet<Component> components = new HashSet<Component>();
@@ -208,7 +209,7 @@ namespace Model
 
 		public Component[] GetComponents()
 		{
-			return components.ToArray();
+			return this.componentDict.Values.ToArray();
 		}
 
 		public override void BeginInit()

+ 34 - 2
Server/Model/Other/OneThreadSynchronizationContext.cs

@@ -1,12 +1,44 @@
-using System.Threading;
+using System;
+using System.Collections.Generic;
+using System.Threading;
 
 namespace Model
 {
 	public class OneThreadSynchronizationContext : SynchronizationContext
 	{
+		// 线程同步队列,发送接收socket回调都放到该队列,由poll线程统一执行
+		private Queue<Action> queue = new Queue<Action>();
+
+		private Queue<Action> localQueue = new Queue<Action>();
+
+		private readonly object lockObject = new object();
+
+		private void Add(Action action)
+		{
+			lock (lockObject)
+			{
+				this.queue.Enqueue(action);
+			}
+		}
+
+		public void Update()
+		{
+			lock (lockObject)
+			{
+				localQueue = queue;
+				queue = new Queue<Action>();
+			}
+
+			while (this.localQueue.Count > 0)
+			{
+				Action a = this.localQueue.Dequeue();
+				a();
+			}
+		}
+
 		public override void Post(SendOrPostCallback callback, object state)
 		{
-			Game.Poller.Add(() => { callback(state); });
+			this.Add(() => { callback(state); });
 		}
 	}
 }

+ 21 - 11
Server/Model/Server.Model.csproj

@@ -45,25 +45,32 @@
       <HintPath>..\Lib\MongoDB.Driver.Core.dll</HintPath>
     </Reference>
     <Reference Include="Newtonsoft.Json">
-      <HintPath>..\..\..\X1\X1\client\trunk\Server\Lib\Newtonsoft.Json.dll</HintPath>
+      <HintPath>..\Lib\Newtonsoft.Json.dll</HintPath>
     </Reference>
     <Reference Include="System" />
     <Reference Include="System.Core" />
+    <Reference Include="System.Numerics" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="..\..\Unity\Assets\Scripts\Base\Message\JsondotnetPacker.cs">
+      <Link>Message\JsondotnetPacker.cs</Link>
+    </Compile>
+    <Compile Include="..\..\Unity\Assets\Scripts\Base\Message\MongoPacker.cs">
+      <Link>Message\MongoPacker.cs</Link>
+    </Compile>
+    <Compile Include="..\..\Unity\Assets\Scripts\Base\Message\ProtobufPacker.cs">
+      <Link>Message\ProtobufPacker.cs</Link>
+    </Compile>
     <Compile Include="..\..\Unity\Assets\Scripts\Entity\Message\Opcode.cs">
       <Link>Entity\Message\Opcode.cs</Link>
     </Compile>
     <Compile Include="..\..\Unity\Assets\Scripts\Entity\Message\OuterMessage.cs">
       <Link>Entity\Message\OuterMessage.cs</Link>
     </Compile>
-    <Compile Include="..\..\Unity\Assets\Scripts\Message\JsondotnetPacker.cs">
-      <Link>Message\JsondotnetPacker.cs</Link>
-    </Compile>
-    <Compile Include="..\..\Unity\Assets\Scripts\Message\MongoPacker.cs">
-      <Link>Message\MongoPacker.cs</Link>
-    </Compile>
     <Compile Include="Component\ActorProxyComponent.cs" />
+    <Compile Include="Component\GamerComponent.cs" />
+    <Compile Include="Entity\Gamer.cs" />
+    <Compile Include="Message\IEntityActorHandler.cs" />
     <Compile Include="Component\OpcodeTypeComponent.cs" />
     <Compile Include="Component\ActorManagerComponent.cs" />
     <Compile Include="Component\BenchmarkComponent.cs" />
@@ -85,10 +92,12 @@
     <Compile Include="Component\NetOuterComponent.cs" />
     <Compile Include="Component\NetworkComponent.cs" />
     <Compile Include="Component\RobotComponent.cs" />
+    <Compile Include="Component\Session\SessionGamerComponent.cs" />
     <Compile Include="Component\TimerComponent.cs" />
     <Compile Include="Component\LocationComponent.cs" />
     <Compile Include="Component\ActorComponent.cs" />
     <Compile Include="Component\UnitComponent.cs" />
+    <Compile Include="Component\Unit\SessionInfoComponent.cs" />
     <Compile Include="Component\Unit\MasterComponent.cs" />
     <Compile Include="Component\Unit\LockComponent.cs" />
     <Compile Include="Component\AppManagerComponent.cs" />
@@ -105,15 +114,14 @@
     <Compile Include="Entity\Config\BuffConfig.cs" />
     <Compile Include="Entity\Config\StartConfig.cs" />
     <Compile Include="Entity\ActorProxy.cs" />
-    <Compile Include="Entity\Message\InnerOpcode.cs" />
     <Compile Include="Entity\SceneEntity.cs" />
-    <Compile Include="Entity\DBSceneEntity.cs" />
     <Compile Include="Entity\DBEntity.cs" />
     <Compile Include="Entity\DBTask.cs" />
     <Compile Include="Entity\DBTaskQueue.cs" />
     <Compile Include="Entity\Game.cs" />
     <Compile Include="Entity\Http.cs" />
     <Compile Include="Entity\Message\InnerMessage.cs" />
+    <Compile Include="Entity\Message\InnerOpcode.cs" />
     <Compile Include="Entity\Scene.cs" />
     <Compile Include="Entity\Session.cs" />
     <Compile Include="Entity\Unit.cs" />
@@ -131,13 +139,14 @@
     <Compile Include="Message\AppType.cs" />
     <Compile Include="Message\ErrorCode.cs" />
     <Compile Include="Message\IMActorHandler.cs" />
-    <Compile Include="Message\IMessagePacker.cs" />
+    <Compile Include="Message\OuterMessageDispatcher.cs" />
+    <Compile Include="Message\InnerMessageDispatcher.cs" />
+    <Compile Include="Message\IMessageDispatcher.cs" />
     <Compile Include="Message\IMHandler.cs" />
     <Compile Include="Message\ActorMessageAttribute.cs" />
     <Compile Include="Message\MessageAttribute.cs" />
     <Compile Include="Message\ActorMessageHandlerAttribute.cs" />
     <Compile Include="Message\MessageHandlerAttribute.cs" />
-    <Compile Include="Message\MessageInfo.cs" />
     <Compile Include="Message\OpcodeHelper.cs" />
     <Compile Include="Message\RpcException.cs" />
     <Compile Include="Object\ComponentDB.cs" />
@@ -152,6 +161,7 @@
     <Compile Include="Object\EntityType.cs" />
     <Compile Include="Object\Object.cs" />
     <Compile Include="Object\ObjectEvents.cs" />
+    <Compile Include="Message\IMessagePacker.cs" />
     <Compile Include="Other\OneThreadSynchronizationContext.cs" />
     <Compile Include="Other\Options.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />

+ 0 - 6
Server/Model/Server.Model.csproj.user

@@ -1,6 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
-  <PropertyGroup>
-    <ProjectView>ProjectFiles</ProjectView>
-  </PropertyGroup>
-</Project>

+ 1 - 1
Server/Server.sln

@@ -1,7 +1,7 @@
 
 Microsoft Visual Studio Solution File, Format Version 12.00
 # Visual Studio 15
-VisualStudioVersion = 15.0.26430.14
+VisualStudioVersion = 15.0.26430.16
 MinimumVisualStudioVersion = 10.0.40219.1
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Server.Base", "Base\Server.Base.csproj", "{E5078EC6-2B0E-4711-BE8B-D99F69638316}"
 EndProject

+ 2 - 1
Server/Server.sln.DotSettings.user

@@ -1,2 +1,3 @@
 <wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
-	<s:Boolean x:Key="/Default/Environment/OpenDocument/OpenDocumentAfterModification/@EntryValue">False</s:Boolean></wpf:ResourceDictionary>
+	<s:Int64 x:Key="/Default/Environment/SearchAndNavigation/DefaultOccurrencesGroupingIndex/@EntryValue">0</s:Int64>
+	<s:String x:Key="/Default/Housekeeping/Layout/DialogWindows/OptionsDialog/SelectedPageId/@EntryValue">EnhancedTooltip.OptionsPage</s:String></wpf:ResourceDictionary>

+ 0 - 9
Unity/Assets/Bundles/Unit.meta

@@ -1,9 +0,0 @@
-fileFormatVersion: 2
-guid: 16b8671bdf883ae41b46bfa33ad75d46
-folderAsset: yes
-timeCreated: 1499149176
-licenseType: Free
-DefaultImporter:
-  userData: 
-  assetBundleName: 
-  assetBundleVariant: 

BIN
Unity/Assets/Res/Code/Hotfix.dll.bytes


BIN
Unity/Assets/Res/Code/Hotfix.pdb.bytes


+ 0 - 9
Unity/Assets/Res/Scenes.meta

@@ -1,9 +0,0 @@
-fileFormatVersion: 2
-guid: e49f438c1d69d61448301cd85b12138a
-folderAsset: yes
-timeCreated: 1499148953
-licenseType: Free
-DefaultImporter:
-  userData: 
-  assetBundleName: 
-  assetBundleVariant: 

+ 0 - 9
Unity/Assets/Res/Unit.meta

@@ -1,9 +0,0 @@
-fileFormatVersion: 2
-guid: d8216925aec124948b7aa2b0798d80f6
-folderAsset: yes
-timeCreated: 1499149187
-licenseType: Free
-DefaultImporter:
-  userData: 
-  assetBundleName: 
-  assetBundleVariant: 

+ 0 - 0
Unity/Assets/Scripts/Config.meta → Unity/Assets/Scripts/Base/Config.meta


+ 0 - 0
Unity/Assets/Scripts/Config/ACategory.cs → Unity/Assets/Scripts/Base/Config/ACategory.cs


+ 0 - 0
Unity/Assets/Scripts/Config/ACategory.cs.meta → Unity/Assets/Scripts/Base/Config/ACategory.cs.meta


+ 0 - 0
Unity/Assets/Scripts/Config/AConfig.cs → Unity/Assets/Scripts/Base/Config/AConfig.cs


+ 0 - 0
Unity/Assets/Scripts/Config/AConfig.cs.meta → Unity/Assets/Scripts/Base/Config/AConfig.cs.meta


+ 0 - 0
Unity/Assets/Scripts/Config/AConfigComponent.cs → Unity/Assets/Scripts/Base/Config/AConfigComponent.cs


+ 0 - 0
Unity/Assets/Scripts/Config/AConfigComponent.cs.meta → Unity/Assets/Scripts/Base/Config/AConfigComponent.cs.meta


+ 0 - 0
Unity/Assets/Scripts/Config/ConfigAttribute.cs → Unity/Assets/Scripts/Base/Config/ConfigAttribute.cs


+ 0 - 0
Unity/Assets/Scripts/Config/ConfigAttribute.cs.meta → Unity/Assets/Scripts/Base/Config/ConfigAttribute.cs.meta


+ 0 - 0
Unity/Assets/Scripts/Config/ConfigHelper.cs → Unity/Assets/Scripts/Base/Config/ConfigHelper.cs


+ 0 - 0
Unity/Assets/Scripts/Config/ConfigHelper.cs.meta → Unity/Assets/Scripts/Base/Config/ConfigHelper.cs.meta


+ 0 - 0
Unity/Assets/Scripts/Config/ICategory.cs → Unity/Assets/Scripts/Base/Config/ICategory.cs


+ 0 - 0
Unity/Assets/Scripts/Config/ICategory.cs.meta → Unity/Assets/Scripts/Base/Config/ICategory.cs.meta


+ 0 - 0
Unity/Assets/Scripts/Event.meta → Unity/Assets/Scripts/Base/Event.meta


+ 0 - 0
Unity/Assets/Scripts/Event/AEventAttribute.cs → Unity/Assets/Scripts/Base/Event/AEventAttribute.cs


+ 0 - 0
Unity/Assets/Scripts/Event/AEventAttribute.cs.meta → Unity/Assets/Scripts/Base/Event/AEventAttribute.cs.meta


+ 0 - 0
Unity/Assets/Scripts/Event/CrossEventAttribute.cs → Unity/Assets/Scripts/Base/Event/CrossEventAttribute.cs


+ 0 - 0
Unity/Assets/Scripts/Event/CrossEventAttribute.cs.meta → Unity/Assets/Scripts/Base/Event/CrossEventAttribute.cs.meta


+ 0 - 0
Unity/Assets/Scripts/Event/CrossIdType.cs → Unity/Assets/Scripts/Base/Event/CrossIdType.cs


+ 0 - 0
Unity/Assets/Scripts/Event/CrossIdType.cs.meta → Unity/Assets/Scripts/Base/Event/CrossIdType.cs.meta


+ 0 - 0
Unity/Assets/Scripts/Event/Env.cs → Unity/Assets/Scripts/Base/Event/Env.cs


+ 0 - 0
Unity/Assets/Scripts/Event/Env.cs.meta → Unity/Assets/Scripts/Base/Event/Env.cs.meta


+ 0 - 0
Unity/Assets/Scripts/Event/EnvKey.cs → Unity/Assets/Scripts/Base/Event/EnvKey.cs


+ 0 - 0
Unity/Assets/Scripts/Event/EnvKey.cs.meta → Unity/Assets/Scripts/Base/Event/EnvKey.cs.meta


+ 0 - 0
Unity/Assets/Scripts/Event/EventAttribute.cs → Unity/Assets/Scripts/Base/Event/EventAttribute.cs


+ 0 - 0
Unity/Assets/Scripts/Event/EventAttribute.cs.meta → Unity/Assets/Scripts/Base/Event/EventAttribute.cs.meta


+ 0 - 0
Unity/Assets/Scripts/Event/EventIdType.cs → Unity/Assets/Scripts/Base/Event/EventIdType.cs


+ 0 - 0
Unity/Assets/Scripts/Event/EventIdType.cs.meta → Unity/Assets/Scripts/Base/Event/EventIdType.cs.meta


+ 0 - 0
Unity/Assets/Scripts/Event/IEvent.cs → Unity/Assets/Scripts/Base/Event/IEvent.cs


+ 0 - 0
Unity/Assets/Scripts/Event/IEvent.cs.meta → Unity/Assets/Scripts/Base/Event/IEvent.cs.meta


+ 37 - 0
Unity/Assets/Scripts/Base/Helper/NetworkHelper.cs

@@ -0,0 +1,37 @@
+using System.Net;
+
+namespace Model
+{
+	public static class NetworkHelper
+	{
+		public static uint NetworkToHostOrder(uint a)
+		{
+			return (uint) IPAddress.NetworkToHostOrder((int) a);
+		}
+
+		public static ushort NetworkToHostOrder(ushort a)
+		{
+			return (ushort)IPAddress.NetworkToHostOrder((short)a);
+		}
+
+		public static ulong NetworkToHostOrder(ulong a)
+		{
+			return (ushort)IPAddress.NetworkToHostOrder((long)a);
+		}
+
+		public static uint HostToNetworkOrder(uint a)
+		{
+			return (uint)IPAddress.HostToNetworkOrder((int)a);
+		}
+
+		public static ushort HostToNetworkOrder(ushort a)
+		{
+			return (ushort)IPAddress.HostToNetworkOrder((short)a);
+		}
+
+		public static ulong HostToNetworkOrder(ulong a)
+		{
+			return (ushort)IPAddress.HostToNetworkOrder((long)a);
+		}
+	}
+}

+ 2 - 2
Unity/Assets/Scripts/Base/Network/TNet/TSocket.cs.meta → Unity/Assets/Scripts/Base/Helper/NetworkHelper.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
-guid: 1246431e11499344b8c036e69e2a71dd
-timeCreated: 1429240758
+guid: a81084246b258ce4fa06d1c51d1ee82b
+timeCreated: 1503567373
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 0 - 0
Unity/Assets/Scripts/IL.meta → Unity/Assets/Scripts/Base/IL.meta


+ 0 - 0
Unity/Assets/Scripts/IL/IAsyncStateMachineAdaptor.cs → Unity/Assets/Scripts/Base/IL/IAsyncStateMachineAdaptor.cs


+ 0 - 0
Unity/Assets/Scripts/IL/IAsyncStateMachineAdaptor.cs.meta → Unity/Assets/Scripts/Base/IL/IAsyncStateMachineAdaptor.cs.meta


+ 0 - 0
Unity/Assets/Scripts/IL/IDisposableAdaptor.cs → Unity/Assets/Scripts/Base/IL/IDisposableAdaptor.cs


+ 0 - 0
Unity/Assets/Scripts/IL/IDisposableAdaptor.cs.meta → Unity/Assets/Scripts/Base/IL/IDisposableAdaptor.cs.meta


+ 0 - 0
Unity/Assets/Scripts/IL/ILAdapterAttribute.cs → Unity/Assets/Scripts/Base/IL/ILAdapterAttribute.cs


+ 0 - 0
Unity/Assets/Scripts/IL/ILAdapterAttribute.cs.meta → Unity/Assets/Scripts/Base/IL/ILAdapterAttribute.cs.meta


+ 0 - 0
Unity/Assets/Scripts/IL/ILBindingAttribute.cs → Unity/Assets/Scripts/Base/IL/ILBindingAttribute.cs


+ 0 - 0
Unity/Assets/Scripts/IL/ILBindingAttribute.cs.meta → Unity/Assets/Scripts/Base/IL/ILBindingAttribute.cs.meta


+ 0 - 0
Unity/Assets/Scripts/IL/ILRedirection.cs → Unity/Assets/Scripts/Base/IL/ILRedirection.cs


+ 0 - 0
Unity/Assets/Scripts/IL/ILRedirection.cs.meta → Unity/Assets/Scripts/Base/IL/ILRedirection.cs.meta


+ 0 - 0
Unity/Assets/Scripts/Message.meta → Unity/Assets/Scripts/Base/Message.meta


Некоторые файлы не были показаны из-за большого количества измененных файлов