public void Awake(long repeatedTime, Action callback) { StartTime = TimeHelper.CurrentLocalMilliseconds(); RepeatedTime = repeatedTime; Callback = callback; Count = 1; }
public void Send(ushort opcode, object message) { if (this.IsDisposed) { throw new Exception("session已经被Dispose了"); } this.LastSendTime = TimeHelper.CurrentLocalMilliseconds(); if (OpcodeHelper.IsNeedDebugLogMessage(opcode)) { Log.Print(message); } MemoryStream stream = this.Stream; stream.Seek(Packet.MessageIndex, SeekOrigin.Begin); stream.SetLength(Packet.MessageIndex); this.Network.MessagePacker.SerializeTo(message, stream); stream.WriteToUshort(0, opcode); this.Send(stream); }
private async Task <IActorResponse> RunInner(IActorRequest iActorRequest) { try { if (ActorId == 0) { Log.Info($"actor send message fail, actorid: {Id}"); Dispose(); return(new ActorResponse() { Error = ErrorCode.ERR_ActorNotOnline }); } LastSendOrRecvTime = TimeHelper.CurrentLocalMilliseconds(); IActorResponse response = await ActorMessageSenderComponent.Instance.CallWithoutException(ActorId, iActorRequest); switch (response.Error) { case ErrorCode.ERR_NotFoundActor: { // 如果没找到Actor,重试 ++FailTimes; if (FailTimes > ActorLocationSender.MaxFailTimes) { Log.Info($"actor send message fail, actorid: {Id}"); Dispose(); return(response); } // 等待0.5s再发送 long instanceId = InstanceId; await TimerComponent.Instance.WaitAsync(500); if (InstanceId != instanceId) { throw new RpcException(ErrorCode.ERR_ActorRemove, $"{DCETRuntime.MongoHelper.ToJson(iActorRequest)}"); } ActorId = await LocationProxyComponent.Instance.Get(Id); return(await RunInner(iActorRequest)); } } LastSendOrRecvTime = TimeHelper.CurrentLocalMilliseconds(); FailTimes = 0; return(response); } catch (RpcException) { Dispose(); throw; } catch (Exception e) { Dispose(); throw new Exception($"{DCETRuntime.MongoHelper.ToJson(iActorRequest)}\n{e}"); } }
private void RunMessage(ushort opcode, object message) { this.LastRecvTime = TimeHelper.CurrentLocalMilliseconds(); if (!(message is IResponse response)) { this.Network.MessageDispatcher.Dispatch(this, opcode, message); return; } #if SERVER if (message is IActorResponse) { this.Network.MessageDispatcher.Dispatch(this, opcode, message); return; } #endif Action <IResponse> action; if (!this.requestCallback.TryGetValue(response.RpcId, out action)) { throw new Exception($"not found rpc, response message: {StringHelper.ToString(response)}"); } this.requestCallback.Remove(response.RpcId); action(response); }
public override void Update() { this.TimeNow = (uint)(TimeHelper.CurrentLocalMilliseconds() - this.StartTime); this.Recv(); this.TimerOut(); foreach (long id in updateChannels) { KChannel kChannel = this.GetKChannel(id); if (kChannel == null) { continue; } if (kChannel.Id == 0) { continue; } kChannel.Update(); } this.updateChannels.Clear(); while (this.removedChannels.Count > 0) { long id = this.removedChannels.Dequeue(); KChannel channel; if (!this.localConnChannels.TryGetValue(id, out channel)) { continue; } this.localConnChannels.Remove(id); channel.Dispose(); } }
public Task MoveToAsync(Vector3 target, float speedValue, CancellationToken cancellationToken) { Unit unit = this.GetParent <Unit>(); if ((target - this.Target).magnitude < 0.1f) { return(Task.CompletedTask); } this.Target = target; this.StartPos = unit.Position; this.StartTime = TimeHelper.CurrentLocalMilliseconds(); float distance = (this.Target - this.StartPos).magnitude; if (Math.Abs(distance) < 0.1f) { return(Task.CompletedTask); } this.needTime = (long)(distance / speedValue * 1000); this.moveTcs = new TaskCompletionSource <bool>(); cancellationToken.Register(() => { this.moveTcs = null; }); return(this.moveTcs.Task); }
public void Check() { long timeNow = TimeHelper.CurrentLocalMilliseconds(); foreach ((int key, ActorMessageSender value) in requestCallback) { if (timeNow < value.CreateTime + TIMEOUT_TIME) { continue; } TimeoutActorMessageSenders.Add(key); } foreach (int rpcId in TimeoutActorMessageSenders) { ActorMessageSender actorMessageSender = requestCallback[rpcId]; requestCallback.Remove(rpcId); Log.Error($"actor request timeout: {rpcId}"); actorMessageSender.Callback.Invoke(new ActorResponse() { Error = ErrorCode.ERR_ActorTimeout }); } TimeoutActorMessageSenders.Clear(); }
public void Awake() { LastSendOrRecvTime = TimeHelper.CurrentLocalMilliseconds(); FailTimes = 0; ActorId = 0; StartAsync(); }
public Task WaitAsync(long time) { long tillTime = TimeHelper.CurrentLocalMilliseconds() + time; var tcs = new TaskCompletionSource <bool>(); OnceWaitTimer timer = EntityFactory.CreateWithParent <OnceWaitTimer, TaskCompletionSource <bool> >(this, tcs); timers[timer.Id] = timer; AddToTimeId(tillTime, timer.Id); return(tcs.Task); }
/// <summary> /// 创建一个RepeatedTimer /// </summary> /// <param name="time"></param> /// <param name="action"></param> /// <returns></returns> public long NewRepeatedTimer(long time, Action action) { if (time < 30) { throw new Exception($"repeated time < 30"); } long tillTime = TimeHelper.CurrentLocalMilliseconds() + time; RepeatedTimer timer = EntityFactory.CreateWithParent <RepeatedTimer, long, Action>(this, time, action); timers[timer.Id] = timer; AddToTimeId(tillTime, timer.Id); return(timer.Id); }
// 开启协程移动,每100毫秒移动一次,并且协程取消的时候会计算玩家真实移动 // 比方说玩家移动了2500毫秒,玩家有新的目标,这时旧的移动协程结束,将计算250毫秒移动的位置,而不是300毫秒移动的位置 public async Task StartMove(CancellationToken cancellationToken) { Unit unit = this.GetParent <Unit>(); this.StartPos = unit.Position; this.StartTime = TimeHelper.CurrentLocalMilliseconds(); float distance = (this.Target - this.StartPos).magnitude; if (Math.Abs(distance) < 0.1f) { return; } this.needTime = (long)(distance / this.Speed * 1000); TimerComponent timerComponent = Game.Scene.GetComponent <TimerComponent>(); // 协程如果取消,将算出玩家的真实位置,赋值给玩家 cancellationToken.Register(() => { long timeNow = TimeHelper.CurrentLocalMilliseconds(); if (timeNow - this.StartTime >= this.needTime) { unit.Position = this.Target; } else { float amount = (timeNow - this.StartTime) * 1f / this.needTime; unit.Position = Vector3.Lerp(this.StartPos, this.Target, amount); } }); while (true) { await timerComponent.WaitAsync(50, cancellationToken); long timeNow = TimeHelper.CurrentLocalMilliseconds(); if (timeNow - this.StartTime >= this.needTime) { unit.Position = this.Target; break; } float amount = (timeNow - this.StartTime) * 1f / this.needTime; unit.Position = Vector3.Lerp(this.StartPos, this.Target, amount); } }
public Task WaitTillAsync(long tillTime, CancellationToken cancellationToken) { if (TimeHelper.CurrentLocalMilliseconds() > tillTime) { return(Task.CompletedTask); } var tcs = new TaskCompletionSource <bool>(); OnceWaitTimer timer = EntityFactory.CreateWithParent <OnceWaitTimer, TaskCompletionSource <bool> >(this, tcs); timers[timer.Id] = timer; AddToTimeId(tillTime, timer.Id); cancellationToken.Register(() => { Remove(timer.Id); }); return(tcs.Task); }
public void Update() { if (TimeId.Count == 0) { return; } long timeNow = TimeHelper.CurrentLocalMilliseconds(); if (timeNow < minTime) { return; } foreach (KeyValuePair <long, List <long> > kv in TimeId.GetDictionary()) { long k = kv.Key; if (k > timeNow) { minTime = k; break; } timeOutTime.Enqueue(k); } while (timeOutTime.Count > 0) { long time = timeOutTime.Dequeue(); foreach (long timerId in TimeId[time]) { timeOutTimerIds.Enqueue(timerId); } TimeId.Remove(time); } while (timeOutTimerIds.Count > 0) { long timerId = timeOutTimerIds.Dequeue(); ITimer timer; if (!timers.TryGetValue(timerId, out timer)) { continue; } timer.Run(); } }
public void Awake(AChannel aChannel) { long timeNow = TimeHelper.CurrentLocalMilliseconds(); this.LastRecvTime = timeNow; this.LastSendTime = timeNow; this.channel = aChannel; this.requestCallback.Clear(); long id = this.Id; channel.ErrorCallback += (c, e) => { this.Network.Remove(id); }; channel.ReadCallback += this.OnRead; }
public KService() { this.StartTime = TimeHelper.CurrentLocalMilliseconds(); this.TimeNow = (uint)(TimeHelper.CurrentLocalMilliseconds() - this.StartTime); this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); //this.socket.Blocking = false; this.socket.Bind(new IPEndPoint(IPAddress.Any, 0)); #if SERVER if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { const uint IOC_IN = 0x80000000; const uint IOC_VENDOR = 0x18000000; uint SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12; this.socket.IOControl((int)SIO_UDP_CONNRESET, new[] { Convert.ToByte(false) }, null); } #endif Instance = this; }
public void Check() { using (ListComponent <long> list = EntityFactory.Create <ListComponent <long> >(Domain)) { long timeNow = TimeHelper.CurrentLocalMilliseconds(); foreach ((long key, Entity value) in Children) { ActorLocationSender actorLocationMessageSender = (ActorLocationSender)value; if (timeNow > actorLocationMessageSender.LastSendOrRecvTime + TIMEOUT_TIME) { list.List.Add(key); } } foreach (long id in list.List) { Remove(id); } } }
public override void Update() { this.TimeNow = (uint)(TimeHelper.CurrentLocalMilliseconds() - this.StartTime); this.Recv(); foreach (var kv in this.localConnChannels) { kv.Value.Update(); } while (this.removedChannels.Count > 0) { long id = this.removedChannels.Dequeue(); KChannel channel; if (!this.localConnChannels.TryGetValue(id, out channel)) { continue; } this.localConnChannels.Remove(id); channel.Dispose(); } }
public void Update() { if (this.moveTcs == null) { return; } Unit unit = this.GetParent <Unit>(); long timeNow = TimeHelper.CurrentLocalMilliseconds(); if (timeNow - this.StartTime >= this.needTime) { unit.Position = this.Target; var tcs = this.moveTcs; this.moveTcs = null; tcs.SetResult(true); return; } float amount = (timeNow - this.StartTime) * 1f / this.needTime; unit.Position = Vector3.Lerp(this.StartPos, this.Target, amount); }
public async Task Send(Session session, int j) { try { await session.Call(new C2R_Ping()); ++k; if (k % 10000 != 0) { return; } long time2 = TimeHelper.CurrentLocalMilliseconds(); long time = time2 - time1; time1 = time2; Log.Info($"Benchmark k: {k} 每1W次耗时: {time} ms {session.Network.Count}"); } catch (Exception e) { Log.Exception(e); } }
public ActorMessageSender(Action <IActorResponse> callback) { this.CreateTime = TimeHelper.CurrentLocalMilliseconds(); this.Callback = callback; }