Skip to content
This repository was archived by the owner on Sep 15, 2024. It is now read-only.

Commit dce211c

Browse files
Check if ip is port forwarded!!
1 parent 3d65713 commit dce211c

3 files changed

Lines changed: 30 additions & 8 deletions

File tree

Scripts/Msc/UIPopupCreateLobby.cs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Godot;
22
using GodotModules.Netcode;
33
using GodotModules.Netcode.Client;
4+
using System.Threading.Tasks;
45

56
namespace GodotModules
67
{
@@ -84,6 +85,30 @@ private async void _on_Create_pressed()
8485
var name = InputTitle.Text.Trim();
8586
var desc = InputDescription.Text.Trim();
8687

88+
Hide();
89+
90+
NetworkManager.StartServer(port, ValidatedMaxPlayerCount);
91+
92+
var dummyClient = new ENetClient();
93+
dummyClient.Start(WebClient.ExternalIp, port);
94+
int attempts = 100;
95+
while (!dummyClient.IsConnected)
96+
{
97+
await Task.Delay(1);
98+
attempts--;
99+
100+
if (attempts == 0)
101+
{
102+
Logger.LogDebug($"The port '{port}' must be port forwarded first!");
103+
NetworkManager.GameServer.Stop();
104+
dummyClient.Stop();
105+
return;
106+
}
107+
}
108+
dummyClient.Stop();
109+
110+
NetworkManager.StartClient(localIp, port);
111+
87112
var info = new LobbyListing
88113
{
89114
Name = name,
@@ -100,11 +125,6 @@ private async void _on_Create_pressed()
100125
SceneGameServersScript.PostServer(info);
101126
SceneLobby.CurrentLobby = info;
102127

103-
Hide();
104-
105-
NetworkManager.StartServer(port, ValidatedMaxPlayerCount);
106-
NetworkManager.StartClient(localIp, port);
107-
108128
await SceneGameServersScript.ClientConnect(async () => {
109129
await NetworkManager.WaitForHostToConnectToServer();
110130
await NetworkManager.GameClient.Send(ClientPacketOpcode.Lobby, new CPacketLobby

Scripts/Netcode/Client/GameClient.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ private void HandlePeerLeave(DisconnectOpcode opcode)
3737
Disconnected = true;
3838
Connected = 0;
3939
NetworkManager.GodotCmds.Enqueue(new GodotCmd(GodotOpcode.Disconnect, opcode));
40-
CancelTokenSource.Cancel();
40+
if (CancelTokenSource != null)
41+
CancelTokenSource.Cancel();
4142
}
4243
}
4344
}

Scripts/Netcode/Server/ENetServer.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,9 @@ protected virtual void ServerCmds()
119119
private async Task ENetThreadWorker(ushort port, int maxClients)
120120
{
121121
Thread.CurrentThread.Name = "Server";
122-
if (SceneLobby.CurrentLobby.Public && WebClient.ConnectionAlive)
123-
WebClient.TimerPingMasterServer.Start();
122+
if (SceneLobby.CurrentLobby != null)
123+
if (SceneLobby.CurrentLobby.Public && WebClient.ConnectionAlive)
124+
WebClient.TimerPingMasterServer.Start();
124125

125126
MaxPlayers = (ushort)maxClients;
126127

0 commit comments

Comments
 (0)