/// <summary> /// Sending a ping request every 30 seconds to all clients. /// </summary> private void PingRequestLoop() { while (IsActive) { IConnection[] connectionList = SerializableConnection.FromIConnection(_connections.ToArray()); //Send a ping request to all clients for (int i = 0; i <= _connections.Count - 1; i++) { var pingPackage = new PingPackage {Receiver = _connections[i].IPAddress}; Send(pingPackage, _connections[i].IPAddress); //add the ping request to the connection manager. _connectionManager.AddPingRequest(new UdpPingRequest(_connections[i].IPAddress, pingPackage.TimeStamp)); //Also update the client list. SendNotificationPackage(NotificationMode.ClientList, connectionList); } //Idle for 15 seconds Thread.Sleep(15000); } }
/// <summary> /// Sets the latency of a connection. /// </summary> /// <param name="pingPackage">The PingPackage.</param> private void SetLatency(PingPackage pingPackage) { DateTime timeNow = DateTime.Now; TimeSpan dif = timeNow - pingPackage.TimeStamp; UdpConnection connection = GetConnection(pingPackage.Receiver); connection.Latency = (float) dif.TotalMilliseconds; //alert the connectionmanager _connectionManager.RemoveByIP(pingPackage.Receiver); //Kick the client if the latency is to high if (!(connection.Latency > TimeOutLatency)) return; SendNotificationPackage(NotificationMode.TimeOut, new IConnection[] {SerializableConnection.FromIConnection(connection)}); _connections.Remove(connection); }
/// <summary> /// Sending a ping request every 30 seconds to all clients. /// </summary> private void PingRequestLoop() { while (IsActive) { IConnection[] connectionList = SerializableConnection.FromIConnection(_connections.ToArray()); //Send a ping request to all clients for (int i = 0; i <= _connections.Count - 1; i++) { var pingPackage = new PingPackage {Receiver = _connections[i].IPAddress}; NetworkStream networkStream = _connections[i].Client.GetStream(); PackageSerializer.Serialize(pingPackage, networkStream); //May not be neccessary networkStream.Flush(); //Also update the client list. SendNotificationPackage(NotificationMode.ClientList, connectionList); } //Idle for 15 seconds Thread.Sleep(15000); } }