Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ListenForPunches issue VS ListenForSinglePunch working #5

Open
Kreshi opened this issue Nov 28, 2020 · 6 comments
Open

ListenForPunches issue VS ListenForSinglePunch working #5

Kreshi opened this issue Nov 28, 2020 · 6 comments

Comments

@Kreshi
Copy link

Kreshi commented Nov 28, 2020

Hello,

here the YouTube video describing the issue: https://youtu.be/aHU8hiAHERY
Tested: locally with public IP - MLAPI.Puncher.Server plus the two game instances were all running on the same device simultaneously.
Used code snippets:

`public void ui_setupHostSingle()
{
closeConnection();

    m_listenTask = Task.Factory.StartNew(() =>
    {
        try
        {
            string puncherServerHost = puncherIPAddressInputField.text;
            using (m_listenPeer = new PuncherClient(puncherServerHost, PUNCHER_SERVER_PORT))
            {
                Debug.Log("[LISTENER] Listening for single punch on our port 1234...");
                IPEndPoint endpoint = m_listenPeer.ListenForSinglePunch(new IPEndPoint(IPAddress.Any, GAME_SERVER_PORT));
                Debug.Log("[LISTENER] Connector: " + endpoint + " punched through our NAT");
            }
        }
        catch (Exception e)
        {
            Debug.LogError(e);
        }
    });

    NetworkingManager.Singleton.GetComponent<EnetTransport.EnetTransport>().Port = GAME_SERVER_PORT;
    NetworkingManager.Singleton.ConnectionApprovalCallback += approvalCheck;
    NetworkingManager.Singleton.StartHost();
}

public void ui_setupHostLoop()
{
    closeConnection();

    m_listenTask = Task.Factory.StartNew(() =>
    {
        try
        {
            string puncherServerHost = puncherIPAddressInputField.text;
            using (m_listenPeer = new PuncherClient(puncherServerHost, PUNCHER_SERVER_PORT))
            {
                Debug.Log("[LISTENER] Listening for single punch on our port 1234...");
                m_listenPeer.OnConnectorPunchSuccessful += (endpoint) =>
                {
                    Debug.Log("[LISTENER] Connector: " + endpoint + " punched through our NAT");
                };
                m_listenPeer.ListenForPunches(new IPEndPoint(IPAddress.Any, GAME_SERVER_PORT));
            }
        }
        catch (Exception e)
        {
            Debug.LogError(e);
        }
    });

    NetworkingManager.Singleton.GetComponent<EnetTransport.EnetTransport>().Port = GAME_SERVER_PORT;
    NetworkingManager.Singleton.ConnectionApprovalCallback += approvalCheck;
    NetworkingManager.Singleton.StartHost();
}

public void ui_setupClient()
{
closeConnection();

    string address = connectToIPAddressInputField.text;

    string puncherServerHost = puncherIPAddressInputField.text;
    using (PuncherClient connectPeer = new PuncherClient(puncherServerHost, PUNCHER_SERVER_PORT))
    {
        Debug.Log("[CONNECTOR] Punching...");

        if (connectPeer.TryPunch(IPAddress.Parse(address), out IPEndPoint connectResult))
        {
            Debug.Log("[CONNECTOR] Punched through to peer: " + connectResult);

            NetworkingManager.Singleton.GetComponent<EnetTransport.EnetTransport>().Port = (ushort)connectResult.Port;
            NetworkingManager.Singleton.GetComponent<EnetTransport.EnetTransport>().Address = connectResult.Address.ToString();

            NetworkingManager.Singleton.NetworkConfig.ConnectionData = System.Text.Encoding.ASCII.GetBytes("room password");
            NetworkingManager.Singleton.StartClient();
        }
        else
        {
            Debug.LogError("[CONNECTOR] Failed to punch");
        }
    }

}`

@TwoTenPvP
Copy link
Member

Internally, the code is very similar. The only difference is whether this or this line executes. Are you able to run a debugger or add some print statements to those lines to see what is being ran?

@Kreshi
Copy link
Author

Kreshi commented Nov 29, 2020

The lines are identical yes. I debugged the code for a while. In the video there are debugging messages which state that the punch-through actually was successfull on the host as well as on the client (check this out: https://drive.google.com/file/d/1v1u0PjrMvHSTrymUDXW2WmNpMKE05ayE/view?usp=sharing) --> something else must be blocking the client from connecting / host from firing an onconnected event.

some more information on this issue:

  • the same bug also happens when i try to connect a game-build with the unity editor EVEN when i use the "ListenForSinglePunch" method.
  • This only happens when the unity editor is running as host and the game-build is running as client.
  • The does not happen the other way around (game-build as host, unity editor as client).
  • And from game-build to game-build it only happens when the "ListenForPunches" method is used like stated above.

To summerize: punch-through gives success-callback on both, host and client, however no connection is established afterwards.

@NoserverStudios
Copy link

NoserverStudios commented Dec 8, 2020

Hey @TwoTenPvP and @Kreshi,

we tried to implement your Puncher-Module into our game but like @Kreshi mentioned the Method "ListenForPunches" not work at all and the function "ListenForSinglePunch" only works for first user joined game.
Where could we download an working NAT punch example project if exists?

Thanks
NoserverStudios

@Kreshi
Copy link
Author

Kreshi commented Dec 17, 2020

Hey @NoserverStudios

As a work-around you could call the ListenForSinglePunch behaviour again whenever a new player connects to the server (haven't tested it though).

Best Regards,
Kreshi

@NoserverStudios
Copy link

Hey @NoserverStudios

As a work-around you could call the ListenForSinglePunch behaviour again whenever a new player connects to the server (haven't tested it though).

Best Regards,
Kreshi

Hey @Kreshi

thanks for your work-around and reply!
We tried it already (Maybe wrong?) without success:

while (true)
{
    using (PuncherClient listener = new PuncherClient("someserver.example.com", 6776))
    {
        listener.ListenForSinglePunch(new IPEndPoint(IPAddress.Any, targetGameServerPort);
    }

    Thread.Sleep(5000);
}

Best regards and merry christmas,
NoserverStudios

@NoserverStudios
Copy link

NoserverStudios commented Jan 12, 2021

Hey there,

is there any "working" example of this NAT Puncher in multi player environment?

Thanks and best regards,
NoserverStudios

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants