fix touch buttons, make master server async on native, fix minor crash

This commit is contained in:
RandomityGuy 2024-05-25 19:48:36 +05:30
parent 89bbbe777a
commit f76c7a7c39
4 changed files with 122 additions and 37 deletions

View file

@ -146,10 +146,12 @@ class MPServerListGui extends GuiImage {
var failed = true; var failed = true;
haxe.Timer.delay(() -> { haxe.Timer.delay(() -> {
if (failed) { if (failed) {
var loadGui:MultiplayerLoadingGui = cast MarbleGame.canvas.content; if (MarbleGame.canvas.content is MultiplayerLoadingGui) {
if (loadGui != null) { var loadGui:MultiplayerLoadingGui = cast MarbleGame.canvas.content;
loadGui.setErrorStatus("Failed to connect to server"); if (loadGui != null) {
Net.disconnect(); loadGui.setErrorStatus("Failed to connect to server");
Net.disconnect();
}
} }
} }
}, 15000); }, 15000);

View file

@ -24,27 +24,99 @@ class MasterServerClient {
var open = false; 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<String> = new sys.thread.Deque<String>();
var stopping:Bool = false;
var stopMutex: sys.thread.Mutex = new sys.thread.Mutex();
#end
public function new(onOpenFunc:() -> Void) { public function new(onOpenFunc:() -> Void) {
ws = WebSocket.create(serverIp); #if hl
ws.onopen = () -> { wsThread = sys.thread.Thread.create(() -> {
open = true; #end
onOpenFunc(); ws = WebSocket.create(serverIp);
} ws.onopen = () -> {
ws.onmessageString = (m) -> { open = true;
handleMessage(m); #if hl
} responses.add(() -> onOpenFunc());
ws.onerror = (m) -> { #end
MarbleGame.canvas.pushDialog(new MessageBoxOkDlg("Failed to connect to master server: " + m)); #if js
open = false; onOpenFunc();
ws = null; #end
instance = null; }
} 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() { public static function process() {
#if sys #if sys
if (instance != null) var resp = responses.pop(false);
instance.ws.process(); if (resp != null) {
resp();
}
#end #end
} }
@ -56,7 +128,6 @@ class MasterServerClient {
onConnect(); onConnect();
else { else {
instance.ws.close(); instance.ws.close();
instance = null;
instance = new MasterServerClient(onConnect); instance = new MasterServerClient(onConnect);
} }
} }
@ -65,18 +136,26 @@ class MasterServerClient {
public static function disconnectFromMasterServer() { public static function disconnectFromMasterServer() {
if (instance != null) { if (instance != null) {
instance.ws.close(); instance.ws.close();
instance = null;
} }
} }
function queueMessage(m:String) {
#if hl
toSend.add(m);
#end
#if js
ws.sendString(m);
#end
}
public function heartBeat() { public function heartBeat() {
ws.sendString(Json.stringify({ queueMessage(Json.stringify({
type: "heartbeat" type: "heartbeat"
})); }));
} }
public function sendServerInfo(serverInfo:ServerInfo) { public function sendServerInfo(serverInfo:ServerInfo) {
ws.sendString(Json.stringify({ queueMessage(Json.stringify({
type: "serverInfo", type: "serverInfo",
name: serverInfo.name, name: serverInfo.name,
players: serverInfo.players, players: serverInfo.players,
@ -91,13 +170,13 @@ class MasterServerClient {
public function sendConnectToServer(serverName:String, sdp:String, isInvite:Bool = false) { public function sendConnectToServer(serverName:String, sdp:String, isInvite:Bool = false) {
if (!isInvite) { if (!isInvite) {
ws.sendString(Json.stringify({ queueMessage(Json.stringify({
type: "connect", type: "connect",
serverName: serverName, serverName: serverName,
sdp: sdp sdp: sdp
})); }));
} else { } else {
ws.sendString(Json.stringify({ queueMessage(Json.stringify({
type: "connectInvite", type: "connectInvite",
sdp: sdp, sdp: sdp,
inviteCode: serverName inviteCode: serverName
@ -107,7 +186,7 @@ class MasterServerClient {
public function getServerList(serverListCb:Array<RemoteServerInfo>->Void) { public function getServerList(serverListCb:Array<RemoteServerInfo>->Void) {
this.serverListCb = serverListCb; this.serverListCb = serverListCb;
ws.sendString(Json.stringify({ queueMessage(Json.stringify({
type: "serverList" type: "serverList"
})); }));
} }
@ -122,7 +201,7 @@ class MasterServerClient {
} }
if (conts.type == "connect") { if (conts.type == "connect") {
if (!Net.isHost) { if (!Net.isHost) {
ws.sendString(Json.stringify({ queueMessage(Json.stringify({
type: "connectFailed", type: "connectFailed",
success: false, success: false,
reason: "The server has shut down" reason: "The server has shut down"
@ -132,7 +211,7 @@ class MasterServerClient {
var joiningPrivate = conts.isPrivate; var joiningPrivate = conts.isPrivate;
if (Net.serverInfo.players >= Net.serverInfo.maxPlayers) { if (Net.serverInfo.players >= Net.serverInfo.maxPlayers) {
ws.sendString(Json.stringify({ queueMessage(Json.stringify({
type: "connectFailed", type: "connectFailed",
success: false, success: false,
reason: "The server is full" reason: "The server is full"
@ -153,7 +232,7 @@ class MasterServerClient {
} }
if (!joiningPrivate && pubCount >= pubSlotsAvail) { if (!joiningPrivate && pubCount >= pubSlotsAvail) {
ws.sendString(Json.stringify({ queueMessage(Json.stringify({
type: "connectFailed", type: "connectFailed",
success: false, success: false,
reason: "The server is full" reason: "The server is full"
@ -166,7 +245,7 @@ class MasterServerClient {
} }
Net.addClientFromSdp(conts.sdp, joiningPrivate, (sdpReply) -> { Net.addClientFromSdp(conts.sdp, joiningPrivate, (sdpReply) -> {
ws.sendString(Json.stringify({ queueMessage(Json.stringify({
success: true, success: true,
type: "connectResponse", type: "connectResponse",
sdp: sdpReply, sdp: sdpReply,

View file

@ -246,7 +246,7 @@ class Net {
}, 5000); }, 5000);
clientDatachannel = client.createDatachannel("mp"); clientDatachannel = client.createDatachannel("mp");
clientDatachannelUnreliable = client.createDatachannelWithOptions("unreliable", false, 0, 600); clientDatachannelUnreliable = client.createDatachannelWithOptions("unreliable", false, null, 600);
var closing = false; var closing = false;
var openFlags = 0; var openFlags = 0;

View file

@ -1,5 +1,6 @@
package touch; package touch;
import net.Net;
import gui.GuiControl; import gui.GuiControl;
import src.MarbleWorld; import src.MarbleWorld;
import h3d.Vector; import h3d.Vector;
@ -118,17 +119,18 @@ class TouchInput {
this.movementInput = new MovementInput(); this.movementInput = new MovementInput();
this.jumpButton = new JumpButton(); this.jumpButton = new JumpButton();
this.powerupButton = new PowerupButton(); this.powerupButton = new PowerupButton();
if (Settings.optionsSettings.rewindEnabled) if (Settings.optionsSettings.rewindEnabled && !Net.isMP)
this.rewindButton = new RewindButton(); this.rewindButton = new RewindButton();
if (ultra) if (ultra)
this.blastbutton = new BlastButton(); this.blastbutton = new BlastButton();
this.pauseButton = new PauseButton(); this.pauseButton = new PauseButton();
this.restartButton = new RestartButton(); if (!Net.isMP)
this.restartButton = new RestartButton();
pauseButton.add(parentGui); pauseButton.add(parentGui);
restartButton.add(parentGui); restartButton.add(parentGui);
jumpButton.add(parentGui); jumpButton.add(parentGui);
powerupButton.add(parentGui); powerupButton.add(parentGui);
if (Settings.optionsSettings.rewindEnabled) if (Settings.optionsSettings.rewindEnabled && !Net.isMP)
rewindButton.add(parentGui); rewindButton.add(parentGui);
if (ultra) if (ultra)
blastbutton.add(parentGui); blastbutton.add(parentGui);
@ -144,7 +146,8 @@ class TouchInput {
this.blastbutton.setVisible(enabled); this.blastbutton.setVisible(enabled);
this.movementInput.setVisible(enabled); this.movementInput.setVisible(enabled);
this.pauseButton.setVisible(enabled); this.pauseButton.setVisible(enabled);
this.restartButton.setVisible(enabled); if (this.restartButton != null)
this.restartButton.setVisible(enabled);
if (this.rewindButton != null) if (this.rewindButton != null)
this.rewindButton.setVisible(enabled); this.rewindButton.setVisible(enabled);
this.cameraInput.enabled = enabled; this.cameraInput.enabled = enabled;
@ -157,7 +160,8 @@ class TouchInput {
blastbutton.remove(parentGui); blastbutton.remove(parentGui);
movementInput.remove(parentGui); movementInput.remove(parentGui);
pauseButton.remove(parentGui); pauseButton.remove(parentGui);
restartButton.remove(parentGui); if (this.restartButton != null)
restartButton.remove(parentGui);
cameraInput.remove(parentGui); cameraInput.remove(parentGui);
if (this.rewindButton != null) if (this.rewindButton != null)
rewindButton.remove(parentGui); rewindButton.remove(parentGui);