proxy progress

This commit is contained in:
rusefi 2020-07-17 00:54:38 -04:00
parent 0d29842418
commit 8d208d0da1
2 changed files with 21 additions and 9 deletions

View File

@ -6,6 +6,8 @@ import com.opensr5.io.WriteStream;
import com.rusefi.binaryprotocol.BinaryProtocol;
import com.rusefi.binaryprotocol.IncomingDataBuffer;
import com.rusefi.binaryprotocol.IoHelper;
import com.rusefi.io.tcp.BinaryProtocolServer;
import org.jetbrains.annotations.NotNull;
import java.io.EOFException;
import java.io.IOException;
@ -31,6 +33,18 @@ public interface IoStream extends WriteStream {
return r.toString();
}
@NotNull
default BinaryProtocolServer.Packet readPacket() throws IOException {
short length = readShort();
return BinaryProtocolServer.readPromisedBytes(getDataBuffer(), length);
}
default void sendPacket(BinaryProtocolServer.Packet packet) throws IOException {
writeShort(packet.getPacket().length);
write(packet.getPacket());
writeInt(packet.getCrc());
}
default void sendPacket(byte[] plainPacket, Logger logger) throws IOException {
byte[] packet;
if (BinaryProtocol.PLAIN_PROTOCOL) {

View File

@ -52,12 +52,12 @@ public class BinaryProtocolProxy {
}
}
private static void proxyControllerResponseToClient(IoStream targetInputStream, DataOutputStream clientOutputStream) throws IOException {
short length = targetInputStream.readShort();
BinaryProtocolServer.Packet packet = BinaryProtocolServer.readPromisedBytes(targetInputStream.getDataBuffer(), length);
public static void proxyControllerResponseToClient(IoStream targetInputStream, DataOutputStream clientOutputStream) throws IOException {
BinaryProtocolServer.Packet packet = targetInputStream.readPacket();
System.out.println("Relaying controller response length=" + length);
clientOutputStream.writeShort(length);
System.out.println("Relaying controller response length=" + packet.getPacket().length);
// todo: replace with IoStream#sendPacket?
clientOutputStream.writeShort(packet.getPacket().length);
clientOutputStream.write(packet.getPacket());
clientOutputStream.writeInt(packet.getCrc());
clientOutputStream.flush();
@ -73,9 +73,7 @@ public class BinaryProtocolProxy {
byte command = (byte) dis.read();
System.out.println("Relaying client command " + BinaryProtocol.findCommand(command));
// sending proxies packet to controller
targetOutputStream.writeShort(length);
targetOutputStream.write(packet.getPacket());
targetOutputStream.writeInt(packet.getCrc());
// sending proxied packet to controller
targetOutputStream.sendPacket(packet);
}
}