public void Dispose()
 {
     if (m_PingClient != null)
     {
         m_PingClient.Dispose();
         m_PingClient = null;
         m_Stats?.StopMeasuring();
         m_Stats = null;
     }
 }
        public void Start(string endpoint)
        {
            if (m_PingClient != null)
                throw new InvalidOperationException($"{nameof(Start)} cannot be called after pinging has already started");

            if (!NetworkUtils.TryParseEndpoint(endpoint, out var serverEndPoint))
                throw new ArgumentException("Could not parse endpoint");

            m_PingClient = new UdpPingClient(serverEndPoint);
            m_Stats = m_PingClient.Stats;
        }
        public MultiplayPingClient(NetworkEndPoint serverEndpoint)
        {
            if (serverEndpoint == null || !serverEndpoint.IsValid)
            {
                throw new ArgumentException($"{nameof(serverEndpoint)} must be valid.");
            }

            m_ServerEndpoint = serverEndpoint;

            // Create a NetworkDriver for the client. We could bind to a specific address but in this case we rely on the
            // implicit bind since we do not need to bing to anything special
            m_ClientDriver             = new UdpNetworkDriver(new INetworkParameter[0]);
            m_ClientToServerConnection = new NativeArray <NetworkConnection>(1, Allocator.Persistent);

            // Initialize ping stats
            Stats          = new PingStats(50);
            m_PendingPings = new NativeArray <PendingPing>(64, Allocator.Persistent);
            m_NumPings     = new NativeArray <uint>(1, Allocator.Persistent);
            m_LastPing     = new NativeArray <ushort>(1, Allocator.Persistent);
        }
        // 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;
        }