public Task <IResponse> Call(IRequest request, CancellationToken cancellationToken) { int rpcId = ++RpcId; var tcs = new TaskCompletionSource <IResponse>(); this.requestCallback[rpcId] = (response) => { try { if (ErrorCode.IsRpcNeedThrowException(response.Error)) { throw new RpcException(response.Error, response.Message); } mLatencyComponent.AddAMsgLan(TimeHelper.GetCurrentTimeUnix() - response.Time); tcs.SetResult(response); } catch (Exception e) { tcs.SetException(new Exception($"Rpc Error: {request.GetType().FullName}", e)); } }; cancellationToken.Register(() => this.requestCallback.Remove(rpcId)); request.RpcId = rpcId; request.Time = TimeHelper.GetCurrentTimeUnix(); this.Send(0x00, request); return(tcs.Task); }
public Task <IResponse> Call(IRequest request) { int rpcId = ++RpcId; var tcs = new TaskCompletionSource <IResponse>(); this.requestCallback[rpcId] = (response) => { try { if (ErrorCode.IsRpcNeedThrowException(response.Error)) { throw new RpcException(response.Error, response.Message); } Log.Info("RemoteTime = " + response.Time + "Local Time = " + TimeHelper.GetCurrentTimeUnix()); mLatencyComponent.AddAMsgLan(TimeHelper.GetCurrentTimeUnix() - response.Time); tcs.SetResult(response); } catch (Exception e) { tcs.SetException(new Exception($"Rpc Error: {request.GetType().FullName}", e)); } }; request.RpcId = rpcId; request.Time = TimeHelper.GetCurrentTimeUnix(); this.Send(0x00, request); return(tcs.Task); }
private void Run(Packet packet) { packet.Flag = packet.Bytes[Packet.FlagIndex]; packet.Opcode = BitConverter.ToUInt16(packet.Bytes, Packet.OpcodeIndex); packet.Stream.Seek(Packet.MessageIndex, SeekOrigin.Begin); byte flag = packet.Flag; ushort opcode = packet.Opcode; #if !SERVER /*if (OpcodeHelper.IsClientHotfixMessage(opcode)) * { * this.Network.MessageDispatcher.Dispatch(this, packet); * return; * }*/ #endif Log.Info(flag.ToHex()); // flag第一位为1表示这是rpc返回消息,否则交由MessageDispatcher分发 if ((flag & 0x01) == 0) { this.Network.MessageDispatcher.Dispatch(this, packet); return; } object message; try { OpcodeTypeComponent opcodeTypeComponent = this.Network.Entity.GetComponent <OpcodeTypeComponent>(); object instance = opcodeTypeComponent.GetInstance(opcode); Log.Info(opcode.ToString()); message = this.Network.MessagePacker.DeserializeFrom(instance, packet.Stream); //Log.Debug($"recv: {JsonHelper.ToJson(message)}"); } catch (Exception e) { // 出现任何消息解析异常都要断开Session,防止客户端伪造消息 Log.Error($"opcode: {opcode} {this.Network.Count} {e} "); this.Error = ErrorCode.ERR_PacketParserError; this.Network.Remove(this.Id); return; } IResponse response = message as IResponse; mLatencyComponent.AddAMsgLan(TimeHelper.GetCurrentTimeUnix() - response.Time); if (response == null) { throw new Exception($"flag is response, but message is not! {opcode}"); } Action <IResponse> action; if (!this.requestCallback.TryGetValue(response.RpcId, out action)) { return; } this.requestCallback.Remove(response.RpcId); action(response); }
public void Reply(IResponse message) { if (this.IsDisposed) { throw new Exception("session已经被Dispose了"); } message.Time = TimeHelper.GetCurrentTimeUnix(); this.Send(0x01, message); }
protected override void Run(Session session, UnitSnapshotMsg message) { Debug.Log("Handle"); ETModel.Game.Scene.GetComponent <LatencyComponent>().AddAMsgLan(message.Time - TimeHelper.GetCurrentTimeUnix()); UnitComponent unitComponent = ETModel.Game.Scene.GetComponent <UnitComponent>(); for (int i = 0; i < message.Units.Count; i++) { if (unitComponent.Get(message.Units[i].Id) == null) { //UpdateInfo; continue; } else { Unit u = (unitComponent.Get(message.Units[i].Id)); u.mInputAssignment = (InputAssignment)message.Units[i].Info.InputAssignment; // u.GetComponent<FrameMoveComponent>().moveData.posX = message.Units[i].MoveComponentBytes.PosX; // u.GetComponent<FrameMoveComponent>().moveData.posY = message.Units[i].MoveComponentBytes.PosY; } } }
public void Send(IMessage message) { message.Time = TimeHelper.GetCurrentTimeUnix(); this.Send(0x00, message); }