Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Commit

Permalink
pinger
Browse files Browse the repository at this point in the history
  • Loading branch information
CubeWhy committed Aug 10, 2023
1 parent fefe02e commit d0c5b95
Show file tree
Hide file tree
Showing 5 changed files with 266 additions and 78 deletions.
24 changes: 24 additions & 0 deletions .idea/runConfigurations/Build_LiquidLunar.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

106 changes: 28 additions & 78 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 80 additions & 0 deletions src/main/java/org/cubewhy/lunarcn/worker/PingWorker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.cubewhy.lunarcn.worker;

public class PingWorker implements Runnable {
private PingWorkerCallable callable;
private int ping;
private boolean runOnce;
private boolean run = true;
private String currentConnection;

public PingWorker(String connection) {
this.currentConnection = connection;
}

public PingWorker() {
}

public PingWorkerCallable getCallable() {
return this.callable;
}

public void setCallable(PingWorkerCallable callable) {
this.callable = callable;
}

public int getPing() {
if (this.callable == null) {
this.ping = 0;
}

return this.ping;
}

public void setRunOnce(boolean runOnce) {
this.runOnce = runOnce;
}

public boolean shouldRunOnce() {
return this.runOnce;
}

public void setRun(boolean run) {
this.run = run;
}

public boolean isRun() {
return run;
}

public void run() {
while (this.run) {
try {
if (this.currentConnection != null) {
this.callable = new PingWorkerCallable(this.currentConnection);
this.currentConnection = null;
}

if (this.callable != null) {
this.ping = Integer.parseInt(String.valueOf(this.callable.getPing()));
}

Thread.sleep(5000L);
} catch (Exception var2) {
var2.printStackTrace();
}

if (this.runOnce) {
this.run = false;
}
}

}

public String getCurrentConnection() {
return this.currentConnection;
}

public void setCurrentConnection(String currentConnection) {
this.currentConnection = currentConnection;
}
}
49 changes: 49 additions & 0 deletions src/main/java/org/cubewhy/lunarcn/worker/PingWorkerCallable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.cubewhy.lunarcn.worker;

import net.minecraft.client.multiplayer.ServerAddress;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;

import static org.cubewhy.lunarcn.utils.ClientUtils.logger;

public class PingWorkerCallable {
private final SocketAddress address;
private final String rawAddress;

public PingWorkerCallable(String address) {
ServerAddress serveraddress = ServerAddress.fromString(address);
this.address = new InetSocketAddress(serveraddress.getIP(), serveraddress.getPort());
this.rawAddress = address;
}

public String getRawAddress() {
return this.rawAddress;
}

public SocketAddress getAddress() {
return this.address;
}

public Long getPing() {
Socket socket = new Socket();

try {
long i = System.currentTimeMillis();
socket.connect(this.address, 1000);
socket.close();
return System.currentTimeMillis() - i;
} catch (IOException var5) {
if (!socket.isClosed()) {
try {
socket.close();
} catch (IOException var4) {
logger.catching(var4);
}
}
return -1L;
}
}
}
Loading

0 comments on commit d0c5b95

Please sign in to comment.