Skip to content

Commit

Permalink
Fix various errors while dealing with long streams (java-native-access#3
Browse files Browse the repository at this point in the history
)

Motivation:

Server should be able to return an arbitrarily long stream, i.e. keep
writing data as long as the channel is writable, and resume writing data
when getting the writabilityChanged event.

Modications:
 - Fix infinite loop in QuicheQuicChannel.streamSendMultiple() when
   Quiche.quiche_conn_stream_send() returns 0.
 - Fix StackOverflowError when calling fireChannelReadComplete() when no read happened.
 - Fix IndexOutOfBoundsException when Quiche.quiche_conn_stream_recv() returns Quiche.QUICHE_ERR_DONE.

Result:

Server can send a long stream.

Co-authored-by: Thomas Devanneaux <[email protected]>
  • Loading branch information
thomdev and thomdev authored Nov 10, 2020
1 parent c97dbf0 commit ddd2c1f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ boolean streamSendMultiple(long streamId, ByteBufAllocator allocator, ChannelOut
res = streamSend(streamId, buffer, false);
}

if (Quiche.throwIfError(res)) {
if (Quiche.throwIfError(res) || res == 0) {
// stream has no capacity left stop trying to send.
return false;
}
Expand Down Expand Up @@ -305,11 +305,10 @@ boolean streamRecv(long streamId, ByteBuf buffer) throws Exception {
long memoryAddress = buffer.memoryAddress();
int recvLen = Quiche.quiche_conn_stream_recv(connectionAddressChecked(), streamId,
memoryAddress + writerIndex, buffer.writableBytes(), finBuffer.memoryAddress());
Quiche.throwIfError(recvLen);
boolean fin = finBuffer.getBoolean(writerIndex);
// Skip the FIN
buffer.setIndex(0, writerIndex + recvLen);
return fin;
if (!Quiche.throwIfError(recvLen)) {
buffer.setIndex(0, writerIndex + recvLen);
}
return finBuffer.getBoolean(0);
}

private void handleWriteEgress() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ void recv() {
allocHandle.reset(config);
ByteBuf byteBuf = null;
boolean close = false;
boolean readCompleteNeeded = false;
QuicheQuicChannel parent = parentQuicChannel();
try {
do {
Expand All @@ -317,12 +318,15 @@ void recv() {
break;
}
readPending = false;
readCompleteNeeded = true;
pipeline.fireChannelRead(byteBuf);
byteBuf = null;
} while (allocHandle.continueReading() && !close);

allocHandle.readComplete();
pipeline.fireChannelReadComplete();
if (readCompleteNeeded) {
pipeline.fireChannelReadComplete();
}
if (close) {
closeOnRead(pipeline);
}
Expand Down

0 comments on commit ddd2c1f

Please sign in to comment.