Full Optimized – High Speed #4818

refactoring - nicer API
This commit is contained in:
rusefillc 2022-11-23 21:03:21 -05:00
parent 9ebd94d486
commit 8386fb1052
4 changed files with 67 additions and 8 deletions

View File

@ -0,0 +1,40 @@
package com.rusefi.io.commands;
import java.io.DataInputStream;
import java.io.IOException;
import static com.rusefi.binaryprotocol.IoHelper.swap16;
public class ByteRange {
private final int offset;
private final int count;
private ByteRange(int offset, int count) {
this.offset = offset;
this.count = count;
}
public int getOffset() {
return offset;
}
public int getCount() {
return count;
}
@Override
public String toString() {
return "{" +
"offset=" + offset +
", count=" + count +
'}';
}
public static ByteRange valueOf(byte[] payload) throws IOException {
try (DataInputStream dis = WriteChunkCommand.createPayLoadStream(payload)) {
int offset = swap16(dis.readShort());
int count = swap16(dis.readShort());
return new ByteRange(offset, count);
}
}
}

View File

@ -0,0 +1,13 @@
package com.rusefi.io.commands;
import org.jetbrains.annotations.NotNull;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
public class WriteChunkCommand {
@NotNull
public static DataInputStream createPayLoadStream(byte[] payload) {
return new DataInputStream(new ByteArrayInputStream(payload, 1, payload.length - 1));
}
}

View File

@ -13,7 +13,9 @@ import com.rusefi.binaryprotocol.IoHelper;
import com.rusefi.config.generated.Fields;
import com.rusefi.io.IoStream;
import com.rusefi.io.LinkManager;
import com.rusefi.io.commands.ByteRange;
import com.rusefi.io.commands.HelloCommand;
import com.rusefi.io.commands.WriteChunkCommand;
import com.rusefi.server.rusEFISSLContext;
import com.rusefi.ui.StatusConsumer;
import org.jetbrains.annotations.NotNull;
@ -170,10 +172,10 @@ public class BinaryProtocolServer {
} else if (command == Fields.TS_PAGE_COMMAND) {
stream.sendPacket(TS_OK.getBytes());
} else if (command == Fields.TS_READ_COMMAND) {
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(payload, 1, payload.length - 1));
DataInputStream dis = WriteChunkCommand.createPayLoadStream(payload);
handleRead(linkManager, dis, stream);
} else if (command == Fields.TS_CHUNK_WRITE_COMMAND) {
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(payload, 1, payload.length - 1));
DataInputStream dis = WriteChunkCommand.createPayLoadStream(payload);
handleWrite(linkManager, payload, dis, stream);
} else if (command == Fields.TS_BURN_COMMAND) {
stream.sendPacket(new byte[]{TS_RESPONSE_BURN_OK});
@ -201,18 +203,16 @@ public class BinaryProtocolServer {
@NotNull
public static byte[] getOutputCommandResponse(byte[] payload, byte[] currentOutputs) throws IOException {
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(payload, 1, payload.length - 1));
int offset = swap16(dis.readShort());
int count = swap16(dis.readShort());
ByteRange byteRange = ByteRange.valueOf(payload);
if (log.debugEnabled())
log.debug("TS_OUTPUT_COMMAND offset=" + offset + "/count=" + count);
log.debug("TS_OUTPUT_COMMAND offset=" + byteRange);
byte[] response = new byte[1 + count];
byte[] response = new byte[1 + byteRange.getCount()];
response[0] = (byte) TS_OK.charAt(0);
if (MOCK_SD_CARD)
currentOutputs[SD_STATUS_OFFSET] = 1 + 4;
if (currentOutputs != null)
System.arraycopy(currentOutputs, offset, response, 1, count);
System.arraycopy(currentOutputs, byteRange.getOffset(), response, 1, byteRange.getCount());
return response;
}

View File

@ -122,6 +122,12 @@ public class TcpServerSandbox {
} else if (command == Fields.TS_OUTPUT_COMMAND) {
byte[] response = getOutputCommandResponse(payload, ecuState.outputs);
stream.sendPacket(response);
} else if (command == Fields.TS_GET_SCATTERED_GET_COMMAND) {
System.out.println("Cool TS_GET_SCATTERED_GET_COMMAND");
} else if (command == Fields.TS_CHUNK_WRITE_COMMAND) {
stream.sendPacket(TS_OK.getBytes());
} else if (command == Fields.TS_BURN_COMMAND) {
stream.sendPacket(new byte[]{Fields.TS_RESPONSE_BURN_OK});
} else if (command == Fields.TS_READ_COMMAND) {
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(payload, 1, payload.length - 1));
int offset = swap16(dis.readShort());