Author Topic: Start.UDP needs an open Port?  (Read 4573 times)

Rexima

  • Jr. Member
  • **
  • Thank You
  • -Given: 7
  • -Receive: 2
  • Posts: 78
    • View Profile
Start.UDP needs an open Port?
« on: February 04, 2014, 03:14:21 AM »
Do i need to open a Port for Start.UDP on my dedicated Server?
Or is it running on the client?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Start.UDP needs an open Port?
« Reply #1 on: February 04, 2014, 04:03:40 AM »
If you use UDP, then yes. TNet does it automatically via UPnP, but it's not guaranteed to work everywhere.

Rexima

  • Jr. Member
  • **
  • Thank You
  • -Given: 7
  • -Receive: 2
  • Posts: 78
    • View Profile
Re: Start.UDP needs an open Port?
« Reply #2 on: February 04, 2014, 05:09:47 AM »
Ok, so, if start the TNet Server.exe with -udp 5129 and open the port 5129.

I need to write in my client Start.UDP(5129); right?

If it's so, i have some problems with my connection.

Local i have no problems, but with my remote server...
 
Remote:
Player 1 is syncing
Player 2 is not syncing, but see how Player 1 walk around.

Local:
Player 1 and Player 2 sync perfectly.

I run the TNet Server on a Linux machine, i will try it later with a Windows Server.
But i think, i made something wrong with the Ports...

//EDIT: So, if i understand it correctly, without UPnP, Network wont work?
« Last Edit: February 04, 2014, 08:17:13 AM by Rexima »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Start.UDP needs an open Port?
« Reply #3 on: February 05, 2014, 04:46:06 PM »
Without UPnP, you have to open ports manually on your router or your computer will not be reachable from the outside. You can still connect to it from the same local area network, but that's it.

For example, for Starlink I used an Amazon EC2 server instance. No UPnP support there meant I had to open up ports manually.

Rexima

  • Jr. Member
  • **
  • Thank You
  • -Given: 7
  • -Receive: 2
  • Posts: 78
    • View Profile
Re: Start.UDP needs an open Port?
« Reply #4 on: February 07, 2014, 05:34:59 PM »
Ok Thank you very much.

I have realy big problems with my synchronisation... it doesnt work, and i try it now since three days...

