// Update is called once per frame
    void Update()
    {
        while (sSocket.Available > 0)
        {
            byte[]   buffer   = new byte[1500];
            EndPoint endpoint = NoneEndpoint;

            sSocket.ReceiveFrom(buffer, ref endpoint);

            int stringLen = 0;
            for (int i = 0; i < buffer.Length; ++i)
            {
                if (buffer[i] == 0)
                {
                    stringLen = i;
                    break;
                }
            }

            string message = Encoding.UTF8.GetString(buffer, 0, stringLen);

            if (LastKnownEndpoint == null)
            {
                string[] splits = message.Split(':');
                LastKnownEndpoint = new IPEndPoint(IPAddress.Parse(splits[0]), EndPointChooser.GamePort);

                sSocket.Shutdown(SocketShutdown.Both);
                sSocket.Close();

                sSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                sSocket.Bind(new IPEndPoint(IPAddress.Any, EndPointChooser.GamePort));

                Debug.Log($"NAT PUNCH DATA: Endpoint: {endpoint}, message: {message}");

                return;
            }
            else if (((IPEndPoint)LastKnownEndpoint).Port != ((IPEndPoint)endpoint).Port)
            {
                ((IPEndPoint)LastKnownEndpoint).Port = ((IPEndPoint)endpoint).Port;
            }


            NetStatTracker.TrackMessageReceived((ulong)stringLen);

            Debug.Log($"Received Data from {endpoint}");

            string[] coords = message.Split('|');

            float x = float.Parse(coords[0]);
            float y = float.Parse(coords[1]);

            transform.position = new Vector3(x, y, transform.position.z);
        }
    }
    private void TrySendPositionData()
    {
        if (DateTime.Now >= mLastDataSentTimeStamp + TickRate && LastKnownEndpoint != null)
        {
            string message = $"{transform.position.x}|{transform.position.y}";

            Debug.Log($"Sending to Endpoint: {LastKnownEndpoint}");

            byte[] bytes = Encoding.UTF8.GetBytes(message);
            sSocket.SendTo(bytes, EndPointChooser.LastKnownEndpoint);

            NetStatTracker.TrackMessageSent((ulong)(bytes.Length * sizeof(byte)));

            if (DateTime.Now - mLastDataSentTimeStamp > TimeSpan.FromMilliseconds(200))
            {
                mLastDataSentTimeStamp = DateTime.Now;
            }
            else
            {
                mLastDataSentTimeStamp += TickRate;
            }
        }
    }
Exemplo n.º 3
0
    void OnGUI()
    {
        GUI.skin = StatsSkin;

        if (mShowGUI)
        {
            var(messagesSentPerSecond, bytesSentPerSecond, messagesReceivedPerSecond, bytesReceivedPerSecond) = NetStatTracker.GetNetStats();

            GUI.Box(new Rect(2, 2, 400, 300), "");
            GUI.Label(new Rect(10, 30, 400, 30), $"Sent (Messages): {messagesSentPerSecond}");
            GUI.Label(new Rect(10, 60, 400, 30), $"Sent (Bps): {bytesSentPerSecond}");
            GUI.Label(new Rect(10, 120, 400, 30), $"Received (Messages): {messagesReceivedPerSecond}");
            GUI.Label(new Rect(10, 150, 400, 30), $"Received (Bps): {bytesReceivedPerSecond}");
            if (GUI.Button(new Rect(120, 250, 150, 40), "Hide Stats"))
            {
                mShowGUI = false;
            }
        }
        else
        {
            if (GUI.Button(new Rect(120, 10, 150, 40), "Show Stats"))
            {
                mShowGUI = true;
            }
        }
    }