public void TestFlush() { var checker = new BandwidthChecker(); var now = 10000; checker.AddSent(1, now - 2); checker.AddSent(2, now); checker.AddSent(4, now + 2); Assert.Equal(2, checker.GetSentPacketsPerSecond(now)); checker.Flush(now - 1); Assert.Equal(1, checker.GetSentPacketsPerSecond(now)); checker.Flush(now + 999); Assert.Equal(0, checker.GetSentPacketsPerSecond(now)); }
void Start() { InitializePacketObservables(); var bandwidthInterval = TimeSpan.FromMilliseconds(100); Observable.Interval(bandwidthInterval).Subscribe(_ => { var ts = TimeUtils.NowTimestamp - 1000; bandwidth.Flush(ts); _receivedBytes.SetValueAndForceNotify(bandwidth.GetReceivedBytesPerSeconds(ts)); _sentBytes.SetValueAndForceNotify(bandwidth.GetSentBytesPerSecond(ts)); }).AddTo(this); var heartbeatInterval = TimeSpan.FromSeconds(Config.HeartbeatInterval); Observable.Interval(heartbeatInterval).SkipUntil(ReadyObservable).Subscribe(_ => { SendImmediate(new HeartbeatPacket()); }).AddTo(this); var flushInterval = TimeSpan.FromMilliseconds(300); Observable.Interval(flushInterval).SkipUntil(ReadyObservable).Subscribe(_ => Flush()).AddTo(this); ErrorRaised.Subscribe(msg => { Debug.LogError($"Error: " + ws.error); if (loop != null) { loop.Dispose(); loop = null; } // dont destory on load로 등록된 네트워크 관련 객체 삭제 // 네트워크 접속 실패시 처음부터 재시작하는게 목적 // TOOD 최초 접속 에러와 게임 도중 접속 에러를 분리하기 Destroy(PingChecker.Instance.gameObject); Destroy(ConnectionManager.Instance.gameObject); SceneManager.LoadScene(initialScene, LoadSceneMode.Single); }).AddTo(this); loop = Observable.FromCoroutine(BeginLoop).Subscribe(); }