Menu.cs
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.IO;
  4. using System.Net;
  5. using TNet;
  6.  
  7. public class Menu : MonoBehaviour
  8. {
  9.  
  10.     public string GameLevel = "Level";
  11.  
  12.     void Start()
  13.     {
  14.         TNManager.Connect(IP, 5127); // Opened TCP port on my server/router
  15.         TNManager.StartUDP(5128); // Opened UDP port on my server/router
  16.     }

Character Controller:
  1.         void Update ()
  2.     {
  3.         if (tno.isMine)
  4.         {
  5.             m_movement = Vector3.zero;
  6.             LMove();
  7.             VMove();
  8.             Move();
  9.             tno.SendQuickly("RMove", Target.Others, m_movement * Time.deltaTime);
  10.         }
  11.  
  12.         }
  13.     [RFC]
  14.     void RMove(Vector3 move)
  15.     {
  16.         Debug.Log("Moviiing");
  17.         m_cController.Move(move);
  18.     }
  19.  

Thats it. And i dont know, why it doesnt work...

//EDIT: If i send it via TCP (tno.Send..) than it will be synchronized.  :'( Please help me
//EDIT2: Your Example3 works perfectly...
« Last Edit: February 07, 2014, 06:30:54 PM by Rexima »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Start.UDP needs an open Port?
« Reply #5 on: February 08, 2014, 01:25:56 PM »
StartUDP() needs to happen after you've connected. You can't activate it right away. Connect() call takes a while to complete.

Look inside TNAutoJoin.cs for how it should be done.

Rexima

  • Jr. Member
  • **
  • Thank You
  • -Given: 7
  • -Receive: 2
  • Posts: 78
    • View Profile
Re: Start.UDP needs an open Port?
« Reply #6 on: February 08, 2014, 02:21:49 PM »
Ok, i think i found the problem, but i dont know how to resolve it.

It tried to wait about > 5 minutes, and after some time, it starts to sync the data.
What could be the problem?

Menu:
http://pastebin.com/97XLUNAf

//EDIT: I deleted the Start.UDP after Connect.

Camera Rotation:
http://pastebin.com/kapqTPFS


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Start.UDP needs an open Port?
« Reply #7 on: February 08, 2014, 03:13:11 PM »
You are basically flooding the network with packets.

You can't send a network packet every frame. That's way too much.

Rexima

  • Jr. Member
  • **
  • Thank You
  • -Given: 7
  • -Receive: 2
  • Posts: 78
    • View Profile
Re: Start.UDP needs an open Port?
« Reply #8 on: February 08, 2014, 06:48:41 PM »
Aaaaah damn it... Sorry my failure...

It works now, thank you.

Whats better to use, TNAutoSync, or an self written sync?

Quote
/// <summary>
/// This script makes it really easy to sync some value across all connected clients.
/// Keep in mind that this script should ideally only be used for rapid prototyping.
/// It's still better to create custom to-the-point sync scripts as they will yield
/// better performance.
/// </summary>

Ok, but why it lags like hell, when i try my own sync?

Thats my synchronisation:
  1. using UnityEngine;
  2. using System.Collections;
  3. using TNet;
  4.  
  5. [RequireComponent(typeof(CharacterController))]
  6. [RequireComponent(typeof(CharacterFirstPersonMouseLook))]
  7. [RequireComponent(typeof(CharacterMotor))]
  8. public class Character : TNBehaviour
  9. {
  10.  
  11.     public int hp = 100;
  12.     public int updatesPerSecond = 3;
  13.     public bool localPlayer = false;
  14.     private GameLocal gamelocal;
  15.  
  16.     void Awake()
  17.     {
  18.         // Only start the coroutine if we wanted to run periodic updates
  19.         if (updatesPerSecond > 0 && TNManager.isInChannel && tno.isMine)
  20.         {
  21.             StartCoroutine(PeriodicSync());
  22.         }
  23.     }
  24.  
  25.         void Start ()
  26.     {
  27.         //gamelocal = GameObject.Find("GameLocal").GetComponent<GameLocal>();
  28.  
  29.         if (tno.isMine)
  30.         {
  31.             localPlayer = true;
  32.         }
  33.         else
  34.         {
  35.             //GetComponent<CharacterFirstPersonMouseLook>().enabled = false;
  36.             GetComponentInChildren<Camera>().enabled = false;
  37.             GetComponentInChildren<AudioListener>().enabled = false;
  38.             GetComponent<MeshRenderer>().enabled = true;
  39.         }
  40.         }
  41.        
  42.         void Update ()
  43.     {
  44.        
  45.         }
  46.  
  47.     IEnumerator PeriodicSync()
  48.     {
  49.         for (; ; )
  50.         {
  51.             tno.SendQuickly("SyncPosRot", Target.Others, transform.position, transform.rotation);
  52.  
  53.             if (updatesPerSecond > 0)
  54.             {
  55.                 yield return new WaitForSeconds(1f / updatesPerSecond);
  56.             }
  57.             else yield return new WaitForSeconds(0.01f);
  58.  
  59.         }
  60.     }
  61.  
  62.     [RFC]
  63.     void SyncPosRot(Vector3 _pos, Quaternion _rot)
  64.     {
  65.         transform.position = _pos;
  66.         transform.rotation = _rot;
  67.     }
  68. }
  69.  

And AutoSync with a period of 4, works more smooth. Whats my problem?



ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Start.UDP needs an open Port?
« Reply #9 on: February 09, 2014, 04:57:18 PM »
Assuming you have a game that takes user input and sets some state values (such as movement axes), keep an eye on those values (axes). If they change more than a notable amount since last frame, such as 5%, send an immediate update.

This way everyone on the network will have an up-to-date set of movement axes and can run their own simulation of what happens (moving forward, strafing, etc).

Since this is not entirely accurate, you should also periodically sync the character's position and rotation in the world (you can use TNAutoSync for this set to send infrequently -- once per second is generally enough).

This approach will guarantee that you will be sending the latest in movement vectors (which is important), and still have an up-to-date position corrected every so often. Less data sent, yet clear, jitter-free movement.

Rexima

  • Jr. Member
  • **
  • Thank You
  • -Given: 7
  • -Receive: 2
  • Posts: 78
    • View Profile
Re: Start.UDP needs an open Port?
« Reply #10 on: February 11, 2014, 02:36:35 AM »
Thanks Aren for this, it's right logic what you say.

I tried it out yesterday, and i have some laggy moments in my game. It runs a sec smooth and than it seems to lagg and than he runs smooth again and lagg again in a sec.

I dont know why. I check every second if my player move's more than 5%...

Could you give us maybe an example how to get the best sync for a character controller? It would be really really nice

//EDIT: If i use only your AutoSync, and set it up to 6 updates per second, its quiet laggy. Is this normal?

//EDIT v2: Problem resolved.

   
  1.  
  2. private float Ntimer;
  3.     public float NTime = 0.2f;
  4.  
  5. void Start ()
  6.     {
  7.  Ntimer = NTime;
  8. }
  9.         void Update ()
  10.     {
  11.         if (tno.isMine)
  12.         {
  13.             m_movement = Vector3.zero;
  14.             LMove();
  15.             VMove();
  16.             Move(m_movement);
  17.  
  18.             if (Ntimer <= 0)
  19.             {
  20.                 tno.SendQuickly("SendPosRot", Target.Others, transform.position, transform.rotation, GetComponent<CharacterTranslation>().m_movement);
  21.                 Ntimer = NTime;
  22.             }
  23.             else
  24.             {
  25.                 Ntimer -= Time.deltaTime;
  26.             }
  27.         }
  28.  
  29.         }
  30.  
  31.     public void Move(Vector3 m_move)
  32.     {
  33.         m_cController.Move(m_move * Time.deltaTime);
  34.     }
  35.  
  36.     [RFC(255)]
  37.     void SendPosRot(Vector3 p, Quaternion r, Vector3 v)
  38.     {
  39.         m_cController.Move(v);
  40.         transform.position = p;
  41.         transform.rotation = r;
  42.     }

I must set the timer to 0.05 to get smooth sync. It's quiet to much, or Aren?
« Last Edit: February 11, 2014, 05:09:26 PM by Rexima »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Start.UDP needs an open Port?
« Reply #11 on: February 11, 2014, 09:23:18 PM »
You are flat out setting the position 20 times per second there. What I was suggesting is only updating the position periodically.

Also, it's a good idea to separate your renderer from your physics whenever possible. This way even if there are discrepancies and the object "jumps" a bit, it won't be noticeable because the renderer will always be "trailing behind" the physics object.

I did that in the cube creation example.