From f3195b2fe7d9b4a20f83a142cec21f2dc26b3a23 Mon Sep 17 00:00:00 2001 From: RandomityGuy <31925790+RandomityGuy@users.noreply.github.com> Date: Wed, 22 May 2024 23:15:25 +0530 Subject: [PATCH] add timeout thing to sdp creation --- src/gui/MPServerListGui.hx | 30 ++++++++++++------------ src/net/Net.hx | 47 +++++++++++++++++++++++++++----------- 2 files changed, 50 insertions(+), 27 deletions(-) diff --git a/src/gui/MPServerListGui.hx b/src/gui/MPServerListGui.hx index d85a02e0..fab27697 100644 --- a/src/gui/MPServerListGui.hx +++ b/src/gui/MPServerListGui.hx @@ -141,21 +141,23 @@ class MPServerListGui extends GuiImage { nextButton.accelerators = [hxd.Key.ENTER]; nextButton.gamepadAccelerator = ["X"]; nextButton.pressedAction = (e) -> { - MarbleGame.canvas.setContent(new MultiplayerLoadingGui("Connecting")); - var failed = true; - haxe.Timer.delay(() -> { - if (failed) { - var loadGui:MultiplayerLoadingGui = cast MarbleGame.canvas.content; - if (loadGui != null) { - loadGui.setErrorStatus("Failed to connect to server"); - Net.disconnect(); + if (curSelection != -1) { + MarbleGame.canvas.setContent(new MultiplayerLoadingGui("Connecting")); + var failed = true; + haxe.Timer.delay(() -> { + if (failed) { + var loadGui:MultiplayerLoadingGui = cast MarbleGame.canvas.content; + if (loadGui != null) { + loadGui.setErrorStatus("Failed to connect to server"); + Net.disconnect(); + } } - } - }, 15000); - Net.joinServer(ourServerList[curSelection].name, false, () -> { - failed = false; - Net.remoteServerInfo = ourServerList[curSelection]; - }); + }, 15000); + Net.joinServer(ourServerList[curSelection].name, false, () -> { + failed = false; + Net.remoteServerInfo = ourServerList[curSelection]; + }); + } }; bottomBar.addChild(nextButton); } diff --git a/src/net/Net.hx b/src/net/Net.hx index 9f0dac98..23462a95 100644 --- a/src/net/Net.hx +++ b/src/net/Net.hx @@ -115,14 +115,23 @@ class Net { if (c != "") candidates.push('a=${c}'); } + var sdpFinished = false; + + var finishSdp = () -> { + if (sdpFinished) + return; + sdpFinished = true; + var sdpObj = StringTools.trim(peer.localDescription); + sdpObj = sdpObj + '\r\n' + candidates.join('\r\n') + '\r\n'; + onFinishSdp(Json.stringify({ + sdp: sdpObj, + type: "answer" + })); + } + peer.onGatheringStateChange = (s) -> { if (s == RTC_GATHERING_COMPLETE) { - var sdpObj = StringTools.trim(peer.localDescription); - sdpObj = sdpObj + '\r\n' + candidates.join('\r\n') + '\r\n'; - onFinishSdp(Json.stringify({ - sdp: sdpObj, - type: "answer" - })); + finishSdp(); } } var reliable:datachannel.RTCDataChannel = null; @@ -159,18 +168,30 @@ class Net { candidates.push('a=${c}'); } + var sdpFinished = false; + var finishSdp = () -> { + if (sdpFinished) + return; + sdpFinished = true; + Console.log("Local Description Set!"); + var sdpObj = StringTools.trim(client.localDescription); + sdpObj = sdpObj + '\r\n' + candidates.join('\r\n') + '\r\n'; + MasterServerClient.instance.sendConnectToServer(serverName, Json.stringify({ + sdp: sdpObj, + type: "offer" + }), isInvite); + } + client.onGatheringStateChange = (s) -> { if (s == RTC_GATHERING_COMPLETE) { - Console.log("Local Description Set!"); - var sdpObj = StringTools.trim(client.localDescription); - sdpObj = sdpObj + '\r\n' + candidates.join('\r\n') + '\r\n'; - MasterServerClient.instance.sendConnectToServer(serverName, Json.stringify({ - sdp: sdpObj, - type: "offer" - }), isInvite); + finishSdp(); } } + haxe.Timer.delay(() -> { + finishSdp(); + }, 5000); + clientDatachannel = client.createDatachannel("mp"); clientDatachannelUnreliable = client.createDatachannelWithOptions("unreliable", false, 0, 600);