This repository has been archived by the owner on Feb 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
CubeWhy
committed
Aug 10, 2023
1 parent
fefe02e
commit d0c5b95
Showing
5 changed files
with
266 additions
and
78 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
49
src/main/java/org/cubewhy/lunarcn/worker/PingWorkerCallable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
Oops, something went wrong.