prepare for racv2 final update (I hate PID collisions)

This commit is contained in:
pixtaded 2025-02-11 20:38:58 +03:00
parent 933af5c5e0
commit 12e595bb73
3 changed files with 16 additions and 14 deletions

View File

@ -99,7 +99,7 @@ public class CrabClient implements Crab {
private void sendPacket(byte PID, String argument, boolean receiveResponse) throws IOException { private void sendPacket(byte PID, String argument, boolean receiveResponse) throws IOException {
if (socket == null || socket.isClosed()) connect(); if (socket == null || socket.isClosed()) connect();
String formattedMessage = String.valueOf((char) PID) + argument; String formattedMessage = (char) PID + argument;
out.print(formattedMessage); out.print(formattedMessage);
out.flush(); out.flush();
@ -113,7 +113,7 @@ public class CrabClient implements Crab {
} }
private void sendMessage(String msg) throws IOException { private void sendMessage(String msg) throws IOException {
sendPacket(COMMUNICATION, this.nickname + msg, false); sendPacket(MESSAGE, this.nickname + msg, false);
closeConnection(); closeConnection();
} }
@ -122,10 +122,10 @@ public class CrabClient implements Crab {
case LOGS_SIZE -> { case LOGS_SIZE -> {
String convertedString = Util.readAsciiNumber(in); String convertedString = Util.readAsciiNumber(in);
if (!convertedString.isEmpty()) lastBufferLength = Integer.parseInt(convertedString); if (!convertedString.isEmpty()) lastBufferLength = Integer.parseInt(convertedString);
} case LOGS -> { } case CACHED_LOGS -> {
byte[] bytes = socket.getInputStream().readNBytes(lastBufferLength - cache.sizeInBytes()); byte[] bytes = socket.getInputStream().readNBytes(lastBufferLength - cache.sizeInBytes());
cache = new Logs(lastBufferLength, cache.content() + new String(bytes, StandardCharsets.UTF_8)); cache = new Logs(lastBufferLength, cache.content() + new String(bytes, StandardCharsets.UTF_8));
} case COMMUNICATION -> { } case LOGS -> {
byte[] bytes = socket.getInputStream().readNBytes(lastBufferLength); byte[] bytes = socket.getInputStream().readNBytes(lastBufferLength);
cache = new Logs(lastBufferLength, new String(bytes, StandardCharsets.UTF_8)); cache = new Logs(lastBufferLength, new String(bytes, StandardCharsets.UTF_8));
} default -> { } default -> {
@ -146,9 +146,9 @@ public class CrabClient implements Crab {
private void getLogs() throws IOException { private void getLogs() throws IOException {
sendPacket(LOGS_SIZE, "", true); sendPacket(LOGS_SIZE, "", true);
if (this.cache.sizeInBytes() < lastBufferLength) { if (this.cache.sizeInBytes() < lastBufferLength) {
sendPacket(LOGS, String.valueOf(cache.sizeInBytes()), true); sendPacket(CACHED_LOGS, String.valueOf(cache.sizeInBytes()), true);
} else if (this.cache.sizeInBytes() != lastBufferLength) { } else if (this.cache.sizeInBytes() != lastBufferLength) {
sendPacket(COMMUNICATION, "", true); sendPacket(LOGS, "", true);
} }
closeConnection(); closeConnection();
printLogs(); printLogs();

View File

@ -2,6 +2,8 @@ package net.pixtaded.crab.common;
public class PID { public class PID {
public static final byte LOGS_SIZE = 0x00; public static final byte LOGS_SIZE = 0x00;
public static final byte COMMUNICATION = 0x01; public static final byte LOGS = 0x01;
public static final byte LOGS = 0x02; public static final byte MESSAGE = 0x01;
public static final byte CACHED_LOGS = 0x02;
public static final byte AUTHENTICATED_MESSAGE = 0x02;
} }

View File

@ -14,9 +14,9 @@ import static net.pixtaded.crab.common.PID.*;
public class ServerThread implements Runnable { public class ServerThread implements Runnable {
private final Socket socket; private final Socket socket;
private PrintWriter out; private final PrintWriter out;
private final BufferedReader in; private final BufferedReader in;
private OutputStream output; private final OutputStream output;
private final InputStream input; private final InputStream input;
private final CrabServer server; private final CrabServer server;
@ -38,7 +38,7 @@ public class ServerThread implements Runnable {
return; return;
} }
switch (PID[0]) { switch (PID[0]) {
case COMMUNICATION -> { case MESSAGE -> {
String msg = new String(input.readNBytes(4096), StandardCharsets.UTF_8).trim(); String msg = new String(input.readNBytes(4096), StandardCharsets.UTF_8).trim();
Date date = new Date(); Date date = new Date();
String address = socket.getInetAddress().getHostAddress(); String address = socket.getInetAddress().getHostAddress();
@ -69,9 +69,9 @@ public class ServerThread implements Runnable {
} }
private void sendLogs(byte PID) throws IOException { private void sendLogs(byte PID) throws IOException {
if (PID == COMMUNICATION) { if (PID == LOGS) {
respond(server.cache.content()); respond(server.cache.content());
} else if (PID == LOGS) { } else if (PID == CACHED_LOGS) {
String clientSize = Util.readAsciiNumber(in); String clientSize = Util.readAsciiNumber(in);
int clientSizeNum = Integer.parseInt(clientSize); int clientSizeNum = Integer.parseInt(clientSize);
byte[] serverLogs = server.cache.content().getBytes(StandardCharsets.UTF_8); byte[] serverLogs = server.cache.content().getBytes(StandardCharsets.UTF_8);