1.2. Просто проверки на работу.
This commit is contained in:
parent
520e143a37
commit
34b6a5061d
2 changed files with 36 additions and 12 deletions
|
|
@ -27,13 +27,27 @@ public class ServerThread extends Thread {
|
|||
this.serverSocket.setPerformancePreferences(0, 2, 1);
|
||||
}
|
||||
|
||||
public ServerThread(final String host, int port) throws IOException {
|
||||
this.minecraftServer = null;
|
||||
this.setName("LegacyPing thread listening " + host + ":" + port);
|
||||
final InetAddress address = InetAddress.getByName(host);
|
||||
this.serverSocket = new ServerSocket(port, 10, address);
|
||||
this.serverSocket.setSoTimeout((int) TimeUnit.SECONDS.toMillis(10L));
|
||||
this.serverSocket.setPerformancePreferences(0, 2, 1);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
new ServerThread("", 25565).start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
this.working.set(true);
|
||||
while (this.working.get() || !this.serverSocket.isClosed()) {
|
||||
while (this.working.get() && !this.serverSocket.isClosed()) {
|
||||
try {
|
||||
final Socket socket = this.serverSocket.accept();
|
||||
socket.setTrafficClass(SocketOptions.TCP_NODELAY & SocketOptions.IP_TOS);
|
||||
System.out.println("[LegacyPing] Got new ping from " + socket.getRemoteSocketAddress());
|
||||
final InputStream inputStream = socket.getInputStream();
|
||||
final OutputStream outputStream = socket.getOutputStream();
|
||||
final int firstByte = inputStream.read();
|
||||
|
|
@ -49,7 +63,9 @@ public class ServerThread extends Thread {
|
|||
}
|
||||
outputStream.flush();
|
||||
socket.close();
|
||||
} catch (IOException ignore) {
|
||||
} catch (SocketTimeoutException ignore) {
|
||||
} catch (IOException ex) {
|
||||
System.out.println("Exception caught: " + ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -66,9 +82,7 @@ public class ServerThread extends Thread {
|
|||
final ByteArrayOutputStream result = new ByteArrayOutputStream();
|
||||
final DataOutputStream dos = new DataOutputStream(result);
|
||||
dos.write(255); // Packet 0xFF (PacketDisconnect)
|
||||
final String response = ((CraftServer) this.minecraftServer).getServer().motd
|
||||
+ '\u0000' + this.minecraftServer.getOnlinePlayers().length
|
||||
+ '\u0000' + this.minecraftServer.getMaxPlayers();
|
||||
final String response = this.getMotd() + '\u0000' + this.getOnlinePlayers() + '\u0000' + this.getMaxPlayers();
|
||||
dos.writeShort(response.length());
|
||||
for (char c : response.toCharArray()) {
|
||||
dos.writeChar(c);
|
||||
|
|
@ -83,9 +97,7 @@ public class ServerThread extends Thread {
|
|||
private void responseForLegacyPing(final OutputStream outputStream) throws IOException {
|
||||
final DataOutputStream clientOutput = new DataOutputStream(outputStream);
|
||||
clientOutput.write(255);
|
||||
final String response = ((CraftServer) this.minecraftServer).getServer().motd
|
||||
+ '\u00a7' + this.minecraftServer.getOnlinePlayers().length
|
||||
+ '\u00a7' + this.minecraftServer.getMaxPlayers();
|
||||
final String response = this.getMotd() + '\u00a7' + this.getOnlinePlayers() + '\u00a7' + this.getMaxPlayers();
|
||||
clientOutput.writeShort(response.length());
|
||||
for (char c : response.toCharArray()) {
|
||||
clientOutput.writeChar(c);
|
||||
|
|
@ -94,9 +106,9 @@ public class ServerThread extends Thread {
|
|||
}
|
||||
|
||||
private void responseForModernPing(final OutputStream outputStream) throws IOException {
|
||||
final String responseJson = MODERN_RESPONSE.replace("%max%", String.valueOf(this.minecraftServer.getMaxPlayers()))
|
||||
.replace("%now%", String.valueOf(this.minecraftServer.getOnlinePlayers().length))
|
||||
.replace("%motd%", ((CraftServer) this.minecraftServer).getServer().motd);
|
||||
final String responseJson = MODERN_RESPONSE.replace("%max%", String.valueOf(this.getMaxPlayers()))
|
||||
.replace("%now%", String.valueOf(this.getOnlinePlayers()))
|
||||
.replace("%motd%", this.getMotd());
|
||||
byte[] bytes = responseJson.getBytes();
|
||||
final DataOutputStream clientOutput = new DataOutputStream(outputStream);
|
||||
this.writeVarInt(clientOutput, bytes.length + 3);
|
||||
|
|
@ -106,6 +118,18 @@ public class ServerThread extends Thread {
|
|||
// Packet data end
|
||||
}
|
||||
|
||||
private String getMotd() {
|
||||
return this.minecraftServer == null ? "This is an example of the Message of the Day" : ((CraftServer) this.minecraftServer).getServer().motd;
|
||||
}
|
||||
|
||||
private int getOnlinePlayers() {
|
||||
return this.minecraftServer == null ? 13 : this.minecraftServer.getOnlinePlayers().length;
|
||||
}
|
||||
|
||||
private int getMaxPlayers() {
|
||||
return this.minecraftServer == null ? 37 : this.minecraftServer.getMaxPlayers();
|
||||
}
|
||||
|
||||
private void writeVarInt(DataOutputStream out, int paramInt) throws IOException {
|
||||
while (true) {
|
||||
if ((paramInt & 0xFFFFFF80) == 0) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue