Extend HPacket instead of creating new packet class

This commit is contained in:
UnfamiliarLegacy 2024-06-21 08:12:46 +02:00
parent ab263a6cfd
commit b1d5faef11
17 changed files with 146 additions and 135 deletions

View File

@ -74,7 +74,7 @@ public class HMessage implements StringifyAble {
this.isBlocked = message.isBlocked(); this.isBlocked = message.isBlocked();
this.index = message.getIndex(); this.index = message.getIndex();
this.direction = message.getDestination(); this.direction = message.getDestination();
this.hPacket = new HPacket(message.getPacket()); this.hPacket = message.getPacket().copy();
} }
@Override @Override

View File

@ -17,7 +17,7 @@ import java.util.Optional;
public class HPacket implements StringifyAble { public class HPacket implements StringifyAble {
private boolean isEdited = false; private boolean isEdited = false;
private byte[] packetInBytes; protected byte[] packetInBytes;
private int readIndex = 6; private int readIndex = 6;
// if identifier != null, this is a placeholder name for the type of packet, headerId will be "-1" // if identifier != null, this is a placeholder name for the type of packet, headerId will be "-1"
@ -754,5 +754,8 @@ public class HPacket implements StringifyAble {
HPacket packet2 = (HPacket) object; HPacket packet2 = (HPacket) object;
return Arrays.equals(packetInBytes, packet2.packetInBytes) && (isEdited == packet2.isEdited); return Arrays.equals(packetInBytes, packet2.packetInBytes) && (isEdited == packet2.isEdited);
} }
public HPacket copy() {
return new HPacket(this);
}
} }

View File

@ -1,11 +1,7 @@
package gearth.protocol; package gearth.protocol;
import gearth.protocol.format.shockwave.ShockMessage;
public interface TrafficListener { public interface TrafficListener {
void onCapture(HMessage message); void onCapture(HMessage message);
void onCapture(ShockMessage message);
} }

View File

@ -1,35 +0,0 @@
package gearth.protocol.format.shockwave;
import gearth.protocol.HMessage;
public class ShockMessage {
private final ShockPacket packet;
private final HMessage.Direction direction;
private final int index;
private boolean isBlocked;
public ShockMessage(ShockPacket packet, HMessage.Direction direction, int index) {
this.packet = packet;
this.direction = direction;
this.index = index;
this.isBlocked = false;
}
public ShockPacket getPacket() {
return packet;
}
public HMessage.Direction getDirection() {
return direction;
}
public int getIndex() {
return index;
}
public boolean isBlocked() {
return isBlocked;
}
}

View File

@ -1,15 +0,0 @@
package gearth.protocol.format.shockwave;
import gearth.protocol.HMessage;
public class ShockPacket {
public ShockPacket(HMessage.Direction direction, byte[] data) {
}
public void resetReadIndex() {
}
}

View File

@ -1,4 +0,0 @@
package gearth.protocol.format.shockwave;
public class ShockPacketIn {
}

View File

@ -1,4 +0,0 @@
package gearth.protocol.format.shockwave;
public class ShockPacketOut {
}

View File

@ -3,7 +3,6 @@ package gearth.protocol.packethandler;
import gearth.misc.listenerpattern.Observable; import gearth.misc.listenerpattern.Observable;
import gearth.protocol.HMessage; import gearth.protocol.HMessage;
import gearth.protocol.TrafficListener; import gearth.protocol.TrafficListener;
import gearth.protocol.format.shockwave.ShockMessage;
import gearth.services.extension_handler.ExtensionHandler; import gearth.services.extension_handler.ExtensionHandler;
import java.io.IOException; import java.io.IOException;
@ -33,14 +32,6 @@ public abstract class PacketHandler {
message.getPacket().resetReadIndex(); message.getPacket().resetReadIndex();
} }
protected void notifyListeners(int i, ShockMessage message) {
((Observable<TrafficListener>) trafficObservables[i]).fireEvent(trafficListener -> {
message.getPacket().resetReadIndex();
trafficListener.onCapture(message);
});
message.getPacket().resetReadIndex();
}
protected void awaitListeners(HMessage message, PacketSender packetSender) { protected void awaitListeners(HMessage message, PacketSender packetSender) {
notifyListeners(0, message); notifyListeners(0, message);
notifyListeners(1, message); notifyListeners(1, message);
@ -52,15 +43,4 @@ public abstract class PacketHandler {
}); });
} }
protected void awaitListeners(ShockMessage message, PacketSender packetSender) {
notifyListeners(0, message);
notifyListeners(1, message);
extensionHandler.handle(message, message2 -> {
notifyListeners(2, message2);
if (!message2.isBlocked()) {
packetSender.send(message2);
}
});
}
} }

