public void Update(TimeSpan delta)
        {
            if (LatencyMonitor.HasFlag(LatencyMonitorMethod.Ping) && !_awaitingPingResponse && !_awaitingEchoResponse &&
                _now >= _pingSent + PingEveryMillis * TimeSpan.TicksPerMillisecond)
            {
                _awaitingPingResponse = true;
                _pingSent             = _now;
                SendMessage(new[] { PingDenoter }, _server.Connections);
            }

            if (_awaitingEchoResponse && _now >= _toEchoSent + 5 * TimeSpan.TicksPerSecond ||
                _awaitingPingResponse && _now >= _pingSent + 5 * TimeSpan.TicksPerSecond)
            {
                Latency = NO_RESPONSE;
                NoResponseCallback();
            }

            for (var i = 0; i < _lastConnections.Count(); i++)
            {
                if (!Connections.Any((c) => c == _lastConnections[i]))
                {
                    OnDisconnect(_lastConnections[i]);
                }
            }
            for (var i = 0; i < Connections.Count(); i++)
            {
                if (!_lastConnections.Any((c) => c == Connections[i]))
                {
                    OnConnect(Connections[i]);
                }
            }
        }
 public void TestStart()
 {
     using (var monitor = new LatencyMonitor(_latency.Object, new LatencySettings()))
     {
         monitor.IsStarted.Should().BeFalse();
         monitor.Start();
         monitor.IsStarted.Should().BeTrue();
     }
 }
        public void TestMeasureOnce()
        {
            using (var monitor = new LatencyMonitor(_latency.Object, new LatencySettings()))
            {
                _latency.Verify(x => x.Roundtrip(), Times.Never, "because Roundtrip() shouldn't have been called just yet");

                monitor.MeasureLatency();
                _latency.Verify(x => x.Roundtrip(), Times.Once, "because Roundtrip() should've been invoked exactly once during MeasureLatency()");
            }
        }
Example #4
0
 public void Send(byte[] bytes)
 {
     if (LatencyMonitor.HasFlag(LatencyMonitorMethod.Echo))
     {
         _awaitingEchoResponse = true;
         _toEchoSent           = DateTimeOffset.UtcNow.Ticks;
     }
     SendMessage((LatencyMonitor.HasFlag(LatencyMonitorMethod.Echo) ? new[] { ToEchoDenoter } : new[] { NormalDenoter })
                 .Concat(bytes).ToArray());
 }
Example #5
0
        protected override ServerPeerBase CreateServerPeer(InitResponse initResponse, object state)
        {
            if (initResponse.ApplicationId == "LatencyMonitor")
            {
                // latency monitor
                LatencyMonitor peer = this.WorkloadController.OnLatencyMonitorPeerConnected(initResponse);
                return(peer);
            }

            // master
            Thread.VolatileWrite(ref this.isReconnecting, 0);
            return(this.MasterPeer = this.CreateMasterPeer(initResponse));
        }
        public void TestDispose()
        {
            LatencyMonitor monitor;

            using (monitor = new LatencyMonitor(_latency.Object, new LatencySettings()))
            {
                monitor.IsDisposed.Should().BeFalse();

                monitor.Start();
                monitor.IsStarted.Should().BeTrue();
            }
            monitor.IsStarted.Should().BeFalse();
            monitor.IsDisposed.Should().BeTrue();
        }
        public void Send(byte[] bytes)
        {
            List <NetConnection> all = _server.Connections;

            if (LatencyMonitor.HasFlag(LatencyMonitorMethod.Echo))
            {
                var randomConnection = all.Random();
                all.RemoveAll((c) => c.RemoteUniqueIdentifier == randomConnection.RemoteUniqueIdentifier);
                _awaitingEchoResponse = true;
                _toEchoSent           = _now;
                SendMessage(new[] { ToEchoDenoter }.Concat(bytes), new List <NetConnection>()
                {
                    randomConnection
                });
            }
            SendMessage(new[] { NormalDenoter }.Concat(bytes), all);
        }
Example #8
0
        public void Update(TimeSpan delta)
        {
            if (LatencyMonitor.HasFlag(LatencyMonitorMethod.Ping) && !_awaitingPingResponse && !_awaitingEchoResponse &&
                _now >= _pingSent + PingEveryMillis * TimeSpan.TicksPerMillisecond)
            {
                _awaitingPingResponse = true;
                _pingSent             = _now;
                SendMessage(new[] { PingDenoter });
            }

            if (_awaitingEchoResponse && _now >= _toEchoSent + 5 * TimeSpan.TicksPerSecond ||
                _awaitingPingResponse && _now >= _pingSent + 5 * TimeSpan.TicksPerSecond)
            {
                Latency = NO_RESPONSE;
                NoResponseCallback();
            }

            if (!_hasFinishedTryingToConnect && ConnectionSuccessful.HasValue)
            {
                (ConnectionSuccessful.Value ? OnConnectionSuccess : OnConnectionFail)();
            }
        }
Example #9
0
 public MonitorService(LatencyMonitorConfig configuration)
 {
     latencyMonitor = new LatencyMonitor(configuration);
 }