Explorar el Código

opcode使用枚举类型

tanghai hace 8 años
padre
commit
f00711e95e

+ 1 - 0
Client-Server.sln.DotSettings.user

@@ -6,6 +6,7 @@
 	<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003APlayerComponent_002Ecs_002Fl_003AUnity_003FAssets_003FScripts_003FComponent_003FPlayerComponent_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
 	<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003APlayerFactory_002Ecs_002Fl_003AUnity_003FAssets_003FScripts_003FFactory_003FPlayerFactory_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
 	<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AUnitComponent_002Ecs_002Fl_003AUnity_003FAssets_003FScripts_003FComponent_003FUnitComponent_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
+	<s:String x:Key="/Default/CodeInspection/Highlighting/AnalysisEnabled/@EntryValue">VISIBLE_FILES</s:String>
 	<s:String x:Key="/Default/CodeStyle/CodeCleanup/Profiles/=Unity/@EntryIndexedValue">&lt;?xml version="1.0" encoding="utf-16"?&gt;&lt;Profile name="Unity"&gt;&lt;CSArrangeThisQualifier&gt;True&lt;/CSArrangeThisQualifier&gt;&lt;CSRemoveCodeRedundancies&gt;True&lt;/CSRemoveCodeRedundancies&gt;&lt;CSMakeFieldReadonly&gt;True&lt;/CSMakeFieldReadonly&gt;&lt;CSUseVar&gt;&lt;BehavourStyle&gt;DISABLED&lt;/BehavourStyle&gt;&lt;LocalVariableStyle&gt;IMPLICIT_WHEN_INITIALIZER_HAS_TYPE&lt;/LocalVariableStyle&gt;&lt;ForeachVariableStyle&gt;IMPLICIT_EXCEPT_SIMPLE_TYPES&lt;/ForeachVariableStyle&gt;&lt;/CSUseVar&gt;&lt;CSOptimizeUsings&gt;&lt;OptimizeUsings&gt;True&lt;/OptimizeUsings&gt;&lt;EmbraceInRegion&gt;False&lt;/EmbraceInRegion&gt;&lt;RegionName&gt;&lt;/RegionName&gt;&lt;/CSOptimizeUsings&gt;&lt;CSShortenReferences&gt;True&lt;/CSShortenReferences&gt;&lt;CSReformatCode&gt;True&lt;/CSReformatCode&gt;&lt;CSMakeAutoPropertyGetOnly&gt;True&lt;/CSMakeAutoPropertyGetOnly&gt;&lt;CSUseAutoProperty&gt;True&lt;/CSUseAutoProperty&gt;&lt;RemoveCodeRedundancies&gt;True&lt;/RemoveCodeRedundancies&gt;&lt;CSCodeStyleAttributes ArrangeTypeAccessModifier="True" ArrangeTypeMemberAccessModifier="True" SortModifiers="True" RemoveRedundantParentheses="True" AddMissingParentheses="True" ArrangeBraces="True" ArrangeAttributes="True" ArrangeArgumentsStyle="True" ArrangeCodeBodyStyle="True" /&gt;&lt;/Profile&gt;</s:String>
 	<s:String x:Key="/Default/Housekeeping/Layout/DialogWindows/RefactoringWizardWindow/Location/@EntryValue">247,-4</s:String>
 	<s:Int64 x:Key="/Default/Environment/SearchAndNavigation/DefaultOccurrencesGroupingIndex/@EntryValue">0</s:Int64>

+ 1 - 1
Server/Hotfix/Other/InnerMessageDispatcher.cs

