Procházet zdrojové kódy

1.ServiceComponet提供TCP UDP选项
2.Mongo FromBson byte数组提供index参数

tanghai před 11 roky
rodič
revize
0a4f447fa6

+ 1 - 0
CSharp/App/Modules/BehaviorTreeModule/NodeType.cs

@@ -53,5 +53,6 @@
 		CastPrefixSpell = 20011,
 		// 跟随unit
 		FollowUnit = 20012,
+		LookAtTarget = 20013,
 	}
 }

+ 17 - 5
CSharp/Game/Model/Component/ServiceComponent.cs

@@ -3,6 +3,7 @@ using Common.Base;
 using Common.Event;
 using Network;
 using TNet;
+using UNet;
 
 namespace Model
 {
@@ -10,9 +11,20 @@ namespace Model
 	{
 		private IService service;
 
-		public void Run(string host, int port)
+		public void Run(string host, int port, NetworkProtocol protocol = NetworkProtocol.TCP)
 		{
-			service = new TService("127.0.0.1", 8888);
+			switch (protocol)
+			{
+				case NetworkProtocol.TCP:
+					service = new TService("127.0.0.1", 8888);
+					break;
+				case NetworkProtocol.UDP:
+					service = new UService("127.0.0.1", 8888);
+					break;
+				default:
+					throw new ArgumentOutOfRangeException("protocol");
+			}
+			
 			service.Add(AcceptChannel);
 
 			service.Run();
@@ -38,10 +50,10 @@ namespace Model
 		{
 			while (true)
 			{
-				byte[] packet = await channel.RecvAsync();
+				byte[] message = await channel.RecvAsync();
 				Env env = new Env();
-				env[EnvKey.Packet] = packet;
-				int opcode = BitConverter.ToUInt16(packet, 0);
+				env[EnvKey.Message] = message;
+				int opcode = BitConverter.ToUInt16(message, 0);
 				World.Instance.GetComponent<EventComponent<MessageAttribute>>().Run(opcode, env);
 			}
 		}

+ 1 - 1
CSharp/Game/Model/EnvKey.cs

@@ -6,6 +6,6 @@
 		public const string OwnerId = "OwnerId";
 		public const string Buff = "Buff";
 		public const string BuffId = "BuffId";
-		public const string Packet = "Packet";
+		public const string Message = "Message";
 	}
 }

+ 4 - 0
CSharp/Game/Model/Model.csproj

@@ -90,6 +90,10 @@
       <Project>{b42d431a-3a54-4649-942a-c5356d7f9fbc}</Project>
       <Name>TNet</Name>
     </ProjectReference>
+    <ProjectReference Include="..\..\Platform\UNet\UNet.csproj">
+      <Project>{d0b4cfac-a368-4742-9863-68776cfa9938}</Project>
+      <Name>UNet</Name>
+    </ProjectReference>
   </ItemGroup>
   <ItemGroup>
     <None Include="packages.config" />

+ 9 - 4
CSharp/Platform/Common/Helper/MongoHelper.cs

@@ -1,4 +1,5 @@
 using System;
+using System.IO;
 using MongoDB.Bson;
 using MongoDB.Bson.IO;
 using MongoDB.Bson.Serialization;
@@ -27,14 +28,18 @@ namespace Common.Helper
 			return obj.ToBson();
 		}
 
-		public static T FromBson<T>(byte[] bytes)
+		public static object FromBson(byte[] bytes, Type type)
 		{
-			return BsonSerializer.Deserialize<T>(bytes);
+			return BsonSerializer.Deserialize(bytes, type);
 		}
 
-		public static object FromBson(byte[] bytes, Type type)
+		public static T FromBson<T>(byte[] bytes, int index = 0)
 		{
-			return BsonSerializer.Deserialize(bytes, type);
+			using (MemoryStream memoryStream = new MemoryStream(bytes))
+			{
+				memoryStream.Seek(index, SeekOrigin.Begin);
+				return (T) BsonSerializer.Deserialize(memoryStream, typeof(T));
+			}
 		}
 	}
 }

+ 6 - 0
CSharp/Platform/Network/IService.cs

@@ -3,6 +3,12 @@ using System.Threading.Tasks;
 
 namespace Network
 {
+	public enum NetworkProtocol
+	{
+		TCP,
+		UDP,
+	}
+
 	public interface IService: IDisposable
 	{
 		/// <summary>

+ 1 - 0
CSharp/Platform/TNet/PacketParser.cs

@@ -10,6 +10,7 @@ namespace TNet
 
 	internal class PacketParser
 	{
+		private const int packetSizeMax = 128 * 1024;
 		private readonly TBuffer buffer;
 
 		private int packetSize;