From e593ec6a7cdf91aaa4fdf289ff6ec42c709cd905 Mon Sep 17 00:00:00 2001 From: rusefi Date: Wed, 14 Aug 2019 21:46:00 -0400 Subject: [PATCH] refactoring --- .../rusefi/binaryprotocol/BinaryProtocol.java | 29 +--------------- .../binaryprotocol/IncomingDataBuffer.java | 34 +++++++++++++++++++ 2 files changed, 35 insertions(+), 28 deletions(-) diff --git a/java_console/io/src/com/rusefi/binaryprotocol/BinaryProtocol.java b/java_console/io/src/com/rusefi/binaryprotocol/BinaryProtocol.java index 74930242b6..3fc979962e 100644 --- a/java_console/io/src/com/rusefi/binaryprotocol/BinaryProtocol.java +++ b/java_console/io/src/com/rusefi/binaryprotocol/BinaryProtocol.java @@ -197,34 +197,7 @@ public class BinaryProtocol implements BinaryProtocolCommands { private byte[] receivePacket(String msg, boolean allowLongResponse) throws InterruptedException, EOFException { long start = System.currentTimeMillis(); synchronized (ioLock) { - boolean isTimeout = incomingData.waitForBytes(msg + " header", start, 2); - if (isTimeout) - return null; - - int packetSize = swap16(incomingData.getShort()); - logger.trace("Got packet size " + packetSize); - if (packetSize < 0) - return null; - if (!allowLongResponse && packetSize > Math.max(BLOCKING_FACTOR, Fields.TS_OUTPUT_SIZE) + 10) - return null; - - isTimeout = incomingData.waitForBytes(msg + " body", start, packetSize + 4); - if (isTimeout) - return null; - - byte[] packet = new byte[packetSize]; - incomingData.getData(packet); - int packetCrc = swap32(incomingData.getInt()); - int actualCrc = getCrc32(packet); - - boolean isCrcOk = actualCrc == packetCrc; - if (!isCrcOk) { - logger.trace(String.format("%x", actualCrc) + " vs " + String.format("%x", packetCrc)); - return null; - } - logger.trace("packet " + Arrays.toString(packet) + ": crc OK"); - - return packet; + return incomingData.getPacket(logger, msg, allowLongResponse, start); } } diff --git a/java_console/io/src/com/rusefi/binaryprotocol/IncomingDataBuffer.java b/java_console/io/src/com/rusefi/binaryprotocol/IncomingDataBuffer.java index 360fcf600c..795eb6dd37 100644 --- a/java_console/io/src/com/rusefi/binaryprotocol/IncomingDataBuffer.java +++ b/java_console/io/src/com/rusefi/binaryprotocol/IncomingDataBuffer.java @@ -2,12 +2,15 @@ package com.rusefi.binaryprotocol; import com.opensr5.Logger; import com.rusefi.Timeouts; +import com.rusefi.config.generated.Fields; import etch.util.CircularByteBuffer; import net.jcip.annotations.ThreadSafe; import java.io.EOFException; import java.util.Arrays; +import static com.rusefi.binaryprotocol.IoHelper.*; + /** * Thread-safe byte queue with blocking {@link #waitForBytes} method * @@ -28,6 +31,37 @@ public class IncomingDataBuffer { this.logger = logger; } + public byte[] getPacket(Logger logger, String msg, boolean allowLongResponse, long start) throws InterruptedException, EOFException { + boolean isTimeout = waitForBytes(msg + " header", start, 2); + if (isTimeout) + return null; + + int packetSize = swap16(getShort()); + logger.trace("Got packet size " + packetSize); + if (packetSize < 0) + return null; + if (!allowLongResponse && packetSize > Math.max(BinaryProtocolCommands.BLOCKING_FACTOR, Fields.TS_OUTPUT_SIZE) + 10) + return null; + + isTimeout = waitForBytes(msg + " body", start, packetSize + 4); + if (isTimeout) + return null; + + byte[] packet = new byte[packetSize]; + getData(packet); + int packetCrc = swap32(getInt()); + int actualCrc = getCrc32(packet); + + boolean isCrcOk = actualCrc == packetCrc; + if (!isCrcOk) { + logger.trace(String.format("%x", actualCrc) + " vs " + String.format("%x", packetCrc)); + return null; + } + logger.trace("packet " + Arrays.toString(packet) + ": crc OK"); + + return packet; + } + public void addData(byte[] freshData) { logger.info("IncomingDataBuffer: " + freshData.length + " byte(s) arrived"); synchronized (cbb) {