Exemple #1
0
        public static async ETTask MoveToAsync(this MoveComponent self, Vector3 target, float speedValue, ETCancellationToken cancellationToken)
        {
            Unit unit = self.GetParent <Unit>();

            if ((target - self.Target).magnitude < 0.1f)
            {
                await ETTask.CompletedTask;
            }

            self.Target = target;

            self.StartPos  = unit.Position;
            self.StartTime = TimeHelper.ClientNow();
            float distance = (self.Target - self.StartPos).magnitude;

            if (Math.Abs(distance) < 0.1f)
            {
                await ETTask.CompletedTask;
            }

            self.needTime = (long)(distance / speedValue * 1000);

            self.moveTcs = new ETTaskCompletionSource();

            cancellationToken.Add(() => { self.moveTcs = null; });
            await self.moveTcs.Task;
        }
        public override void Update(DMoveComponent self)
        {
            if (self.Callback == null)
            {
                return;
            }

            DUnit unit    = self.GetParent <DUnit>();
            long  timeNow = TimeHelper.ClientNow();

            if (timeNow - self.StartTime >= self.needTime)
            {
                unit.Position = self.Target;

                Action <bool> callback = self.Callback;
                self.Callback = null;
                callback.Invoke(true);

                return;
            }

            float amount = (timeNow - self.StartTime) * 1f / self.needTime;

            unit.Position = Vector3.Lerp(self.StartPos, self.Target, amount);
        }
Exemple #3
0
        public static void Check(this SessionIdleCheckerComponent self)
        {
            Session session = self.GetParent <Session>();
            long    timeNow = TimeHelper.ClientNow();

#if !SERVER
            if (timeNow - session.LastRecvTime > 6 * 1000)
            {
                if (session.GetComponent <SwitchRouterComponent>() == null)
                {
                    session.AddComponent <SwitchRouterComponent>();
                }
            }
            return;
#else
            if (timeNow - session.LastRecvTime < 30 * 1000 && timeNow - session.LastSendTime < 30 * 1000)
            {
                return;
            }

            Log.Info(
                $"session timeout: {session.Id} {timeNow} {session.LastRecvTime} {session.LastSendTime} {timeNow - session.LastRecvTime} {timeNow - session.LastSendTime}");
            session.Error = ErrorCore.ERR_SessionSendOrRecvTimeout;

            session.Dispose();
#endif
        }
        public override void Awake(HPRegainComponent self, int hpregain)
        {
            self.HpRegain = hpregain;

            self.BeStart           = true;
            self.StartMilliseconds = TimeHelper.ClientNow();
        }
Exemple #5
0
        public override void Update()
        {
            this.TimeNow = (uint)(TimeHelper.ClientNow() - 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();
            }
        }
Exemple #6
0
        public static async ETTask StartChange(this UIMaskView self, float interval, bool isStart, string imagePath = null)
        {
            if (GalGameEngineComponent.Instance.State == GalGameEngineComponent.GalGameEngineState.FastForward)
            {
                self.CloseSelf().Coroutine();
                return;
            }
            var hasSprite = !string.IsNullOrEmpty(imagePath);

            if (!hasSprite)
            {
                self.bg.SetEnabled(true);
                self.bg2.SetEnabled(false);
                self.bg.SetImageColor(Color.black);
                long tillTime = TimeHelper.ClientNow() + (int)(interval * 1000);
                while (TimeHelper.ClientNow() < tillTime)
                {
                    float flag = (tillTime - TimeHelper.ClientNow()) / 1000f;
                    if (isStart)
                    {
                        self.bg.SetImageAlpha(flag);
                    }
                    else
                    {
                        self.bg.SetImageAlpha(1 - flag);
                    }

                    await TimerComponent.Instance.WaitAsync(1);
                }
            }
            else
            {
                self.bg.SetEnabled(true);
                await self.bg2.SetSpritePath(imagePath);

                self.bg.SetEnabled(false);
                long tillTime = TimeHelper.ClientNow() + (int)(interval * 1000);
                var  rect     = self.bg2.GetTransform() as RectTransform;
                while (TimeHelper.ClientNow() < tillTime)
                {
                    float flag = (tillTime - TimeHelper.ClientNow()) / 1000f;
                    if (isStart)
                    {
                        rect.sizeDelta = self.MaxSize * 5 * Mathf.Pow((1 - flag), 2);
                    }
                    else
                    {
                        rect.sizeDelta = self.MaxSize * 5 * Mathf.Pow(flag, 2);
                    }

                    await TimerComponent.Instance.WaitAsync(1);
                }
            }

            if (isStart)
            {
                self.CloseSelf().Coroutine();
            }
        }
