Search Unity

UNET ping problems (30+ms on local)

Discussion in 'UNet' started by UFODriverr, May 9, 2018.

  1. UFODriverr

    UFODriverr

    Joined:
    Nov 20, 2014
    Posts:
    21
    I apologize, i couldn't find solution in posted threads.
    So my problem is:
    I have big ping even on local machine (i didn't test it yet on internet connection), 30+ to be exact.
    I implemented simple PingPong/2.
    So my code is (right now its all in one script):

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5. using TMPro;
    6.  
    7. public class MyMessageTypes
    8. {
    9.     public static short Ping = 1005;
    10.     public static short Pong = 1005;
    11. };
    12.  
    13. public class TipTap:MessageBase{
    14.    
    15. };
    16.  
    17. public class NetworkTest : MonoBehaviour {
    18.  
    19.     NetworkClient myClient;
    20.  
    21.     int port = 4545;
    22.  
    23.     float ttimer = 0;
    24.     float DCSeconds = 3;
    25.  
    26.     Coroutine CurCor = null;
    27.  
    28.     public float NetPing = 0;
    29.     public float NetPing2 = 0;
    30.  
    31.     float pingtime = 0;
    32.     bool SendPing = true;
    33.  
    34.  
    35.     public void StartServer(){
    36.         if (CurCor == null) {
    37.             CurCor = StartCoroutine (StartServerE());
    38.         }
    39.     }
    40.  
    41.     public void StartClient(TMP_InputField IPAddress){
    42.         if (CurCor == null) {
    43.             CurCor = StartCoroutine (StartClientE(IPAddress));
    44.         }
    45.     }
    46.  
    47.     void Update(){
    48.         if (myClient != null) {
    49.             if (myClient.isConnected && SendPing) {
    50.                 pingtime = Time.time;
    51.                 myClient.Send (MyMessageTypes.Ping,new TipTap());
    52.                 SendPing = false;
    53.             }
    54.         }
    55.     }
    56.  
    57.  
    58.  
    59.     IEnumerator StartServerE(){
    60.         ttimer = 0;
    61.         NetworkServer.Listen (port);
    62.         NetworkServer.RegisterHandler (MsgType.Connect,OnSConnected);
    63.         NetworkServer.RegisterHandler (MsgType.Disconnect,OnSDConnected);
    64.         NetworkServer.RegisterHandler (MyMessageTypes.Ping,OnPingS);
    65.         while (!NetworkServer.active) {
    66.             Debug.Log ("Still creating server");
    67.             ttimer += Time.deltaTime;
    68.             if (ttimer > DCSeconds) {
    69.                 Debug.Log ("Cant create server");
    70.                 yield return null;
    71.             }
    72.             yield return new WaitForEndOfFrame ();
    73.         }
    74.         Debug.Log ("Server created");
    75.         yield return null;
    76.     }
    77.  
    78.     IEnumerator StartClientE(TMP_InputField IPAddress){
    79.         ttimer = 0;
    80.         myClient = new NetworkClient();
    81.         myClient.RegisterHandler(MsgType.Connect, OnConnected);    
    82.         myClient.RegisterHandler(MyMessageTypes.Pong, OnPong);    
    83.         myClient.Connect(IPAddress.text, port);
    84.         while (!myClient.isConnected) {
    85.             Debug.Log ("Still connecting to server");
    86.             ttimer += Time.deltaTime;
    87.             if (ttimer > DCSeconds) {
    88.                 Debug.Log ("Cant connect to server");
    89.                 yield return null;
    90.             }
    91.             yield return new WaitForEndOfFrame ();
    92.         }
    93.         yield return null;
    94.     }
    95.  
    96.     //Client messages
    97.  
    98.     public void OnConnected(NetworkMessage netMsg)
    99.     {
    100.         Debug.Log("Connected to server");
    101.     }
    102.  
    103.     public void OnPong(NetworkMessage netMsg){
    104.         NetPing = Mathf.Round(((Time.time - pingtime)*1000)/2);
    105.         SendPing = true;
    106.     }
    107.     //Server messages
    108.  
    109.     public void OnSConnected(NetworkMessage netMsg)
    110.     {
    111.         Debug.Log("Some one connected to server:"+netMsg.conn.connectionId);
    112.     }
    113.  
    114.     public void OnSDConnected(NetworkMessage netMsg)
    115.     {
    116.         Debug.Log("Some one disconnected from server");
    117.     }
    118.  
    119.     public void OnPingS(NetworkMessage netMsg){
    120.         NetworkServer.SendToClient (netMsg.conn.connectionId,MyMessageTypes.Pong,new TipTap());
    121.     }
    122. }
    123.  
    Am i doing something wrong (i mean i know that this is weak implementation, but still... 30+ its kinda much)?
    Maybe i missed some configuration where i can set min latency or something similar?

    I hope you will understand what i wrote there =)
    Apologize for bad English.
     
  2. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    Packets are not send straight away. There is a send delay in the transport in order to compact messages down by using less header space. You can remove this delay with the ConnectionConfig.
     
  3. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    There is Network.sendRate

    My first step would also be to check if it's specific to Unity. How fast does the terminal command "ping localhost" return? Also, is there anything in the way of your local communication? (A firewall, virus scanner...)

    Also, messages are queued. What happens if you directly send a socket communication using winsock?
     
  4. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    Network.sendRate is not a part of UNET. It's part of RakNet. It's called SendDelay and it's in the ConnectionConfig.
    https://docs.unity3d.com/ScriptReference/Networking.ConnectionConfig.SendDelay.html
     
    newjerseyrunner likes this.
  5. UFODriverr

    UFODriverr

    Joined:
    Nov 20, 2014
    Posts:
    21
    Thx for help but still have problems. I made

    Code (CSharp):
    1. config.SendDelay = 0;
    2. config.AckDelay = 0;
    But still have huge ping.
    I decided to make average from last 10 and got 50-60 ms.
    And its local. On same machine.

    Still cant find problem =(
    I dont have any firewalls or any else.
     
  6. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987