示例#1
0
        async UniTaskVoid Tick()
        {
            try
            {
                Thread.VolatileWrite(ref lastReceived, stopWatch.ElapsedMilliseconds);

                while (open)
                {
                    long now      = stopWatch.ElapsedMilliseconds;
                    long received = Thread.VolatileRead(ref lastReceived);
                    if (now > received + Timeout)
                    {
                        break;
                    }

                    kcp.Update((uint)now);

                    uint check = kcp.Check((uint)now);

                    if (check <= now)
                    {
                        check = (uint)(now + 10);
                    }


                    await UniTask.Delay((int)(check - now));
                }
            }
            catch (SocketException)
            {
                // this is ok, the connection was closed
            }
            catch (ObjectDisposedException)
            {
                // fine,  socket was closed,  no more ticking needed
            }
            catch (Exception ex)
            {
                Debug.LogException(ex);
            }
            finally
            {
                open = false;
                dataAvailable?.TrySetResult();
                Close();
            }
        }