use of io.netty_voltpatches.buffer.CompositeByteBuf in project voltdb by VoltDB.
the class TLSMessagingChannel method writeMessage.
@Override
public int writeMessage(ByteBuffer message) throws IOException {
if (!message.hasRemaining()) {
return 0;
}
CompositeByteBuf outbuf = Unpooled.compositeBuffer();
ByteBuf msg = Unpooled.wrappedBuffer(message);
final int needed = CipherExecutor.framesFor(msg.readableBytes());
for (int have = 0; have < needed; ++have) {
final int slicesz = Math.min(CipherExecutor.FRAME_SIZE, msg.readableBytes());
ByteBuf clear = msg.readSlice(slicesz).writerIndex(slicesz);
ByteBuf encr = m_ce.allocator().ioBuffer(packetBufferSize()).writerIndex(packetBufferSize());
ByteBuffer src = clear.nioBuffer();
ByteBuffer dst = encr.nioBuffer();
try {
m_encrypter.tlswrap(src, dst);
} catch (TLSException e) {
outbuf.release();
encr.release();
throw new IOException("failed to encrypt tls frame", e);
}
assert !src.hasRemaining() : "encryption wrap did not consume the whole source buffer";
encr.writerIndex(dst.limit());
outbuf.addComponent(true, encr);
}
int bytesWritten = 0;
try {
while (outbuf.isReadable()) {
bytesWritten += outbuf.readBytes(m_socketChannel, outbuf.readableBytes());
}
} catch (IOException e) {
throw e;
} finally {
outbuf.release();
}
message.position(message.position() + msg.readerIndex());
return bytesWritten;
}
use of io.netty_voltpatches.buffer.CompositeByteBuf in project voltdb by VoltDB.
the class TLSMessagingChannel method readMessage.
@Override
public ByteBuffer readMessage() throws IOException {
final int appsz = applicationBufferSize();
ByteBuf readbuf = m_ce.allocator().ioBuffer(packetBufferSize());
CompositeByteBuf msgbb = Unpooled.compositeBuffer();
try {
ByteBuf clear = m_ce.allocator().buffer(appsz).writerIndex(appsz);
ByteBuffer src, dst;
do {
readbuf.clear();
if (!m_decrypter.readTLSFrame(m_socketChannel, readbuf)) {
return null;
}
src = readbuf.nioBuffer();
dst = clear.nioBuffer();
} while (m_decrypter.tlsunwrap(src, dst) == 0);
msgbb.addComponent(true, clear.writerIndex(dst.limit()));
int needed = msgbb.readableBytes() >= 4 ? validateLength(msgbb.readInt()) : NOT_AVAILABLE;
while (msgbb.readableBytes() < (needed == NOT_AVAILABLE ? 4 : needed)) {
clear = m_ce.allocator().buffer(appsz).writerIndex(appsz);
do {
readbuf.clear();
if (!m_decrypter.readTLSFrame(m_socketChannel, readbuf)) {
return null;
}
src = readbuf.nioBuffer();
dst = clear.nioBuffer();
} while (m_decrypter.tlsunwrap(src, dst) == 0);
msgbb.addComponent(true, clear.writerIndex(dst.limit()));
if (needed == NOT_AVAILABLE && msgbb.readableBytes() >= 4) {
needed = validateLength(msgbb.readInt());
}
}
ByteBuffer retbb = ByteBuffer.allocate(needed);
msgbb.readBytes(retbb);
msgbb.discardReadComponents();
assert !msgbb.isReadable() : "read from unblocked channel that received multiple messages?";
return (ByteBuffer) retbb.flip();
} finally {
readbuf.release();
msgbb.release();
}
}
Aggregations