/// <summary> /// 主动向客户端通知 或者 回客户端消息, 先编码消息, 再编码长度 /// </summary> public void Send(Cmd cmd, IMessage message) { var comMsg = new CommonMessage { Cmd = cmd }; //server ack client message or notify message ProtoUtil.AckCommonMsg(cmd, comMsg, message); NetPacket netPacket = new NetPacket(); netPacket.cmd = (int)cmd; netPacket.data = comMsg.ToByteArray(); //先编码消息, 再编码长度 byte[] byteArr = lengthEncode(messageEncode(netPacket)); Write(byteArr); }
/// <summary> /// 广播注册事件 /// </summary> /// <param name="packet"></param> private void DispatchCmdEvent(NetPacket packet) { /// CommonMessage解码 var commonMsg = CommonMessage.Parser.ParseFrom(packet.data); var packetCmd = packet.cmd; IMessage retMessage = ProtoUtil.AckCommonMsg(commonMsg); /// C#端处理 /// 分发注册的事件 Listeners.Dispatch(packet.cmd, (a) => a(retMessage)); /// 分发回调的事件 DispatchCmdCallback(packet.cmd, retMessage); /// Lua测消息接收(未解析的数据,Lua测单独处理) /// int : cmd /// byte[] : CommonMessage OnLuaNetMsgRecv?.Invoke(packet.cmd, packet.data); }