mirror of
https://github.com/BGHDDevelopment/PlayerBalancer.git
synced 2024-10-05 19:07:52 +02:00
parent
7ff5411e76
commit
9873a645f8
@ -48,27 +48,30 @@ public final class ServerListPing {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public StatusResponse ping(InetSocketAddress host, int timeout) throws IOException {
|
public StatusResponse ping(InetSocketAddress host, int timeout) throws IOException {
|
||||||
try (Socket socket = new Socket()) {
|
Socket socket = new Socket();
|
||||||
|
|
||||||
socket.setSoTimeout(timeout);
|
socket.setSoTimeout(timeout);
|
||||||
socket.connect(host, timeout);
|
socket.connect(host, timeout);
|
||||||
|
|
||||||
try (DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
|
DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
|
||||||
DataInputStream dataInputStream = new DataInputStream(socket.getInputStream())) {
|
DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
|
||||||
ByteArrayOutputStream b = new ByteArrayOutputStream();
|
|
||||||
DataOutputStream handshake = new DataOutputStream(b);
|
ByteArrayOutputStream byteArrStream = new ByteArrayOutputStream();
|
||||||
|
DataOutputStream handshake = new DataOutputStream(byteArrStream);
|
||||||
handshake.writeByte(0x00); //packet id for handshake
|
handshake.writeByte(0x00); //packet id for handshake
|
||||||
writeVarInt(handshake, 4); //protocol version
|
writeVarInt(handshake, 4); //protocol version
|
||||||
writeVarInt(handshake, host.getHostString().length()); //host length
|
writeVarInt(handshake, host.getHostString().length()); //host length
|
||||||
handshake.writeBytes(host.getHostString()); //host string
|
handshake.writeBytes(host.getHostString()); //host string
|
||||||
handshake.writeShort(host.getPort()); //port
|
handshake.writeShort(host.getPort()); //port
|
||||||
writeVarInt(handshake, 1); //state (1 for handshake)
|
writeVarInt(handshake, 1); //state (1 for handshake)
|
||||||
|
handshake.close(); // close handshake packet write stream
|
||||||
|
|
||||||
writeVarInt(dataOutputStream, b.size()); //prepend size
|
writeVarInt(dataOutputStream, byteArrStream.size()); //prepend size
|
||||||
dataOutputStream.write(b.toByteArray()); //write handshake packet
|
dataOutputStream.write(byteArrStream.toByteArray()); //write handshake packet
|
||||||
|
|
||||||
|
|
||||||
dataOutputStream.writeByte(0x01); //size is only 1
|
dataOutputStream.writeByte(0x01); //size is only 1
|
||||||
dataOutputStream.writeByte(0x00); //packet id for ping
|
dataOutputStream.writeByte(0x00); //packet id for ping
|
||||||
|
|
||||||
int size = readVarInt(dataInputStream); //size of packet
|
int size = readVarInt(dataInputStream); //size of packet
|
||||||
int id = readVarInt(dataInputStream); //packet id
|
int id = readVarInt(dataInputStream); //packet id
|
||||||
|
|
||||||
@ -93,12 +96,13 @@ public final class ServerListPing {
|
|||||||
dataInputStream.readFully(in); //read json string
|
dataInputStream.readFully(in); //read json string
|
||||||
String json = new String(in);
|
String json = new String(in);
|
||||||
|
|
||||||
|
|
||||||
long now = System.currentTimeMillis();
|
long now = System.currentTimeMillis();
|
||||||
dataOutputStream.writeByte(0x09); //size of packet
|
dataOutputStream.writeByte(0x09); //size of packet
|
||||||
dataOutputStream.writeByte(0x01); //0x01 for ping
|
dataOutputStream.writeByte(0x01); //0x01 for ping
|
||||||
dataOutputStream.writeLong(now); //time!?
|
dataOutputStream.writeLong(now); //time!?
|
||||||
|
|
||||||
|
dataOutputStream.close(); // close request write stream
|
||||||
|
|
||||||
readVarInt(dataInputStream);
|
readVarInt(dataInputStream);
|
||||||
id = readVarInt(dataInputStream);
|
id = readVarInt(dataInputStream);
|
||||||
if (id == -1) {
|
if (id == -1) {
|
||||||
@ -112,10 +116,12 @@ public final class ServerListPing {
|
|||||||
long pingTime = dataInputStream.readLong(); //read response
|
long pingTime = dataInputStream.readLong(); //read response
|
||||||
StatusResponse response = gson.fromJson(json, StatusResponse.class);
|
StatusResponse response = gson.fromJson(json, StatusResponse.class);
|
||||||
response.time = (int) (now - pingTime);
|
response.time = (int) (now - pingTime);
|
||||||
|
|
||||||
|
dataInputStream.close(); // close response read stream
|
||||||
|
socket.close(); // close socket
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class StatusResponse {
|
public static class StatusResponse {
|
||||||
private BaseComponent description;
|
private BaseComponent description;
|
||||||
|
Loading…
Reference in New Issue
Block a user