Ignore EOFException too. try-catch + auto-closeable

This commit is contained in:
Dereku 2024-09-04 10:21:01 +07:00
parent 7608e43669
commit 6729de5894

View file

@ -55,12 +55,11 @@ public class ServerThread extends Thread {
public void run() { public void run() {
this.working.set(true); this.working.set(true);
while (this.working.get() && !this.serverSocket.isClosed()) { while (this.working.get() && !this.serverSocket.isClosed()) {
try { try (final Socket socket = this.serverSocket.accept();
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 DataInputStream inputStream = new DataInputStream(socket.getInputStream()); final DataInputStream inputStream = new DataInputStream(socket.getInputStream());
final DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream()); final DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream())) {
socket.setTrafficClass(SocketOptions.TCP_NODELAY & SocketOptions.IP_TOS);
final int packetLength = inputStream.readByte() | inputStream.readByte() << 1; // idk why it doesn't a VarInt final int packetLength = inputStream.readByte() | inputStream.readByte() << 1; // idk why it doesn't a VarInt
final int protocolVersion = this.readVarInt(inputStream); final int protocolVersion = this.readVarInt(inputStream);
final String hostname = this.readUtf(inputStream); final String hostname = this.readUtf(inputStream);
@ -76,15 +75,10 @@ public class ServerThread extends Thread {
outputStream.flush(); outputStream.flush();
inputStream.readShort(); // legacy data inputStream.readShort(); // legacy data
this.writeVarInt(outputStream, this.readVarInt(inputStream)); // Packet length (9) this.writeVarInt(outputStream, this.readVarInt(inputStream)); // Packet length (9)
this.writeVarInt(outputStream, this.readVarInt(inputStream)); // Packet id (1) this.writeVarInt(outputStream, this.readVarInt(inputStream)); // Packet id (1)
outputStream.writeLong(inputStream.readLong()); // timestamp outputStream.writeLong(inputStream.readLong()); // timestamp
} catch (SocketTimeoutException | EOFException ignore) {
inputStream.close();
outputStream.close();
socket.close();
} catch (SocketTimeoutException ignore) {
} catch (IOException ex) { } catch (IOException ex) {
System.out.println("Exception caught: " + ex); System.out.println("Exception caught: " + ex);
} }