diff --git a/javascript/eagswebrtc.js b/javascript/eagswebrtc.js index 8c05e0b..902a887 100644 --- a/javascript/eagswebrtc.js +++ b/javascript/eagswebrtc.js @@ -351,12 +351,22 @@ window.initializeLANClient = (() => { this.peerConnection.addEventListener("icecandidate", (evt) => { if(evt.candidate) { - if(iceCandidates.length === 0) setTimeout(() => { - if(this.peerConnection !== null && this.peerConnection.connectionState !== "disconnected") { - this.iceCandidateHandler(JSON.stringify(iceCandidates)); - iceCandidates.length = 0; - } - }, 3000); + if(iceCandidates.length === 0) { + let candidateState = [ 0, 0 ]; + let runnable; + setTimeout(runnable = () => { + if(this.peerConnection !== null && this.peerConnection.connectionState !== "disconnected") { + const trial = ++candidateState[1]; + if(candidateState[0] !== iceCandidates.length && trial < 3) { + candidateState[0] = iceCandidates.length; + setTimeout(runnable, 2000); + return; + } + this.iceCandidateHandler(JSON.stringify(iceCandidates)); + iceCandidates.length = 0; + } + }, 2000); + } iceCandidates.push({ sdpMLineIndex: evt.candidate.sdpMLineIndex, candidate: evt.candidate.candidate }); } }); @@ -366,7 +376,7 @@ window.initializeLANClient = (() => { this.dataChannel.addEventListener("open", async (evt) => { while(iceCandidates.length > 0) { - await new Promise(resolve => setTimeout(resolve, 0)); + await new Promise(resolve => setTimeout(resolve, 10)); } this.remoteDataChannelHandler(this.dataChannel); }); @@ -467,19 +477,29 @@ window.initializeLANServer = (() => { this.peerConnection.addEventListener("icecandidate", (evt) => { if(evt.candidate) { - if(iceCandidates.length === 0) setTimeout(() => { - if(this.peerConnection !== null && this.peerConnection.connectionState !== "disconnected") { - this.client.iceCandidateHandler(this.peerId, JSON.stringify(iceCandidates)); - iceCandidates.length = 0; - } - }, 3000); + if(iceCandidates.length === 0) { + let candidateState = [ 0, 0 ]; + let runnable; + setTimeout(runnable = () => { + if(this.peerConnection !== null && this.peerConnection.connectionState !== "disconnected") { + const trial = ++candidateState[1]; + if(candidateState[0] !== iceCandidates.length && trial < 3) { + candidateState[0] = iceCandidates.length; + setTimeout(runnable, 2000); + return; + } + this.client.iceCandidateHandler(JSON.stringify(iceCandidates)); + iceCandidates.length = 0; + } + }, 2000); + } iceCandidates.push({ sdpMLineIndex: evt.candidate.sdpMLineIndex, candidate: evt.candidate.candidate }); } }); this.peerConnection.addEventListener("datachannel", async (evt) => { while(iceCandidates.length > 0) { - await new Promise(resolve => setTimeout(resolve, 0)); + await new Promise(resolve => setTimeout(resolve, 10)); } this.dataChannel = evt.channel; this.client.remoteClientDataChannelHandler(this.peerId, this.dataChannel); diff --git a/src/main/java/net/lax1dude/eaglercraft/AssetRepository.java b/src/main/java/net/lax1dude/eaglercraft/AssetRepository.java index 4c15dec..47a031a 100644 --- a/src/main/java/net/lax1dude/eaglercraft/AssetRepository.java +++ b/src/main/java/net/lax1dude/eaglercraft/AssetRepository.java @@ -14,8 +14,8 @@ import org.json.JSONObject; public class AssetRepository { - private static final HashMap filePool = new HashMap(); - public static final HashMap fileNameOverrides = new HashMap(); + private static final HashMap filePool = new HashMap<>(); + public static final HashMap fileNameOverrides = new HashMap<>(); public static final void loadOverrides(JSONObject json) { JSONObject overrides = json.optJSONObject("assetOverrides", null); diff --git a/src/main/java/net/lax1dude/eaglercraft/IntegratedServerLAN.java b/src/main/java/net/lax1dude/eaglercraft/IntegratedServerLAN.java index 62d4a2c..e3e42cd 100644 --- a/src/main/java/net/lax1dude/eaglercraft/IntegratedServerLAN.java +++ b/src/main/java/net/lax1dude/eaglercraft/IntegratedServerLAN.java @@ -59,7 +59,7 @@ public class IntegratedServerLAN { } } EaglerAdapter.sleep(50); - }while(EaglerAdapter.steadyTimeMillis() - millis < 1000l); + }while(EaglerAdapter.steadyTimeMillis() - millis < 2500l); System.out.println("Relay [" + sock.getURI() + "] relay provide ICE servers timeout"); closeLAN(); return null; @@ -257,6 +257,15 @@ public class IntegratedServerLAN { localICECandidate = ((LANPeerEvent.LANPeerICECandidateEvent)evt).candidates; continue read_loop; } + break; + } + case RECEIVED_DESCRIPTION: { + if(evt instanceof LANPeerEvent.LANPeerDescriptionEvent) { + lanRelaySocket.writePacket(new IPacket04Description(clientId, ((LANPeerEvent.LANPeerDescriptionEvent)evt).description)); + state = SENT_DESCRIPTION; + continue read_loop; + } + break; } case RECEIVED_ICE_CANDIDATE: { if(evt instanceof LANPeerEvent.LANPeerICECandidateEvent) { @@ -265,33 +274,31 @@ public class IntegratedServerLAN { continue read_loop; } } - case RECEIVED_DESCRIPTION: { - if(evt instanceof LANPeerEvent.LANPeerDescriptionEvent) { - lanRelaySocket.writePacket(new IPacket04Description(clientId, ((LANPeerEvent.LANPeerDescriptionEvent)evt).description)); - state = SENT_DESCRIPTION; - continue read_loop; - } - } case SENT_ICE_CANDIDATE: case RECEIVED_SUCCESS: { if(evt instanceof LANPeerEvent.LANPeerDataChannelEvent) { EaglerAdapter.enableChannel("NET|" + clientId); + IntegratedServer.sendIPCPacket(new IPCPacket0CPlayerChannel(clientId, true)); state = CONNECTED; continue read_loop; } + break; } case CONNECTED: { if(evt instanceof LANPeerEvent.LANPeerPacketEvent) { EaglerAdapter.sendToIntegratedServer("NET|" + clientId, ((LANPeerEvent.LANPeerPacketEvent)evt).payload); continue read_loop; + }else if(evt instanceof LANPeerEvent.LANPeerICECandidateEvent) { + continue read_loop; } + break; } default: { break; } } if(state != CLOSED) { - System.err.println("LAN client '" + clientId + "' had an accident: " + evt.getClass().getSimpleName()); + System.err.println("LAN client '" + clientId + "' had an accident: " + evt.getClass().getSimpleName() + " (state " + state + ")"); } disconnect(); return; diff --git a/src/main/java/net/lax1dude/eaglercraft/LANClientNetworkManager.java b/src/main/java/net/lax1dude/eaglercraft/LANClientNetworkManager.java index 5843ac5..f20c033 100644 --- a/src/main/java/net/lax1dude/eaglercraft/LANClientNetworkManager.java +++ b/src/main/java/net/lax1dude/eaglercraft/LANClientNetworkManager.java @@ -128,7 +128,7 @@ public class LANClientNetworkManager implements INetworkManager { } EaglerAdapter.sleep(20); - }while(EaglerAdapter.steadyTimeMillis() - lm < 5000l); + }while(EaglerAdapter.steadyTimeMillis() - lm < 10000l); // no channel was opened sock.writePacket(new IPacket06ClientFailure(ipkt.peerId)); @@ -167,7 +167,7 @@ public class LANClientNetworkManager implements INetworkManager { continue mainLoop; } EaglerAdapter.sleep(20); - }while(EaglerAdapter.steadyTimeMillis() - lm < 5000l); + }while(EaglerAdapter.steadyTimeMillis() - lm < 10000l); // no ice candidates were sent sock.close(); diff --git a/src/teavm/java/net/lax1dude/eaglercraft/adapter/EaglerAdapterImpl2.java b/src/teavm/java/net/lax1dude/eaglercraft/adapter/EaglerAdapterImpl2.java index 3c3a2b0..4732261 100644 --- a/src/teavm/java/net/lax1dude/eaglercraft/adapter/EaglerAdapterImpl2.java +++ b/src/teavm/java/net/lax1dude/eaglercraft/adapter/EaglerAdapterImpl2.java @@ -4274,7 +4274,7 @@ public class EaglerAdapterImpl2 { } } - private static final int fragmentSize = 65536; + private static final int fragmentSize = 0xFF00; public static final void serverLANWritePacket(String peer, byte[] data) { if (data.length > fragmentSize) {