@@ -5,7 +5,7 @@ namespace Hotfix
 {
 	public class InnerMessageDispatcher: IMessageDispatcher
 	{
-		public void Dispatch(Session session, ushort opcode, int offset, byte[] messageBytes, AMessage message)
+		public void Dispatch(Session session, Opcode opcode, int offset, byte[] messageBytes, AMessage message)
 		{
 			// 收到actor rpc request
 			if (message is ActorRpcRequest actorRpcRequest)

+ 1 - 1
Server/Hotfix/Other/OuterMessageDispatcher.cs

@@ -5,7 +5,7 @@ namespace Hotfix
 {
 	public class OuterMessageDispatcher: IMessageDispatcher
 	{
-		public async void Dispatch(Session session, ushort opcode, int offset, byte[] messageBytes, AMessage message)
+		public async void Dispatch(Session session, Opcode opcode, int offset, byte[] messageBytes, AMessage message)
 		{
 			// gate session收到actor消息直接转发给actor自己去处理
 			if (message is AActorMessage)

+ 2 - 2
Server/Model/Base/Message/MessageAttribute.cs

@@ -4,9 +4,9 @@ namespace Model
 {
 	public class MessageAttribute: Attribute
 	{
-		public ushort Opcode { get; private set; }
+		public Opcode Opcode { get; }
 
-		public MessageAttribute(ushort opcode)
+		public MessageAttribute(Opcode opcode)
 		{
 			this.Opcode = opcode;
 		}

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

@@ -19,7 +19,7 @@ namespace Model
 	
 	public class OpcodeTypeComponent : Component
 	{
-		private Dictionary<ushort, Type> opcodeType { get; set; }
+		private Dictionary<Opcode, Type> opcodeType { get; set; }
 		private Dictionary<Type, MessageAttribute> messageOpcode { get; set; }
 
 		public void Awake()
@@ -29,7 +29,7 @@ namespace Model
 
 		public void Load()
 		{
-			this.opcodeType = new Dictionary<ushort, Type>();
+			this.opcodeType = new Dictionary<Opcode, Type>();
 			this.messageOpcode = new Dictionary<Type, MessageAttribute>();
 
 			Type[] types = DllHelper.GetMonoTypes();
@@ -47,7 +47,7 @@ namespace Model
 			}
 		}
 
-		public ushort GetOpcode(Type type)
+		public Opcode GetOpcode(Type type)
 		{
 			if (!this.messageOpcode.TryGetValue(type, out MessageAttribute messageAttribute))
 			{
@@ -56,7 +56,7 @@ namespace Model
 			return messageAttribute.Opcode;
 		}
 
-		public Type GetType(ushort opcode)
+		public Type GetType(Opcode opcode)
 		{
 			if (!this.opcodeType.TryGetValue(opcode, out Type messageType))
 			{

+ 0 - 49
Server/Model/Entity/Message/InnerOpcode.cs

@@ -1,49 +0,0 @@
-namespace Model
-{
-	// 1-999
-	public static partial class Opcode
-	{
-		public const ushort ActorRequest = 1;
-		public const ushort ActorResponse = 2;
-		public const ushort ActorRpcRequest = 3;
-		public const ushort ActorRpcResponse = 4;
-		public const ushort G2G_LockRequest = 10;
-		public const ushort G2G_LockResponse = 11;
-		public const ushort G2G_LockReleaseRequest = 12;
-		public const ushort G2G_LockReleaseResponse = 13;
-
-		public const ushort M2A_Reload = 20;
-		public const ushort A2M_Reload = 21;
-
-		public const ushort DBSaveRequest = 26;
-		public const ushort DBSaveResponse = 27;
-		public const ushort DBQueryRequest = 28;
-		public const ushort DBQueryResponse = 29;
-		public const ushort DBSaveBatchResponse = 37;
-		public const ushort DBSaveBatchRequest = 38;
-		public const ushort DBQueryBatchRequest = 61;
-		public const ushort DBQueryBatchResponse = 62;
-		public const ushort DBQueryJsonRequest = 65;
-		public const ushort DBQueryJsonResponse = 66;
-
-		public const ushort ObjectAddRequest = 70;
-		public const ushort ObjectAddResponse = 71;
-		public const ushort ObjectRemoveRequest = 72;
-		public const ushort ObjectRemoveResponse = 73;
-		public const ushort ObjectLockRequest = 74;
-		public const ushort ObjectLockResponse = 75;
-		public const ushort ObjectUnLockRequest = 76;
-		public const ushort ObjectUnLockResponse = 77;
-		public const ushort ObjectGetRequest = 78;
-		public const ushort ObjectGetResponse = 79;
-
-		public const ushort R2G_GetLoginKey = 101;
-		public const ushort G2R_GetLoginKey = 102;
-
-		public const ushort G2M_CreateUnit = 103;
-		public const ushort M2G_CreateUnit = 104;
-
-		public const ushort M2M_TrasferUnitRequest = 105;
-		public const ushort M2M_TrasferUnitResponse = 106;
-	}
-}

+ 1 - 1
Unity/Assets/Scripts/Base/Message/ClientDispatcher.cs

@@ -4,7 +4,7 @@ namespace Model
 {
 	public class ClientDispatcher: IMessageDispatcher
 	{
-		public void Dispatch(Session session, ushort opcode, int offset, byte[] messageBytes, AMessage message)
+		public void Dispatch(Session session, Opcode opcode, int offset, byte[] messageBytes, AMessage message)
 		{
 			// 如果是帧同步消息,交给ClientFrameComponent处理
 			FrameMessage frameMessage = message as FrameMessage;

+ 1 - 1
Unity/Assets/Scripts/Base/Message/IMessageDispatcher.cs

@@ -2,6 +2,6 @@
 {
 	public interface IMessageDispatcher
 	{
-		void Dispatch(Session session, ushort opcode, int offset, byte[] messageBytes, AMessage message);
+		void Dispatch(Session session, Opcode opcode, int offset, byte[] messageBytes, AMessage message);
 	}
 }

+ 2 - 2
Unity/Assets/Scripts/Base/Message/MessageAttribute.cs

@@ -4,9 +4,9 @@ namespace Model
 {
 	public class MessageAttribute: Attribute
 	{
-		public ushort Opcode { get; private set; }
+		public Opcode Opcode { get; }
 
-		public MessageAttribute(ushort opcode)
+		public MessageAttribute(Opcode opcode)
 		{
 			this.Opcode = opcode;
 		}

+ 2 - 2
Unity/Assets/Scripts/Base/Message/MessageInfo.cs

@@ -2,10 +2,10 @@
 {
 	public struct MessageInfo
 	{
-		public ushort Opcode { get; set; }
+		public Opcode Opcode { get; set; }
 		public AMessage Message { get; set; }
 
-		public MessageInfo(ushort opcode, AMessage message)
+		public MessageInfo(Opcode opcode, AMessage message)
 		{
 			this.Opcode = opcode;
 			this.Message = message;

+ 1 - 1
Unity/Assets/Scripts/Component/ClientFrameComponent.cs

@@ -71,7 +71,7 @@
             for (int i = 0; i < frameMessage.Messages.Count; ++i)
             {
 	            AFrameMessage message = frameMessage.Messages[i];
-	            ushort opcode = Game.Scene.GetComponent<OpcodeTypeComponent>().GetOpcode(message.GetType());
+                Opcode opcode = Game.Scene.GetComponent<OpcodeTypeComponent>().GetOpcode(message.GetType());
                 Game.Scene.GetComponent<MessageDispatherComponent>().Handle(new MessageInfo() { Opcode= opcode, Message = message });
             }
         }

+ 9 - 37
Unity/Assets/Scripts/Component/MessageDispatherComponent.cs

@@ -67,40 +67,12 @@ namespace Model
 		}
 	}
 
-	public static class Opcode2Name
-	{
-		private static Dictionary<int, string> _init = new Dictionary<int, string>();
-		public static string GetName(int code)
-		{
-			if (_init.Count == 0)
-			{
-				Type type = typeof(Opcode);
-				FieldInfo[] fields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
-				foreach (FieldInfo field in fields)
-				{
-					if (!field.IsStatic)
-					{
-						continue;
-					}
-					int codeID = (ushort)field.GetValue(null);
-					if (_init.ContainsKey(codeID))
-					{
-						Log.Warning($"重复的Opcode:{codeID}");
-						continue;
-					}
-					_init.Add(codeID, field.Name);
-				}
-			}
-			return _init[code];
-		}
-	}
-
 	/// <summary>
 	/// 消息分发组件
 	/// </summary>
 	public class MessageDispatherComponent : Component
 	{
-		private Dictionary<ushort, List<IMessageMethod>> handlers;
+		private Dictionary<Opcode, List<IMessageMethod>> handlers;
 
 
 		public void Awake()
@@ -110,7 +82,7 @@ namespace Model
 
 		public void Load()
 		{
-			handlers = new Dictionary<ushort, List<IMessageMethod>>();
+			handlers = new Dictionary<Opcode, List<IMessageMethod>>();
 
 			Type[] types = DllHelper.GetMonoTypes();
 
@@ -123,11 +95,11 @@ namespace Model
 				}
 				MessageHandlerAttribute messageHandlerAttribute = (MessageHandlerAttribute)attrs[0];
 				IMHandler iMHandler = (IMHandler)Activator.CreateInstance(type);
-				if (!this.handlers.ContainsKey(messageHandlerAttribute.Opcode))
+				if (!this.handlers.ContainsKey((Opcode)messageHandlerAttribute.Opcode))
 				{
-					this.handlers.Add(messageHandlerAttribute.Opcode, new List<IMessageMethod>());
+					this.handlers.Add((Opcode)messageHandlerAttribute.Opcode, new List<IMessageMethod>());
 				}
-				this.handlers[messageHandlerAttribute.Opcode].Add(new IMessageMonoMethod(iMHandler));
+				this.handlers[(Opcode)messageHandlerAttribute.Opcode].Add(new IMessageMonoMethod(iMHandler));
 			}
 
 			// hotfix dll
@@ -146,11 +118,11 @@ namespace Model
 				IMHandler iMHandler = (IMHandler)Activator.CreateInstance(type);
 				IMessageMethod iMessageMethod = new IMessageMonoMethod(iMHandler);
 #endif
-				if (!this.handlers.ContainsKey(messageHandlerAttribute.Opcode))
+				if (!this.handlers.ContainsKey((Opcode)messageHandlerAttribute.Opcode))
 				{
-					this.handlers.Add(messageHandlerAttribute.Opcode, new List<IMessageMethod>());
+					this.handlers.Add((Opcode)messageHandlerAttribute.Opcode, new List<IMessageMethod>());
 				}
-				this.handlers[messageHandlerAttribute.Opcode].Add(iMessageMethod);
+				this.handlers[(Opcode)messageHandlerAttribute.Opcode].Add(iMessageMethod);
 			}
 		}
 
@@ -159,7 +131,7 @@ namespace Model
 			List<IMessageMethod> actions;
 			if (!this.handlers.TryGetValue(messageInfo.Opcode, out actions))
 			{
-				Log.Error($"消息 {Opcode2Name.GetName(messageInfo.Opcode)}({messageInfo.Opcode}) 没有处理");
+				Log.Error($"消息 {messageInfo.Opcode} 没有处理");
 				return;
 			}
 

+ 3 - 3
Unity/Assets/Scripts/Component/OpcodeTypeComponent.cs

@@ -13,7 +13,7 @@ namespace Model
 
 	public class OpcodeTypeComponent : Component
 	{
-		private readonly DoubleMap<ushort, Type> opcodeTypes = new DoubleMap<ushort, Type>();
+		private readonly DoubleMap<Opcode, Type> opcodeTypes = new DoubleMap<Opcode, Type>();
 
 		public void Awake()
 		{
@@ -36,12 +36,12 @@ namespace Model
 			}
 		}
 
-		public ushort GetOpcode(Type type)
+		public Opcode GetOpcode(Type type)
 		{
 			return this.opcodeTypes.GetKeyByValue(type);
 		}
 
-		public Type GetType(ushort opcode)
+		public Type GetType(Opcode opcode)
 		{
 			return this.opcodeTypes.GetValueByKey(opcode);
 		}

+ 66 - 18
Unity/Assets/Scripts/Entity/Message/Opcode.cs

@@ -1,24 +1,72 @@
 namespace Model
 {
 	// 1000开始
-	public static partial class Opcode
+	public enum Opcode: ushort
 	{
-		public const ushort FrameMessage = 1000;
-		public const ushort C2R_Login = 1001;
-		public const ushort R2C_Login = 1002;
-		public const ushort R2C_ServerLog = 1003;
-		public const ushort C2G_LoginGate = 1004;
-		public const ushort G2C_LoginGate = 1005;
-		public const ushort C2G_EnterMap = 1006;
-		public const ushort G2C_EnterMap = 1007;
-		public const ushort C2M_Reload = 1008;
-
-		public const ushort Actor_Test = 2001;
-		public const ushort Actor_TestRequest = 2002;
-		public const ushort Actor_TestResponse = 2003;
-		public const ushort Actor_TransferRequest = 2004;
-		public const ushort Actor_TransferResponse = 2005;
-		public const ushort Frame_ClickMap = 2006;
-		public const ushort Actor_CreateUnits = 2007;
+		FrameMessage = 1000,
+		C2R_Login = 1001,
+		R2C_Login = 1002,
+		R2C_ServerLog = 1003,
+		C2G_LoginGate = 1004,
+		G2C_LoginGate = 1005,
+		C2G_EnterMap = 1006,
+		G2C_EnterMap = 1007,
+		C2M_Reload = 1008,
+		M2C_Reload = 1009,
+		C2R_Ping = 1010,
+		R2C_Ping = 1011,
+
+		Actor_Test = 2001,
+		Actor_TestRequest = 2002,
+		Actor_TestResponse = 2003,
+		Actor_TransferRequest = 2004,
+		Actor_TransferResponse = 2005,
+		Frame_ClickMap = 2006,
+		Actor_CreateUnits = 2007,
+
+
+		// server inner opcode
+		ActorRequest = 1,
+		ActorResponse = 2,
+		ActorRpcRequest = 3,
+		ActorRpcResponse = 4,
+		G2G_LockRequest = 10,
+		G2G_LockResponse = 11,
+		G2G_LockReleaseRequest = 12,
+		G2G_LockReleaseResponse = 13,
+
+		M2A_Reload = 20,
+		A2M_Reload = 21,
+
+		DBSaveRequest = 26,
+		DBSaveResponse = 27,
+		DBQueryRequest = 28,
+		DBQueryResponse = 29,
+		DBSaveBatchResponse = 37,
+		DBSaveBatchRequest = 38,
+		DBQueryBatchRequest = 61,
+		DBQueryBatchResponse = 62,
+		DBQueryJsonRequest = 65,
+		DBQueryJsonResponse = 66,
+
+		ObjectAddRequest = 70,
+		ObjectAddResponse = 71,
+		ObjectRemoveRequest = 72,
+		ObjectRemoveResponse = 73,
+		ObjectLockRequest = 74,
+		ObjectLockResponse = 75,
+		ObjectUnLockRequest = 76,
+		ObjectUnLockResponse = 77,
+		ObjectGetRequest = 78,
+		ObjectGetResponse = 79,
+
+		R2G_GetLoginKey = 101,
+		G2R_GetLoginKey = 102,
+
+		G2M_CreateUnit = 103,
+		M2G_CreateUnit = 104,
+
+		M2M_TrasferUnitRequest = 105,
+		M2M_TrasferUnitResponse = 106,
 	}
 }

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

@@ -1,5 +1,3 @@
-// 服务器与客户端之间的消息 Opcode从1-9999
-
 using System.Collections.Generic;
 using ProtoBuf;
 
@@ -109,7 +107,6 @@ namespace Model
 		public AMessage Message;
 	}
 
-	// 服务端发给客户端,每帧一条
 	[Message(Opcode.FrameMessage)]
 	public class FrameMessage : AActorMessage
 	{
@@ -117,7 +114,6 @@ namespace Model
 		public List<AFrameMessage> Messages = new List<AFrameMessage>();
 	}
 
-	// 客户端点击地图
 	[ProtoContract]
 	[Message(Opcode.Frame_ClickMap)]
 	public class Frame_ClickMap: AFrameMessage
@@ -134,17 +130,17 @@ namespace Model
 		public AppType AppType;
 	}
 
-	[Message(11)]
+	[Message(Opcode.M2C_Reload)]
 	public class M2C_Reload: AResponse
 	{
 	}
 
-	[Message(14)]
+	[Message(Opcode.C2R_Ping)]
 	public class C2R_Ping: ARequest
 	{
 	}
 
-	[Message(15)]
+	[Message(Opcode.R2C_Ping)]
 	public class R2C_Ping: AResponse
 	{
 	}

+ 7 - 6
Unity/Assets/Scripts/Entity/Session.cs

@@ -97,7 +97,8 @@ namespace Model
 
 		private void RunDecompressedBytes(ushort opcode, byte[] messageBytes, int offset)
 		{
-			Type messageType = this.network.Entity.GetComponent<OpcodeTypeComponent>().GetType(opcode);
+			Opcode op = (Opcode)opcode;
+			Type messageType = this.network.Entity.GetComponent<OpcodeTypeComponent>().GetType(op);
 			object message = this.network.MessagePacker.DeserializeFrom(messageType, messageBytes, offset, messageBytes.Length - offset);
 
 			//Log.Debug($"recv: {MongoHelper.ToJson(message)}");
@@ -117,7 +118,7 @@ namespace Model
 				return;
 			}
 
-			this.network.MessageDispatcher.Dispatch(this, opcode, offset, messageBytes, (AMessage)message);
+			this.network.MessageDispatcher.Dispatch(this, op, offset, messageBytes, (AMessage)message);
 		}
 
 		/// <summary>
@@ -207,8 +208,8 @@ namespace Model
 		private void SendMessage(object message)
 		{
 			//Log.Debug($"send: {MongoHelper.ToJson(message)}");
-			ushort opcode = this.network.Entity.GetComponent<OpcodeTypeComponent>().GetOpcode(message.GetType());
-
+			Opcode opcode = this.network.Entity.GetComponent<OpcodeTypeComponent>().GetOpcode(message.GetType());
+			ushort op = (ushort)opcode;
 			byte[] messageBytes = this.network.MessagePacker.SerializeToByteArray(message);
 			if (messageBytes.Length > 100)
 			{
@@ -216,11 +217,11 @@ namespace Model
 				if (newMessageBytes.Length < messageBytes.Length)
 				{
 					messageBytes = newMessageBytes;
-					opcode |= 0x8000;
+					op |= 0x8000;
 				}
 			}
 
-			byte[] opcodeBytes = BitConverter.GetBytes(opcode);
+			byte[] opcodeBytes = BitConverter.GetBytes(op);
 			
 			this.byteses[0] = opcodeBytes;
 			this.byteses[1] = messageBytes;

+ 1 - 1
Unity/Assets/Scripts/Handler/Actor_CreateUnitsHandler.cs

@@ -2,7 +2,7 @@
 
 namespace Model
 {
-	[MessageHandler(Opcode.Actor_CreateUnits)]
+	[MessageHandler((int)Opcode.Actor_CreateUnits)]
 	public class Actor_CreateUnitsHandler : AMHandler<Actor_CreateUnits>
 	{
 		protected override void Run(Actor_CreateUnits message)

+ 1 - 1
Unity/Assets/Scripts/Handler/Actor_TestHandler.cs

@@ -1,6 +1,6 @@
 namespace Model
 {
-	[MessageHandler(Opcode.Actor_Test)]
+	[MessageHandler((int)Opcode.Actor_Test)]
 	public class Actor_TestHandler : AMHandler<Actor_Test>
 	{
 		protected override void Run(Actor_Test message)

+ 1 - 1
Unity/Assets/Scripts/Handler/Frame_ClickMapHandler.cs

@@ -2,7 +2,7 @@
 
 namespace Model
 {
-	[MessageHandler(Opcode.Frame_ClickMap)]
+	[MessageHandler((int)Opcode.Frame_ClickMap)]
 	public class Frame_ClickMapHandler : AMHandler<Frame_ClickMap>
 	{
 		protected override void Run(Frame_ClickMap message)