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) => { this.peerConnection.addEventListener("icecandidate", (evt) => {
if(evt.candidate) { if(evt.candidate) {
if(iceCandidates.length === 0) setTimeout(() => { if(iceCandidates.length === 0) {
if(this.peerConnection !== null && this.peerConnection.connectionState !== "disconnected") { let candidateState = [ 0, 0 ];
this.iceCandidateHandler(JSON.stringify(iceCandidates)); let runnable;
iceCandidates.length = 0; setTimeout(runnable = () => {
} if(this.peerConnection !== null && this.peerConnection.connectionState !== "disconnected") {
}, 3000); 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 }); iceCandidates.push({ sdpMLineIndex: evt.candidate.sdpMLineIndex, candidate: evt.candidate.candidate });
} }
}); });
@ -366,7 +376,7 @@ window.initializeLANClient = (() => {
this.dataChannel.addEventListener("open", async (evt) => { this.dataChannel.addEventListener("open", async (evt) => {
while(iceCandidates.length > 0) { while(iceCandidates.length > 0) {
await new Promise(resolve => setTimeout(resolve, 0)); await new Promise(resolve => setTimeout(resolve, 10));
} }
this.remoteDataChannelHandler(this.dataChannel); this.remoteDataChannelHandler(this.dataChannel);
}); });
@ -467,19 +477,29 @@ window.initializeLANServer = (() => {
this.peerConnection.addEventListener("icecandidate", (evt) => { this.peerConnection.addEventListener("icecandidate", (evt) => {
if(evt.candidate) { if(evt.candidate) {
if(iceCandidates.length === 0) setTimeout(() => { if(iceCandidates.length === 0) {
if(this.peerConnection !== null && this.peerConnection.connectionState !== "disconnected") { let candidateState = [ 0, 0 ];
this.client.iceCandidateHandler(this.peerId, JSON.stringify(iceCandidates)); let runnable;
iceCandidates.length = 0; setTimeout(runnable = () => {
} if(this.peerConnection !== null && this.peerConnection.connectionState !== "disconnected") {
}, 3000); 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 }); iceCandidates.push({ sdpMLineIndex: evt.candidate.sdpMLineIndex, candidate: evt.candidate.candidate });
} }
}); });
this.peerConnection.addEventListener("datachannel", async (evt) => { this.peerConnection.addEventListener("datachannel", async (evt) => {
while(iceCandidates.length > 0) { while(iceCandidates.length > 0) {
await new Promise(resolve => setTimeout(resolve, 0)); await new Promise(resolve => setTimeout(resolve, 10));
} }
this.dataChannel = evt.channel; this.dataChannel = evt.channel;
this.client.remoteClientDataChannelHandler(this.peerId, this.dataChannel); this.client.remoteClientDataChannelHandler(this.peerId, this.dataChannel);

View File

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

View File

@ -59,7 +59,7 @@ public class IntegratedServerLAN {
} }
} }
EaglerAdapter.sleep(50); EaglerAdapter.sleep(50);
}while(EaglerAdapter.steadyTimeMillis() - millis < 1000l); }while(EaglerAdapter.steadyTimeMillis() - millis < 2500l);
System.out.println("Relay [" + sock.getURI() + "] relay provide ICE servers timeout"); System.out.println("Relay [" + sock.getURI() + "] relay provide ICE servers timeout");
closeLAN(); closeLAN();
return null; return null;
@ -257,6 +257,15 @@ public class IntegratedServerLAN {
localICECandidate = ((LANPeerEvent.LANPeerICECandidateEvent)evt).candidates; localICECandidate = ((LANPeerEvent.LANPeerICECandidateEvent)evt).candidates;
continue read_loop; 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: { case RECEIVED_ICE_CANDIDATE: {
if(evt instanceof LANPeerEvent.LANPeerICECandidateEvent) { if(evt instanceof LANPeerEvent.LANPeerICECandidateEvent) {
@ -265,33 +274,31 @@ public class IntegratedServerLAN {
continue read_loop; 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 SENT_ICE_CANDIDATE:
case RECEIVED_SUCCESS: { case RECEIVED_SUCCESS: {
if(evt instanceof LANPeerEvent.LANPeerDataChannelEvent) { if(evt instanceof LANPeerEvent.LANPeerDataChannelEvent) {
EaglerAdapter.enableChannel("NET|" + clientId); EaglerAdapter.enableChannel("NET|" + clientId);
IntegratedServer.sendIPCPacket(new IPCPacket0CPlayerChannel(clientId, true));
state = CONNECTED; state = CONNECTED;
continue read_loop; continue read_loop;
} }
break;
} }
case CONNECTED: { case CONNECTED: {
if(evt instanceof LANPeerEvent.LANPeerPacketEvent) { if(evt instanceof LANPeerEvent.LANPeerPacketEvent) {
EaglerAdapter.sendToIntegratedServer("NET|" + clientId, ((LANPeerEvent.LANPeerPacketEvent)evt).payload); EaglerAdapter.sendToIntegratedServer("NET|" + clientId, ((LANPeerEvent.LANPeerPacketEvent)evt).payload);
continue read_loop; continue read_loop;
}else if(evt instanceof LANPeerEvent.LANPeerICECandidateEvent) {
continue read_loop;
} }
break;
} }
default: { default: {
break; break;
} }
} }
if(state != CLOSED) { 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(); disconnect();
return; return;

View File

@ -128,7 +128,7 @@ public class LANClientNetworkManager implements INetworkManager {
} }
EaglerAdapter.sleep(20); EaglerAdapter.sleep(20);
}while(EaglerAdapter.steadyTimeMillis() - lm < 5000l); }while(EaglerAdapter.steadyTimeMillis() - lm < 10000l);
// no channel was opened // no channel was opened
sock.writePacket(new IPacket06ClientFailure(ipkt.peerId)); sock.writePacket(new IPacket06ClientFailure(ipkt.peerId));
@ -167,7 +167,7 @@ public class LANClientNetworkManager implements INetworkManager {
continue mainLoop; continue mainLoop;
} }
EaglerAdapter.sleep(20); EaglerAdapter.sleep(20);
}while(EaglerAdapter.steadyTimeMillis() - lm < 5000l); }while(EaglerAdapter.steadyTimeMillis() - lm < 10000l);
// no ice candidates were sent // no ice candidates were sent
sock.close(); 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) { public static final void serverLANWritePacket(String peer, byte[] data) {
if (data.length > fragmentSize) { if (data.length > fragmentSize) {