Functional for client

This commit is contained in:
ayunami2000 2022-08-19 23:21:08 -04:00
parent f49e79cf42
commit 038ae4d325
8 changed files with 15924 additions and 15891 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -70,11 +70,11 @@ window.initializeVoiceClient = (() => {
} }
this.peerConnection.addEventListener("connectionstatechange", (evt) => { this.peerConnection.addEventListener("connectionstatechange", (evt) => {
if(evt.connectionState === 'disconnected') { if(self.peerConnection.connectionState === 'disconnected') {
self.client.signalDisconnect(self.peerId); self.client.signalDisconnect(self.peerId);
} else if (evt.connectionState === 'connected') { } else if (self.peerConnection.connectionState === 'connected') {
if (self.client.peerState != PEERSTATE_SUCCESS) self.client.peerState = PEERSTATE_SUCCESS; if (self.client.peerState != PEERSTATE_SUCCESS) self.client.peerState = PEERSTATE_SUCCESS;
} else if (evt.connectionState === 'failed') { } else if (self.peerConnection.connectionState === 'failed') {
if (self.client.peerState == PEERSTATE_LOADING) self.client.peerState = PEERSTATE_FAILED; if (self.client.peerState == PEERSTATE_LOADING) self.client.peerState = PEERSTATE_FAILED;
self.client.signalDisconnect(self.peerId); self.client.signalDisconnect(self.peerId);
} }
@ -392,26 +392,41 @@ window.initializeLANClient = (() => {
} }
sendPacketToServer(buffer) { sendPacketToServer(buffer) {
this.dataChannel.send(buffer); if(this.dataChannel.readyState == "open") {
this.dataChannel.send(buffer);
}else {
this.signalRemoteDisconnect(false);
}
} }
signalRemoteConnect() { signalRemoteConnect() {
const self = this; const self = this;
const iceCandidates = [];
this.peerConnection.addEventListener("icecandidate", (evt) => { this.peerConnection.addEventListener("icecandidate", (evt) => {
if(evt.candidate) { if(evt.candidate) {
self.iceCandidateHandler(JSON.stringify({ sdpMLineIndex: evt.candidate.sdpMLineIndex, candidate: evt.candidate.candidate })); if(iceCandidates.length == 0) setTimeout(() => {
if(self.peerConnection != null && self.peerConnection.connectionState != "disconnected") {
self.iceCandidateHandler(JSON.stringify(iceCandidates));
iceCandidates.length = 0;
}
}, 1500);
iceCandidates.push({ sdpMLineIndex: evt.candidate.sdpMLineIndex, candidate: evt.candidate.candidate });
} }
}); });
this.channel = this.peerConnection.createDataChannel("lan"); this.dataChannel = this.peerConnection.createDataChannel("lan");
this.dataChannel.binaryType = "arraybuffer";
this.channel.addEventListener("open", (evt) => { this.dataChannel.addEventListener("open", async (evt) => {
self.remoteDataChannelHandler(self.channel); while(iceCandidates.length > 0) {
await new Promise(resolve => setTimeout(resolve, 0));
}
self.remoteDataChannelHandler(self.dataChannel);
}); });
this.channel.addEventListener("message", (evt) => { this.dataChannel.addEventListener("message", (evt) => {
console.log(evt.data);
self.remotePacketHandler(evt.data); self.remotePacketHandler(evt.data);
}, false); }, false);
@ -422,22 +437,22 @@ window.initializeLANClient = (() => {
}, (err) => { }, (err) => {
console.error("Failed to set local description! " + err); console.error("Failed to set local description! " + err);
self.readyState = READYSTATE_FAILED; self.readyState = READYSTATE_FAILED;
self.signalRemoteDisconnect(); self.signalRemoteDisconnect(false);
}); });
}, (err) => { }, (err) => {
console.error("Failed to set create offer! " + err); console.error("Failed to set create offer! " + err);
self.readyState = READYSTATE_FAILED; self.readyState = READYSTATE_FAILED;
self.signalRemoteDisconnect(); self.signalRemoteDisconnect(false);
}); });
this.peerConnection.addEventListener("connectionstatechange", (evt) => { this.peerConnection.addEventListener("connectionstatechange", (evt) => {
if(evt.connectionState === 'disconnected') { if(self.peerConnection.connectionState === 'disconnected') {
self.signalRemoteDisconnect(); self.signalRemoteDisconnect(false);
} else if (evt.connectionState === 'connected') { } else if (self.peerConnection.connectionState === 'connected') {
self.readyState = READYSTATE_CONNECTED; self.readyState = READYSTATE_CONNECTED;
} else if (evt.connectionState === 'failed') { } else if (self.peerConnection.connectionState === 'failed') {
self.readyState = READYSTATE_FAILED; self.readyState = READYSTATE_FAILED;
self.signalRemoteDisconnect(); self.signalRemoteDisconnect(false);
} }
}); });
} }
@ -448,21 +463,24 @@ window.initializeLANClient = (() => {
} catch (e) { } catch (e) {
console.error(e); console.error(e);
this.readyState = READYSTATE_FAILED; this.readyState = READYSTATE_FAILED;
this.signalRemoteDisconnect(); this.signalRemoteDisconnect(false);
} }
} }
signalRemoteICECandidate(candidate) { signalRemoteICECandidate(candidates) {
try { try {
this.peerConnection.addIceCandidate(JSON.parse(candidate)); const candidateList = JSON.parse(candidates);
for (let candidate of candidateList) {
this.peerConnection.addIceCandidate(candidate);
}
} catch (e) { } catch (e) {
console.error(e); console.error(e);
this.readyState = READYSTATE_FAILED; this.readyState = READYSTATE_FAILED;
this.signalRemoteDisconnect(); this.signalRemoteDisconnect(false);
} }
} }
signalRemoteDisconnect() { signalRemoteDisconnect(quiet) {
if(this.dataChannel != null) { if(this.dataChannel != null) {
this.dataChannel.close(); this.dataChannel.close();
this.dataChannel = null; this.dataChannel = null;
@ -470,7 +488,7 @@ window.initializeLANClient = (() => {
if(this.peerConnection != null) { if(this.peerConnection != null) {
this.peerConnection.close(); this.peerConnection.close();
} }
this.remoteDisconnectHandler(); if(!quiet) this.remoteDisconnectHandler();
this.readyState = READYSTATE_DISCONNECTED; this.readyState = READYSTATE_DISCONNECTED;
} }
@ -505,27 +523,38 @@ window.initializeLANServer = (() => {
this.dataChannel = null; this.dataChannel = null;
const self = this; const self = this;
const iceCandidates = [];
this.peerConnection.addEventListener("icecandidate", (evt) => { this.peerConnection.addEventListener("icecandidate", (evt) => {
if(evt.candidate) { if(evt.candidate) {
self.client.iceCandidateHandler(self.peerId, JSON.stringify({ sdpMLineIndex: evt.candidate.sdpMLineIndex, candidate: evt.candidate.candidate })); if(iceCandidates.length == 0) setTimeout(() => {
if(self.peerConnection != null && self.peerConnection.connectionState != "disconnected") {
self.client.iceCandidateHandler(self.peerId, JSON.stringify(iceCandidates));
iceCandidates.length = 0;
}
}, 1500);
iceCandidates.push({ sdpMLineIndex: evt.candidate.sdpMLineIndex, candidate: evt.candidate.candidate });
} }
}); });
this.peerConnection.addEventListener("datachannel", (evt) => { this.peerConnection.addEventListener("datachannel", async (evt) => {
while(iceCandidates.length > 0) {
await new Promise(resolve => setTimeout(resolve, 0));
}
self.dataChannel = evt.channel; self.dataChannel = evt.channel;
self.client.remoteClientDataChannelHandler(self.peerId, self.dataChannel); self.client.remoteClientDataChannelHandler(self.peerId, self.dataChannel);
self.dataChannel.addEventListener("message", (evt) => { self.dataChannel.addEventListener("message", (evt) => {
console.log(evt.data);
self.client.remoteClientPacketHandler(self.peerId, evt.data); self.client.remoteClientPacketHandler(self.peerId, evt.data);
}, false); }, false);
}); }, false);
this.peerConnection.addEventListener("connectionstatechange", (evt) => { this.peerConnection.addEventListener("connectionstatechange", (evt) => {
if(evt.connectionState === 'disconnected') { if(self.peerConnection.connectionState === 'disconnected') {
self.client.signalRemoteDisconnect(self.peerId); self.client.signalRemoteDisconnect(self.peerId);
} else if (evt.connectionState === 'connected') { } else if (self.peerConnection.connectionState === 'connected') {
if (self.client.peerState != PEERSTATE_SUCCESS) self.client.peerState = PEERSTATE_SUCCESS; if (self.client.peerState != PEERSTATE_SUCCESS) self.client.peerState = PEERSTATE_SUCCESS;
} else if (evt.connectionState === 'failed') { } else if (self.peerConnection.connectionState === 'failed') {
if (self.client.peerState == PEERSTATE_LOADING) self.client.peerState = PEERSTATE_FAILED; if (self.client.peerState == PEERSTATE_LOADING) self.client.peerState = PEERSTATE_FAILED;
self.client.signalRemoteDisconnect(self.peerId); self.client.signalRemoteDisconnect(self.peerId);
} }
@ -575,9 +604,12 @@ window.initializeLANServer = (() => {
} }
} }
addICECandidate(candidate) { addICECandidate(candidates) {
try { try {
this.peerConnection.addIceCandidate(new RTCIceCandidate(JSON.parse(candidate))); const candidateList = JSON.parse(candidates);
for (let candidate of candidateList) {
this.peerConnection.addIceCandidate(new RTCIceCandidate(candidate));
}
if (this.client.peerStateIce != PEERSTATE_SUCCESS) this.client.peerStateIce = PEERSTATE_SUCCESS; if (this.client.peerStateIce != PEERSTATE_SUCCESS) this.client.peerStateIce = PEERSTATE_SUCCESS;
} catch (err) { } catch (err) {
console.error("Failed to parse ice candidate for \"" + this.peerId + "\"! " + err); console.error("Failed to parse ice candidate for \"" + this.peerId + "\"! " + err);
@ -649,8 +681,11 @@ window.initializeLANServer = (() => {
sendPacketToRemoteClient(peerId, buffer) { sendPacketToRemoteClient(peerId, buffer) {
var thePeer = this.peerList.get(peerId); var thePeer = this.peerList.get(peerId);
if((typeof thePeer !== "undefined") && thePeer !== null) { if((typeof thePeer !== "undefined") && thePeer !== null) {
console.log(123); if(thePeer.dataChannel.readyState == "open") {
thePeer.dataChannel.send(buffer); thePeer.dataChannel.send(buffer);
}else {
this.signalRemoteDisconnect(peerId);
}
} }
} }

@ -63,6 +63,7 @@ public class GuiScreenLANConnecting extends GuiScreen {
try { try {
netHandler = new NetClientHandler(mc, netMgr); netHandler = new NetClientHandler(mc, netMgr);
this.mc.setNetManager(netMgr); this.mc.setNetManager(netMgr);
netMgr.setNetHandler(netHandler);
netHandler.addToSendQueue(new Packet2ClientProtocol(61, EaglerProfile.username, "127.0.0.1", mc.gameSettings.renderDistance)); netHandler.addToSendQueue(new Packet2ClientProtocol(61, EaglerProfile.username, "127.0.0.1", mc.gameSettings.renderDistance));
} catch (IOException e) { } catch (IOException e) {
this.mc.displayGuiScreen(new GuiDisconnected(parent, "connect.failed", "disconnect.genericReason", "could not create nethandler", "")); this.mc.displayGuiScreen(new GuiDisconnected(parent, "connect.failed", "disconnect.genericReason", "could not create nethandler", ""));

@ -293,24 +293,20 @@ public class IntegratedServerLAN {
protected void update() { protected void update() {
if(state == CONNECTED) { if(state == CONNECTED) {
LANPeerEvent evt; LANPeerEvent evt;
while((evt = EaglerAdapter.serverLANGetEvent(clientId)) != null) { while(state == CONNECTED && (evt = EaglerAdapter.serverLANGetEvent(clientId)) != null) {
if(state == CONNECTED) { if(evt instanceof LANPeerEvent.LANPeerPacketEvent) {
if(evt instanceof LANPeerEvent.LANPeerPacketEvent) { EaglerAdapter.sendToIntegratedServer("NET|" + clientId, ((LANPeerEvent.LANPeerPacketEvent)evt).payload);
EaglerAdapter.sendToIntegratedServer(clientId, ((LANPeerEvent.LANPeerPacketEvent)evt).payload); }else if(evt instanceof LANPeerEvent.LANPeerDisconnectEvent) {
}else if(evt instanceof LANPeerEvent.LANPeerDisconnectEvent) { System.err.println("LAN client '" + clientId + "' disconnected");
System.err.println("LAN client '" + clientId + "' disconnected"); disconnect();
disconnect(); }else {
}else { System.err.println("LAN client '" + clientId + "' had an accident: " + evt.getClass().getSimpleName());
System.err.println("LAN client '" + clientId + "' had an accident: " + evt.getClass().getSimpleName()); disconnect();
disconnect();
}
} }
} }
if(state == CONNECTED) { PKT pk;
PKT pk; while(state == CONNECTED && (pk = EaglerAdapter.recieveFromIntegratedServer("NET|" + clientId)) != null) {
while((pk = EaglerAdapter.recieveFromIntegratedServer("NET|" + clientId)) != null) { EaglerAdapter.serverLANWritePacket(clientId, pk.data);
EaglerAdapter.serverLANWritePacket(clientId, pk.data);
}
} }
} }
} }

@ -251,7 +251,7 @@ public class LANClientNetworkManager implements INetworkManager {
* message, just a vague stack trace. But making a multi-catch around just * message, just a vague stack trace. But making a multi-catch around just
* readPacketData and processPacket has no issues * readPacketData and processPacket has no issues
* *
* You'e welcome for the two hours of my time and single line changes I made * You're welcome for the two hours of my time and single line changes I made
* in a fuck ton of irrelevant files leading up to this bullshit revelation * in a fuck ton of irrelevant files leading up to this bullshit revelation
*/ */

@ -3718,7 +3718,7 @@ public class EaglerAdapterImpl2 {
} }
public static final void clientLANCloseConnection() { public static final void clientLANCloseConnection() {
rtcLANClient.signalRemoteDisconnect(); rtcLANClient.signalRemoteDisconnect(false);
} }
public static final void clientLANSendPacket(byte[] pkt) { public static final void clientLANSendPacket(byte[] pkt) {
@ -3770,7 +3770,7 @@ public class EaglerAdapterImpl2 {
} }
rtcLANClient.setICEServers(servers); rtcLANClient.setICEServers(servers);
if(clientLANReadyState() == rtcLANClient.READYSTATE_CONNECTED || clientLANReadyState() == rtcLANClient.READYSTATE_CONNECTING) { if(clientLANReadyState() == rtcLANClient.READYSTATE_CONNECTED || clientLANReadyState() == rtcLANClient.READYSTATE_CONNECTING) {
rtcLANClient.signalRemoteDisconnect(); rtcLANClient.signalRemoteDisconnect(true);
} }
rtcLANClient.initializeClient(); rtcLANClient.initializeClient();
rtcLANClient.signalRemoteConnect(); rtcLANClient.signalRemoteConnect();

@ -38,7 +38,7 @@ public interface EaglercraftLANClient extends JSObject {
void signalRemoteICECandidate(String candidate); void signalRemoteICECandidate(String candidate);
void signalRemoteDisconnect(); void signalRemoteDisconnect(boolean quiet);
@JSFunctor @JSFunctor
public static interface ICECandidateHandler extends JSObject { public static interface ICECandidateHandler extends JSObject {