work on webrtc

This commit is contained in:
lax1dude 2024-12-09 22:29:05 -08:00
parent c3bb53977d
commit 8633e6f782
5 changed files with 55 additions and 28 deletions

View File

@ -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);

View File

@ -14,8 +14,8 @@ import org.json.JSONObject;
public class AssetRepository {
private static final HashMap<String,byte[]> filePool = new HashMap();
public static final HashMap<String, String> fileNameOverrides = new HashMap();
private static final HashMap<String,byte[]> filePool = new HashMap<>();
public static final HashMap<String, String> fileNameOverrides = new HashMap<>();
public static final void loadOverrides(JSONObject json) {
JSONObject overrides = json.optJSONObject("assetOverrides", null);

View File

@ -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;

View File

@ -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();

View File

@ -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) {