Parcourir la source

外网消息提供protobuf支持

tanghai il y a 8 ans
Parent
commit
0bbbe8ccf5

+ 0 - 30
Server/Model/Base/Message/AMessage.cs

@@ -1,30 +0,0 @@
-using MongoDB.Bson.Serialization.Attributes;
-
-namespace Model
-{
-	[BsonKnownTypes(typeof(ARequest))]
-	[BsonKnownTypes(typeof(AResponse))]
-	[BsonKnownTypes(typeof(AActorMessage))]
-	public abstract class AMessage
-	{
-	}
-
-	[BsonKnownTypes(typeof(AActorRequest))]
-	public abstract class ARequest: AMessage
-	{
-		[BsonIgnoreIfDefault]
-		public uint RpcId;
-	}
-
-	/// <summary>
-	/// 服务端回的RPC消息需要继承这个抽象类
-	/// </summary>
-	[BsonKnownTypes(typeof(AActorResponse))]
-	public abstract class AResponse: AMessage
-	{
-		public uint RpcId;
-
-		public int Error = 0;
-		public string Message = "";
-	}
-}

+ 1 - 0
Server/Model/Server.Model.csproj

@@ -17,6 +17,7 @@
   </PropertyGroup>
 
   <ItemGroup>
+    <Compile Include="..\..\Unity\Assets\Scripts\Base\Message\AMessage.cs" Link="Base\Message\AMessage.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Message\AppType.cs" Link="Message\AppType.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Message\IMessageDispatcher.cs" Link="Message\IMessageDispatcher.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Message\IMessagePacker.cs" Link="Message\IMessagePacker.cs" />

+ 6 - 0
Unity/Assets/Scripts/Base/Message/AActorMessage.cs

@@ -1,7 +1,10 @@
 using MongoDB.Bson.Serialization.Attributes;
+using ProtoBuf;
 
 namespace Model
 {
+	[ProtoContract]
+	[ProtoInclude(20000, typeof(AFrameMessage))]
 	public abstract class AActorMessage : AMessage
 	{
 	}
@@ -14,9 +17,12 @@ namespace Model
 	{
 	}
 
+	[ProtoContract]
+	[ProtoInclude(30000, typeof(Frame_ClickMap))]
 	[BsonKnownTypes(typeof(Frame_ClickMap))]
 	public abstract class AFrameMessage : AActorMessage
 	{
+		[ProtoMember(1)]
 		public long Id;
 	}
 }

+ 25 - 3
Unity/Assets/Scripts/Base/Message/AMessage.cs

@@ -1,22 +1,44 @@
-namespace Model
+using MongoDB.Bson.Serialization.Attributes;
+using ProtoBuf;
+
+namespace Model
 {
+	[BsonKnownTypes(typeof(ARequest))]
+	[BsonKnownTypes(typeof(AResponse))]
+	[BsonKnownTypes(typeof(AActorMessage))]
 	public abstract class AMessage
 	{
 	}
 
-	public abstract class ARequest: AMessage
+	[ProtoContract]
+	[ProtoInclude(10000, typeof(C2R_Login))]
+	[ProtoInclude(10001, typeof(C2G_LoginGate))]
+	[ProtoInclude(10002, typeof(C2G_EnterMap))]
+	[BsonKnownTypes(typeof(AActorRequest))]
+	public abstract class ARequest : AMessage
 	{
+		[ProtoMember(1000)]
+		[BsonIgnoreIfDefault]
 		public uint RpcId;
 	}
 
 	/// <summary>
 	/// 服务端回的RPC消息需要继承这个抽象类
 	/// </summary>
-	public abstract class AResponse: AMessage
+	[ProtoContract]
+	[ProtoInclude(10000, typeof(R2C_Login))]
+	[ProtoInclude(10001, typeof(G2C_LoginGate))]
+	[ProtoInclude(10002, typeof(G2C_EnterMap))]
+	[BsonKnownTypes(typeof(AActorResponse))]
+	public abstract class AResponse : AMessage
 	{
+		[ProtoMember(1000)]
 		public uint RpcId;
 
+		[ProtoMember(1001)]
 		public int Error = 0;
+
+		[ProtoMember(1002)]
 		public string Message = "";
 	}
 }

+ 17 - 7
Unity/Assets/Scripts/Entity/Message/OuterMessage.cs

@@ -1,39 +1,45 @@
 // 服务器与客户端之间的消息 Opcode从1-9999
 
 using System.Collections.Generic;
-using MongoDB.Bson.Serialization;
+using ProtoBuf;
 
 namespace Model
 {
+	[ProtoContract]
 	[Message(Opcode.C2R_Login)]
 	public class C2R_Login: ARequest
 	{
+		[ProtoMember(1)]
 		public string Account;
+
+		[ProtoMember(2)]
 		public string Password;
 	}
 
+	[ProtoContract]
 	[Message(Opcode.R2C_Login)]
 	public class R2C_Login: AResponse
 	{
+		[ProtoMember(1)]
 		public string Address { get; set; }
-		
+
+		[ProtoMember(2)]
 		public long Key { get; set; }
 	}
 
+	[ProtoContract]
 	[Message(Opcode.C2G_LoginGate)]
 	public class C2G_LoginGate: ARequest
 	{
+		[ProtoMember(1)]
 		public long Key;
-
-		public C2G_LoginGate(long key)
-		{
-			this.Key = key;
-		}
 	}
 
+	[ProtoContract]
 	[Message(Opcode.G2C_LoginGate)]
 	public class G2C_LoginGate: AResponse
 	{
+		[ProtoMember(1)]
 		public long PlayerId;
 	}
 
@@ -68,15 +74,19 @@ namespace Model
 	{
 	}
 
+	[ProtoContract]
 	[Message(Opcode.C2G_EnterMap)]
 	public class C2G_EnterMap: ARequest
 	{
 	}
 
+	[ProtoContract]
 	[Message(Opcode.G2C_EnterMap)]
 	public class G2C_EnterMap: AResponse
 	{
+		[ProtoMember(1)]
 		public long UnitId;
+		[ProtoMember(2)]
 		public int Count;
 	}
 

+ 1 - 1
Unity/Assets/Scripts/Entity/Session.cs

@@ -100,7 +100,7 @@ namespace Model
 			Type messageType = this.network.Entity.GetComponent<OpcodeTypeComponent>().GetType(opcode);
 			object message = this.network.MessagePacker.DeserializeFrom(messageType, messageBytes, offset, messageBytes.Length - offset);
 
-			//Log.Debug($"recv: {JsonHelper.ToJson(message)}");
+			//Log.Debug($"recv: {MongoHelper.ToJson(message)}");
 
 			AResponse response = message as AResponse;
 			if (response != null)

+ 1 - 1
Unity/Hotfix/UI/UILogin/Component/UILoginComponent.cs

@@ -39,7 +39,7 @@ namespace Hotfix
 				Session gateSession = Hotfix.Scene.ModelScene.GetComponent<NetOuterComponent>().Create(r2CLogin.Address);
 				Game.Scene.AddComponent<SessionComponent>().Session = gateSession;
 
-				G2C_LoginGate g2CLoginGate = await SessionComponent.Instance.Session.Call<G2C_LoginGate>(new C2G_LoginGate(r2CLogin.Key));
+				G2C_LoginGate g2CLoginGate = await SessionComponent.Instance.Session.Call<G2C_LoginGate>(new C2G_LoginGate() {Key = r2CLogin.Key});
 				Log.Info("登陆gate成功!");
 
 				// 创建Player