Skip to content

Commit

Permalink
Increase the state reversing count to 2048 to make less impact on per…
Browse files Browse the repository at this point in the history
…formance

The reversed byte transactions slows down the process as sender has to wait for the incoming byte.
  • Loading branch information
velitasali committed Aug 24, 2020
1 parent 367f4e5 commit 6c04829
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ public final class Config
/**
*
*/
public static final int DATA_EXCHANGE_BYTE_SIZE = 8192;
public static final int DATA_EXCHANGE_BUFFER_SIZE = 8192;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;

import static org.monora.coolsocket.core.config.Config.DATA_EXCHANGE_BUFFER_SIZE;

/**
* This class connects to both clients and servers. This accepts a valid socket instance, and writes to and reads from
* it.
Expand Down Expand Up @@ -180,8 +182,6 @@ public static ActiveConnection connect(SocketAddress socketAddress, int readTime
return new ActiveConnection(socket, readTimeout);
}

// TODO: 8/6/20 Improve this

/**
* Retrieve the {@link InfoExchange} value from the remote and do the appropriate operation based on that.
* <p>
Expand Down Expand Up @@ -420,7 +420,7 @@ public int read(Description description) throws IOException
boolean chunked = description.flags.chunked();

if (description.nextAvailable <= 0) {
if (description.transactionCount++ == 1000) {
if (description.transactionCount++ == 2048) {
writeState(description);
description.transactionCount = 0;
} else
Expand Down Expand Up @@ -461,7 +461,7 @@ public int read(Description description) throws IOException
*/
public Description readBegin() throws IOException
{
return readBegin(new byte[8192]);
return readBegin(new byte[DATA_EXCHANGE_BUFFER_SIZE]);
}

/**
Expand Down Expand Up @@ -774,7 +774,7 @@ public synchronized void write(Description description, byte[] bytes, int offset
if (length < 0 || offset + length > bytes.length)
throw new IndexOutOfBoundsException("The pointed data location is not valid.");

if (description.transactionCount++ == 1000) {
if (description.transactionCount++ == 2048) {
readState(description);
description.transactionCount = 0;
} else
Expand Down Expand Up @@ -810,7 +810,7 @@ public synchronized void write(Description description, byte[] bytes, int offset
public synchronized void write(Description description, InputStream inputStream) throws IOException
{
int len;
byte[] buffer = new byte[8192];
byte[] buffer = new byte[DATA_EXCHANGE_BUFFER_SIZE];
while ((len = inputStream.read(buffer)) != -1) {
write(description, buffer, 0, len);
}
Expand Down Expand Up @@ -846,7 +846,7 @@ public synchronized Description writeBegin(long flags) throws IOException
*/
public synchronized Description writeBegin(long flags, long totalLength) throws IOException
{
ByteBuffer byteBuffer = ByteBuffer.allocate(8192);
ByteBuffer byteBuffer = ByteBuffer.allocate(DATA_EXCHANGE_BUFFER_SIZE);
int operationId = (int) (Integer.MAX_VALUE * Math.random());
Description description = new Description(flags, operationId, totalLength, byteBuffer);
byteBuffer.putLong(flags)
Expand Down Expand Up @@ -984,7 +984,7 @@ public Description(Flags flags, int operationId, long totalLength, ByteBuffer by
if (byteBuffer == null)
throw new NullPointerException("Buffer cannot be null.");

if (byteBuffer.capacity() < 8192)
if (byteBuffer.capacity() < DATA_EXCHANGE_BUFFER_SIZE)
throw new BufferUnderflowException();

if (totalLength < 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public void onConnected(ActiveConnection activeConnection)
@Test(expected = CancelledException.class)
public void readerCancelsWhenReadingLargeChunksTest() throws InterruptedException, IOException
{
final byte[] data = new byte[8196];
final byte[] data = new byte[8192];
final CoolSocket coolSocket = new CoolSocket(PORT)
{
@Override
Expand All @@ -283,8 +283,8 @@ public void onConnected(ActiveConnection activeConnection)
try {
ActiveConnection.Description description = activeConnection.readBegin();
while (description.hasAvailable()) {
activeConnection.read(description);
activeConnection.cancel();
activeConnection.read(description);
}

} catch (CancelledException ignored) {
Expand Down
4 changes: 1 addition & 3 deletions src/test/java/org/monora/coolsocket/core/SafetyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ public void onConnected(ActiveConnection activeConnection)

// writes to 1st
activeConnection.write(description1, bytes);
} catch (DescriptionMismatchException ignored) {
} catch (IOException e) {
e.printStackTrace();
} catch (IOException ignored) {
}
}
};
Expand Down

0 comments on commit 6c04829

Please sign in to comment.