Exemple #7
0
        private static void StartMove(this MoveComponent self)
        {
            self.BeginTime = TimeHelper.ClientNow();
            self.StartTime = self.BeginTime;
            self.SetNextTarget();

            self.MoveTimer = TimerComponent.Instance.NewFrameTimer(CallbackType.MoveTimer, self);
        }
Exemple #8
0
        private static void StartMove(this MoveComponent self)
        {
            Unit unit = self.GetParent <Unit>();

            self.BeginTime = TimeHelper.ClientNow();
            self.StartTime = self.BeginTime;
            self.SetNextTarget();

            self.MoveTimer = TimerComponent.Instance.NewFrameTimer(TimerType.MoveTimer, self);
        }
Exemple #9
0
        public override int Check(AIComponent aiComponent, AIConfig aiConfig)
        {
            long sec = TimeHelper.ClientNow() / 1000 % 15;

            if (sec >= 10)
            {
                return(0);
            }
            return(1);
        }
Exemple #10
0
        public static async ETTask <bool> MoveToAsync(this DMoveComponent self, Vector3 target, float speedValue, ETCancellationToken cancellationToken = null)
        {
            await ETTask.CompletedTask;

            DUnit unit = self.GetParent <DUnit>();

            if ((target - self.Target).magnitude < 0.1f)
            {
                return(true);
            }

            self.Target = target;


            self.StartPos  = unit.Position;
            self.StartTime = TimeHelper.ClientNow();
            float distance = (self.Target - self.StartPos).magnitude;

            if (Math.Abs(distance) < 0.1f)
            {
                return(true);
            }

            self.needTime = (long)(distance / speedValue * 1000);

            ETTask <bool> moveTcs = ETTask <bool> .Create();

            self.Callback = (ret) => { moveTcs.SetResult(ret); };

            void CancelAction()
            {
                if (self.Callback != null)
                {
                    Action <bool> callback = self.Callback;
                    self.Callback = null;
                    callback.Invoke(false);
                }
            }

            bool moveRet;

            try
            {
                cancellationToken?.Add(CancelAction);
                moveRet = await moveTcs;
            }
            finally
            {
                cancellationToken?.Remove(CancelAction);
            }

            return(moveRet);
        }
Exemple #11
0
        public void Awake(AService aService)
        {
            this.AService = aService;
            long timeNow = TimeHelper.ClientNow();

            this.LastRecvTime = timeNow;
            this.LastSendTime = timeNow;

            this.requestCallbacks.Clear();

            Log.Info($"session create: zone: {this.DomainZone()} id: {this.Id} {timeNow} ");
        }
Exemple #12
0
            protected override void Awake(Session self, AService aService)
            {
                self.AService = aService;
                long timeNow = TimeHelper.ClientNow();

                self.LastRecvTime = timeNow;
                self.LastSendTime = timeNow;

                self.requestCallbacks.Clear();

                Log.Info($"session create: zone: {self.DomainZone()} id: {self.Id} {timeNow} ");
            }
Exemple #13
0
        public static void OnRead(this NetInnerComponent self, long channelId, MemoryStream memoryStream)
        {
            Session session = self.GetChild <Session>(channelId);

            if (session == null)
            {
                return;
            }

            session.LastRecvTime = TimeHelper.ClientNow();
            SessionStreamDispatcher.Instance.Dispatch(self.SessionStreamDispatcherType, session, memoryStream);
        }
Exemple #14
0
        public static void OnRead(this NetKcpComponent self, long channelId, MemoryStream memoryStream)
        {
            Session session = self.GetChild <Session>(channelId);

            if (session == null)
            {
                return;
            }

            session.LastRecvTime = TimeHelper.ClientNow();
            self.MessageDispatcher.Dispatch(session, memoryStream);
        }
Exemple #15
0
        // 开启协程移动,每100毫秒移动一次,并且协程取消的时候会计算玩家真实移动
        // 比方说玩家移动了2500毫秒,玩家有新的目标,这时旧的移动协程结束,将计算250毫秒移动的位置,而不是300毫秒移动的位置
        public async ETTask StartMove(ETCancellationToken cancellationToken)
        {
            Unit unit = this.GetParent <Unit>();

            this.StartPos  = unit.Position;
            this.StartTime = TimeHelper.ClientNow();
            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?.Add(() =>
            {
                long timeNow = TimeHelper.ClientNow();
                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)
            {
                //新版TimerComponent实现不同于5.0的TimerComponent,需要自己判断是取消还是自然结束,并且return,否则将不会取消任务,并可能会造成cancellationToken泄漏
                if (!await timerComponent.WaitAsync(50, cancellationToken))
                {
                    return;
                }

                long timeNow = TimeHelper.ClientNow();

                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);
            }
        }
