public override void Update() { this.TimeNow = (uint)TimeHelper.Now(); while (true) { if (this.timerMap.Count <= 0) { break; } var kv = this.timerMap.First(); if (kv.Key > TimeNow) { break; } List <long> timeOutId = kv.Value; foreach (long id in timeOutId) { this.updateChannels.Add(id); } this.timerMap.Remove(kv.Key); } foreach (long id in updateChannels) { KChannel kChannel; if (!this.idChannels.TryGetValue(id, out kChannel)) { continue; } if (kChannel.Id == 0) { continue; } kChannel.Update(this.TimeNow); } this.updateChannels.Clear(); while (true) { if (this.removedChannels.Count <= 0) { break; } long id = this.removedChannels.Dequeue(); this.idChannels.Remove(id); } }
/// <summary> /// Rpc请求 /// </summary> public Task <T> RpcCall <T, K>(string address, K request, int waitTime = 0) { AChannel channel = this.service.GetChannel(address); ++this.requestId; byte[] requestBuffer = MongoHelper.ToBson(request); Opcode opcode = EnumHelper.FromString <Opcode>(request.GetType().Name); byte[] opcodeBuffer = BitConverter.GetBytes((ushort)opcode); byte[] idBuffer = BitConverter.GetBytes(this.requestId); channel.SendAsync(new List <byte[]> { opcodeBuffer, idBuffer, requestBuffer }); var tcs = new TaskCompletionSource <T>(); this.requestCallback[this.requestId] = (messageBytes, status) => { switch (status) { case RpcResponseStatus.Timeout: tcs.SetException(new Exception($"rpc timeout {opcode} {MongoHelper.ToJson(request)}")); return; case RpcResponseStatus.Exception: BinaryFormatter formatter = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.All)); Exception exception; using (MemoryStream stream = new MemoryStream(messageBytes, 6, messageBytes.Length - 6)) { exception = (Exception)formatter.Deserialize(stream); } tcs.SetException(exception); return; } // RpcResponseStatus.Succee T response = MongoHelper.FromBson <T>(messageBytes, 6); tcs.SetResult(response); }; if (waitTime > 0) { this.service.Timer.Add(TimeHelper.Now() + waitTime, () => { this.RpcCallback(channel, this.requestId, null, RpcResponseStatus.Timeout); }); } return(tcs.Task); }
public ETTask WaitAsync(long time, ETCancellationToken cancellationToken) { long tillTime = TimeHelper.Now() + time; if (TimeHelper.Now() > tillTime) { return(ETTask.CompletedTask); } ETTaskCompletionSource tcs = new ETTaskCompletionSource(); OnceWaitTimer timer = EntityFactory.CreateWithParent <OnceWaitTimer, ETTaskCompletionSource>(this, tcs); this.timers[timer.Id] = timer; AddToTimeId(tillTime, timer.Id); cancellationToken.Register(() => { this.Remove(timer.Id); }); return(tcs.Task); }
public KService(IPEndPoint ipEndPoint) { this.TimeNow = (uint)TimeHelper.Now(); this.socket = new UdpClient(ipEndPoint); #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.Client.IOControl((int)SIO_UDP_CONNRESET, new[] { Convert.ToByte(false) }, null); } #endif this.StartRecv(); }
public Task WaitAsync(long time, CancellationToken cancellationToken) { TaskCompletionSource <bool> tcs = new TaskCompletionSource <bool>(); Timer timer = new Timer { Id = IdGenerater.GenerateId(), Time = TimeHelper.Now() + time, tcs = tcs }; this.timers[timer.Id] = timer; this.timeId.Add(timer.Time, timer.Id); cancellationToken.Register(() => { this.Remove(timer.Id); }); return(tcs.Task); }
public Buff(int configId, ObjectId ownerId) { this.configId = configId; this.ownerId = ownerId; if (this.Config.Duration != 0) { this.Expiration = TimeHelper.Now() + this.Config.Duration; } if (this.Expiration != 0) { // 注册Timer回调 Env env = new Env(); env[EnvKey.OwnerId] = this.OwnerId; env[EnvKey.BuffId] = this.Id; this.TimerId = World.Instance.GetComponent <TimerComponent>() .Add(this.Expiration, EventType.BuffTimeout, env); } }
// 每10s扫描一次过期的actorproxy进行回收,过期时间是1分钟 public async void Start() { ActorProxyComponent self = this.Get(); List <long> timeoutActorProxyIds = new List <long>(); while (true) { await Game.Scene.GetComponent <TimerComponent>().WaitAsync(1000); if (self.Id == 0) { return; } timeoutActorProxyIds.Clear(); long timeNow = TimeHelper.Now(); foreach (long id in self.ActorProxys.Keys) { ActorProxy actorProxy = self.Get(id); if (actorProxy == null) { continue; } if (timeNow < actorProxy.LastSendTime + 10000) { continue; } timeoutActorProxyIds.Add(id); } foreach (long id in timeoutActorProxyIds) { self.Remove(id); } } }
public KService() { this.TimeNow = (uint)TimeHelper.Now(); this.socket = new UdpClient(new IPEndPoint(IPAddress.Any, 0)); this.StartRecv(); }
public KService(string host, int port) { this.TimeNow = (uint)TimeHelper.Now(); this.socket = new UdpClient(new IPEndPoint(IPAddress.Parse(host), port)); this.StartRecv(); }
public KService(IPEndPoint ipEndPoint) { this.TimeNow = (uint)TimeHelper.Now(); this.socket = new UdpClient(ipEndPoint); this.StartRecv(); }