// Properly dispose of Ping Client void ShutdownPingClient() { if (m_PingClient != null) { Debug.Log("Shutting down existing MultiplayPingClient"); m_PingClient.Dispose(); m_Stats?.StopMeasuring(); } m_PingClient = null; }
// Initialize a new Ping Client by processing values input by user void StartNewPingClient() { // Make sure we properly shut down the old ping client before getting a new one ShutdownPingClient(); // Start with a default IPv4 endpoint var serverEndPoint = NetworkEndPoint.LoopbackIpv4; serverEndPoint.Port = DefaultServerPortToPing; var address = "loopback"; // Fix common formatting issues m_CustomIp = m_CustomIp.Trim().Replace(" ", ""); var port = DefaultServerPortToPing; // Attempt to parse server endpoint string into a NetworkEndPoint var usedDefaultIp = true; var usedDefaultPort = true; if (!string.IsNullOrEmpty(m_CustomIp)) { var endpoint = m_CustomIp.Split(':'); switch (endpoint.Length) { case 1: // Try to parse IP, but use default port usedDefaultIp = false; break; case 2: // Try to parse IP usedDefaultIp = false; // Try to parse port usedDefaultPort = !ushort.TryParse(endpoint[1].Trim(), out port); break; default: // Any other cases automatically require both IP and Port to be defaulted break; } // Try to parse the first element into an IP address if (!usedDefaultIp && !NetworkEndPoint.TryParse(endpoint[0], port, out serverEndPoint)) { usedDefaultIp = true; } else { address = endpoint[0]; } } // If we're not allowed to fall back to defaults and we need to, error if (!m_UseDefaultsIfParsingFails && (usedDefaultIp || usedDefaultPort)) { throw new ArgumentException("Could not fully parse endpoint"); } // Success - Spin up the new MultiplayPingClient Debug.Log($"Connecting to PingServer at {address}:{port}."); m_PingClient = new MultiplayPingClient(serverEndPoint); m_Stats = m_PingClient.Stats; }