Exemple #16
0
        public static void OnRead(this NetKcpComponent self, long channelId, MemoryStream memoryStream)
        {
            Session session = self.GetChild <Session>(channelId);

            if (session == null)
            {
                return;
            }

            session.LastRecvTime = TimeHelper.ClientNow();

            Game.EventSystem.Callback(self.SessionStreamDispatcherType, session, memoryStream);
        }
        public static void Check(this SessionIdleCheckerComponent self)
        {
            Session session = self.GetParent <Session>();
            long    timeNow = TimeHelper.ClientNow();

            if (timeNow - session.LastRecvTime < 30 * 1000 && timeNow - session.LastSendTime < 30 * 1000)
            {
                return;
            }

            Log.Info($"session timeout: {session.Id} {timeNow} {session.LastRecvTime} {session.LastSendTime} {timeNow - session.LastRecvTime} {timeNow - session.LastSendTime}");
            session.Error = ErrorCode.ERR_SessionSendOrRecvTimeout;

            session.Dispose();
        }
Exemple #18
0
        public KService(ThreadSynchronizationContext threadSynchronizationContext, AddressFamily addressFamily, ServiceType serviceType)
        {
            this.ServiceType = serviceType;
            this.ThreadSynchronizationContext = threadSynchronizationContext;
            this.startTime = TimeHelper.ClientNow();
            this.socket    = new Socket(addressFamily, SocketType.Dgram, ProtocolType.Udp);

            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);
            }
        }
Exemple #19
0
        public KService(ThreadSynchronizationContext threadSynchronizationContext, ServiceType serviceType)
        {
            this.ServiceType = serviceType;
            this.ThreadSynchronizationContext = threadSynchronizationContext;
            this.StartTime = TimeHelper.ClientNow();
            this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            // 作为客户端不需要修改发送跟接收缓冲区大小
            this.socket.Bind(new IPEndPoint(IPAddress.Any, 0));

            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);
            }
        }
Exemple #20
0
 public KService(ThreadSynchronizationContext threadSynchronizationContext, IPEndPoint ipEndPoint, ServiceType serviceType)
 {
     this.ServiceType = serviceType;
     this.ThreadSynchronizationContext = threadSynchronizationContext;
     this.StartTime = TimeHelper.ClientNow();
     this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
     this.socket.SendBufferSize = Kcp.OneM * 64;
     this.socket.ReceiveBufferSize = Kcp.OneM * 64;
     this.socket.Bind(ipEndPoint);
     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);
     }
 }
Exemple #21
0
        public KService()
        {
            this.StartTime = TimeHelper.ClientNow();
            this.TimeNow   = (uint)(TimeHelper.ClientNow() - 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;
        }
Exemple #22
0
        private static async ETVoid SessionPingAsync(PingComponent self)
        {
            long    time1      = TimeHelper.ClientNow();
            Session session    = self.GetParent <Session>();
            long    instanceId = self.InstanceId;

            G2C_Ping response = await session.Call(self.C2G_Ping) as G2C_Ping;

            if (self.InstanceId != instanceId)
            {
                return;
            }

            long time2 = TimeHelper.ClientNow();

            self.Ping = time2 - time1;

            Game.TimeInfo.ServerMinusClientTime = response.Time + (time2 - time1) / 2 - time2;
        }
Exemple #23
0
        public async ETTask ChangeRouter(SwitchRouterComponent self)
        {
            Session session = self.GetParent <Session>();

            session.RemoveComponent <SessionIdleCheckerComponent>();
            var    gateid          = session.GetComponent <RouterDataComponent>().Gateid;
            var    routercomponent = session.AddComponent <GetRouterComponent, long, long>(gateid, session.Id);
            string routerAddress   = await routercomponent.Tcs;

            session.RemoveComponent <GetRouterComponent>();
            if (routerAddress == "")
            {
                session.Dispose();
                return;
            }
            (session.AService as KService).ChangeAddress(session.Id, NetworkHelper.ToIPEndPoint(routerAddress));
            session.LastRecvTime = TimeHelper.ClientNow();
            session.AddComponent <SessionIdleCheckerComponent, int>(NetThreadComponent.checkInteral);
            session.RemoveComponent <SwitchRouterComponent>();
        }
        public static void Awake(this RouterServiceComponent self, IPEndPoint ipEndPoint)
        {
            self.StartTime      = TimeHelper.ClientNow();
            self.CurrTimeSecond = TimeHelper.ClientNowSeconds();
            self.socket         = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            if (!RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                self.socket.SendBufferSize    = Kcp.OneM * 64;
                self.socket.ReceiveBufferSize = Kcp.OneM * 64;
            }

            self.socket.Bind(ipEndPoint);
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                const uint IOC_IN            = 0x80000000;
                const uint IOC_VENDOR        = 0x18000000;
                uint       SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12;
                self.socket.IOControl((int)SIO_UDP_CONNRESET, new[] { Convert.ToByte(false) }, null);
            }
        }
