From f76c7a7c39a8f834790b60f32873b38bdd9ca1c5 Mon Sep 17 00:00:00 2001 From: RandomityGuy <31925790+RandomityGuy@users.noreply.github.com> Date: Sat, 25 May 2024 19:48:36 +0530 Subject: [PATCH] fix touch buttons, make master server async on native, fix minor crash --- src/gui/MPServerListGui.hx | 10 ++- src/net/MasterServerClient.hx | 133 +++++++++++++++++++++++++++------- src/net/Net.hx | 2 +- src/touch/TouchInput.hx | 14 ++-- 4 files changed, 122 insertions(+), 37 deletions(-) diff --git a/src/gui/MPServerListGui.hx b/src/gui/MPServerListGui.hx index fab27697..6115410b 100644 --- a/src/gui/MPServerListGui.hx +++ b/src/gui/MPServerListGui.hx @@ -146,10 +146,12 @@ class MPServerListGui extends GuiImage { 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 (MarbleGame.canvas.content is MultiplayerLoadingGui) { + var loadGui:MultiplayerLoadingGui = cast MarbleGame.canvas.content; + if (loadGui != null) { + loadGui.setErrorStatus("Failed to connect to server"); + Net.disconnect(); + } } } }, 15000); diff --git a/src/net/MasterServerClient.hx b/src/net/MasterServerClient.hx index 4816ed2b..a689dbed 100644 --- a/src/net/MasterServerClient.hx +++ b/src/net/MasterServerClient.hx @@ -24,27 +24,99 @@ class MasterServerClient { var open = false; + #if hl + var wsThread:sys.thread.Thread; + static var responses:sys.thread.Deque<() -> Void> = new sys.thread.Deque<() -> Void>(); + var toSend:sys.thread.Deque = new sys.thread.Deque(); + var stopping:Bool = false; + var stopMutex: sys.thread.Mutex = new sys.thread.Mutex(); + #end + public function new(onOpenFunc:() -> Void) { - ws = WebSocket.create(serverIp); - ws.onopen = () -> { - open = true; - onOpenFunc(); - } - ws.onmessageString = (m) -> { - handleMessage(m); - } - ws.onerror = (m) -> { - MarbleGame.canvas.pushDialog(new MessageBoxOkDlg("Failed to connect to master server: " + m)); - open = false; - ws = null; - instance = null; - } + #if hl + wsThread = sys.thread.Thread.create(() -> { + #end + ws = WebSocket.create(serverIp); + ws.onopen = () -> { + open = true; + #if hl + responses.add(() -> onOpenFunc()); + #end + #if js + onOpenFunc(); + #end + } + ws.onmessageString = (m) -> { + #if hl + responses.add(() -> handleMessage(m)); + #end + #if js + handleMessage(m); + #end + } + ws.onerror = (m) -> { + #if hl + responses.add(() -> { + MarbleGame.canvas.pushDialog(new MessageBoxOkDlg("Failed to connect to master server: " + m)); + }); + #end + #if js + MarbleGame.canvas.pushDialog(new MessageBoxOkDlg("Failed to connect to master server: " + m)); + #end + #if hl + stopMutex.acquire(); + #end + open = false; + ws = null; + instance = null; + #if hl + stopMutex.acquire(); + stopping = true; + stopMutex.release(); + wsThread = null; + #end + } + ws.onclose = (?e) -> { + #if hl + stopMutex.acquire(); + #end + open = false; + ws = null; + instance = null; + #if hl + stopping = true; + stopMutex.release(); + wsThread = null; + #end + } + #if hl + while (true) { + stopMutex.acquire(); + if (stopping) + break; + while (true) { + var s = toSend.pop(false); + if (s == null) + break; + ws.sendString(s); + } + + ws.process(); + stopMutex.release(); + Sys.sleep(0.1); + } + #end + #if hl + }); + #end } public static function process() { #if sys - if (instance != null) - instance.ws.process(); + var resp = responses.pop(false); + if (resp != null) { + resp(); + } #end } @@ -56,7 +128,6 @@ class MasterServerClient { onConnect(); else { instance.ws.close(); - instance = null; instance = new MasterServerClient(onConnect); } } @@ -65,18 +136,26 @@ class MasterServerClient { public static function disconnectFromMasterServer() { if (instance != null) { instance.ws.close(); - instance = null; } } + function queueMessage(m:String) { + #if hl + toSend.add(m); + #end + #if js + ws.sendString(m); + #end + } + public function heartBeat() { - ws.sendString(Json.stringify({ + queueMessage(Json.stringify({ type: "heartbeat" })); } public function sendServerInfo(serverInfo:ServerInfo) { - ws.sendString(Json.stringify({ + queueMessage(Json.stringify({ type: "serverInfo", name: serverInfo.name, players: serverInfo.players, @@ -91,13 +170,13 @@ class MasterServerClient { public function sendConnectToServer(serverName:String, sdp:String, isInvite:Bool = false) { if (!isInvite) { - ws.sendString(Json.stringify({ + queueMessage(Json.stringify({ type: "connect", serverName: serverName, sdp: sdp })); } else { - ws.sendString(Json.stringify({ + queueMessage(Json.stringify({ type: "connectInvite", sdp: sdp, inviteCode: serverName @@ -107,7 +186,7 @@ class MasterServerClient { public function getServerList(serverListCb:Array->Void) { this.serverListCb = serverListCb; - ws.sendString(Json.stringify({ + queueMessage(Json.stringify({ type: "serverList" })); } @@ -122,7 +201,7 @@ class MasterServerClient { } if (conts.type == "connect") { if (!Net.isHost) { - ws.sendString(Json.stringify({ + queueMessage(Json.stringify({ type: "connectFailed", success: false, reason: "The server has shut down" @@ -132,7 +211,7 @@ class MasterServerClient { var joiningPrivate = conts.isPrivate; if (Net.serverInfo.players >= Net.serverInfo.maxPlayers) { - ws.sendString(Json.stringify({ + queueMessage(Json.stringify({ type: "connectFailed", success: false, reason: "The server is full" @@ -153,7 +232,7 @@ class MasterServerClient { } if (!joiningPrivate && pubCount >= pubSlotsAvail) { - ws.sendString(Json.stringify({ + queueMessage(Json.stringify({ type: "connectFailed", success: false, reason: "The server is full" @@ -166,7 +245,7 @@ class MasterServerClient { } Net.addClientFromSdp(conts.sdp, joiningPrivate, (sdpReply) -> { - ws.sendString(Json.stringify({ + queueMessage(Json.stringify({ success: true, type: "connectResponse", sdp: sdpReply, diff --git a/src/net/Net.hx b/src/net/Net.hx index 3ac76348..1d283370 100644 --- a/src/net/Net.hx +++ b/src/net/Net.hx @@ -246,7 +246,7 @@ class Net { }, 5000); clientDatachannel = client.createDatachannel("mp"); - clientDatachannelUnreliable = client.createDatachannelWithOptions("unreliable", false, 0, 600); + clientDatachannelUnreliable = client.createDatachannelWithOptions("unreliable", false, null, 600); var closing = false; var openFlags = 0; diff --git a/src/touch/TouchInput.hx b/src/touch/TouchInput.hx index f5085884..6b72695b 100644 --- a/src/touch/TouchInput.hx +++ b/src/touch/TouchInput.hx @@ -1,5 +1,6 @@ package touch; +import net.Net; import gui.GuiControl; import src.MarbleWorld; import h3d.Vector; @@ -118,17 +119,18 @@ class TouchInput { this.movementInput = new MovementInput(); this.jumpButton = new JumpButton(); this.powerupButton = new PowerupButton(); - if (Settings.optionsSettings.rewindEnabled) + if (Settings.optionsSettings.rewindEnabled && !Net.isMP) this.rewindButton = new RewindButton(); if (ultra) this.blastbutton = new BlastButton(); this.pauseButton = new PauseButton(); - this.restartButton = new RestartButton(); + if (!Net.isMP) + this.restartButton = new RestartButton(); pauseButton.add(parentGui); restartButton.add(parentGui); jumpButton.add(parentGui); powerupButton.add(parentGui); - if (Settings.optionsSettings.rewindEnabled) + if (Settings.optionsSettings.rewindEnabled && !Net.isMP) rewindButton.add(parentGui); if (ultra) blastbutton.add(parentGui); @@ -144,7 +146,8 @@ class TouchInput { this.blastbutton.setVisible(enabled); this.movementInput.setVisible(enabled); this.pauseButton.setVisible(enabled); - this.restartButton.setVisible(enabled); + if (this.restartButton != null) + this.restartButton.setVisible(enabled); if (this.rewindButton != null) this.rewindButton.setVisible(enabled); this.cameraInput.enabled = enabled; @@ -157,7 +160,8 @@ class TouchInput { blastbutton.remove(parentGui); movementInput.remove(parentGui); pauseButton.remove(parentGui); - restartButton.remove(parentGui); + if (this.restartButton != null) + restartButton.remove(parentGui); cameraInput.remove(parentGui); if (this.rewindButton != null) rewindButton.remove(parentGui);