public void Dispatch(Session session, PacketInfo packetInfo) { if (OpcodeHelper.IsClientHotfixMessage(packetInfo.Opcode)) { HotfixCallback.Invoke(session, packetInfo); return; } Type messageType = Game.Scene.GetComponent <OpcodeTypeComponent>().GetType(packetInfo.Opcode); IMessage message = (IMessage)session.Network.MessagePacker.DeserializeFrom(messageType, packetInfo.Bytes, packetInfo.Index, packetInfo.Length); // 如果是帧同步消息,交给ClientFrameComponent处理 FrameMessage frameMessage = message as FrameMessage; if (frameMessage != null) { Game.Scene.GetComponent <ClientFrameComponent>().Add(session, frameMessage); return; } // 普通消息或者是Rpc请求消息 MessageInfo messageInfo = new MessageInfo(packetInfo.Opcode, message); Game.Scene.GetComponent <MessageDispatherComponent>().Handle(session, messageInfo); }
public void Dispatch(Session session, PacketInfo packetInfo) { #if ILRuntime // 热更消息抛到hotfix层 if (OpcodeHelper.IsClientHotfixMessage(packetInfo.Header.Opcode)) { Game.EventSystem.Run(EventIdType.RecvHotfixMessage, packetInfo); return; } #endif Type messageType = Game.Scene.GetComponent <OpcodeTypeComponent>().GetType(packetInfo.Opcode); IMessage message = (IMessage)session.Network.MessagePacker.DeserializeFrom(messageType, packetInfo.Bytes, packetInfo.Index, packetInfo.Length); // 如果是帧同步消息,交给ClientFrameComponent处理 FrameMessage frameMessage = message as FrameMessage; if (frameMessage != null) { Game.Scene.GetComponent <ClientFrameComponent>().Add(session, frameMessage); return; } // 普通消息或者是Rpc请求消息 MessageInfo messageInfo = new MessageInfo(packetInfo.Opcode, message); Game.Scene.GetComponent <MessageDispatherComponent>().Handle(session, messageInfo); }
private void Run(Packet packet) { if (packet.Length < Packet.MinSize) { Log.Error($"message error length < {Packet.MinSize}, ip: {this.RemoteAddress}"); this.Network.Remove(this.Id); return; } byte flag = packet.Flag(); ushort opcode = packet.Opcode(); PacketInfo packetInfo = new PacketInfo { Opcode = opcode, Bytes = packet.Bytes }; if ((flag & 0xC0) > 0) { uint rpcId = packet.RpcId(); packetInfo.RpcId = rpcId; packetInfo.Index = Packet.RpcIdIndex + 4; packetInfo.Length = (ushort)(packet.Length - packetInfo.Index); // flag第2位表示这是rpc返回消息 if ((flag & 0x40) > 0) { Action <PacketInfo> action; if (!this.requestCallback.TryGetValue(rpcId, out action)) { return; } this.requestCallback.Remove(rpcId); action(packetInfo); return; } } else { packetInfo.RpcId = 0; packetInfo.Index = Packet.RpcIdIndex; packetInfo.Length = (ushort)(packet.Length - packetInfo.Index); } this.Network.MessageDispatcher.Dispatch(this, packetInfo); }
public static async Task <IResponse> Call(this Session session, IRequest request) { OpcodeTypeComponent opcodeTypeComponent = Game.Scene.GetComponent <OpcodeTypeComponent>(); ushort opcode = opcodeTypeComponent.GetOpcode(request.GetType()); byte[] bytes = session.Network.MessagePacker.SerializeToByteArray(request); PacketInfo packetInfo = await session.Call(opcode, bytes); Type responseType = opcodeTypeComponent.GetType(packetInfo.Opcode); object message = session.Network.MessagePacker.DeserializeFrom(responseType, packetInfo.Bytes, packetInfo.Index, packetInfo.Length); IResponse response = (IResponse)message; if (response.Error > 100) { throw new RpcException(response.Error, response.Message); } return(response); }
public void Dispatch(Session session, PacketInfo packetInfo) { Type messageType = Game.Scene.GetComponent <OpcodeTypeComponent>().GetType(packetInfo.Header.Opcode); IMessage message = (IMessage)session.Network.MessagePacker.DeserializeFrom(messageType, packetInfo.Bytes, packetInfo.Index, packetInfo.Length); // 如果是帧同步消息,交给ClientFrameComponent处理 FrameMessage frameMessage = message as FrameMessage; if (frameMessage != null) { Game.Scene.GetComponent <ClientFrameComponent>().Add(session, frameMessage); return; } // 普通消息或者是Rpc请求消息 if (message is IMessage || message is IRequest) { MessageInfo messageInfo = new MessageInfo(packetInfo.Header.Opcode, message); Game.Scene.GetComponent <MessageDispatherComponent>().Handle(session, messageInfo); return; } throw new Exception($"message type error: {message.GetType().FullName}"); }
private void Run(Packet packet) { if (packet.Length < Packet.MinSize) { Log.Error($"message error length < {Packet.MinSize}, ip: {this.RemoteAddress}"); this.Network.Remove(this.Id); return; } ushort headerSize = BitConverter.ToUInt16(packet.Bytes, 0); Header header = this.Network.MessagePacker.DeserializeFrom <Header>(packet.Bytes, 2, headerSize); byte flag = header.Flag; PacketInfo packetInfo = new PacketInfo { Header = header, Index = (ushort)(headerSize + 2), Bytes = packet.Bytes, Length = (ushort)(packet.Length - 2 - headerSize) }; // flag第2位表示这是rpc返回消息 if ((flag & 0x40) > 0) { uint rpcId = header.RpcId; Action <PacketInfo> action; if (!this.requestCallback.TryGetValue(rpcId, out action)) { return; } this.requestCallback.Remove(rpcId); action(packetInfo); return; } this.Network.MessageDispatcher.Dispatch(this, packetInfo); }