Exemple #25
0
        private static async ETTask PingAsync(PingComponent self)
        {
            Session session    = self.GetParent <Session>();
            long    instanceId = self.InstanceId;

            while (true)
            {
                if (self.InstanceId != instanceId)
                {
                    return;
                }

                long time1 = TimeHelper.ClientNow();
                try
                {
                    G2C_Ping response = await session.Call(self.C2G_Ping) as G2C_Ping;

                    if (self.InstanceId != instanceId)
                    {
                        return;
                    }

                    long time2 = TimeHelper.ClientNow();
                    self.Ping = time2 - time1;

                    Game.TimeInfo.ServerMinusClientTime = response.Time + (time2 - time1) / 2 - time2;

                    await TimerComponent.Instance.WaitAsync(2000);
                }
                catch (RpcException e)
                {
                    // session断开导致ping rpc报错,记录一下即可,不需要打成error
                    Log.Info($"ping error: {self.Id} {e.Error}");
                    return;
                }
                catch (Exception e)
                {
                    Log.Error($"ping error: \n{e}");
                }
            }
        }
Exemple #26
0
        private static void StartMove(this MoveComponent self)
        {
            Unit unit = self.GetParent <Unit>();

            self.BeginTime = TimeHelper.ClientNow();
            self.StartTime = self.BeginTime;
            self.SetNextTarget();

            self.MoveTimer = TimerComponent.Instance.NewFrameTimer(() =>
            {
                try
                {
                    self.MoveForward(false);
                }
                catch (Exception e)
                {
                    Log.Error($"move timer error: {unit.Id}\n{e}");
                }
            }
                                                                   );
        }
Exemple #27
0
        public override void Update(MoveComponent self)
        {
            if (self.moveTcs == null)
            {
                return;
            }

            Unit unit    = self.GetParent <Unit>();
            long timeNow = TimeHelper.ClientNow();

            if (timeNow - self.StartTime >= self.needTime)
            {
                unit.Position = self.Target;
                ETTaskCompletionSource tcs = self.moveTcs;
                self.moveTcs = null;
                tcs.SetResult();
                return;
            }

            float amount = (timeNow - self.StartTime) * 1f / self.needTime;

            unit.Position = Vector3.Lerp(self.StartPos, self.Target, amount);
        }
Exemple #28
0
        public override void Update()
        {
            this.TimeNow = (uint)(TimeHelper.ClientNow() - 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 static async ETTask Send(this WebSocketBenchmarkComponent self, Session session, int j)
        {
            try
            {
                await session.Call(new C2R_Ping());

                ++self.k;

                if (self.k % 10000 != 0)
                {
                    return;
                }

                long time2 = TimeHelper.ClientNow();
                long time  = time2 - self.time1;
                self.time1 = time2;
                Log.Info($"Benchmark k: {self.k} 每1W次耗时: {time} ms {session.Network.Count}");
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }
        public override void Update(HPRegainComponent self)
        {
            if (self.BeStart)
            {
                // 按秒恢复
                long disTime = TimeHelper.ClientNow() - self.StartMilliseconds;
                if (disTime > 1000)
                {
                    DUnit            tower = self.GetParent <DUnit>();
                    NumericComponent nm    = tower.GetComponent <NumericComponent>();
                    int hpMax = nm.GetAsInt(NumericType.MaxHp);
                    int hp    = nm.GetAsInt(NumericType.Hp);
                    // 到最大值,不再恢复
                    if (hp >= hpMax)
                    {
                        return;
                    }

                    // 确认恢复值
                    int hpRegain = ((int)disTime / 1000) * self.HpRegain;
                    if (hp + hpRegain > hpMax)
                    {
                        hpRegain = hpMax - hp;
                    }

                    // 设置增量值
                    int hpAdd = nm.GetAsInt(NumericType.HpAdd) + hpRegain;
                    NumericAction.SetUnitNumericAction(tower, NumericType.HpAdd, hpAdd);

                    // 矫正剩余时间
                    int extTime = (int)disTime % 1000;
                    self.StartMilliseconds = TimeHelper.ClientNow() - extTime;
                }
                return;
            }
        }