public Task <IResponse> Call(IRequest request, CancellationToken cancellationToken) { uint rpcId = ++RpcId; var tcs = new TaskCompletionSource <IResponse>(); OpcodeTypeComponent opcodeTypeComponent = this.Network.Entity.GetComponent <OpcodeTypeComponent>(); ushort opcode = opcodeTypeComponent.GetOpcode(request.GetType()); byte[] bytes = this.Network.MessagePacker.SerializeToByteArray(request); this.requestCallback[rpcId] = (packetInfo) => { try { Type responseType = opcodeTypeComponent.GetType(packetInfo.Opcode); object message = this.Network.MessagePacker.DeserializeFrom(responseType, packetInfo.Bytes, packetInfo.Index, packetInfo.Length); IResponse response = (IResponse)message; if (response.Error > ErrorCode.ERR_Exception) { throw new RpcException(response.Error, response.Message); } tcs.SetResult(response); } catch (Exception e) { tcs.SetException(new Exception($"Rpc Error: {packetInfo.Opcode}", e)); } }; const byte flag = 0x80; this.SendMessage(flag, opcode, rpcId, bytes); return(tcs.Task); }
public void Send(IMessage message) { OpcodeTypeComponent opcodeTypeComponent = Game.Scene.GetComponent <OpcodeTypeComponent>(); ushort opcode = opcodeTypeComponent.GetOpcode(message.GetType()); byte[] bytes = this.Network.MessagePacker.SerializeToByteArray(message); this.Send(opcode, bytes); }
public static void Reply(this Session session, uint rpcId, IResponse message) { OpcodeTypeComponent opcodeTypeComponent = Game.Scene.GetComponent <OpcodeTypeComponent>(); ushort opcode = opcodeTypeComponent.GetOpcode(message.GetType()); byte[] bytes = session.Network.MessagePacker.SerializeToByteArray(message); session.Reply(opcode, rpcId, bytes); }
public void Reply(uint rpcId, IResponse message) { if (this.IsDisposed) { throw new Exception("session已经被Dispose了"); } OpcodeTypeComponent opcodeTypeComponent = Game.Scene.GetComponent <OpcodeTypeComponent>(); ushort opcode = opcodeTypeComponent.GetOpcode(message.GetType()); byte[] bytes = this.Network.MessagePacker.SerializeToByteArray(message); const byte flag = 0x40; this.SendMessage(flag, opcode, rpcId, bytes); }
public void Load() { this.handlers.Clear(); Model.MessageDispatherComponent messageDispatherComponent = Game.Scene.GetComponent <Model.MessageDispatherComponent>(); Model.OpcodeTypeComponent opcodeTypeComponent = Game.Scene.GetComponent <Model.OpcodeTypeComponent>(); Type[] types = DllHelper.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); } } }
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); }