public void Send(byte flag, IMessage message) { OpcodeTypeComponent opcodeTypeComponent = this.Network.Entity.GetComponent <OpcodeTypeComponent>(); ushort opcode = opcodeTypeComponent.GetOpcode(message.GetType()); byte[] bytes = this.Network.MessagePacker.SerializeToByteArray(message); Send(flag, opcode, bytes); }
public void Load() { this.handlers.Clear(); BK.MessageDispatherComponent messageDispatherComponent = BK.Game.MessageDispatherComponent; BK.OpcodeTypeComponent opcodeTypeComponent = BK.Game.OpcodeTypeComponent; List <Type> types = BK.Game.Hotfix.GetHotfixTypes(); foreach (Type type in types) { object[] attrs = type.GetCustomAttributes(typeof(MessageHandlerAttribute), false); if (attrs.Length == 0) { continue; } IMHandler iMHandler = Activator.CreateInstance(type) as IMHandler; if (iMHandler == null) { Log.Error($"message handle {type.Name} 需要继承 IMHandler"); continue; } Type messageType = iMHandler.GetMessageType(); ushort opcode = this.Entity.GetComponent <OpcodeTypeComponent>().GetOpcode(messageType); if (opcode != 0) { this.RegisterHandler(opcode, iMHandler); } // 尝试注册到mono层 if (messageDispatherComponent != null && opcodeTypeComponent != null) { ushort monoOpcode = opcodeTypeComponent.GetOpcode(messageType); if (monoOpcode == 0) { continue; } MessageProxy messageProxy = new MessageProxy(messageType, (session, o) => { iMHandler.Handle(session, o); }); messageDispatherComponent.RegisterHandler(monoOpcode, messageProxy); } } }