📓

NatGameJoiner

public class NatGameJoiner : MonoBehaviour
{
    public ClientConnection clientConnection;
    public bool attemptToJoin = false;
    public int attempt = 1;
    public int maxAttempts = 3;
    public float attemptTimer = 0;
    public float attemptTimerMax = 1;//seconds?
    public float timeoutTimer = 0;
    public float timeTimerMax = 3;
    public int gameID;
    public IPEndPoint masterserverIP;
    //optional
    public Text text;
    public NetworkSetup networkSetup;
//...

This Unity component is used to help initiate the NAT hole punch through process from the clients side to try join a game server listed on the Master Server.

NAT Punch Through passes the details of Client and Server to each other using the Master Server as a middle man which should have its ports forward. Once the Client and Server receive each others details they will attempt to reach each other across the network until each of their routers are ok with incoming packets from the other. This may take multiple attempts which you can set to something else, but 3 seems like a good number to run with and 1 is too few.

Methods

public static void ConnectToServer(ClientConnection clientConnection, int gameID, IPEndPoint masterserverIP)

Starts the process, first we need the clients ClientConnection as a reference, the gameID to try join on the Master Server and Master Servers IP and port info wrapped up in an IPEndPoint. This will call AttemptConnection

public static void ResetNatJoiner()

Resets the static NatGameJoiner so its able to join another Server again when needed

public void AttemptConnection(ClientConnection clientConnection, int gameID, IPEndPoint masterserverIP)

Maybe the name is misleading, but sets up all the variables to attempt this process, once its set to go, the Update function will take care of the rest.

Note: I change the MenuScreen to connecting in my UI so I can reflect this info, but you may want to alter this. Probably should have put UI changing stuff here…

void Update()

This Unity Update method runs each frame and with the right attributes setup it will run through its attempts to call clientConnection.NatPunchJoinGame until it either works or we run out of attempts to try

private void ResetJoiner()

Sets attributes in this class back to defaults so we are ready to use it again if need be