MessageDispatherComponent.cs 2.4 KB
Newer Older
1 2 3
using System;
using System.Collections.Generic;

4
namespace Model
5 6 7 8
{
	/// <summary>
	/// 消息分发组件
	/// </summary>
9
	[ObjectEvent(EntityEventId.MessageDispatherComponent)]
10
	public class MessageDispatherComponent: Component, IAwake, ILoad
11
	{
T
tanghai 已提交
12 13
		private Dictionary<ushort, List<IInstanceMethod>> handlers;
		private DoubleMap<ushort, Type> opcodeTypes = new DoubleMap<ushort, Type>();
T
tanghai 已提交
14

15
		public void Awake()
16 17 18 19
		{
			this.Load();
		}

20
		public void Load()
21
		{
T
tanghai 已提交
22 23
			this.handlers = new Dictionary<ushort, List<IInstanceMethod>>();
			this.opcodeTypes = new DoubleMap<ushort, Type>();
24

25
			Type[] monoTypes = DllHelper.GetMonoTypes();
T
tanghai 已提交
26
			foreach (Type monoType in monoTypes)
27
			{
T
tanghai 已提交
28 29
				object[] attrs = monoType.GetCustomAttributes(typeof(MessageAttribute), false);
				if (attrs.Length == 0)
30
				{
T
tanghai 已提交
31
					continue;
32 33
				}

T
tanghai 已提交
34 35
				MessageAttribute messageAttribute = attrs[0] as MessageAttribute;
				if (messageAttribute == null)
36
				{
T
tanghai 已提交
37 38
					continue;
				}
39

T
tanghai 已提交
40 41
				this.opcodeTypes.Add(messageAttribute.Opcode, monoType);
			}
42
			
43
			Type[] types = DllHelper.GetMonoTypes();
44

45
			foreach (Type type in types)
T
tanghai 已提交
46 47
			{
				object[] attrs = type.GetCustomAttributes(typeof(MessageHandlerAttribute), false);
T
tanghai 已提交
48 49 50 51
				if (attrs.Length == 0)
				{
					continue;
				}
T
tanghai 已提交
52
				MessageHandlerAttribute messageHandlerAttribute = (MessageHandlerAttribute)attrs[0];
53

T
tanghai 已提交
54
				IInstanceMethod method = new ILInstanceMethod(type, "Handle");
55

T
tanghai 已提交
56 57 58
				if (!this.handlers.ContainsKey(messageHandlerAttribute.Opcode))
				{
					this.handlers.Add(messageHandlerAttribute.Opcode, new List<IInstanceMethod>());
59
				}
T
tanghai 已提交
60
				this.handlers[messageHandlerAttribute.Opcode].Add(method);
61 62 63 64 65
			}
		}

		public ushort GetOpcode(Type type)
		{
T
tanghai 已提交
66
			return this.opcodeTypes.GetKeyByValue(type);
T
tanghai 已提交
67 68
		}

69
		public void Handle(Session session, MessageInfo messageInfo)
70
		{
T
tanghai 已提交
71
			List<IInstanceMethod> actions;
72
			if (!this.handlers.TryGetValue(messageInfo.Opcode, out actions))
73
			{
74
				Log.Error($"消息 {messageInfo.Opcode} 没有处理");
75 76 77
				return;
			}

T
tanghai 已提交
78
			Type messageType = this.opcodeTypes.GetValueByKey(messageInfo.Opcode);
79
			object message = JsonHelper.FromJson(messageType, messageInfo.MessageBytes, messageInfo.Offset, messageInfo.Count);
T
tanghai 已提交
80 81
			messageInfo.Message = message;

T
tanghai 已提交
82
			foreach (IInstanceMethod ev in actions)
83 84 85
			{
				try
				{
86
					ev.Run(session, messageInfo.Message);
87 88 89 90 91 92 93
				}
				catch (Exception e)
				{
					Log.Error(e.ToString());
				}
			}
		}
T
tanghai 已提交
94

95 96 97 98 99 100 101 102 103 104 105
		public override void Dispose()
		{
			if (this.Id == 0)
			{
				return;
			}

			base.Dispose();
		}
	}
}