Finished protocol

This commit is contained in:
UnlegitDqrk
2026-02-11 23:11:33 +01:00
parent 23a3293060
commit ae98225043
35 changed files with 339 additions and 2911 deletions

View File

@@ -8,6 +8,9 @@ import org.openautonomousconnection.protocol.versions.v1_0_0.beta.INSResponseSta
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;
/**
@@ -114,4 +117,50 @@ public abstract class OACPacket extends Packet {
*/
protected void onResponseCodeRead(DataInputStream inputStream, UUID clientID) {
}
/**
* Writes a string map in a deterministic way (no Java object serialization).
*
* @param out output stream
* @param map map to write (may be null)
* @throws IOException on I/O errors
*/
protected final void writeStringMap(DataOutputStream out, Map<String, String> map) throws IOException {
if (map == null || map.isEmpty()) {
out.writeInt(0);
return;
}
out.writeInt(map.size());
for (Map.Entry<String, String> e : map.entrySet()) {
// Null keys/values are normalized to empty strings to keep the wire format stable.
out.writeUTF((e.getKey() != null) ? e.getKey() : "");
out.writeUTF((e.getValue() != null) ? e.getValue() : "");
}
}
/**
* Reads a string map in a deterministic way (no Java object serialization).
*
* @param in input stream
* @return headers map (never null)
* @throws IOException on I/O errors / invalid sizes
*/
protected final Map<String, String> readStringMap(DataInputStream in) throws IOException {
int size = in.readInt();
if (size < 0) {
throw new IOException("Negative map size");
}
if (size == 0) {
return Collections.emptyMap();
}
Map<String, String> map = new LinkedHashMap<>(Math.max(16, size * 2));
for (int i = 0; i < size; i++) {
String key = in.readUTF();
String value = in.readUTF();
map.put(key, value);
}
return map;
}
}