Skip to content
Permalink
Browse files
[NO ISSUE][REP] Account for SSL socket data transfer behavior
- user model changes: no
- storage format changes: no
- interface changes: no

Details:

- Account for the fact that a call to an SSLSocketChannel
  read operation can return 0 read bytes even in a blocking
  socket due to incomplete SSL packet.

Change-Id: I0e1e69cba7336e0cfca5def870ab16334ce8c19f
Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/11064
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Murtadha Hubail <mhubail@apache.org>
Reviewed-by: Ali Alsuliman <ali.al.solaiman@gmail.com>
  • Loading branch information
mhubail committed Apr 15, 2021
1 parent 40be6e7 commit 94b1306e380dc923a964abf60900d55e545a4e4f
Showing 1 changed file with 18 additions and 16 deletions.
@@ -60,30 +60,32 @@ public static void readBytes(ISocketChannel socketChannel, ByteBuffer byteBuffer

public static void sendFile(FileChannel fileChannel, ISocketChannel socketChannel) throws IOException {
long pos = 0;
long fileSize = fileChannel.size();
long remainingBytes = fileSize;
long transferredBytes = 0;

while ((transferredBytes += fileChannel.transferTo(pos, remainingBytes, socketChannel)) < fileSize) {
pos += transferredBytes;
remainingBytes -= transferredBytes;
long remainingBytes = fileChannel.size();
try {
while (remainingBytes > 0) {
long sentBytes = fileChannel.transferTo(pos, remainingBytes, socketChannel);
pos += sentBytes;
remainingBytes -= sentBytes;
}
socketChannel.getSocketChannel().socket().getOutputStream().flush();
} catch (Exception e) {
LOGGER.info("failed to send file; file size {}, pos {}, remainingBytes {}", fileChannel.size(), pos,
remainingBytes);
}
socketChannel.getSocketChannel().socket().getOutputStream().flush();
}

public static void downloadFile(FileChannel fileChannel, ISocketChannel socketChannel) throws IOException {
long remainingBytes = fileChannel.size();
long pos = 0;
long fileSize = fileChannel.size();
long count = fileSize;
long numTransferred = 0;
try {
while ((numTransferred += fileChannel.transferFrom(socketChannel, pos, count)) < fileSize) {
pos += numTransferred;
count -= numTransferred;
while (remainingBytes > 0) {
long readBytes = fileChannel.transferFrom(socketChannel, pos, remainingBytes);
pos += readBytes;
remainingBytes -= readBytes;
}
} catch (Exception e) {
LOGGER.info("failed to download file; file size {}, pos {}, count {}, numTransferred {}", fileSize, pos,
count, numTransferred);
LOGGER.info("failed to download file; file size {}, pos {}, remainingBytes {}", fileChannel.size(), pos,
remainingBytes);
throw e;
}
}

0 comments on commit 94b1306

Please sign in to comment.