proxy progress

This commit is contained in:
rusefi 2020-07-09 23:07:49 -04:00
parent d84bf62c3b
commit 9dcd952f0e
3 changed files with 16 additions and 17 deletions

View File

@ -20,9 +20,9 @@ public interface WriteStream {
byte[] array = new byte[4]; byte[] array = new byte[4];
array[0] = (byte) ((v >>> 24) & 0xFF); array[0] = (byte) ((v >>> 24) & 0xFF);
array[0] = (byte) ((v >>> 16) & 0xFF); array[1] = (byte) ((v >>> 16) & 0xFF);
array[0] = (byte) ((v >>> 8) & 0xFF); array[2] = (byte) ((v >>> 8) & 0xFF);
array[0] = (byte) ((v >>> 0) & 0xFF); array[3] = (byte) ((v >>> 0) & 0xFF);
write(array); write(array);
} }
} }

View File

@ -1,6 +1,7 @@
package com.rusefi.io.tcp; package com.rusefi.io.tcp;
import com.opensr5.Logger; import com.opensr5.Logger;
import com.rusefi.io.IoStream;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.DataInputStream; import java.io.DataInputStream;
@ -12,7 +13,7 @@ import java.util.function.Function;
import static com.rusefi.binaryprotocol.BinaryProtocolCommands.COMMAND_PROTOCOL; import static com.rusefi.binaryprotocol.BinaryProtocolCommands.COMMAND_PROTOCOL;
public class BinaryProtocolProxy { public class BinaryProtocolProxy {
public static void createProxy(Socket targetEcuSocket, int serverProxyPort) { public static void createProxy(IoStream targetEcuSocket, int serverProxyPort) {
Function<Socket, Runnable> clientSocketRunnableFactory = new Function<Socket, Runnable>() { Function<Socket, Runnable> clientSocketRunnableFactory = new Function<Socket, Runnable>() {
@Override @Override
public Runnable apply(Socket clientSocket) { public Runnable apply(Socket clientSocket) {
@ -27,14 +28,11 @@ public class BinaryProtocolProxy {
BinaryProtocolServer.tcpServerSocket(serverProxyPort, "proxy", clientSocketRunnableFactory, Logger.CONSOLE, null); BinaryProtocolServer.tcpServerSocket(serverProxyPort, "proxy", clientSocketRunnableFactory, Logger.CONSOLE, null);
} }
private static void runProxy(Socket targetEcuSocket, Socket clientSocket) { private static void runProxy(IoStream targetEcu, Socket clientSocket) {
/* /*
* Each client socket is running on it's own thread * Each client socket is running on it's own thread
*/ */
try { try {
DataInputStream targetInputStream = new DataInputStream(targetEcuSocket.getInputStream());
DataOutputStream targetOutputStream = new DataOutputStream(targetEcuSocket.getOutputStream());
DataInputStream clientInputStream = new DataInputStream(clientSocket.getInputStream()); DataInputStream clientInputStream = new DataInputStream(clientSocket.getInputStream());
DataOutputStream clientOutputStream = new DataOutputStream(clientSocket.getOutputStream()); DataOutputStream clientOutputStream = new DataOutputStream(clientSocket.getOutputStream());
@ -44,18 +42,18 @@ public class BinaryProtocolProxy {
BinaryProtocolServer.handleProtocolCommand(clientSocket); BinaryProtocolServer.handleProtocolCommand(clientSocket);
continue; continue;
} }
proxyClientRequestToController(clientInputStream, firstByte, targetOutputStream); proxyClientRequestToController(clientInputStream, firstByte, targetEcu);
proxyControllerResponseToClient(targetInputStream, clientOutputStream); proxyControllerResponseToClient(targetEcu, clientOutputStream);
} }
} catch (IOException e) { } catch (IOException | InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
private static void proxyControllerResponseToClient(DataInputStream targetInputStream, DataOutputStream clientOutputStream) throws IOException { private static void proxyControllerResponseToClient(IoStream targetInputStream, DataOutputStream clientOutputStream) throws IOException, InterruptedException {
short length = targetInputStream.readShort(); short length = targetInputStream.readShort();
BinaryProtocolServer.Packet packet = BinaryProtocolServer.readPromisedBytes(targetInputStream, length); BinaryProtocolServer.Packet packet = BinaryProtocolServer.readPromisedBytes(targetInputStream.getDataBuffer(), length);
System.out.println("Relaying controller response length=" + length); System.out.println("Relaying controller response length=" + length);
clientOutputStream.writeShort(length); clientOutputStream.writeShort(length);
@ -64,7 +62,7 @@ public class BinaryProtocolProxy {
clientOutputStream.flush(); clientOutputStream.flush();
} }
private static void proxyClientRequestToController(DataInputStream in, byte firstByte, DataOutputStream targetOutputStream) throws IOException { private static void proxyClientRequestToController(DataInputStream in, byte firstByte, IoStream targetOutputStream) throws IOException {
byte secondByte = in.readByte(); byte secondByte = in.readByte();
int length = firstByte * 256 + secondByte; int length = firstByte * 256 + secondByte;
@ -79,6 +77,5 @@ public class BinaryProtocolProxy {
targetOutputStream.write(secondByte); targetOutputStream.write(secondByte);
targetOutputStream.write(packet.getPacket()); targetOutputStream.write(packet.getPacket());
targetOutputStream.writeInt(packet.getCrc()); targetOutputStream.writeInt(packet.getCrc());
targetOutputStream.flush();
} }
} }

View File

@ -10,6 +10,7 @@ import com.rusefi.config.Field;
import com.rusefi.config.generated.Fields; import com.rusefi.config.generated.Fields;
import com.rusefi.io.tcp.BinaryProtocolProxy; import com.rusefi.io.tcp.BinaryProtocolProxy;
import com.rusefi.io.tcp.BinaryProtocolServer; import com.rusefi.io.tcp.BinaryProtocolServer;
import com.rusefi.io.tcp.TcpIoStream;
import com.rusefi.tune.xml.Constant; import com.rusefi.tune.xml.Constant;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.junit.Test; import org.junit.Test;
@ -101,9 +102,10 @@ public class TcpCommunicationIntegrationTest {
assertTrue(serverCreated.await(30, TimeUnit.SECONDS)); assertTrue(serverCreated.await(30, TimeUnit.SECONDS));
int proxyPort = 6103; int proxyPort = 6103;
Socket targetEcuSocket;
IoStream targetEcuSocket;
try { try {
targetEcuSocket = new Socket(LOCALHOST, controllerPort); targetEcuSocket = new TcpIoStream(LOGGER, new Socket(LOCALHOST, controllerPort));
} catch (IOException e) { } catch (IOException e) {
throw new IllegalStateException("Failed to connect to controller " + LOCALHOST + ":" + controllerPort); throw new IllegalStateException("Failed to connect to controller " + LOCALHOST + ":" + controllerPort);
} }