View File

@ -1,8 +1,7 @@
package gearth.protocol.packethandler.shockwave; package gearth.protocol.packethandler.shockwave;
import gearth.protocol.HMessage; import gearth.protocol.HMessage;
import gearth.protocol.format.shockwave.ShockMessage; import gearth.protocol.HPacket;
import gearth.protocol.format.shockwave.ShockPacket;
import gearth.protocol.packethandler.PacketHandler; import gearth.protocol.packethandler.PacketHandler;
import gearth.protocol.packethandler.shockwave.buffers.ShockwaveBuffer; import gearth.protocol.packethandler.shockwave.buffers.ShockwaveBuffer;
import gearth.services.extension_handler.ExtensionHandler; import gearth.services.extension_handler.ExtensionHandler;
@ -14,13 +13,14 @@ import java.io.OutputStream;
public abstract class ShockwavePacketHandler extends PacketHandler { public abstract class ShockwavePacketHandler extends PacketHandler {
private static final Logger logger = LoggerFactory.getLogger(ShockwavePacketHandler.class); protected static final Logger logger = LoggerFactory.getLogger(ShockwavePacketHandler.class);
private final HMessage.Direction direction; private final HMessage.Direction direction;
private final ShockwaveBuffer payloadBuffer; private final ShockwaveBuffer payloadBuffer;
private final OutputStream outputStream;
private final Object flushLock; private final Object flushLock;
protected final OutputStream outputStream;
ShockwavePacketHandler(HMessage.Direction direction, ShockwaveBuffer payloadBuffer, OutputStream outputStream, ExtensionHandler extensionHandler, Object[] trafficObservables) { ShockwavePacketHandler(HMessage.Direction direction, ShockwaveBuffer payloadBuffer, OutputStream outputStream, ExtensionHandler extensionHandler, Object[] trafficObservables) {
super(extensionHandler, trafficObservables); super(extensionHandler, trafficObservables);
this.direction = direction; this.direction = direction;
@ -29,21 +29,10 @@ public abstract class ShockwavePacketHandler extends PacketHandler {
this.flushLock = new Object(); this.flushLock = new Object();
} }
@Override
public boolean sendToStream(byte[] buffer) {
synchronized (sendLock) {
try {
outputStream.write(buffer);
return true;
} catch (IOException e) {
logger.error("Error while sending packet to stream.", e);
return false;
}
}
}
@Override @Override
public void act(byte[] buffer) throws IOException { public void act(byte[] buffer) throws IOException {
logger.info("Direction {} Received {} bytes", this.direction, buffer.length);
payloadBuffer.push(buffer); payloadBuffer.push(buffer);
flush(); flush();
@ -51,10 +40,10 @@ public abstract class ShockwavePacketHandler extends PacketHandler {
public void flush() throws IOException { public void flush() throws IOException {
synchronized (flushLock) { synchronized (flushLock) {
final ShockPacket[] packets = payloadBuffer.receive(); final HPacket[] packets = payloadBuffer.receive();
for (final ShockPacket packet : packets){ for (final HPacket packet : packets){
final ShockMessage message = new ShockMessage(packet, direction, currentIndex); final HMessage message = new HMessage(packet, direction, currentIndex);
awaitListeners(message, x -> sendToStream(x.getPacket().toBytes())); awaitListeners(message, x -> sendToStream(x.getPacket().toBytes()));

View File

@ -4,10 +4,25 @@ import gearth.protocol.HMessage;
import gearth.protocol.packethandler.shockwave.buffers.ShockwaveInBuffer; import gearth.protocol.packethandler.shockwave.buffers.ShockwaveInBuffer;
import gearth.services.extension_handler.ExtensionHandler; import gearth.services.extension_handler.ExtensionHandler;
import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
public class ShockwavePacketIncomingHandler extends ShockwavePacketHandler { public class ShockwavePacketIncomingHandler extends ShockwavePacketHandler {
public ShockwavePacketIncomingHandler(OutputStream outputStream, ExtensionHandler extensionHandler, Object[] trafficObservables) { public ShockwavePacketIncomingHandler(OutputStream outputStream, ExtensionHandler extensionHandler, Object[] trafficObservables) {
super(HMessage.Direction.TOCLIENT, new ShockwaveInBuffer(), outputStream, extensionHandler, trafficObservables); super(HMessage.Direction.TOCLIENT, new ShockwaveInBuffer(), outputStream, extensionHandler, trafficObservables);
} }
@Override
public boolean sendToStream(byte[] buffer) {
synchronized (sendLock) {
try {
outputStream.write(buffer);
outputStream.write(new byte[] {0x01});
return true;
} catch (IOException e) {
logger.error("Error while sending packet to stream.", e);
return false;
}
}
}
} }

View File

@ -1,13 +1,31 @@
package gearth.protocol.packethandler.shockwave; package gearth.protocol.packethandler.shockwave;
import gearth.encoding.Base64Encoding;
import gearth.protocol.HMessage; import gearth.protocol.HMessage;
import gearth.protocol.packethandler.shockwave.buffers.ShockwaveOutBuffer; import gearth.protocol.packethandler.shockwave.buffers.ShockwaveOutBuffer;
import gearth.services.extension_handler.ExtensionHandler; import gearth.services.extension_handler.ExtensionHandler;
import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
public class ShockwavePacketOutgoingHandler extends ShockwavePacketHandler { public class ShockwavePacketOutgoingHandler extends ShockwavePacketHandler {
public ShockwavePacketOutgoingHandler(OutputStream outputStream, ExtensionHandler extensionHandler, Object[] trafficObservables) { public ShockwavePacketOutgoingHandler(OutputStream outputStream, ExtensionHandler extensionHandler, Object[] trafficObservables) {
super(HMessage.Direction.TOSERVER, new ShockwaveOutBuffer(), outputStream, extensionHandler, trafficObservables); super(HMessage.Direction.TOSERVER, new ShockwaveOutBuffer(), outputStream, extensionHandler, trafficObservables);
} }
@Override
public boolean sendToStream(byte[] buffer) {
synchronized (sendLock) {
try {
byte[] bufferLen = Base64Encoding.encode(buffer.length, 3);
outputStream.write(bufferLen);
outputStream.write(buffer);
return true;
} catch (IOException e) {
logger.error("Error while sending packet to stream.", e);
return false;
}
}
}
} }

View File

@ -1,11 +1,11 @@
package gearth.protocol.packethandler.shockwave.buffers; package gearth.protocol.packethandler.shockwave.buffers;
import gearth.protocol.format.shockwave.ShockPacket; import gearth.protocol.HPacket;
public interface ShockwaveBuffer { public interface ShockwaveBuffer {
void push(byte[] data); void push(byte[] data);
ShockPacket[] receive(); HPacket[] receive();
} }

View File

@ -1,7 +1,8 @@
package gearth.protocol.packethandler.shockwave.buffers; package gearth.protocol.packethandler.shockwave.buffers;
import gearth.protocol.format.shockwave.ShockPacket; import gearth.protocol.HPacket;
import gearth.protocol.packethandler.ByteArrayUtils; import gearth.protocol.packethandler.ByteArrayUtils;
import gearth.protocol.packethandler.shockwave.packets.ShockPacket;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -16,7 +17,7 @@ public class ShockwaveInBuffer implements ShockwaveBuffer {
} }
@Override @Override
public ShockPacket[] receive() { public HPacket[] receive() {
if (buffer.length < 3) { if (buffer.length < 3) {
return new ShockPacket[0]; return new ShockPacket[0];
} }

View File

@ -1,13 +1,20 @@
package gearth.protocol.packethandler.shockwave.buffers; package gearth.protocol.packethandler.shockwave.buffers;
import gearth.encoding.Base64Encoding;
import gearth.protocol.HPacket; import gearth.protocol.HPacket;
import gearth.protocol.format.shockwave.ShockPacket;
import gearth.protocol.packethandler.ByteArrayUtils; import gearth.protocol.packethandler.ByteArrayUtils;
import gearth.protocol.packethandler.shockwave.packets.ShockPacket;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
public class ShockwaveOutBuffer implements ShockwaveBuffer { public class ShockwaveOutBuffer implements ShockwaveBuffer {
private static final int PACKET_LENGTH_SIZE = 3;
private static final int PACKET_HEADER_SIZE = 2;
private static final int PACKET_SIZE_MIN = PACKET_LENGTH_SIZE + PACKET_HEADER_SIZE;
private byte[] buffer = new byte[0]; private byte[] buffer = new byte[0];
@Override @Override
@ -16,13 +23,27 @@ public class ShockwaveOutBuffer implements ShockwaveBuffer {
} }
@Override @Override
public ShockPacket[] receive() { public HPacket[] receive() {
if (buffer.length < 5) { if (buffer.length < PACKET_SIZE_MIN) {
return new ShockPacket[0]; return new ShockPacket[0];
} }
ArrayList<HPacket> all = new ArrayList<>(); ArrayList<ShockPacket> out = new ArrayList<>();
return all.toArray(new ShockPacket[all.size()]); while (buffer.length >= PACKET_SIZE_MIN) {
int length = Base64Encoding.decode(new byte[]{buffer[0], buffer[1], buffer[2]});
if (buffer.length < length + PACKET_LENGTH_SIZE) {
break;
}
int endPos = length + PACKET_LENGTH_SIZE;
byte[] packet = Arrays.copyOfRange(buffer, PACKET_LENGTH_SIZE, endPos);
out.add(new ShockPacket(packet));
buffer = Arrays.copyOfRange(buffer, endPos, buffer.length);
}
return out.toArray(new ShockPacket[0]);
} }
} }

View File

@ -0,0 +1,60 @@
package gearth.protocol.packethandler.shockwave.packets;
import gearth.encoding.Base64Encoding;
import gearth.protocol.HMessage;
import gearth.protocol.HPacket;
import java.nio.charset.StandardCharsets;
import java.security.InvalidParameterException;
public class ShockPacket extends HPacket {
public ShockPacket(byte[] packet) {
super(packet);
}
public ShockPacket(HPacket packet) {
super(packet);
}
public ShockPacket(String packet) {
super(packet);
}
public ShockPacket(int header) {
super(header);
}
public ShockPacket(int header, byte[] bytes) {
super(header, bytes);
}
public ShockPacket(int header, Object... objects) throws InvalidParameterException {
super(header, objects);
}
public ShockPacket(String identifier, HMessage.Direction direction) throws InvalidParameterException {
super(identifier, direction);
}
public ShockPacket(String identifier, HMessage.Direction direction, Object... objects) throws InvalidParameterException {
super(identifier, direction, objects);
}
@Override
public int headerId() {
final String header = new String(this.readBytes(2, 0), StandardCharsets.ISO_8859_1);
final int headerId = Base64Encoding.decode(header.getBytes());
return headerId;
}
@Override
public int length() {
return this.packetInBytes.length;
}
@Override
public HPacket copy() {
return new ShockPacket(this);
}
}

View File

@ -0,0 +1,7 @@
package gearth.protocol.packethandler.shockwave.packets;
public class ShockPacketUnsupported extends UnsupportedOperationException {
public ShockPacketUnsupported(String message) {
super(message);
}
}

View File

@ -7,7 +7,6 @@ import gearth.protocol.HConnection;
import gearth.protocol.HMessage; import gearth.protocol.HMessage;
import gearth.protocol.HPacket; import gearth.protocol.HPacket;
import gearth.protocol.connection.HState; import gearth.protocol.connection.HState;
import gearth.protocol.format.shockwave.ShockMessage;
import gearth.services.extension_handler.extensions.ExtensionListener; import gearth.services.extension_handler.extensions.ExtensionListener;
import gearth.services.extension_handler.extensions.GEarthExtension; import gearth.services.extension_handler.extensions.GEarthExtension;
import gearth.services.extension_handler.extensions.extensionproducers.ExtensionProducer; import gearth.services.extension_handler.extensions.extensionproducers.ExtensionProducer;
@ -182,26 +181,6 @@ public class ExtensionHandler {
maybeFinishHmessage(hMessage); maybeFinishHmessage(hMessage);
} }
public void handle(ShockMessage hMessage, OnHMessageHandled callback) {
synchronized (hMessageStuffLock) {
Pair<HMessage.Direction, Integer> msgDirectionAndId = new Pair<>(hMessage.getDirection(), hMessage.getIndex());
originalMessages.put(msgDirectionAndId, hMessage);
finishManipulationCallback.put(hMessage, callback);
editedMessages.put(hMessage, null);
allAwaitingMessages.add(hMessage);
synchronized (gEarthExtensions) {
awaitManipulation.put(hMessage, new HashSet<>(gEarthExtensions));
for (GEarthExtension extension : gEarthExtensions) {
extension.packetIntercept(new HMessage(hMessage));
}
}
}
maybeFinishHmessage(hMessage);
}
private ExtensionProducerObserver createExtensionProducerObserver() { private ExtensionProducerObserver createExtensionProducerObserver() {
return new ExtensionProducerObserver() { return new ExtensionProducerObserver() {
@Override @Override